Write your own C # framework from scratch (19) -- Web-layer backend permission Module

Source: Internet
Author: User

I don't know whether the system has been written for nearly three months. I feel a little hard to write the specific page functions recently. In many places, if I open my mouth, it can be very detailed and comprehensive, if you can write a text, you won't be able to write it too much. If you want to make it clear in some places, you must use several times of text + examples + Images in changes to make it clear, writing this is too time-consuming and very busy recently, so I can only do my best. I hope you will study it on your own. If you don't understand anything, send me a comment or email, I will explain it again in detail.

 

Back to the question: For page access permissions and permission Control for each button, several different methods have been used long ago, such as assigning names or codes to each control, then bind these values when writing code, such as using XML to Configure permissions. These methods are troublesome, and because they all use encoding methods, they need to be bound one by one during development, which is prone to errors. After continuous improvement, we finally completed the registration and management of page controls used by the system to bind control permissions (if similar, it is a coincidence, haha ...), create a menu, bind the corresponding file (PAGE), and add the name to the public identification library, on the page control permission management page, bind various page controls (just click the mouse) and assign different operation permissions through positions (roles, you only need to set the postmaster's position, and the postmaster has all the permissions of the roles it binds.

See the following description.

First, register the menu at the backend (menu binding page)

 

Create a public page permission ID to be used

Bind the operation control to the menu page for which you want to bind the page control permissions (the public control name entered on the left column and the page control bound on the right column can be easily bound by clicking the mouse)

Create a department

Create positions (roles) in different departments)

Set Menu and page control operation permissions for different roles

 

Development Description: (This section describes different content from the previous chapter)

1. PagePowerSignPublicList. aspx. cs public page control permission identification list management File

This page is a classic normal list page, compare code, there are some places different from the menu page

1 # region load data 2 // <summary> Read data </summary> 3 public override void LoadData () 4 {5 // set sorting 6 if (sortList = null) 7 {8 Sort (null); 9} 10 11 // bind the Grid table 12 bll. bindGrid (Grid1, Grid1.PageIndex + 1, Grid1.PageSize, null, sortList); 13} 14 15 # endregion

For the call of the sorting function, the menu page has a sense list, so you need to customize the sorting function. For the normal list, you can directly call the Sort (null) function of the parent class.

Binding a Grid table is also different from calling a menu page, bll. the BindGrid () function uses the page on which the current page needs to be added to the parameter. The number of rows is displayed on each page, and null is the condition parameter. It will explain in detail how to use it later, the last parameter is the sorting rule parameter.

In addition, the Delete () Delete function also uses the batch Delete function. However, it checks before deletion. If the specified record cannot be deleted, a prompt is displayed, indicating that the record of that Id cannot be deleted, you can simply compare the two list files.

 

2. PagePowerSignPublicEdit. aspx. cs public page control permission identity editing File

Note the Save () function in this file.

