Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the extension package of permission Rights Management in Laravel

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use the permission rights management expansion package in Laravel", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Laravel permission rights management expansion package how to use" article.

What is a multi-user role

For example, for example, the forums we usually use

Webmaster-has the highest authority, the most important is the right to manage users

Administrator-the management of some articles will not have a great impact on the website

Vip-download access to some resources

Ordinary users can only add, delete, modify, comment and so on their own articles.

Tourists can only make basic browsing.

Build a table

Roles-role information: webmaster, etc.

Permissions-permission information: manage content, etc.

Model_has_roles-role corresponding to model = role corresponding to user

Role_has_permissions-role corresponding permissions = what permissions does the role have?

Model_has_permissions-Model corresponding permissions = user has permissions

Let's sort out the relationship.

Permissions (permissions) and roles (roles). A permission may be owned by multiple roles, and a role may have multiple permissions. Relationship: many-to-many (role_has_permissions)

One-to-many users and permissions (model_has_permissions)

One-to-many users and roles (model_has_roles)

For the second time, the relationship is clear, that is, when the user has what role or permission, that is, to perform the corresponding action.

1. Install expansion Pack

Composer require "spatie/laravel-permission:~2.7"

Generate the database migration file:

Php artisan vendor:publish-provider= "Spatie\ Permission\ PermissionServiceProvider"-tag= "migrations"

You can see the relevant table information in the migration directory and perform database migration.

Php artisan migrate

Generate configuration information:

Php artisan vendor:publish-provider= "Spatie\ Permission\ PermissionServiceProvider"-tag= "config"

Load under the User model

.use Spatie\ Permission\ Traits\ HasRoles; / / useclass User extends Authenticatable {use HasRoles; / / load role related information.

Create roles and users

Use Spatie\ Permission\ Models\ Role;use Spatie\ Permission\ Models\ Permission;.$role = Role::create (['name' = >' writer']); / / create role $permission = Permission::create (['name' = >' edit articles']); / / create permission to use

Add permissions to the user

$user- > givePermissionTo ('edit articles')

Add roles for users

$user- > assignRole ('writer'); $user- > assignRole ([' writer', 'admin'])

Delete permissions to users

$user- > revokePermissionTo ('edit articles')

Add permissions to a role

$role- > givePermissionTo ('edit articles')

Add permissions to a role

$role- > givePermissionTo ('edit articles')

Revoke a permission and add a new permission

$user- > syncPermissions (['edit articles',' delete articles'])

Get the role set of the current user

$user- > getRoleNames ()

Synchronize multiple roles to permissions

$role- > syncPermissions ($permissions); / / @ param array $permissions$permission- > syncRoles ($roles)

Remove permissions from a role

$role- > revokePermissionTo ($permission); $permission- > removeRole ($role)

Get the permission list of the current user

$permissions = $user- > permissions

Get all the permissions of the user, either directly (odel_has_permissions), from roles, or from both

$permissions = $user- > getDirectPermissions (); $permissions = $user- > getPermissionsViaRoles (); $permissions = $user- > getAllPermissions ()

Get the user's role collection collection

$roles = $user- > getRoleNames (); / / Returns a collection

Returns the user of the specified role | Returns only users with the role 'writer'

$users = User::role ('writer')-> get (); / /

Returns the user with the specified permission

$users = User::permission ('edit articles')-> get ()

What is the role of the user?

$user- > hasRole ('writer'); validation class

Check to see if there is a permission

$user- > hasPermissionTo ('edit articles'); $user- > can (' edit articles')

Check whether there is a role | or column

$user- > hasRole ('writer'); $user- > hasAnyRole (Role::all ()); $user- > hasAllRoles (Role::all ())

Pass an id value to determine whether there is a permission

$user- > hasPermissionTo ('1'); $user- > hasPermissionTo (Permission::find (1)-> id); $user- > hasPermissionTo ($somePermission- > id)

Whether you have a set of permissions

$user- > hasAnyPermission (['edit articles',' publish articles', 'unpublish articles'])

Check whether a role has certain permissions | remove certain permissions

$role- > hasPermissionTo ('edit articles'); $role- > revokePermissionTo (' edit articles') / / remove the template using @ role ('writer') I am a writercodes else I am not a writer...@endrole----@hasrole (' writer') I am a writings templates else I am not a writer...@endhasrole----@can ('edit articles') / / have a permission to perform operations / / @ endcan data fill use Illuminate\ Database\ Seeder Use Spatie\ Permission\ Models\ Role;use Spatie\ Permission\ Models\ Permission;class RolesAndPermissionsSeeder extends Seeder {public function run () {/ / Reset cached roles and permissions app () ['cache']-> forget (' spatie.permission.cache'); / / create permissions Permission::create (['name' = >' edit articles']); Permission::create (['name' = >' delete articles']) Permission::create (['name' = >' publish articles']); Permission::create (['name' = >' unpublish articles']); / / create roles and assign created permissions $role = Role::create (['name' = >' writer']); $role- > givePermissionTo ('edit articles'); $role = Role::create ([' name' = > 'moderator']) $role- > givePermissionTo (['publish articles',' unpublish articles']); $role = Role::create (['name' = >' super-admin']); $role- > givePermissionTo (Permission::all ()) } hint: if you modify the information table related to permissions in the database, you must delete php artisan cache:forget spatie.permission.cache app () ['cache']-> forget (' spatie.permission.cache') with the method / / command to clear the cache. The above is about the content of this article on "how to use the extension package of permission Rights Management in Laravel". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report