Download the PHP package zaichaopan/access-granted without Composer
On this page you can find all versions of the php package zaichaopan/access-granted. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zaichaopan/access-granted
More information about zaichaopan/access-granted
Files in zaichaopan/access-granted
Package access-granted
Short Description A Package to Add Role and Permission
License MIT
Informations about the package access-granted
Access Granted
This package allows you to add roles to users, add permissions to users and add permissions to role in your laravel app. It can be used in laravel 5.5 or higher.
- Access Granted
- Installation
- Usage
- Migration
- Assign Role to user
- Assign Permission to Role
- Add Permissions to User
- Has Permission through Role
- Middleware
- Blade Directive
Installation
Usage
Migration
After install the package, run the migration command to migrate roles, permissions, role_user, permission_user, and __permission_role__ tables.
The schema of the these tables
Assign Role to user
To assign role to user, add HasRoles trait the User model
This trait provides the follow methods
- giveRole
It can be used to give a roles or multiple roles to a user. For example:
Note:
If the user is given a role that he or she already has, the role won't be add again. And if the user is given a role that does not exist in the roles table, the invalid role won't be added.
- updateRole
It can be used to update user role. After calling this method on a user, he or she will only have the new roles. All the old roles will be removed.
Note:
If the updated role cannot be found, the it will be ignore and the user still keeps his o her old roles.
- removeRole
It can be used to remove a role or multiple roles from user. For example:
Note:
If the removed role cannot be found or the user doesn't have it, it will be ignored.
- removeAllRoles
It is used to remove all roles from the user. For example:
- hasRole
It can be used to determine if a user has a role or any role from a list of roles. For example:
Assign Permission to Role
This package provides HasPermissions trait which is used by Role model by default. So you can give permissions to a role, you can use the following methods provided by the trait.
- givePermissionTo
It can be used to give a permission or multiple permissions to a role, for example:
If the given permission is valid or is already give, it will be ignored.
- updatePermissionTo
It can be used to update the permissions of a role. After calling this method on a role, only the new permissions remain and all the old permissions it has will be removed. If an invalid permission is provided, it will be ignore. If all the provided permissions are invalid, the method returns false and role will still keep its old permissions. For example:
- withdrawPermissionTo
It can be used to withdraw a permission or multiple permissions from a role. If a withdrew permission is invalid, it will be ignored. If all the withdrew roles are invalid, the method returns false and the role will still keep its old permissions
- withdrawAllPermissions
It can be used to withdraw all the permission from the role.
- hasPermission
It can be used to determine if a role has a specific permission. For example:
Add Permissions to User
To add permissions to user, you just need to add HasPermissions trait to your user model.
Now you can use all the methods discussed above in add permissions to role section to give permissions.
Has Permission through Role
When we uses both HasRoles and HasPermissions trait in the User model, to determine if a user has a specific permission, we have to check if he or she has the permission through permissions table or if his or her role has this permission.
To make things easier, this package provides another trait HasPermissionThroughRole. To use it, just add it to your User model
This trait provides a method hasPermissionThroughRole which can be used to determine if a user has a role that contains this permission. For example:
Note:
If we call hasPermission method, it can only check if the user has the permission through the permissions table. To avoid confusion, we can alias this method when using hasPermissions trait and override the hasPermission method to include check user's role permissions.
Middleware
This package provides two middleware: RoleMiddleware and PermissionMiddleware. They can be used to protect any route that needs a specific role and permission to access.
To use them, register them in your Kernel.php
To use them:
Blade Directive
This package provides blade directive which can be used in your blade to protect content that only use with a given role can access.
To only allow user with given permission to access some content. Using