1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\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
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\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
17 module.exports = function(userObject, groups, defaultPermissions = []){
\r
18 return getPermissions(userObject, groups, defaultPermissions);
\r
21 function getPermissions(userObject, groups, defaultPermissions = []){
\r
22 if(!groups || !groups.length){
\r
28 for(let i = 0; i < groups.length; i++){
\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
36 //add default that was passed in
\r
37 let perms = JSON.parse(JSON.stringify(defaultPermissions));
\r
40 groups[i].roles.forEach((elem, val) => {
\r
41 if(userInfo.roles.includes(elem.roleName)){
\r
42 elem.permissions.forEach(elem => {
\r
49 addGroupPermissions(results, groups[i], perms);
\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
57 if(groups[i].childGroups){
\r
58 groups[i].childGroups.forEach((e,v) => {
\r
59 addGroupPermissions(results, e, perms);
\r
68 function addGroupPermissions(results, group, permissions){
\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
79 //If group doesn't exist add it to the array.
\r
80 if(groupIndex == null){
\r
81 groupIndex = results.push(group) - 1;
\r
84 //add permissions to group
\r
85 if(results[groupIndex].permissions){
\r
86 permissions = permissions.concat(results[groupIndex].permissions);
\r
89 permissions = new Set(permissions);
\r
92 results[groupIndex].permissions = Array.from(permissions);
\r