I am working on a dashboard application where I have several user roles like globaladmin, editors, etc. Now I want to use these roles with a single UserController resource.
For example, globaladmins should be able to execute all Restful methods, while the editor can only view and update the user.
I know that trust comes with middlemen out of the box, which is perfect for what I need. But it only works on routes (in this case, I will need a separate controller for each role) .
My UserController looks something like this.
Class UserController extends BaseController { $protected $viewfolder; public function __construct { // Checking for role and then assigning the folder name of the views $role = User::getRole(); switch($role) case 'globaladmin': $this->viewfolder = 'globaladmin'; break; case 'editor': $this->viewfolder = 'editor'; break; default: abort(401, 'Access Denied'); break; } public function index(){ if( Entrust::can('view-all-users') ){ $users = User:all(); } return view( $this->viewfolder.'.users.viewuser', compact('users')); } public function create() public function update() public function delete() }
I need middleware in the constructor that will check the user role, and then allow this method to be used only if the role has permission to use it. But this should be done in a good way without any hacks, because I will use it on other controllers as well.
php laravel-5 acl user-roles
omer farooq
source share