public ResResultModel SaveUserInRole(string userName, string roleName, bool isInRole) { try { //if (!HttpContext.Current.User.IsInRole("Administrators")) throw new ArgumentException(MC.Role_InvalidError); if (string.IsNullOrWhiteSpace(userName)) { return(ResResult.Response(false, MC.GetString(MC.Request_InvalidArgument, "用户名"), "")); } if (string.IsNullOrWhiteSpace(roleName)) { return(ResResult.Response(false, MC.GetString(MC.Request_InvalidArgument, "角色"), "")); } if (isInRole) { if (!Roles.IsUserInRole(userName, roleName)) { Roles.AddUserToRole(userName, roleName); } } else { if (Roles.IsUserInRole(userName, roleName)) { Roles.RemoveUserFromRole(userName, roleName); } } return(ResResult.Response(true, "调用成功", "")); } catch (System.Configuration.Provider.ProviderException pex) { return(ResResult.Response(false, pex.Message, "")); } catch (Exception ex) { return(ResResult.Response(false, ex.Message, "")); } }
public ResResultModel SaveRoleMenu(RoleMenuFmModel model) { try { if (model == null) { return(ResResult.Response(false, MC.Request_Params_InvalidError, "")); } if (string.IsNullOrWhiteSpace(model.MenuItemJson)) { return(ResResult.Response(false, MC.Request_Params_InvalidError, "")); } model.MenuItemJson = HttpUtility.UrlDecode(model.MenuItemJson); if (string.IsNullOrWhiteSpace(model.RoleName) && string.IsNullOrWhiteSpace(model.UserName)) { return(ResResult.Response(false, MC.Request_Params_InvalidError, "")); } List <RoleMenuFmInfo> list = JsonConvert.DeserializeObject <List <RoleMenuFmInfo> >(model.MenuItemJson); string roleId = ""; string userId = ""; var isRole = !string.IsNullOrWhiteSpace(model.RoleName); RoleMenu roleMenuBll = null; UserMenu userMenuBll = null; if (isRole) { roleMenuBll = new RoleMenu(); var roleBll = new Role(); var roleModel = roleBll.GetModel(model.RoleName); if (roleModel == null) { return(ResResult.Response(false, MC.GetString(MC.Request_NotExist, "角色'" + model.RoleName + "'对应数据"), "")); } roleId = roleModel.RoleId.ToString(); } else { userMenuBll = new UserMenu(); userId = Membership.GetUser(model.UserName).ProviderUserKey.ToString(); } var menuBll = new Menus(); foreach (var item in list) { var menuModel = menuBll.GetModel(Guid.Parse(item.MenuId.ToString())); menuModel.AllowRoles = menuModel.AllowRoles.Replace("*", ""); var isChanged = false; var isAccessChanged = false; var isInsert = false; var apaItems = new List <string>(); if (isRole) { var roleMenuModel = roleMenuBll.GetModel(Guid.Parse(roleId), Guid.Parse(item.MenuId.ToString())); if (roleMenuModel == null) { isInsert = true; roleMenuModel = new RoleMenuInfo(); roleMenuModel.RoleId = Guid.Parse(roleId); roleMenuModel.MenuId = Guid.Parse(item.MenuId.ToString()); } if (!string.IsNullOrEmpty(roleMenuModel.OperationAccess)) { apaItems = roleMenuModel.OperationAccess.Split(',').ToList(); } #region 允许角色 if (item.IsView) { if (!menuModel.AllowRoles.Contains(roleId)) { var allowRoles = menuModel.AllowRoles.Split(',').ToList(); allowRoles.Add(roleId); menuModel.AllowRoles = string.Join(",", allowRoles.ToArray()).Trim(','); isChanged = true; } } else { if (menuModel.AllowRoles.Contains(roleId)) { var allowRoles = menuModel.AllowRoles.Split(',').ToList(); allowRoles.Remove(roleId); if (allowRoles.Count > 0) { menuModel.AllowRoles = string.Join(",", allowRoles.ToArray()).Trim(','); } else { menuModel.AllowRoles = ""; } isChanged = true; } } #endregion #region 角色其它权限 if (item.IsAdd) { if (!apaItems.Contains(((int)EnumData.EnumOperationAccess.新增).ToString())) { apaItems.Add(((int)EnumData.EnumOperationAccess.新增).ToString()); roleMenuModel.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); isAccessChanged = true; } } else { if (apaItems.Contains(((int)EnumData.EnumOperationAccess.新增).ToString())) { apaItems.Remove(((int)EnumData.EnumOperationAccess.新增).ToString()); isAccessChanged = true; if (apaItems.Count > 0) { roleMenuModel.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); } else { roleMenuModel.OperationAccess = ""; } } } if (item.IsDelete) { if (!apaItems.Contains(((int)EnumData.EnumOperationAccess.除).ToString())) { apaItems.Add(((int)EnumData.EnumOperationAccess.除).ToString()); roleMenuModel.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); isAccessChanged = true; } } else { if (apaItems.Contains(((int)EnumData.EnumOperationAccess.除).ToString())) { apaItems.Remove(((int)EnumData.EnumOperationAccess.除).ToString()); isAccessChanged = true; if (apaItems.Count > 0) { roleMenuModel.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); } else { roleMenuModel.OperationAccess = ""; } } } if (item.IsEdit) { if (!apaItems.Contains(((int)EnumData.EnumOperationAccess.编辑).ToString())) { apaItems.Add(((int)EnumData.EnumOperationAccess.编辑).ToString()); roleMenuModel.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); isAccessChanged = true; } } else { if (apaItems.Contains(((int)EnumData.EnumOperationAccess.编辑).ToString())) { apaItems.Remove(((int)EnumData.EnumOperationAccess.编辑).ToString()); isAccessChanged = true; if (apaItems.Count > 0) { roleMenuModel.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); } else { roleMenuModel.OperationAccess = ""; } } } #endregion if (isAccessChanged) { if (isInsert) { roleMenuBll.Insert(roleMenuModel); } else { roleMenuBll.Update(roleMenuModel); } } } else { var userMenuInfo = userMenuBll.GetModel(Guid.Parse(userId), Guid.Parse(item.MenuId.ToString())); if (userMenuInfo == null) { isInsert = true; userMenuInfo = new UserMenuInfo(); userMenuInfo.UserId = Guid.Parse(userId); userMenuInfo.MenuId = Guid.Parse(item.MenuId.ToString()); } if (!string.IsNullOrEmpty(userMenuInfo.OperationAccess)) { apaItems = userMenuInfo.OperationAccess.Split(',').ToList(); } #region 拒绝用户 if (item.IsView) { if (!menuModel.DenyUsers.Contains(userId)) { var denyUsers = menuModel.DenyUsers.Split(',').ToList(); denyUsers.Add(userId); menuModel.DenyUsers = string.Join(",", denyUsers.ToArray()).Trim(','); isChanged = true; } } else { if (menuModel.DenyUsers.Contains(userId)) { var denyUsers = menuModel.DenyUsers.Split(',').ToList(); denyUsers.Remove(userId); if (denyUsers.Count > 0) { menuModel.DenyUsers = string.Join(",", denyUsers.ToArray()).Trim(','); } else { menuModel.DenyUsers = ""; } isChanged = true; } } #endregion #region 拒绝用户其它权限 if (item.IsAdd) { if (!apaItems.Contains(((int)EnumData.EnumOperationAccess.新增).ToString())) { apaItems.Add(((int)EnumData.EnumOperationAccess.新增).ToString()); userMenuInfo.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); isAccessChanged = true; } } else { if (apaItems.Contains(((int)EnumData.EnumOperationAccess.新增).ToString())) { apaItems.Remove(((int)EnumData.EnumOperationAccess.新增).ToString()); isAccessChanged = true; if (apaItems.Count > 0) { userMenuInfo.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); } else { userMenuInfo.OperationAccess = ""; } } } if (item.IsDelete) { if (!apaItems.Contains(((int)EnumData.EnumOperationAccess.除).ToString())) { apaItems.Add(((int)EnumData.EnumOperationAccess.除).ToString()); userMenuInfo.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); isAccessChanged = true; } } else { if (apaItems.Contains(((int)EnumData.EnumOperationAccess.除).ToString())) { apaItems.Remove(((int)EnumData.EnumOperationAccess.除).ToString()); isAccessChanged = true; if (apaItems.Count > 0) { userMenuInfo.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); } else { userMenuInfo.OperationAccess = ""; } } } if (item.IsEdit) { if (!apaItems.Contains(((int)EnumData.EnumOperationAccess.编辑).ToString())) { apaItems.Add(((int)EnumData.EnumOperationAccess.编辑).ToString()); userMenuInfo.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); isAccessChanged = true; } } else { if (apaItems.Contains(((int)EnumData.EnumOperationAccess.编辑).ToString())) { apaItems.Remove(((int)EnumData.EnumOperationAccess.编辑).ToString()); isAccessChanged = true; if (apaItems.Count > 0) { userMenuInfo.OperationAccess = string.Join(",", apaItems.ToArray()).Trim(','); } else { userMenuInfo.OperationAccess = ""; } } } #endregion if (isAccessChanged) { if (isInsert) { userMenuBll.Insert(userMenuInfo); } else { userMenuBll.Update(userMenuInfo); } } } if (isChanged) { menuBll.Update(menuModel); } } return(ResResult.Response(true, "", "")); } catch (Exception ex) { return(ResResult.Response(false, "异常:" + ex.Message + "", "")); } }