/// <summary> /// /// </summary> /// <param name="roleId"></param> /// <param name="action"></param> /// <returns></returns> public async Task <bool> ActionValidate(long roleId, string action) { var authorizeurldata = new List <AuthorizeActionModel>(); var cachedata = await Cache.GetAsync <List <AuthorizeActionModel> >(roleId.ToString(), "authorizeurl"); if (cachedata == null) { var moduledata = await ModuleApp.GetSaasModuleListAsync(); var buttondata = (await ModuleButtonApp.GetSaasModuleListAsync()); var authorizedata = await RoleAuthorizeRep.Find(o => o.ObjectId == roleId).ToListAsync(); foreach (var item in authorizedata) { if (item.ItemType == 1) { Module module = moduledata.Find(t => t.Id == item.ItemId); if (module != null) { authorizeurldata.Add(new AuthorizeActionModel { Id = module.Id, UrlAddress = module.UrlAddress }); } } else if (item.ItemType == 2) { ModuleButton moduleButton = buttondata.Find(t => t.Id == item.ItemId); if (moduleButton != null) { authorizeurldata.Add(new AuthorizeActionModel { Id = moduleButton.ModuleId, UrlAddress = moduleButton.UrlAddress }); } } } await Cache.AddAsync(roleId.ToString(), authorizeurldata, new TimeSpan(0, 30, 0), "authorizeurl"); } else { authorizeurldata = cachedata; } foreach (var item in authorizeurldata) { if (!string.IsNullOrEmpty(item.UrlAddress)) { string[] url = item.UrlAddress.Split('?'); if (url[0].ToLower() == action.ToLower()) { return(true); } } } return(false); }
/// <summary> /// /// </summary> /// <param name="roleId"></param> /// <param name="curUser"></param> /// <returns></returns> public async Task <List <ModuleButton> > GetButtonList(long roleId, bool bSys) { List <ModuleButton> data; if (bSys) { data = await Cache.GetAsync <List <ModuleButton> >("sys", "buttons"); if (data == null) { data = await ModuleButtonApp.GetSaasModuleListAsync(new ModuleButtonOption { IsEnabled = true }); await Cache.AddAsync("sys", data, new TimeSpan(0, 30, 0), "buttons"); } } else { data = await Cache.GetAsync <List <ModuleButton> >(roleId.ToString(), "buttons"); if (data == null) { data = new List <ModuleButton>(); var buttondata = await ModuleButtonApp.GetSaasModuleListAsync(new ModuleButtonOption { IsEnabled = true }); var authorizedata = await RoleAuthorizeRep.Find(o => o.ObjectId == roleId && o.ItemType == 2).ToListAsync(); foreach (var item in authorizedata) { ModuleButton moduleButtonEntity = buttondata.Find(t => t.Id == item.ItemId); if (moduleButtonEntity != null) { data.Add(moduleButtonEntity); } } await Cache.AddAsync(roleId.ToString(), data, new TimeSpan(0, 30, 0), "buttons"); } } return(data.OrderBy(t => t.SortCode).ToList()); }
/// <summary> /// 角色添加 /// </summary> /// <param name="entity"></param> /// <returns></returns> public async Task <ResultDto <Role> > CreateAsync(Role entity, List <long> permissionIds, CurrentUser currentUser) { entity.CreatorTime = DateTime.Now; var moduledata = await ModuleApp.GetSaasModuleListAsync(); var buttondata = await ModuleButtonApp.GetSaasModuleListAsync(); List <long> allpermissionIds = new List <long>(); foreach (long id in permissionIds) { allpermissionIds.Add(id); if (moduledata.Count(o => o.Id == id) > 0) { var md = moduledata.Where(o => o.Id == id).FirstOrDefault(); if (md.ParentId != 0) { allpermissionIds.AddRange(await Fibonacci(md.ParentId)); } } else if (buttondata.Count(o => o.Id == id) > 0) { var md = buttondata.Where(o => o.Id == id).FirstOrDefault(); if (md.ModuleId != 0) { allpermissionIds.AddRange(await Fibonacci(md.ModuleId)); } } } allpermissionIds = allpermissionIds.Distinct().ToList(); List <RoleAuthorize> ras = new List <RoleAuthorize>(); foreach (long id in allpermissionIds)// permissionIds { int itemType = 0; if (moduledata.Count(o => o.Id == id) > 0) { itemType = 1; } else if (buttondata.Count(o => o.Id == id) > 0) { itemType = 2; } if (itemType > 0) { RoleAuthorize ra = new RoleAuthorize { ObjectId = entity.Id, ObjectType = 1, ItemId = id, ItemType = itemType, CreatorTime = DateTime.Now }; ras.Add(ra); } } UnitWork.Add <Role>(entity); UnitWork.BatchAdd <RoleAuthorize>(ras.ToArray()); UnitWork.Save(); if (currentUser != null) { await OperateLogApp.InsertLogAsync <Role>(currentUser, "添加角色", entity); } return(ResultDto <Role> .Suc(entity)); }
public async Task <ResultDto <Role> > UpdateAsync(Role entity, List <long> permissionIds, CurrentUser currentUser) { var moduledata = await ModuleApp.GetSaasModuleListAsync(); var buttondata = await ModuleButtonApp.GetSaasModuleListAsync(); var authorizs = await RoleAuthorizeApp.GetListAsync(entity.Id, 1); List <long> allpermissionIds = new List <long>(); foreach (long id in permissionIds) { allpermissionIds.Add(id); if (moduledata.Count(o => o.Id == id) > 0) { var md = moduledata.Where(o => o.Id == id).FirstOrDefault(); if (md.ParentId != 0) { allpermissionIds.AddRange(await Fibonacci(md.ParentId)); } } else if (buttondata.Count(o => o.Id == id) > 0) { var md = buttondata.Where(o => o.Id == id).FirstOrDefault(); if (md.ModuleId != 0) { allpermissionIds.AddRange(await Fibonacci(md.ModuleId)); } } } allpermissionIds = allpermissionIds.Distinct().ToList(); //现有 List <long> itemIds = authorizs.Select(o => o.ItemId).ToList(); List <long> deleteIds = authorizs.Where(o => !allpermissionIds.Contains(o.ItemId) && o.ObjectId == entity.Id && o.ObjectType == 1).Select(o => o.Id).ToList(); List <RoleAuthorize> ras = new List <RoleAuthorize>(); foreach (long id in allpermissionIds) { if (itemIds.Contains(id)) { continue; } int itemType = 0; if (moduledata.Count(o => o.Id == id) > 0) { itemType = 1; } else if (buttondata.Count(o => o.Id == id) > 0) { itemType = 2; } if (itemType > 0) { RoleAuthorize ra = new RoleAuthorize { ObjectId = entity.Id, ObjectType = 1, ItemId = id, ItemType = itemType, CreatorTime = DateTime.Now }; ras.Add(ra); } } UnitWork.Update <Role>(entity); UnitWork.BatchAdd <RoleAuthorize>(ras.ToArray()); UnitWork.Delete <RoleAuthorize>(o => deleteIds.Contains(o.Id)); UnitWork.Save(); if (currentUser != null) { await OperateLogApp.InsertLogAsync <Role>(currentUser, "修改角色", entity); } await RemoveCacheAsync(entity.Id); return(ResultDto <Role> .Suc(entity)); }