public List <Permission> GetUserPermissions(string userid) { if (PermissionCache.PermissionData == null) { PermissionCache.ResetPermissionCache(context.Permissions.Where(a => a.IsDeleted != 1).ToList(), context.UserRoles.ToList(), context.RolePermissions.ToList()); } else { if (PermissionCache.time == null || PermissionCache.time.AddHours(PermissionCache.addtime) < DateTime.Now) { PermissionCache.time = DateTime.Now; PermissionCache.ResetPermissionCache(context.Permissions.Where(a => a.IsDeleted != 1).ToList(), context.UserRoles.ToList(), context.RolePermissions.ToList()); } } return((from x in PermissionCache.PermissionData.Permissions join y in PermissionCache.PermissionData.RolePermissions on x.Id equals y.PermissionId join z in PermissionCache.PermissionData.UserRoles on y.RoleId equals z.RoleId join u in context.Roles on z.RoleId equals u.Id where z.UserId.Equals(userid, StringComparison.OrdinalIgnoreCase) && x.IsDeleted != 1 && u.IsDeleted != 1 select x).Distinct().ToList()); }
/// <summary> /// /// </summary> /// <param name="roleId"></param> /// <param name="permissionsStr"></param> /// <returns></returns> public object Save(string roleId, string permissionsStr) { var permissions = permissionsStr.Split(',').ToList(); Role role = this.DAL.Find <Role>(roleId); if (role != null) { //删除 var dels = context.RolePermissions.Where(a => a.RoleId.Equals(roleId) && permissions.Contains(a.PermissionId) == false); foreach (var x in dels) { context.RolePermissions.Remove(x); } //添加 var temp = context.RolePermissions.Where(a => a.RoleId.Equals(roleId)).Select(a => a.PermissionId); var adds = permissions.Where(a => temp.Contains(a) == false).ToList(); adds = context.Permissions.Where(a => adds.Contains(a.Id)).Select(a => a.Id).ToList(); foreach (var x in adds) { context.RolePermissions.Add(new RolePermission { Id = FCake.Core.Common.DataHelper.GetSystemID(), PermissionId = x, RoleId = roleId }); } context.SaveChanges(); PermissionCache.ResetRolePermissions(roleId, context.RolePermissions.Where(a => a.RoleId.Equals(roleId)).ToList()); return(OpResult.Success("数据保存成功")); } return(OpResult.Fail("数据保存错误")); }
public static void SaveRoles(string userid, List <string> roleids) { userid = userid.IsNullOrTrimEmpty() ? "" : userid.ToUpper(); roleids = roleids.Select(a => a.ToUpper()).ToList(); EFDbContext context = new EFDbContext(); var user = context.Users.Find(userid.ToUpper()); if (user != null) { //删除 var dels = context.UserRoles.Where(a => a.UserId.Equals(userid) && roleids.Contains(a.RoleId) == false); foreach (var x in dels) { context.UserRoles.Remove(x); } //添加 var temp = context.UserRoles.Where(a => a.UserId.Equals(userid)).Select(a => a.RoleId); var adds = roleids.Where(a => temp.Contains(a) == false).ToList(); adds = context.Roles.Where(a => adds.Contains(a.Id)).Select(a => a.Id).ToList(); foreach (var x in adds) { context.UserRoles.Add(new UserRole { Id = FCake.Core.Common.DataHelper.GetSystemID(), RoleId = x, UserId = userid }); } context.SaveChanges(); PermissionCache.ResetUserRoles(userid, context.UserRoles.Where(a => a.UserId.Equals(userid)).ToList()); } }
public string CheckDelete(Permission source, EFDbContext context) { try { PermissionCache.Delete(source); } catch (Exception e) { return(e.Message); } return(""); }
public string CheckEdit(Permission source, EFDbContext context) { try { if (source.Name.IsNullOrTrimEmpty()) { throw new Exception("名称不能为空"); } if (source.Code.IsNullOrTrimEmpty()) { throw new Exception("代码不能为空"); } if (context.Permissions.Any(a => a.Code.Equals(source.Code) && a.Id.Equals(source.Id) == false && a.MenuId.Equals(source.MenuId) && a.IsDeleted != 1)) { throw new Exception("代码已存在:" + source.Name); } PermissionCache.Edit(source); } catch (Exception e) { return(e.Message); } return(""); }