当前位置:网站首页>Simple and flexible permission design?
Simple and flexible permission design?
2022-06-24 12:32:00 【Mr egg】
you : What can I know after watching ? I : Let's talk about the following two points One . Based on understanding RBAC Table design of ideas Two . How table data is used in actual development scenarios you : I think it's still a little dry I : I don't want you to think , I want me to feel (͡ ͡° ͜ つ ͡͡°)
Daniel :Hi, Brother egg , Recently received a demand , You need to add permission related functions to existing projects , Think about it. I've been focusing on the front end for a long time ,N It's been a long time since I touched the watch , Do you know anything about this ?
Mr. egg :[]( ̄▽ ̄)* A little understanding a little understanding ~! Existing projects , That's not the way “ Follow one's inclinations ” Slightly . Say it. , About existing projects DB Information about
Daniel : Database is used MySQL, The connection to the database is Sequelize, One ORM Of Node.js library .
Mr. egg :OK, The process of this combination suggestion is : First use EER Figure tools ( Such as MySQLWorkbench) Design table structure , Then export SQL, Finally through Sequelize-Auto Automatic generation Model
Daniel : Yes, brother egg , Automatic generation SQL, Automatic generation Model. Long time no see , You are still so lazy (^▽^ ). You can say so casually , I've solved my first problem . Then let's talk about authority design
RBAC Table design
Daniel : Permission to design , Is this piece complicated ?
Mr. egg : You can be as complex as you want to be , What kind of difficulty coefficient do you want ?<( ̄ˇ ̄)/
Daniel : No no no , I want to be simple and flexible , It's easy to expand that kind of ʅ(´◔౪◔)ʃ
Mr. egg : It's very demanding . Now the industry is using more RBAC(Role-based access control) The idea of , Role based access control . Don't talk much , I'll go straight to the picture
It's very simple , You just need to give the user a role , And roles determine what resources can be used (Resource) What kind of operation (Operation),Operation It is commonly CRUD
Daniel :users Why doesn't the watch password ah , Why? code What is varchar(45) ah
Mr. egg : Hello, hello. , Don't worry about the details, OK ?ヘ(・_|
Daniel : Good good . The design of this watch looks very simple , Can't you ?
Mr. egg : Come on , According to your actual scene , Come on
Function permissions
Daniel : Suppose there are users A And the user B; There is project management in the system , User management has two functions ; user A It's the administrator , Both functions are accessible . And users B It's the average user , Only access project management , How to get ?
Mr. egg : small token of kindly feelings .┏ (^ω^)=*
1. Create data
- Create resource data : project management , User management is a resource at the functional module level , The data are as follows :
// resources:
{ code: 'projects', name: 'projects', type: 'module' },
{ code: 'users', name: 'users', type: 'module' },
Copy code - Create roles and give relevant operation permissions
// roles:
{ code: 'admin', name: 'admin' },
{ code: 'guess', name: 'guess' },
// role_permissions:
{ roleCode: 'admin', resourceCode: 'projects', operation: 'C' },
{ roleCode: 'admin', resourceCode: 'projects', operation: 'D' },
{ roleCode: 'admin', resourceCode: 'projects', operation: 'R' },
{ roleCode: 'admin', resourceCode: 'projects', operation: 'U' },
{ roleCode: 'admin', resourceCode: 'users', operation: 'C' },
{ roleCode: 'admin', resourceCode: 'users', operation: 'D' },
{ roleCode: 'admin', resourceCode: 'users', operation: 'R' },
{ roleCode: 'admin', resourceCode: 'users', operation: 'U' },
{ roleCode: 'guess', resourceCode: 'projects', operation: 'R' },
Copy code - Create users and give them roles
// users:
{ code: 'user_a', name: 'user_a' },
{ code: 'user_b', name: 'user_b' },
// user_role:
{ userCode: 'user_a', roleCode: 'admin' },
{ userCode: 'user_b', roleCode: 'guess' },
Copy code 2. Consumption data
Now let's provide the front-end children with data to determine the user A Which functional modules can be seen , And whether to display the creation , Delete etc
SELECT
u.code userCode,
res.code resourceCode,
GROUP_CONCAT(DISTINCT rp.operation) operations
FROM
resources res,
role_permissions rp,
roles r,
user_role ur,
users u
WHERE
res.code = rp.resource_code
AND rp.role_code = r.code
AND r.code = ur.role_code
AND ur.user_code = u.code
AND res.type = 'module'
AND u.code = 'user_a'
GROUP BY u.code , res.code
Copy code Got user_a The authority of is as follows :
userCode | resourceCode | operations |
|---|---|---|
user_a | projects | R,D,U,C |
user_a | users | R,D,U,C |
such , The front end just needs to judge projects Owned or not R Of operation, You can decide whether to display the item function menu . If there is C, The Create button is displayed ; Yes D, The delete button is displayed ; Yes U, The Edit button is displayed
3. View simplification
Daniel : The problem is that it's solved , But that SQL, Is it a bit complicated (~ ̄▽ ̄)~
Mr. egg : forehead , exactly . Let's simplify it .
By SQL Create a view of user function module permissions view
CREATE VIEW `user_module_view` AS
SELECT
ur.user_code,
rp.resource_code,
CONCAT('|',
GROUP_CONCAT(DISTINCT rp.operation
SEPARATOR '|'),
'|') operation
FROM
user_role ur,
role_permissions rp,
resources rs
WHERE
ur.role_code = rp.role_code
AND rs.code = rp.resource_code
AND rs.type = 'module'
GROUP BY rp.resource_code , ur.user_code
Copy code Now we can just put the lengthy SQL Simplified to the following single table operation :
SELECT
*
FROM
user_module_view
WHERE
user_code = 'user_a'
Copy code remarks : above
CONCAT(....)Middle makeoperationThe result format is :|C|R|U|D|, This is to get throughlike( Such as like '%|R|%') To query whether you have a certain permission
Data access
Daniel : Then I'll go on with the topic . user A And the user B Although all of them have the function of project management read jurisdiction , But users B It's the average user , Suppose the user B Belong to OrgB organization , Then he can only check OrgB The next project should be done yesterday ?
Mr. egg : That would be good for operation Expanded . Now let's revise role_permission The data of
{ roleCode: 'guess', resourceCode: 'projects', operation: 'R' }
=>
{ roleCode: 'guess', resourceCode: 'projects', operation: 'R_ORG' },
Copy code This means guess The role is right projects Resource ownership org Scope read jurisdiction . In this way, when the server interface fetches the item list data , According to R_ORG To determine the filtering condition of the list data
Data item level permissions
Daniel : There seems to be no problem with regular requirements . But now I have a permission related requirement , I don't know if you can use this
Mr. egg : Come on , I'll be with you today ( ̄︶ ̄)
Daniel : Then I will not be polite . In my project management function , Every project is created with... By default view / edit / admin role . The above example can only be used for the specified range ( such as org) Do the same for the project , But different projects specify different actions , It doesn't seem to work
Mr. egg :[]( ̄▽ ̄)* Let's change the angle , How about treating every project as a resource .
Daniel : Can you be more specific ? It's better to say what to do when creating a project
Mr. egg : Cough cough cough ~, That's all right. , Come on
As you ask , When creating a project , You need to initialize the corresponding built-in roles , In this way, users can be assigned roles . Let's talk about creating a project project_a, Which tables need to be added which data
// 1. add resource:
{ code: 'project_a', name: 'project_a', type: 'project' }
// 2. add roles:
{ code: 'pro_a_view', name: 'pro_a_view' },
{ code: 'pro_a_edit', name: 'pro_a_edit' },
{ code: 'pro_a_admin', name: 'pro_a_admin' },
// 3. add role_permission:
{ roleCode: 'pro_a_view', resourceCode: 'project_a', operation: 'R' },
{ roleCode: 'pro_a_edit', resourceCode: 'project_a', operation: 'R' },
{ roleCode: 'pro_a_edit', resourceCode: 'project_a', operation: 'U' },
{ roleCode: 'pro_a_admin', resourceCode: 'project_a', operation: 'R' },
{ roleCode: 'pro_a_admin', resourceCode: 'project_a', operation: 'U' },
{ roleCode: 'pro_a_admin', resourceCode: 'project_a', operation: 'D' },
Copy code So you just need to give the user B increase pro_a_view role , user B That is to have the right to project_a Read permission of
Note that there operation did not C, Because a resource is a single project , So where does a single project come from create Well ? Is that so? (^▽^ )
Daniel : Okay , It looks the same as when the whole project function is used as a resource . But I found a problem , If you use each project as a resource , Then I want to query the users B What can I see , It seems very troublesome . You can't find one by one , And then put it together
Mr. egg : Of course , Remember that we used it View view Do you ? Now we also give project Create a resource of type view Well
CREATE VIEW 'user_project_view' AS
SELECT
ur.user_code,
rp.resource_code,
CONCAT('|',
GROUP_CONCAT(DISTINCT rp.operation
SEPARATOR '|'),
'|') operation
FROM
user_role ur,
role_permissions rp,
resources rs
WHERE
ur.role_code = rp.role_code
AND rs.code = rp.resource_code
AND rs.type = 'project'
GROUP BY rp.resource_code , ur.user_code
Copy code In this way, users can also be queried by a single table B You can view the list of items and the operation permission of each item
SELECT
*
FROM
user_project_view
WHERE
user_code = 'user_b'
AND operation LIKE '%|R|%'
Copy code Special permission requirements
Daniel : Oh, yes . I have one last need , It's the image resources in the project , If the user B Yes project_a Have edit role , You can only delete the image resources you added , You can't delete a picture resource that someone else has added , Can this be realized . I don't want to be a resource record like a project
Mr. egg :(lll¬ω¬) This... ...
Daniel : Looks like you're stuck , ha-ha
Mr. egg : No more than that. . Powerful operation It's not a vegetarian . All I have to do is edit Character's update The operation authority is increased limited You can use the modifier of . Such as U_LIMITED
Daniel : All right , That makes sense . because operation You can extend the , So as long as we define its behavior , It's like everything can be done
Mr. egg :All right. Scalability is a must , and operation That's the key to expansion .operation Defines the operation identifier , According to the contract of operation identifier, the developer , Just implement the specified logic
Daniel : got it , Thank you , Brother egg , Goodbye
Mr. egg : You're welcome , Don't give away when you're good !
It's over here , Thank you for watching. !(๑¯∀¯๑)
边栏推荐
- Database migration tool flyway vs liquibase (II)
- Opencv learning notes -- Separation of color channels and multi-channel mixing
- The idea of "6 points + gene family" without experiment~
- Popular science of data annotation: ten common image annotation methods
- 电商红包雨是如何实现的?拿去面试用(典型高并发)
- Reading at night -- about microservices and containers
- Is it safe to open an account under the conditions of new bonds
- 万名校园开发者花式玩AI,亮点看这张图就够啦!
- Concentrate on research preparation, Tencent cloud, see you next year!
- 怎样打新债具体操作 开户是安全的吗
猜你喜欢
![[digital ic/fpga] booth multiplier](/img/42/3da3b1d3cc82cb9c0694241148011b.png)
[digital ic/fpga] booth multiplier

