当前位置:网站首页>Data permissions should be designed like this, yyyds!
Data permissions should be designed like this, yyyds!
2022-07-26 14:59:00 【Misty jam】
my Knowledge planet officially launched ( Stamp link ), Looking forward to your joining , Let's rush together !
Hello everyone , I am ethereal !
In the actual development of the project, we not only need to control which resources a user can access , You also need to control that users can only access some part of the data in the resource .
To control which resources a user can access, we have a mature permission management model, namely RBAC, But the controlling user can only access some resources ( That is what we often call data permission ) Use RBAC Models are not enough , In this paper, we try to RBAC On the basis of the model, the management and control of data rights are integrated .
First, let's take a look at RBAC Model .
RBAC Model
RBAC yes Role-BasedAccess Control English abbreviations , It means role based access control .
RBAC Different roles will be defined in the system in advance , Different roles have different permissions , A role is actually a set of permissions . All users of the system will be assigned different roles , A user may have multiple roles . Use RBAC Can greatly simplify the management of permissions .
RBAC The model can also be subdivided into RBAC0,RBAC1,RBAC2,RBAC3. Here we don't discuss the differences between them , Interested students can do their own research , We mainly focus on the common RBAC0 Model .
As shown in the figure below is a classic RBAC0 Database design of the model .

stay RBAC Under the model , The system only validates users A Belong to role or not RoleX, Instead of judging users A Access only to users B The data of DataB. We call this problem “ Horizontal authority management issues ”.
Data access
List data permissions , It mainly controls row data through data permission , Let different people have different rules for viewing data ; To achieve data permissions , The most important thing is to abstract data rules .
Data rules
For example, the business opportunity data of our system , You need to control data access from the following dimensions .
Sales people can only look at their own data ;
The sales manager of each region can only look at the data of each region ( The sales manager of Anhui region looks at the business opportunity data of Anhui region ), The same is true of BG The leaders in charge can only see where they are BG Business opportunity data of ;
Financial personnel can only look at data with the amount less than 10000 .
The above dimensions are data rules .
In this way, we have also clarified several key elements of data rules , Rule field , Regular expressions , Rule value , The rules corresponding to the above three scenarios are as follows :
Rule field : founder , Regular expressions := , Rule value : Currently logged in by
Rule field : My region , Regular expressions := , Rule value : Anhui region
Rule field : Sales amount , Regular expressions :< , Rule value :10000

Rule field configuration description :
Conditional expression : Greater than / Greater than or equal to / Less than / Less than or equal to / be equal to / contain / Fuzzy / It's not equal to
Rule value : Specify the value ( Fixed value / System context variables )Linked resources 、 user
Data rules are not enough , We also need to bind data rules to resources and users .
The binding of data rules and resources is very simple , We just need to create an intermediate table , As shown in the figure below :

In this way, resources can be associated with data rules .
In application design, we need a separate data rule management function , Convenient for us to enter data rules , And then on the resource management page ( Like a list of business opportunities ) You can choose built-in data rules to bind resources and rules .
So how to make different users have different data rules ?
stay RBAC In the model , Users manage resources by granting different roles , Similarly, we can associate roles with data rules when granting permissions , In this way, different users have different data rules .
It's a bit awkward. , Let's go back to the example above .
Salesman 、 Regional Sales Manager 、 Financial people have different roles , They all have the resource permission of opportunity list , However, when binding business opportunity list resource permissions to these roles, we can check the corresponding data rules ( The binding of resources and data rules has been realized ). Reflected in the database design, we can in the role resource corresponding relationship table Role_Permission Add a field to store the associated data rules , If there are multiple data rules, you can use the separator to split .
Final RBAC The model evolved into a model like this :

