added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / hooks / permissions / get-permissions.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 module.exports = function(userObject, groups, defaultPermissions = []){\r
18     return getPermissions(userObject, groups, defaultPermissions);\r
19 }\r
20 \r
21 function getPermissions(userObject, groups, defaultPermissions = []){\r
22     if(!groups || !groups.length){\r
23         return [];\r
24     }\r
25 \r
26     let results = [];\r
27 \r
28     for(let i = 0; i < groups.length; i++){\r
29 \r
30         //Get user's roles in group\r
31         let userInfo = groups[i].members.find((e) => {\r
32             return e.userId.toString() == userObject._id.toString();\r
33         });\r
34 \r
35         //grab permissions\r
36         //add default that was passed in\r
37         let perms = JSON.parse(JSON.stringify(defaultPermissions));\r
38 \r
39         if(userInfo){\r
40             groups[i].roles.forEach((elem, val) => {\r
41                 if(userInfo.roles.includes(elem.roleName)){\r
42                     elem.permissions.forEach(elem => {\r
43                         perms.push(elem);\r
44                     })\r
45                 }\r
46             });\r
47         }\r
48 \r
49         addGroupPermissions(results, groups[i], perms);\r
50 \r
51         //Run recusivley for parent and child groups\r
52         if(groups[i].parentGroups){\r
53             groups[i].parentGroups.forEach((e, v) => {\r
54                 addGroupPermissions(results, e, ['read'])\r
55             });\r
56         }\r
57         if(groups[i].childGroups){\r
58             groups[i].childGroups.forEach((e,v) => {\r
59                 addGroupPermissions(results, e, perms);\r
60             });\r
61         }\r
62 \r
63     }\r
64 \r
65     return results;\r
66 }\r
67 \r
68 function addGroupPermissions(results, group, permissions){\r
69 \r
70     // Look for group in result to make sure it doesnt alreay exist\r
71     let groupIndex = null;\r
72     results.forEach((elem, val) => {\r
73         if(elem._id.toString() == group._id.toString()){\r
74             groupIndex = val;\r
75             return;\r
76         }\r
77     })\r
78 \r
79     //If group doesn't exist add it to the array.\r
80     if(groupIndex == null){\r
81         groupIndex = results.push(group) - 1;\r
82     }\r
83 \r
84     //add permissions to group \r
85     if(results[groupIndex].permissions){\r
86         permissions = permissions.concat(results[groupIndex].permissions);\r
87     }\r
88 \r
89     permissions = new Set(permissions);\r
90 \r
91     //set permissions\r
92     results[groupIndex].permissions = Array.from(permissions);\r
93 }