In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "PHP uses Casbin to do RBAC and RESTful access control". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
PHP-Casbin is a powerful and efficient open source access control framework that supports rights management based on various access control models (RBAC ABAC ACL).
Here use the official database adapter extension: DBAL Adapter.
Installation
Install via composer:
Composer require casbin/casbincomposer require casbin/dbal-adapter uses RBAC Model
Model.conf is as follows:
[request_definition] r = sub, obj, act [policy _ definition] p = sub, obj, act# RBAC definition of role inheritance relationship [role_definition] g = _, _ [policy_effect] e = some (where (p.eft = = allow)) [matchers] m = g (r.sub, p.sub) & & keyMatch3 (r.obj, p.obj) & & regexMatch (r.act, p.act) initializes a Casbin enforceruse Casbin\ Enforcer;use CasbinAdapter\ DBAL\ Adapter $adapter = Adapter::newAdapter (['driver' = >' pdo_mysql', 'host' = >' 127.0.0.1 policies, 'dbname' = >' test', 'user' = >' root', 'password' = >', 'port' = >' 3306 policies,]); $enforcer = new Enforcer ('path/to/model.conf', $adapter); add policies
Assign roles to alice and bob:
/ / alice has the admin role$enforcer- > addRoleForUser ('alice',' admin'); / / bob has the member role$enforcer- > addRoleForUser ('bob',' member')
Assign permissions to the member role, and the member role only has permission to view foo resources:
$enforcer- > addPermissionForUser ('member',' / foo', 'GET'); $enforcer- > addPermissionForUser (' member','/ foo/:id', 'GET')
The admin role has the permission to add, delete, modify and query foo:
/ / admin inherits all permissions of member$enforcer- > addRoleForUser ('admin',' member'); $enforcer- > addPermissionForUser ('admin',' / foo', 'POST'); $enforcer- > addPermissionForUser (' admin','/ foo/:id', 'PUT'); $enforcer- > addPermissionForUser (' admin','/ foo/:id', 'DELETE')
After the roles and permissions are assigned, the policy rules in the database are roughly as follows:
G, alice, adming, bob, memberp, memeber, / foo, GETp, memeber, / foo/:id, GETg, admin, memberp, admin, / foo, POSTp, admin, / foo/:id, PUTp, admin, / foo/:id, DELETE authentication permissions
Alice has the admin role and inherits all permissions of admin and member roles.
$enforcer- > enforce ('alice',' / foo', 'GET'); / / true$enforcer- > enforce (' alice','/ foo', 'GET'); / / true$enforcer- > enforce (' alice','/ foo', 'POST'); / / true$enforcer- > enforce (' alice','/ foo/1', 'PUT'); / / true$enforcer- > enforce (' alice','/ foo/1', 'DELETE'); / / true
Bob has the role of member and inherits only the permissions of member.
Enforcer- > enforce ('bob',' / foo', 'GET'); / / true$enforcer- > enforce (' bob','/ foo', 'GET'); / / true$enforcer- > enforce (' bob','/ foo', 'POST'); / / false$enforcer- > enforce (' bob','/ foo/1', 'PUT'); / / false$enforcer- > enforce (' bob','/ foo/1', 'DELETE') / / false, this is the end of the introduction of "PHP's method of using Casbin to do RBAC and RESTful access control". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.