如何优雅的写 Controller 层代码?

GLOG从入门到入门
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/fa/e52bd8a1a404a759ef6ba88e8da0f0.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

"Meng Hua Lu" is about to have a grand finale. It's better to learn it first than to look ahead!

Install Kali on the U disk and persist it

电商红包雨是如何实现的?拿去面试用(典型高并发)

链接器 --- Linker

Linker --- linker

Group planning - General Review
随机推荐
Hardware enterprise website ranking, 8 commonly used processes
QT -- the qtabwidget supports dragging tabbar items
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习
Cluster control management
RTMP streaming platform easydss video on demand interface search bar development label fuzzy query process introduction
Is it safe to open an account under the conditions of new bonds
哪个商业保险养老险好?2022年商业养老保险产品排名
Is it safe to open an account for how many new bonds you can apply for
计组-总复习
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
Ingenious conception - iron death regulatory factor classification and prognosis 6+
FreeRTOS概述与体验
2022年有什么低门槛的理财产品?钱不多
Is GF Securities reliable? Is it safe to open a securities account?
The solution of distributed system: directory, message queue, transaction system and others
Programmer: after 5 years in a company with comfortable environment, do you want to continue to cook frogs in warm water or change jobs?
Example of SMS interface verification code function implemented by ThinkPHP framework
Popular science of data annotation: ten common image annotation methods
Adjustment method of easynvr video platform equipment channel page display error