123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using CZFW.Framework.Interface;
- using CZFW.Framework.Model.Entity;
- using CZFW.Framework.Model.ViewModel;
- using System.Collections.Generic;
- using System.Linq;
- using CZFW.Framework.Model;
- namespace CZFW.Framework.Logic
- {
- public class UserRoleNavigationLogic : LogicBase<UserRoleNavigationEntity>, IUserRoleNavigation
- {
- public IList<NavigationModel> GetNavigationTreeByRoleId(int roleId)
- {
- var list = (from rn in GetQueryable()
- join nav in DbContext.Set<NavigationEntity>()
- on rn.NavigationId equals nav.Id
- where rn.RoleId == roleId
- select new NavigationModel()
- {
- Description = nav.Description,
- ParentMark = nav.ParentMark,
- Id = nav.Id,
- Icon = nav.Icon,
- Mark = nav.Mark,
- Name = nav.Name,
- SortOrder = nav.SortOrder??0,
- Thumbnail = nav.Thumbnail,
- Url = nav.Url
- }).ToList();
- var res = SetTree(list, "0");
- return res;
- }
- public IList<NavigationModel> GetNavigationTreeByUserId(int userId)
- {
- var roles = DbContext.Set<UserRoleEntity>()
- .Where(x => x.UserId == userId)
- .Select(x => x.RoleId).ToList();
- var list = (from rn in GetQueryable()
- join nav in DbContext.Set<NavigationEntity>()
- on rn.NavigationId equals nav.Id
- where roles.Contains(rn.RoleId)||rn.UserId==userId
- select new NavigationModel()
- {
- Description = nav.Description,
- ParentMark = nav.ParentMark,
- Id = nav.Id,
- Icon = nav.Icon,
- Mark = nav.Mark,
- Name = nav.Name,
- SortOrder = nav.SortOrder??0,
- Thumbnail = nav.Thumbnail,
- Url = nav.Url
- }).Distinct().ToList();
- var res = SetTree(list, "0");
- return res;
- }
- public ResultModel GrantRole(int roleId, int navId)
- {
- var res = new ResultModel();
- var count = GetCount(x => x.RoleId == roleId && x.NavigationId == navId && roleId != 0);
- if (count > 0)
- {
- res.Success = false;
- res.Message = "该角色已经觉有该授权!";
- return res;
- }
- var item = new UserRoleNavigationEntity()
- {
- RoleId = roleId,
- NavigationId = navId
- };
- var tp = AddEntity(item);
- return tp;
- }
- public ResultModel GrantRoleNavigations(int roleId, IList<int> navIds)
- {
- var exisitingIds = GetQueryable()
- .Where(x => x.RoleId == roleId && roleId != 0 && navIds.Contains(x.NavigationId))
- .Select(x => x.NavigationId).ToList();
- var nonExisitingIds = navIds.Where(x => !exisitingIds.Contains(x)).ToList();
- var roleNavs = new List<UserRoleNavigationEntity>();
- foreach (var item in nonExisitingIds)
- {
- var entity = new UserRoleNavigationEntity()
- {
- NavigationId = item,
- RoleId = roleId
- };
- roleNavs.Add(entity);
- }
- var res = AddEntities(roleNavs);
- return res;
- }
- public ResultModel GrantUser(int userId, int navId)
- {
- var res = new ResultModel();
- var count = GetCount(x => x.UserId == userId && x.NavigationId == navId && userId != 0);
- if (count > 0)
- {
- res.Success = false;
- res.Message = "该角色已经觉有该授权!";
- return res;
- }
- var item = new UserRoleNavigationEntity()
- {
- UserId = userId,
- NavigationId = navId
- };
- var tp = AddEntity(item);
- return tp;
- }
- public ResultModel GrantUserNavigations(int userId, IList<int> navIds)
- {
- var exisitingIds = GetQueryable()
- .Where(x => x.UserId == userId && userId != 0 && navIds.Contains(x.NavigationId))
- .Select(x => x.NavigationId).ToList();
- var nonExisitingIds = navIds.Where(x => !exisitingIds.Contains(x)).ToList();
- var roleNavs = new List<UserRoleNavigationEntity>();
- foreach (var item in nonExisitingIds)
- {
- var entity = new UserRoleNavigationEntity()
- {
- NavigationId = item,
- UserId = userId
- };
- roleNavs.Add(entity);
- }
- var res = AddEntities(roleNavs);
- return res;
- }
- public ResultModel RevokeRole(int roleId, int navId)
- {
- if (roleId == 0) return new ResultModel("请指定角色Id");
- var res = RemoveEntities(x => x.RoleId == roleId && navId == x.NavigationId);
- return res;
- }
- public ResultModel RevokeRoleNavigations(int roleId, IList<int> navIds)
- {
- if (roleId == 0)
- return new ResultModel() { Success = false, Message = "请提供正确角色Id" };
- var res = RemoveEntities(x => x.RoleId == roleId && navIds.Contains(x.NavigationId));
- return res;
- }
- public ResultModel RevokeUser(int userId, int navId)
- {
- if (userId == 0) return new ResultModel("请指定用户Id");
- var res = RemoveEntities(x => x.UserId == userId && navId == x.NavigationId);
- return res;
- }
- public ResultModel RevokeUserNavigations(int userId, IList<int> navIds)
- {
- if (userId == 0)
- return new ResultModel() { Success = false, Message = "请提供正确用户Id" };
- var res = RemoveEntities(x => x.UserId == userId && navIds.Contains(x.NavigationId));
- return res;
- }
- public IList<NavigationModel> SetTree(IList<NavigationModel> list, string parentMark)
- {
- var result = new List<NavigationModel>();
- foreach (var item in list)
- {
- if (item.Mark!="0"&&item.ParentMark == parentMark)
- {
- result.Add(item);
- item.Children = SetTree(list, item.Mark);
- }
- }
- result = result.OrderByDescending(x => x.SortOrder).ToList();
- return result;
- }
- }
- }
|