当前位置:网站首页>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 :

be based on SqlSugar A step-by-step introduction to the development framework (1)-- Design and use of framework basic classes

be based on SqlSugar The development framework of is introduced step by step (2)-- Query processing based on intermediate table

be based on SqlSugar The development framework of is introduced step by step (3)-- Implement code generation tools Database2Sharp Integrated development

be based on SqlSugar The development framework of is introduced step by step (4)-- In the data access base class GUID The primary key is automatically assigned  

be based on SqlSugar The development framework of is introduced step by step (5)-- In the service layer, interface injection is used to realize IOC Inversion of control

be based on SqlSugar The development framework of is introduced step by step (6)-- Inject user identity information into the base class interface  

be based on SqlSugar The development framework of is introduced step by step (7)-- The option mode is adopted in the file upload module 【Options】 Handle regular uploads and FTP Upload files

 《 be based on SqlSugar The development framework of is introduced step by step (8)-- Implement user operation logging in base class function encapsulation

be based on SqlSugar The development framework of is introduced step by step (9)-- combination Winform Control to implement permission control of fields

原网站

版权声明
本文为[Wuhacong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241456366677.html