当前位置:网站首页>Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
2022-06-24 15:03:00 【Wuhacong】
Permission control of fields , Generally, it controls the accessibility of some sensitive fields of a business object by the personnel in the corresponding role : Include visible 、 Editable . This essay is based on SqlSugar Introduction to field control management based on the development framework of .
When designing field permissions , We need to know whether this is based on RBAC The concept of , Role based authorization , And our field list belongs to the list of specific business objects , The business objects here refer to our specific business modules , Such as basic customer information 、 Basic personnel information 、 Quotation, etc , We control fields based on these businesses .
1、 Design of field permission table and interface management
be based on SqlSugar Development framework of , Corresponding to the processed business object information , Is the relevant entity object information , We design two databases in the database , An information used to store the name of the corresponding entity class , Such as ID, Full name of entity class , Class path and other principal information ; A field list information that stores the corresponding role configuration , Combined, the field permission control of the corresponding role can be realized , The database table design information is as follows .
Some field information of the two business tables are as follows .
The interface for setting field permissions in the system is as follows .
Configure the information of each entity object through the list , If there is no corresponding entity , Select list from the assembly to join .
Add the corresponding entity information , We can get the Chinese information of the corresponding field reference according to the name reflection interface , Then show it , Used to add to the control list .
After this configuration , The system records the relevant configuration information , We can then use this configuration information , stay Winform The display and processing of controls are performed on the interface .
2、 stay Winform Display and process controls on the interface
On the interface , In order to bind the relationship between interface controls and fields , I need to set up a Tag To mark , Then according to the system configuration information , Automatically control field permissions ( hide 、 Set read-only , No restrictions, etc )
/// <summary>
/// Set the permission of the control field to show or hide ( Field permissions are not used by default )
/// </summary>
private async void SetPermit()
{
#region Set the correspondence between control and field
this.txtName.Tag = "Name";
this.txtAge.Tag = "Age";
#endregion
// Get the list permission
var permitDict = await BLLFactory<IFieldPermitService>.Instance.GetColumnsPermit(typeof(CustomerInfo).FullName, LoginUserInfo.Id.ToInt32());
this.SetControlPermit(permitDict, this.layoutControl1);
await Task.CompletedTask;
}
Set the field display mode of the control SetControlPermit , The code is as follows .
/// <summary>
/// Set the visibility of the control 、 Read and write permissions are displayed
/// </summary>
/// <param name="panel"> The control object </param>
/// <param name="permitDict"> Field and permission Dictionary , The dictionary value is permission control :0 read-write ,1 read-only ,2 Hide values ,3 No display </param>
/// <param name="layoutControl"> If there is a layout , Use the layout control , Otherwise it's empty </param>
public static void SetControlPermit(this Control panel, Dictionary<string, int> permitDict, LayoutControl layoutControl = null)
{
foreach (Control ctrl in panel.Controls)
{
var baseCtrl = ctrl as BaseEdit;
if (baseCtrl != null)
{
var tag = string.Concat(baseCtrl.Tag);
if (!string.IsNullOrEmpty(tag) && permitDict.ContainsKey(tag))
{
var permit = permitDict[tag];
var visible = (permit == 0 || permit == 1);//2、3 invisible
if (layoutControl != null)
{
var layoutItem = layoutControl.GetItemByControl(baseCtrl);
if (layoutItem != null)
{
layoutItem.ToVisibility(visible);
}
}
baseCtrl.Visible = visible;
baseCtrl.ReadOnly = permit == 1;
}
}
ctrl.SetControlPermit(permitDict, layoutControl);
}
}
The above code is mainly the control in the variable panel , And decide tag label , Then read only 、 invisible 、 Normal judgment .
In the list interface , We can hide the content by setting hidden characters , The interface effect is shown below .
If not visible , This column will not be displayed on the interface , Instead of hiding .
Also similar to the edit control interface , We also provide corresponding methods in the list interface , Used to hide some information about list fields , As shown in the following code .
// Get field display permission , And set up ( Field permissions are not used by default )
this.winGridViewPager1.gridView1.SetColumnsPermit(permitDict);
Its implementation rules are similar , Process according to the configured field permission control point information , Decide whether to show , Is it hidden , Is it handled normally .
its permitDic It can also be judged according to the configuration information .
// Get the corresponding display field according to the business object , If not set , So according to FieldPermit Get the field permission list by configuring the table
var permitDict = await BLLFactory<IFieldPermitService>.Instance.GetColumnsPermit(typeof(BlackIPInfo).FullName, Portal.gc.UserInfo.Id);
var displayColumns = await BLLFactory<IBlackIPService>.Instance.GetDisplayColumns();
if (permitDict != null && permitDict.Keys.Count > 0)
{
//0 read-write ,1 read-only ,2 Hide values ,3 No display
displayColumns = string.Join(",", permitDict.Keys.Where(s => permitDict[s] < 2));
}
this.winGridViewPager1.DisplayColumns = displayColumns;
The following is the general control method of specific logic , Take it as the extension function of the control , We only need one line of code to call write control
/// <summary>
/// According to the value of the parameter permission Dictionary :0 read-write ,1 read-only ,2 Hide values ,3 No display , Set column permissions .
/// </summary>
/// <param name="gridView">GridView object </param>
/// <param name="fieNamePermitDict"> Field and permission Dictionary , The dictionary value is permission control :0 read-write ,1 read-only ,2 Hide values ,3 No display </param>
public static void SetColumnsPermit(this GridView gridView, Dictionary<string,int> fieNamePermitDict)
{
char passwordChar = '*';
foreach (GridColumn col in gridView.Columns)
{
var include = fieNamePermitDict.ContainsKey(col.FieldName);
if (include)
{
int permit = fieNamePermitDict[col.FieldName];
switch (permit)
{
case 0:// Normally visible 、 read-write
col.OptionsColumn.AllowEdit = true;
col.OptionsColumn.ReadOnly = false;
col.AppearanceHeader.ForeColor = Color.Black;
col.Visible = true;
break;
case 1:
// read-only
col.OptionsColumn.AllowEdit = false;
col.OptionsColumn.ReadOnly = true;
col.AppearanceHeader.ForeColor = Color.Gray;
col.Visible = true;
break;
case 2:
// Hide values
var edit = col.CreateTextEdit();
col.Tag = string.Concat(passwordChar);// It is used to judge at the interface , Avoid setting DisplayText
edit.PasswordChar = passwordChar;
col.Visible = true;
break;
case 3:
// invisible
col.Visible = false;
break;
}
}
}
}
Handle through the above code , We can achieve the right Winform List in the interface , Edit the controls of the form , Perform the related field permission control to display . The following is the interface effect .
This is done in the system background , You can set the information hiding or read-only processing of some sensitive fields as required .
Series articles :
边栏推荐
- 作为一名开发者,对你影响最深的书籍是哪一本?
- MES在流程和离散制造企业的15个差别(下)
- Bert-whitening 向量降维及使用
- Is it safe to open an account for stock speculation in the top ten securities app rankings in China
- Detailed explanation of redis data types
- Redis consistency hash and hash slot
- 高薪程序员&面试题精讲系列115之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
- laravel8使用faker调用工厂填充数据
- R语言plotly可视化:使用plotly可视化数据划分后的训练集和测试集、使用不同的形状标签表征、训练集、测试集、以及数据集的分类标签(Display training and test split
- Six stones Management: garbage dump effect: if you don't manage your work, you will become a garbage dump
猜你喜欢
Multimeter resistance measurement diagram and precautions
How to avoid placing duplicate orders
【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
如何避免下重复订单
ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型
box-sizing
常见的单例模式&简单工厂
Bert whitening vector dimension reduction and its application
GO语言并发模型-MPG模型
laravel8使用faker调用工厂填充数据
随机推荐
Analysis of similarities and differences between redis and memcached in cache use
在CVS中恢复到早期版本
Online text entity extraction capability helps applications analyze massive text data
Openinstall joins hands with the book chain to help channel data analysis and create the era of Book Networking
10_那些格调很高的个性签名
June training (day 23) - dictionary tree
简谈企业Power BI CI /CD 实施框架
The security market has entered a trillion era, and the security B2B online mall system has been accurately connected to deepen the enterprise development path
Golang implements BigInteger large number calculation
As a developer, what is the most influential book for you?
[ansible problem processing] remote execution user environment variable loading problem
pgsql查询分组中某个字段最大或者最小的一条数据
时间同步业务的闭环管理——时间监测
Method after charging the idea plug-in material theme UI
Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
Six stones Management: garbage dump effect: if you don't manage your work, you will become a garbage dump
List of PostgreSQL
常见的单例模式&简单工厂
leetcode. 12 --- integer to Roman numeral
laravel下视图间共享数据