Bump @kodeine
Laravel ACL
Hi everyone,
I'm using the laravel-acl package made by @kodeine.
I've a question about Route Protection (https://github.com/kodeine/laravel-acl/wiki/Protect-Routes).
I start with this role/permission:
Permission::create([
'name' => 'internships.all',
'slug' => [
'create' => true,
'view' => true,
'update' => true,
'delete' => true,
],
'description' => 'Full access to internships resource'
]);
$role = Role::create([
'name' => 'Admin',
'slug' => 'admin',
'description' => 'Administrator of the website'
]);
$role->assignPermission('internships.all');
My question is "How can I protect my resource's route?".
If we look at my route file:
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'acl'], 'can' => 'view.dashboard.admin'], function()
{
...
/**
* Internship resource.
*/
get('internships', ['as' => 'admin.internships.index', 'can' => 'view.internships', 'uses' => 'Admin\InternshipsController@index']);
post('internships', ['as' => 'admin.internships.store', 'can' => 'store.internships', 'uses' => 'Admin\InternshipsController@store']);
get('internships/create', ['as' => 'admin.internships.create', 'can' => 'create.internships', 'uses' => 'Admin\InternshipsController@create']);
get('internships/{internships}', ['as' => 'admin.internships.show', 'can' => 'show.internships', 'uses' => 'Admin\InternshipsController@show']);
put('internships/{internships}', ['as' => 'admin.internships.update', 'can' => 'update.internships', 'uses' => 'Admin\InternshipsController@update']);
get('internships/{internships}/edit', ['as' => 'admin.internships.edit', 'can' => 'edit.internships', 'uses' => 'Admin\InternshipsController@edit']);
...
}
I want to check if the user can CRUD an internhips BUT with this package (and according to my permission), I need to write something like:
get('internships', ['as' => 'admin.internships.index', 'can' => 'view.internships.all', 'uses' => 'Admin\InternshipsController@index']);
So the permission are completely useless? Because they need to be unique, and we need to write their names on the routes file.
Do I miss understand how to do that or it's a misconception of the package?
@RomainLanz, i have used a little different approach, permission inheriting. So when you create new permission, you can set inheritance of a different permission. Let the example code speak.
First we create Roles.
$roleTeacher = Role::create([
'name' => 'Teacher',
'slug' => 'teacher',
'description' => 'Teacher [...]'
]);
$roleStudent = Role::create([
'name' => 'Student',
'slug' => 'student',
'description' => 'Student [...]'
]);
Now lets create Permissions for Teacher and Student.
$permissionInternship = Permission::create([
'name' => 'internships',
'slug' => [ // an array of permissions.
'create' => true,
'view' => true,
'update' => true,
'delete' => true,
],
'description' => 'manage internships'
]);
$permissionStudent = Permission::create([
'name' => 'internships.student',
'slug' => [ // an array of permissions only for student
'create' => false,
],
// we use permission inheriting.
'inherit_id' => $permissionInternship->getKey(),
'description' => 'student internship permissions'
]);
Note:
inherit_idin internships.student. sinceinternships.studentinherit permissions frominternshipswe can can forget aboutinternships.studentbecause now we recognize it asinternships. so getPermissions will return array('internships' => [...permissions merged with internships.student...])
Lets assign those permissions to newly created roles.
$roleTeacher->assignPermission('internships'); // or assignPermission($permissionInternship->id)
$roleStudent->assignPermission('internships.student');
And then we can assign those roles to our user.
$user->assignRole($roleTeacher);
$user->assignRole($roleStudent);
//$user->revokeRole('teacher');
Finally, lets do our tests.
// user has teacher and student role
dump($user->can('create.internships')); // results true
// user has teacher role
dump($user->can('create.internships')); // results true
// user has student role
dump($user->can('create.internships')); // results false
dump($user->getPermissions());
Let me know what you think of this. I am doing final touches and will upload to github shortly. Should i be tagging it as 0.1.1 ?
Please or to participate in this conversation.