1 # region save 2 /// <summary> 3 // data save 4 /// </summary> 5 /// <returns> </returns> 6 public override string save () 7 {8 string result = string. empty; 9 int id = ConvertHelper. cint0 (hidId. text); 10 11 try12 {13 # region data verification 14 15 if (string. isNullOrEmpty (txtCName. text. trim () 16 {17 return txtCName. label + "cannot be blank! "; 18} 19 var sName = StringHelper. left (txtCName. text, 20); 20 if (PagePowerSignPublicBll. getInstence (). exist (x => x. CName = sName & x. id! = Id) 21 {22 return txtCName. Label + "already exists! Enter again! "; 23} 24 if (string. IsNullOrEmpty (txtEName. Text. Trim () 25 {26 return txtEName. Label +" cannot be blank! "; 27} 28 var sEname = StringHelper. left (txtEName. text, 50); 29 if (PagePowerSignPublicBll. getInstence (). exist (x => x. EName = sEname & x. id! = Id) 30 {31 return txtEName. Label + "already exists! Enter again! "; 32} 33 34 # endregion35 36 # region value 37 // define whether to update the ID-that is, whether the name of the current record has changed 38 bool isUpdate = false; 39 40 // obtain the object 41 var model = new PagePowerSignPublic (x => x. id = id); 42 43 // determine whether the name is changed 44 if (id> 0 & (sName! = Model. CName | sEname! = Model. EName) 45 {46 isUpdate = true; 47} 48 49 // set the name to 50 model. CName = sName; 51 // set the English name 52 model. EName = sEname; 53 # endregion54 55 // ------------------------------------------------------------ 56 // store it to database 57 PagePowerSignPublicBll. getInstence (). save (this, model); 58 59 // determine whether to synchronously update the joined table field 60 if (isUpdate) 61 {62 // call the update function to synchronously update all records corresponding to 63 PagePowerSignBll. getInstence (). updateValue_For_PagePowerSignPublic_Id (th Is, model. id, PagePowerSignTable. CName, model. CName, PagePowerSignTable. EName, model. EName); 64} 65} 66 catch (Exception e) 67 {68 result = "failed to save! "; 69 70 // if an exception occurs, save the Error Log Information 71 CommonBll. WriteLog (result, e); 72} 73 74 return result; 75} 76 # endregionView Code

In the function, you can find the following code:

1 // define whether to update the identity -- that is, whether the name of the current record has changed 2 bool isUpdate = false; 3 4 // determine whether the name is changed. 5 if (id> 0 & (sName! = Model. CName | sEname! = Model. EName) 6 {7 isUpdate = true; 8} 9 10 // determine whether to synchronously update the joined table field 11 if (isUpdate) 12 {13 // call the update function, synchronously update all records corresponding to 14 PagePowerSignBll. getInstence (). updateValue_For_PagePowerSignPublic_Id (this, model. id, PagePowerSignTable. CName, model. CName, PagePowerSignTable. EName, model. EName); 15}

This is used when we modify the name of the record in Table A, synchronously modify all records referenced in this record Name field in other associated tables

Why? When you look at the database structure of the data dictionary, you will find that many tables are associated with other tables, not only by referencing the IDs of other tables, the name corresponding to this Id is also referenced. This operation makes it possible to almost eliminate the need for multi-Table association when writing a query statement, because the content we want to display already exists in the queried table, after this processing, when we modify the name of the relevant table, we must modify the name of the associated table synchronously, for the modification of these joined table names, our T4 template also generates corresponding functions, such as: UpdateValue_For _ TABLE name_id () function, then you can simply follow the above code writing method to implement it.

 

3. UseLogList. aspx. cs user operation log management File

Note the InquiryCondition () function in this file.

1 /// <summary> 2 /// query Condition 3 /// </summary> 4 /// <returns> </returns> 5 private List <ConditionHelper. sqlqueryCondition> InquiryCondition () 6 {7 var wheres = new List <ConditionHelper. sqlqueryCondition> (); 8 9 // if the Id has a value, it indicates that the operation log of the specified administrator is queried. 10 if (_ id! = 0) 11 {12 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. where, LoginLogTable. manager_Id, Comparison. equals, _ id); 13} 14 15 // start time 16 if (! String. isNullOrEmpty (dpStart. text. trim () 17 {18 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. and, LoginLogTable. addDate, Comparison. greaterOrEquals, StringHelper. filterSql (dpStart. text); 19 // end time 20 if (! String. isNullOrEmpty (dpEnd. text. trim () 21 {22 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. and, LoginLogTable. addDate, Comparison. lessOrEquals, StringHelper. filterSql (dpEnd. text); 23} 24} 25 26 // ip address 27 if (! String. isNullOrEmpty (txtIp. text. trim () 28 {29 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. and, LoginLogTable. ip, Comparison. equals, StringHelper. filterSql (txtIp. text); 30} 31 // logon remarks 32 if (! String. isNullOrEmpty (txtloginfo. text. trim () 33 {34 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. and, LoginLogTable. notes, Comparison. like, "%" + StringHelper. filterSql (txtloginfo. text) + "%"); 35} 36 37 return wheres; 38}

 

This is a query condition function. The ConditionHelper. SqlqueryCondition class is a custom encapsulation condition class.

Its Constructor (public SqlqueryCondition (ConstraintType ctype, string columnname, Comparison cparsion, object value, bool isParentheses = false) has a total of five parameters, four of which are required parameters

The first ConstraintType ctype is the query type, mainly ConstraintType. Where, ConstraintType. And, And ConstraintType. Or.

If there are multiple conditions, ConstraintType. Where can only be placed at the beginning, and only one condition can use this parameter, which is rarely used.

ConstraintType. And indicates the relationship between the current parameter And the preceding parameter.

ConstraintType. Or usually refers to the Or relationship between the current parameter and the preceding parameter.

The second parameter string columnname is the name of the condition column field.

The third parameter Comparison cparsion is an expression that uses Comparison. X to set, according to the needs of the =,>,> =, <, <=, like, in, not in ......

The fourth parameter, object value, is a condition value. If it is an in or not in expression, the condition value must be of the data type, such as string [], int [], and object [].

The fifth bool isParentheses parameter is left parenthesis, while the right parenthesis uses new ConditionHelper. sqlqueryCondition () or new ConditionHelper. sqlqueryCondition (Comparison. closeParentheses) to add, and then when the in or not in expression, must follow the right brackets

 

Example 1:

(A> 2 Or A <= 10) And B = 100

1   var wheres = new List<ConditionHelper.SqlqueryCondition>();2 3   wheres.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.Where, "A", Comparison.GreaterThan, 2, true));4 5   wheres.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.Or, "A", Comparison.LessOrEquals, 10));6 7   wheres.Add(new ConditionHelper.SqlqueryCondition());8 9   wheres.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, "B", Comparison.Equals, 100));

Example 2:

A = 2 And B in arr And C like 'abc % '(left And right brackets must be added for an in query)

1 var wheres = new List <ConditionHelper. sqlqueryCondition> (); 2 3 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. where, "A", Comparison. equals, 2); 4 // Add the left bracket 5 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. and, "B", Comparison. in, arr, true); 6 // Add the right brackets 7 wheres. add (new ConditionHelper. sqlqueryCondition (); 8 9 wheres. add (new ConditionHelper. sqlqueryCondition (ConstraintType. and, "C", Comparison. like, "abc % "));

This encapsulation class will eventually generate the SubSonic3.0 underlying call condition parameters. After testing, it is found that the SubSonic3.0 underlying call condition parameters only support single brackets and do not support nested brackets, after multiple nesting, only single parentheses are generated and errors occur. Therefore, it is best to use other methods to implement multiple nested parentheses, such as Linq, stored procedures, and SQL statement concatenation.

 

4. Summary

This update has many code functions, for example, the Department, position, public page control permission ID, page control permission management, online user list, user login log, user operation log, error log, and other functions have been completed, related Classes and functions of online users are also optimized.

The content seems to be quite a lot. In fact, it is all about calling the previously written template functions and other public functions. When you view the cs code on so many pages, you can find that the content is similar, they are all very formatted. If you have tried to add new features to these completed template pages, you can find that it is very simple and the development is very fast.

As for how to implement each page, you don't have to elaborate on it as before. It will explain some special functions or function calls, for more information, we can only start from the various functional applications in the logic layer, so we will not go into detail one by one. There are a lot of comments in the Code. Let's take a look.

 

It is best to attach several pages

 

 

Weblayer back-end permission limit .rar

 

 

Copyright:

This article was originally published by AllEmpty and published in the blog Park. You are welcome to repost it. You must keep this statement without your consent and provide the original article link clearly on the article page,Otherwise, the right to pursue legal liability is reserved.. If you have any questions, you can use1654937@qq.comThank you very much for contacting me.

 

As long as you are interested in learning and making progress together, you can add Q group: 327360708 to this topic.

 

For more information, please refer to blog: http://www.cnblogs.com/EmptyFS/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.