private string GetRolePermissions(long userId, long roleId) { var blModule = new BlModule(); var modules = blModule.LoadAll(userId).ToList(); var toRetModules = (from m in modules select new TreeItemVm { id = "M_" + m.Id, parent = "#", text = m.Id, icon = "fa fa-folder colorMain ", state = "{\"opened\": \"true\"}", li_attr = "{\"class\" : \"form-control-label\"}", a_attr = "{\"class\": \"no_checkbox\"}" }).ToList(); //Get all modules permissions and set is active for assigned ones var blPermission = new BlPermission(); var permissions = blPermission.LoadAll(userId).ToList(); permissions.ForEach(p => p.IsActive = p.Roles.Where(r => r.Id == roleId).Any()); //Get is active from parent and set them as disabled var parentRolesIds = LoadParents(userId, LoadSingle(userId, roleId).SingleItemAsList()).Where(role => role.Id != roleId).Select(parentRole => parentRole.Id); permissions.ForEach(p => p.IsActiveInherited = p.Roles.Where(r => parentRolesIds.Contains(r.Id)).Any()); var toRetPermissions = (from p in permissions select new TreeItemVm { id = p.Id.ToString(), parent = "M_" + p.Module.Id, text = p.Code, icon = "fa fa-gear " + (p.IsActive ? "colorGreen" : "colorRed"), state = "{\"selected\": \"" + (p.IsActive || p.IsActiveInherited ? "true" : "false") + "\", \"disabled\": \"" + (p.IsActiveInherited ? "true" : "false") + "\"}", li_attr = "{\"class\" : \"form-control-label " + (p.IsActiveInherited ? "jstree-disabled" : "") + "\"}" }).ToList(); toRetModules.AddRange(toRetPermissions); var toRet = new JavaScriptSerializer().Serialize(toRetModules); return(toRet); }
public Role Edit(long userId, Role toEdit, string roleUsers = "", string rolePermissions = "") { using (var tran = new TransactionScope()) { var oldImage = LoadSingle(userId, toEdit.Id, true); oldImage.ParentRole = toEdit.ParentRole; oldImage.Code = toEdit.Code; var toRet = _repository.Edit(oldImage); //Adjust role users var arRoleUsers = roleUsers.Split(','); var blUser = new BlUser(); var allUsers = blUser.LoadAll(userId); foreach (var user in allUsers) { if (user.Roles.Where(r => r.Id == toRet.Id).Any() && !arRoleUsers.Contains(user.Id.ToUiString())) { user.Roles.Remove(user.Roles.Where(ur => ur.Id == toRet.Id).First()); blUser.Edit(userId, user); } if (!user.Roles.Where(r => r.Id == toRet.Id).Any() && arRoleUsers.Contains(user.Id.ToUiString())) { user.Roles.Add(LoadSingle(userId, toRet.Id)); blUser.Edit(userId, user); } } //Adjust role permissions var arRolePermissions = rolePermissions.Split(','); var blPermission = new BlPermission(); var allPermissions = blPermission.LoadAll(userId); foreach (var permission in allPermissions) { if (permission.Roles.Where(r => r.Id == toRet.Id).Any() && !arRolePermissions.Contains(permission.Id.ToUiString())) { permission.Roles.Remove(permission.Roles.Where(rp => rp.Id == toRet.Id).First()); blPermission.Edit(userId, permission); } if (!permission.Roles.Where(r => r.Id == toRet.Id).Any() && arRolePermissions.Contains(permission.Id.ToUiString())) { permission.Roles.Add(LoadSingle(userId, toRet.Id)); blPermission.Edit(userId, permission); } } //if (toRet.Users == null) //{toRet.Users = new List<User>();} //toRet.Users = toRet.Users.ToList(); //foreach (var permissionUser in toRet.Users) //{ // var user = BlUser.LoadSingle(Convert.ToInt64(permissionUser.Id)); // if (!toRet.Users.Where(u => roleUsers.Split(',').Contains(u.Id.ToUiString())).Any()) // { // user.Roles.Remove(user.Roles.Where(ur => ur.Id == toRet.Id).First()); // blUser.Edit(userId, user); // } //} //foreach (var roleUserId in arRoleUser) //{ // var user = BlUser.LoadSingle(Convert.ToInt64(roleUserId)); // if (!toRet.Users.Where(u => u.Id == userId).Any()) // { // user.Roles.Remove(user.Roles.Where(r => r.Id == toRet.Id).First()); // blUser.Edit(userId, user); // } // else // { // user.Roles.Add(LoadSingle(userId, toRet.Id)); // blUser.Edit(userId, user); // } //} //} BlLog.Log(userId, Module, "Edit role", "RoleModified", new object[] { toEdit.Code }); tran.Complete(); return(toRet); } }