privilege System, one of the common standard subsystems for web development. Combined with some of their own thinking and practice, from the beginning of this chapter of the Authority system design and implementation of the road.
recently, the project's Permissions menu construction process was refactored to return the JSON-formatted permission tree to the front end.
This article, just a rough introduction of the problem, and give 4 methods of the overall idea, followed by the detailed introduction of these 4 methods, and then introduce the complete authority system design and implementation.
the structure of the permission table :
ACL, PARENT_ACL, the most important thing is these 2 fields, with these 2 fields, you can construct a tree.
JSON format required by the front end:
"Data": [{
"ACL": 1,
"Children": [{
"ACL": 11,
"Children": [{
"ACL": 111,
}]
}
Method 1:
Add 1 level fields to the database, and the topmost level is 1, increasing by 1 for each increase.
All permissions nodes are obtained from the database in ascending order.
list<map<string, object>> rootlist = new arraylist<map<string, object>> ();
map<string, map<string, object>> rootmap = new hashmap<string, map<string, Object>> ();
For (traversal) {
Creating nodes, adding to the root node map
if (top node) {
Add to RootList
} else {
Get the parent node and put yourself in the children of the parent node.
}
}
The end of the traversal, RootList is the request.
This approach is a colleague's idea, the key is 2 points, one is to maintain level (add and modify the time required), 2 is to followLevel Ascendingsort.
Method 2: The database does not require a level field and is implemented with recursion.
list<map<string, object>> finalrootlist = new arraylist<map<string, object>> ();
list<map<string, object>> rootlist = Findrootlist (privilegelist);
list<map<string, object>> notrootlist = Findnotrootlist (privilegelist);
Identify the root node and then construct the child nodes for those root nodes.
For (map<string, object> root:rootlist) {
Construct a child node.
Buildchildlist (Root, notrootlist);
Finalrootlist.add (root);
}
The key code buildchildlist is a recursive function.
Buildchildlist () {
From all non-root nodes, find the 1th level child node of the current nodes and join the node's children.
Buildchildlist ();
}
The advantage is that you don't have to maintain level or maintain PARENT_ACL without maintaining the level field, adding and modifying permissions.
Method 3: Exactly according to Method 1, the only difference is that the database does not maintain the Level field, but after the query data comes out, manually calculate the level field of each node to sort. The following steps are basically the same as Method 1.
Therefore, the only difficulty with this method is how to calculate the depth of each node of an n-fork tree.
Method 4:calculate the depth of each node of an n-fork tree, and through practice, there are at least 2 ways.
A. Follow the recursive approach of Method 2, then maintain a level, go deep into the child, level++, and return to levels--。 The level of the current node is saved in time, recursion ends, and level is calculated.
B. Referring to a line of thought, the "unordered Tree format list, conversion print out the standard format treelist".
The author is also in accordance with the idea of recursion, on the basis of this code, and then maintain 1 level, you can.
The idea of A and B is similar in that it maintains 1 level in the recursive process, except that A's method only calculates the level, and b not only calculates the level, but also sorts the nodes. Of course, a method is also possible.
Method 5: This method is not feasible, the problem is that there is the possibility of repeated computations.
The initial level of all nodes is extra i1.
Locate all the root nodes.
Traverse each node, and if you find that you have a parent node, put your level and all of the parent's level+1.
Problem: There is no guarantee of multiple sub-nodes, there are child nodes, their parent nodes, repeated increase level, and repeated calculation of the number of difficult to statistics.
Ray fansunion-, a knowledgeable internet technology worker, provides paid IT consulting services
November 17, 2014
Hubei-Wuhan-courtesy Gate
Original starting: http://fansunion.cn/article/detail/566.html
Design and implementation of simple and common permission system (i): Constructing N (n>=4) Method of Permission menu tree