Skip to content

Instantly share code, notes, and snippets.

@trovster
Created October 29, 2015 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trovster/0d2dd8c5c04eaca71cb5 to your computer and use it in GitHub Desktop.
Save trovster/0d2dd8c5c04eaca71cb5 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController as Controller;
use App\Models\User\User;
use App\Models\User\Role as UserRole;
use App\Http\Requests\UserRequest;
class UserController extends Controller
{
/**
* User model being used.
*/
protected $user;
/**
* User Role model being used.
*/
protected $user_role;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(User $user, UserRole $user_role)
{
$this->middleware('admin.user');
$this->user = $user;
$this->user_role = $user_role;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response()->view('admin/user/index', [
'users' => $this->user->all()
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return response()->view('admin/user/create', [
'roles' => $this->user_role->lists('role', 'id')
]);
}
/**
* Store a newly created resource in storage.
*
* @param App\Http\Requests\UserRequest $request
* @return \Illuminate\Http\Response
*/
public function store(UserRequest $request)
{
$this->user->create($request->all());
return redirect()->route('admin::admin.user.index')->with('message', 'A new user has been successfully created.')->with('success', true);
}
/**
* Display the specified resource.
*
* @param App\Models\User\User $user
* @return \Illuminate\Http\Response
*/
public function show(User $user)
{
return response()->view('admin/user/show', [
'user' => $user
]);
}
/**
* Show the form for editing the specified resource.
*
* @param App\Models\User\User $user
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
return response()->view('admin/user/edit', [
'roles' => $this->user_role->lists('role', 'id'),
'user' => $user
]);
}
/**
* Update the specified resource in storage.
*
* @param App\Http\Requests\UserRequest $request
* @param App\Models\User\User $user
* @return \Illuminate\Http\Response
*/
public function update(UserRequest $request, User $user)
{
$user->update($request->all());
return redirect()->route('admin::admin.user.index')->with('message', 'The user has been successfully updated.')->with('success', true);
}
/**
* Remove the specified resource from storage.
*
* @param App\Models\User\User $user
* @return \Illuminate\Http\Response
*/
public function destroy(User $user)
{
$user->delete();
return redirect()->route('admin::admin.user.index')->with('message', 'The user has been successfully deleted.')->with('success', true);
}
}
<?php
return [
/*
|--------------------------------------------------------------------------
| User Language Lines
|--------------------------------------------------------------------------
|
*/
'index' => 'You are not allowed to view users.',
'create' => 'You are not allowed to create users.',
'store' => 'You are not allowed to create users.',
'show' => 'You are not allowed to view this user.',
'edit' => 'You are not allowed to edit this user.',
'update' => 'You are not allowed to edit this user.',
'destroy' => 'You are not allowed to delete this user.',
];
<?php
namespace App\Http\Middleware\Admin;
use Closure;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Models\User\User as Model;
class User
{
use AuthorizesRequests;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->authorize(last(explode('.', $request->route()->getName())), $request->user ?: new Model);
return $next($request);
}
}
<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use App\Models\User\User as AuthUser;
use App\Models\User\User;
class UserPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the authorised user can view the users listing.
*
* @param \App\Models\User\User $auth
* @return boolean
*/
public function index(AuthUser $auth)
{
return true ? true : abort(403, trans('user.index'));
}
/**
* Determine if the authorised user can create users.
*
* @param \App\Models\User\User $auth
* @return boolean
*/
public function create(AuthUser $auth)
{
return $auth->role->id === 1 ? true : abort(403, trans('user.create'));
}
/**
* Determine if the given user can view a user.
*
* @param \App\Models\User\User $auth
* @param \App\Models\User\User $user
* @return boolean
*/
public function show(AuthUser $auth, User $user)
{
return true ? true : abort(403, trans('user.show'));
}
/**
* Determine if the authorised user can edit the user.
*
* @param \App\Models\User\User $auth
* @param \App\Models\User\User $user
* @return boolean
*/
public function edit(AuthUser $auth, User $user)
{
return $auth->role->id === 1 ? true : abort(403, trans('user.edit'));
}
/**
* Determine if the authorised user can update user.
*
* @param \App\Models\User\User $auth
* @param \App\Models\User\User $user
* @return boolean
*/
public function update(AuthUser $auth, User $user)
{
return $auth->role->id === 1 ? true : abort(403, trans('user.update'));
}
/**
* Determine if the authorised user can delete the user.
*
* @param \App\Models\User\User $auth
* @param \App\Models\User\User $user
* @return boolean
*/
public function destroy(AuthUser $auth, User $user)
{
return $auth->role->id === 1 && $auth->id !== $user->id ? true : abort(403, trans('user.destroy'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment