Source code GitHub: https://github.com/ZhaoRd/Zrd_0001_AuthorityManagement
1. Introduction
For the Rights management system, the system module is a necessary part, then how to process and collect the module information is a housekeeper step, before looking at Guo Min Osharp, I can think of is through the administrator through the management of the background management, The Osharp uses the Attirbute method to collect the module information. The demo draws on the Osharp way, by using attribute to define the module information, the program starts by judging the attribute module and the specific function of the information collection.
For the newly collected information, how to determine which of these new information needs to be updated, what needs to be removed from the existing information, and what needs to be added? Here you need to use the set operation, see below for details.
2. Definition of module information
In the Web, the access path to operate a function is Controller/action, in this demo, a controller is a function, an action is a specific operation permissions. In the source code, define the Systemmodelattribute to define the function information, the code is as follows:
<summary>///collection system module. </summary> public class Systemmodelattribute:attribute {///<summary>//module name. </summary> private string name; <summary>///group name. </summary> private String GroupName; <summary>//Read-only module name. </summary> public string Name {get {return this.name; }}///<summary>///group name. </summary> public string GroupName {get {return this.groupname; set {//If the name is initialized through the constructor, the other settings are invalid if (string. IsNullOrEmpty (This.groupname)) {this.groupname = value; }}}///<summary> permissions for module. </summary> Private Permissionvalue Permissionvalue; <summary>///module permissions. </summary> public Permissionvalue Permissionvalue {get {return thi S.permissionvalue; }}///<summary>//Initializes a new instance of the <see cref= "Systemmodelattribute"/> ; Class. </summary>//<param name= "name" >//Module name. </param>//<param name= "Permissionvalue" >//Module permission values. </param>//<param name= "GroupName" >//Group name. </param> public Systemmodelattribute (string name, Permissionvalue permissionvalue = Permissionvalue.all, string groupName = null) {this.name = name; This.groupname = GroupName; This.permissionvalue = Permissionvalue; } }
Using the attribute on the controller, you can define a module, using the same method as
Systemmodelattribute can define the module information that needs to be collected, in the same vein, you can define a attribute to define the permission information that needs to be collected, the source code is as follows
<summary>/// Permissions information settings for information collection. </summary> public class Permissionsettingattribute:attribute {/// <summary> // Specific permissions. </summary> private readonly permissionvalue permissionvalue; <summary>// Initializes a new instance of the <see cref= "Permissionsettingattribute"/> class. </summary>// <param name= "value" >//The value. </param> public Permissionsettingattribute (permissionvalue value) { This.permissionvalue = value; } <summary>// Gets the permission value. </summary> public permissionvalue permissionvalue { get { return} This.permissionvalue;}}}
Use the following methods:
Use Systemmodelattribute and Permissionsettingattribute to define what is needed to collect information.
3. Information collection
We use different attribute on the controller and action to define the information, how do we collect the information in the code? The main code is as follows
The first is to get to the assembly where the controller resides, and then to process the type based on the type obtained, the specific Invoke method is as follows
<summary>//Parsing controller type to collect attribute information. </summary>//<param name= "target" >//The target. </param>//<returns>//The <see cref= "IEnumerable"/>. </returns> public ienumerable<functiondto> Invoke (Type target) {var result = new List<functiondto> (); var targetType = target; Whether to use Systemmodelattribute if (!targettype.isdefined (typeof (Systemmodelattribute), false)) { return result; }//Get all controllers in the method var methods = Targettype.getmethods (); To obtain the specific permissions of the function module, you must use the Permissionsettingattribute to define the permission values, and take all the functions and operations//For example: A controller only defines the create and edit, then the function module's permissions It's create|. Edit var functionpermissionvalue = (from MethodInfo in methods where Methodinf O.isdefined (typeof (PermissionsettingAttribute), False) Select Methodinfo.getcustomattribute<permissionsettingattribute> ()). Aggregate (Permissionvalue.none, current, permissionsetting) = Current | perm Issionsetting.permissionvalue); Get Systemmodelattribute Specific information var description = targettype.getcustomattribute<systemmodelattribute> (); var areaname = this. Getarea (target); var function = new Functiondto () {functionname = Descript Ion. Name, modelname = areaname, Permissionvalue = function Permissionvalue}; Result. ADD (function); return result; }
Through such a function, you can collect a controller in the permission information, a function is a controller,controller in the use of Permissionsettingattribute defined specific operation permissions, All defined permission values are taken and calculated,
, a controller contains create and edit, which means that the maximum permissions for this function module are create| Edit, so that when a permission assignment is made, only the permissions that the feature has is assigned, and permissions that are not assigned cannot be allocated.
4. Information Processing
After the above processing, then the function information and permission information have been collected, the information is saved to the database, you can quickly and easily initialize the system's functional information.
In the aspect of information processing, the main use of set operations, in order to explain the set operation, I have the existing database of function information set defined as a set, the new collection is defined as a collection of B, then for the B set, how to determine which is required to add new to the database, which needs to be updated to the database, There are already functions in the database, and which ones need to be deleted?
A. For features that need to be added, it can be understood that there is a collection of B but not a set
B. For features that need to be updated, it can be understood that there is a collection of B and a
C. For features that need to be removed, it can be understood that there is a collection of a in the set but does not exist in the B
To deal with a B C three case from the set angle, then
A. b set minus a set
B. B Set intersection a set
C. A set minus B set
Through the collection operation, we can get the function information that needs to be added, updated and deleted.
code is as follows:
<summary>///Initialize system functions. </summary>//<param name= "Functiondtos" >//The function DTOs. </param> public void Initmodel (ienumerable<functiondto> functiondtos) {var funct ions = This.functionRepository.FindAll (). ToList (); var addfunctions = Functiondtos.select (Mapper.map<functiondto, function>). AsEnumerable (); Create a condition that determines whether the two Function is equal to var functioncomparer = Equalityhelper<function>. Createcomparer (m = m.modelname + "#" + m.functionname); var enumerable = Addfunctions as function[]?? Addfunctions.toarray (); Included in the collection to be processed (addfunctions)//But not included in the already existing collection (functions)//represent the module//difference operation that needs to be added to the system var toaddfunctions = Enumerable. Except (functions, functioncomparer); Contained in an already existing collection (functions)//But not contained in the set to be processed (addfunctions)//represents the need to be removed from the systemThe module//differential set operation var todeletefunctions = functions. Except (enumerable, Functioncomparer); That is included in the collection to be processed (addfunctions)//is contained in the already existing collection (functions)//represents the need to update the content//intersection operation VAR Toupdatefunctions = functions. Intersect (enumerable, Functioncomparer); LogHelper.Logger.Info (String. Format ("NEW: {0}"; Update function: {1}; Delete function: {2}; ", Toaddfunctions.count (), Toupdatefunctions.count (), Todeletefunctions.count ())); foreach (Var addfunction in toaddfunctions) {addfunction.id = Guid.NewGuid (); var role = This.roleRepository.Find (SPECIFICATION<ROLE>. Eval (U = u.rolename = = "system administrator"); THIS.FUNCTIONREPOSITORY.ADD (addfunction); Initialize the system administrator's permissions This.functionInRoleRepository.Add (new Functioninrole () { ID = Guidhelper.generateguid (), Function = addfunction, Permissionvalue = ad Dfunction.permissionvalue, role = role}); } foreach (Var deletefunction in todeletefunctions) {This.functionRepository.Remove ( DeleteFunction); } foreach (Var updatefunction in toupdatefunctions) {var function = UpdateFunction; var query = Enumerable. Where (m = = M.functionname = function. functionname); var newvalue = string. IsNullOrEmpty (updatefunction.modelname)? Query. Singleordefault (U = u.modelname = = NULL): Query. Singleordefault (U = u.modelname = = function. ModelName); if (newvalue = = null) {continue; } updatefunction.functionname = Newvalue.functionname; Updatefunction.actionname = Newvalue.actionName; Updatefunction.areasname = Newvalue.areasname; Updatefunction.controllername = Newvalue.controllername; Updatefunction.description = newvalue.description; Updatefunction.modelname = Newvalue.modelname; Updatefunction.permissionvalue = Newvalue.permissionvalue; This.functionRepository.Update (updatefunction); } this.functionInRoleRepository.Context.Commit (); }
5. Summary
The definition and collection of information is mainly using attribute, which provides a way to use attribute and how to use it.
This article also provides a way to use the collection to process the data, extending the idea that I will deal with the collection again, before always using the for loop to process two sets, then through the set operation, it is convenient to get the collection information.
Recommended QQ Group:
278252889 (angularjs Chinese community)
5008599 (MVC EF Communication Group)
134710707 (ABP Architecture Design Exchange Group)
59557329 (C # Base)
230516560 (. NET DDD Base)
Contact information: qq:351157970
Fourth Chapter function initialization