Tiered and limited permissions #2122
ChristianPavilonis
started this conversation in
Show and tell
Replies: 1 comment 2 replies
-
Looks great, but why don't use the cache everywhere? less db calls, better performance -$permissionClass = $this->getPermissionClass();
-$permission = $permissionClass::where([
- 'name' => $permission,
- 'tier' => $tier,
-])->first();
+$permission = $this->getPermissionClass()::getPermissions([
+ 'name' => $permission,
+ 'tier' => $tier,
+], true)->first(); return $this->getPermissionClass()
- ::where('name', $permission)
- ->get()
+ ::getPermissions(['name' => $permission])
->reduce(function ($carry, Permission $permission) { |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Here's something I hacked on to extend this package for a subscription platform I built. We needed many different tiers of the same permissions, for example a monthly subscriber could download 5 videos per month and 10 for yearly.I extended the permission class and added a few columns to the permissions table.
Here's my migration:
My extended permission model:
Previously you couldn't create two permissions with the same name, overriding the
create()
method allows for multiple permissions with the same name but with different tiers.Finally I made a
HasTieredPermissions
trait for our user:The trait includes the HasRoles trait. I override the
hasPermissionTo
method to account for multiple permissions under the same name.In addition I add a method to assign a permission of a specific tier, a method to get the highest tier a user has in case they have multiple of the same permission, and lastly a method to get the limit of a permission.
Example Usage:
I enjoyed hacking on this hopefully it'll be useful to someone.
Beta Was this translation helpful? Give feedback.
All reactions