/// <summary> /// Generates the permissions for a given groups an fill the group trees /// </summary> /// <param name="pGroups"></param> /// <param name="pTree"></param> /// <param name="pAdminGroup"></param> /// <remarks></remarks> /// <returns></returns> private void _BuildPermissions(List <Jwt> pGroups, Dictionary <string, GroupTree> pTree, string pAdminGroup) { if (pGroups == null) { return; } foreach (Jwt group in pGroups) { //Check if the data is filled if (group.GroupCode == null) { return; } if (group.Type == null) { return; } //Add Products to ProductList if (group.Type == CNST_PRODUCT) { if (IamProducts == null) { IamProducts = new HashSet <string>(); } if (!IamProducts.Contains(group.GroupCode)) { IamProducts.Add(group.GroupCode); } } //Fill the tree with the data on the group pTree[group.GroupCode] = new GroupTree { Groups = new Dictionary <string, GroupTree>(), GroupType = group.Type }; //Check the Products //if (group.GroupAdditional?.Count > 0) this._ExtractAdditionalGroups(group.GroupAdditional); _FillPermissionsFromProducts(group.GroupPermissions, Permissions, group.GroupCode, pAdminGroup, group.Type); //Call recursivity Dictionary <string, GroupTree> groupTree = pTree[group.GroupCode].Groups; _BuildPermissions(group.GroupDescendants, groupTree, pAdminGroup); } }
/// <summary> /// Get the permissions of an object and return the permissions and if the user is Admin /// </summary> /// <param name="pRoles"></param> /// <param name="pGroup"></param> /// <param name="pP"></param> /// <param name="pAdminGroup"></param> /// <remarks></remarks> /// <returns>A tuple with item1 = is Admin, Item2 = permissions.</returns> private Tuple <bool, Dictionary <string, Dictionary <string, HashSet <string> > > > _GetObjects(List <string> pRoles, string pGroup, Dictionary <string, Dictionary <string, HashSet <string> > > pP, string pAdminGroup, string pGroupType) { bool isAdmin = false; foreach (string rol in pRoles) { foreach (KeyValuePair <string, Dictionary <string, string> > perm in _ExtractPermissions(rol)) { if (!pP.ContainsKey(perm.Key)) { pP.Add(perm.Key, new Dictionary <string, HashSet <string> >()); } if (!pP[perm.Key].ContainsKey(pGroup)) { //If is a internal team, has permission for all orgs if (pGroupType == CNST_TEAM) { if (!pP[perm.Key].ContainsKey(CNST_ALL)) { pP[perm.Key].Add(CNST_ALL, null); } //Add all products for interal teams if (IamProducts == null) { IamProducts = new HashSet <string>(); } if (!IamProducts.Contains(CNST_ALL)) { IamProducts.Add(CNST_ALL); } } pP[perm.Key].Add(pGroup, null); } } } if (pAdminGroup == pGroup || pP.ContainsKey(CNST_CREATE) && pP.ContainsKey(CNST_READ) && pP.ContainsKey(CNST_UPDATE) && pP.ContainsKey(CNST_DELETE)) { isAdmin = true; } return(new Tuple <bool, Dictionary <string, Dictionary <string, HashSet <string> > > >(isAdmin, pP)); }