当前位置:网站首页>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 :
边栏推荐
- R language plot visualization: the visualization model creates a grid in the classification contour (contour) and meshgrid of the entire data space, in which the distance between each point is determi
- Laravel 8 realizes auth login
- 六石管理学:垃圾场效应:工作不管理,就会变成垃圾场
- 同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩
- 不要小看了积分商城,它的作用可以很大
- 10_那些格調很高的個性簽名
- 手机注册股票开户 炒股开户安全吗
- 常见的缺陷管理工具——禅道,从安装到使用手把手教会你
- openinstall携手书链:助力渠道数据分析,共创书联网时代
- 港股上市公司公告 API 数据接口
猜你喜欢

pgsql查询分组中某个字段最大或者最小的一条数据

在宇宙的眼眸下,如何正确地关心东数西算?

As a developer, what is the most influential book for you?

Common singleton mode & simple factory

laravel 8 实现Auth登录

【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维

左手代码,右手开源,开源路上的一份子

Two way combination of business and technology to build a bank data security management system

同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩

API data interface for announcement of Hong Kong listed companies
随机推荐
第八章 操作位和位串(四)
GO语言-init()函数-包初始化
Huangchuping presided over the video conference on fixed-point contact with Zhuhai, resolutely implemented the deployment requirements of the provincial Party committee, and ensured positive results i
入行 4 年,跳槽 2 次,我摸透了软件测试这一行
Openinstall joins hands with the book chain to help channel data analysis and create the era of Book Networking
Is financial management of securities account safe??
Overview of SAP marketing cloud functions (IV)
10_那些格调很高的个性签名
PgSQL queries the largest or smallest data of a field in a group
从pair到unordered_map,理论+leetcode题目实战
A brief introduction to the lexical analysis of PostgreSQL
Database considerations
Data sharing between laravel lower views
10_ Those high-profile personal signatures
Go language - use of goroutine coroutine
缓存使用中Redis,Memcached的共性和差异分析
Py之toad:toad的简介、安装、使用方法之详细攻略
box-sizing
[environment setup] zip volume compression
Brief discussion on the implementation framework of enterprise power Bi CI /cd