According to the above design, we need to distinguish the data permissions of each region, and we need to establish different regional roles , For example, sales manager of Anhui Province 、 Regional sales manager in Shanghai , Then check the corresponding data rules for the roles . It's like RBAC1 The concept of role inheritance in .
In this way, we have basically realized RBAC Binding to data rules , But we also have a problem of how to land in the system .
Here we need the help of the famous AOP To implement the , This article only talks about principle, not implementation , So let's just mention the implementation plan by the way .
Customize a data permission annotation , For example
PermissionDataIn the corresponding resource request method , For example, add a custom annotation to the business opportunity list
@PermissionDatautilize AOP Capture all the data rules of the user's corresponding role and perform SQL Splicing , In the end in SQL Layer to achieve data filtering .
Continue to optimize
In the above design, we achieve data permissions by binding different data rules to different roles , But consider the following scenario : The range of data a role needs to see is “ Business opportunity data of Anhui Province and consumer business division ”, In this scenario, according to our previous design, we need to establish two data rules :
My region = Anhui region
Business unit = Consumer Division
And then set up 2 Different roles , Grant different data rules separately , If there are many scenes like this, it's easy for characters to explode , So we'll take it out here Data rule group The concept of .
A data rule group has multiple data rules , Data rules pass through each other AND Connect , Put on an application design :

In the database design, it becomes as follows :

Summary
Pass above 8 In the design of the table, we have realized RBAC The combination of model and Data permission , Of course, there is still room for further optimization . For example, we can extract the corresponding dictionary table for the rule field and rule value here , Let the data rule table associate these dictionary fields , In this way, when the data rules are configured in the application layer, the administrator does not need to fill in them manually, but selects them from the dictionary items , Reduce the probability of data rule configuration error .
Data permission is a relatively complex function , Here we choose to be in RBAC Based on the model , If you have a better solution, please leave me a message .
The next article will implement the code according to this model , If you are interested, you can pay attention ~
And finally ( Don't whore for nothing , Please pay attention to )
A purely technical exchange group has been opened ( The crowd is full ), The atmosphere in the group is pretty good , No advertisement , No routine , Simple bragging force , Talk about life , If you want to enter, you can add my wechat through the QR code below , Remarks into group !

边栏推荐
- SiamRPN:建议区域网络与孪生网络
- 【使用工具条绘图】
- 基于CAS的SSO单点客户端配置
- Difference between filter and interceptor
- C# NanUI 相关功能整合
- Self encoder AE (autoencoder) program
- Summary and analysis of image level weakly supervised image semantic segmentation
- Brief description of llcc68 broadcast wake-up
- Leetcode summary
- Next generation visual transformer: Unlocking the correct combination of CNN and transformer
猜你喜欢

Simple implementation of pytorch

怎样在nature上查文献?

堆叠降噪自动编码器 Stacked Denoising Auto Encoder(SDAE)

VP视频结构化框架

JMeter distributed

CVE-2022-33891 Apache spark shell 命令注入漏洞复现

Canvas mesh wave animation JS special effect
![[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example](/img/47/0e2a159bbbc7af8ef6263ca4ccc724.png)
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example

Maya imports the model into unity

华为应用已经调用了checkAppUpdate接口,为什么应用内不提示版本更新
随机推荐
【方差分析】之matlab求解
《MySQL高级篇》五、InnoDB数据存储结构
[dry goods] data structure and algorithm principle behind MySQL index
堆叠降噪自动编码器 Stacked Denoising Auto Encoder(SDAE)
【华为联机对战服务】客户端退出重连或中途进入游戏,新玩家如何补帧?
数据权限就该这么设计,yyyds!
PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
jmeter分布式
Maya imports the model into unity
WPF common function integration
SharkTeam发布2022年第二季度Web3安全态势感知报告
Create Yum warehouse inside the enterprise
php反序列化部分学习
Advanced MySQL v. InnoDB data storage structure
14. Bridge-Based Active Domain Adaptation for Aspect Term Extraction 阅读笔记
Kubernetes ---- pod configuration resource quota
SA-Siam:用于实时目标跟踪的孪生网络
[Nuxt 3] (十二) 项目目录结构 1
Leetcode659. split the array into continuous subsequences (hash table)
Transc knowledge representation model