Comments (4)
Hello @ssssteve,
You can use @if (Auth::user()->can('view.client'));
When you create permissions for client.as.admin just add another param, inherit_id = id of 'client' permission. So view.client.admin will now be known as view.client since your user will have permission of client.as.admin and links to client permission. We start recognizing user as client (inherited permission) and forget client.as.admin.
I'm sorry I'm not home so I can't write examples since I'm replying you via cell phone. I'll write you examples in few hours.
from laravel-acl.
@ssssteve, Let the example code speak for a better picture.
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_id
in internships.student.
sinceinternships.student
inherit permissions frominternships
we can can forget aboutinternships.student
because 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());
EDIT: I have updated the wiki as well.
from laravel-acl.
Hi @kodeine, thanks very much for your extremely rapid response. That sounds just right, and having just modified my Permissions seeder it seems to work really well.
Just as an added example, I'll mention that I'm doing it slightly differently to your example, which might be somewhat safer. I've set up a top level Permission but set all the permissions to false
:
$clientPermission = Permission::create([
'name' => 'client',
'slug' => [
'create' => false,
'view' => false,
'update' => false,
'delete' => false,
'suspend' => false
],
'description' => 'Manage client permissions'
]);
I can then set specific permissions true
on a per role basis:
Permission::create([
'name' => 'client.as.webdev',
'slug' => [
'create' => true,
'view' => true,
'update' => true,
'delete' => true,
'suspend' => true
],
'inherit_id' => $clientPermission->getKey(),
'description' => 'Manage client permissions as webdev'
]);
$roleWebdev->assignPermission('client.as.webdev');
and for the lowest level role:
Permission::create([
'name' => 'client.as.viewer',
'slug' => [],
'inherit_id' => $clientPermission->getKey(),
'description' => 'Manage client permissions as viewer'
]);
$roleViewer->assignPermission('client.as.viewer');
This way permissions are explicitly set if required, and adding a new permission should be inherited safely until overridden for a specific role.
Thanks for your help!
from laravel-acl.
@ssssteve, you're welcome :)
from laravel-acl.
Related Issues (20)
- Custom User model
- Function name must be a string
- how to add user based permission
- Error Undefined constant 'Kodeine\Acl\Middleware\HasPermission'
- Method Illuminate\Database\Eloquent\Collection::lists does not exist.
- Laravel 6.0 HOT 9
- Config prefix datatable HOT 1
- Code break HOT 3
- Can't get package to work in Laravel 6 HOT 4
- Need fix foreign key table name HOT 4
- Library is doesn't migrate files HOT 2
- Can I use this package in Laravel lumen? HOT 1
- version 2.0.4 not getting installed from composer HOT 2
- Prefix na tabela Role User HOT 3
- Works on Laravel 7.x? HOT 1
- cache tags not available in version 2.0 HOT 1
- Laravel 8.x Support HOT 2
- Different structure of data returned for JSON ($role->toJSON()) after cache expire HOT 1
- No way to invalidate cache manually HOT 2
- Laravel 9 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-acl.