public bool HasPermission(long userId, string permissionName) { using (var db = new WeixinDbContext()) { CommonService <User> commonService = new CommonService <User>(db); var user = commonService.GetAll().Include(u => u.Roles). AsNoTracking().SingleOrDefault(u => u.Id == userId); if (user == null) { throw new ArgumentException("找不到id=" + userId + "的用户"); } /*每个Role都有一个Permissions属性 * Roles.SelectMany(r => r.Permissions)就是遍历Roles的每一个Role * 然后把每个Role的Permissions放到一个集合中*/ var permissions = user.Roles.SelectMany(r => r.Permissions); //有任何一个权限匹配,就返回true return(permissions.Any(p => p.PermissionName == permissionName)); } }
public async Task AddPermissonIds(long roleId, long[] permissionIds) { using (var db = new WeixinDbContext()) { CommonService <Role> roleCs = new CommonService <Role>(db); var role = await roleCs.GetById(roleId); if (role == null) { throw new ArgumentException("该角色不存在"); } CommonService <Permission> permissionCs = new CommonService <Permission>(db); var permissions = await permissionCs.GetAll(). Where(p => permissionIds.Contains(p.Id)).ToArrayAsync(); foreach (var item in permissions) { role.Permissions.Add(item); } await db.SaveChangesAsync(); } }
public async Task <long> AddNew(string permissionName, string description) { using (var db = new WeixinDbContext()) { var entity = new Permission() { PermissionName = permissionName, Description = description }; CommonService <Permission> commonService = new CommonService <Permission>(db); var exists = await commonService.GetAll().AnyAsync(p => p.PermissionName == permissionName); if (exists) { throw new ArgumentException("改权限项已经存在"); } db.Permission.Add(entity); await db.SaveChangesAsync(); return(entity.Id); } }
public async Task AddRoleIds(long userId, long[] roleIds) { using (var db = new WeixinDbContext()) { CommonService <User> userCs = new CommonService <User>(db); var user = await userCs.GetById(userId); if (user == null) { throw new ArgumentException("用户不存在" + userId); } CommonService <Role> roleCs = new CommonService <Role>(db); //寻找数据库里的roleId和传入的roleId的交集 var roles = roleCs.GetAll().Where(r => roleIds.Contains(r.Id)).ToArray(); foreach (var role in roles) { user.Roles.Add(role); } await db.SaveChangesAsync(); } }
public async Task <long> AddNew(string appId, string className, string handleName) { using (var db = new WeixinDbContext()) { CommonService <HandlerConfig> service = new CommonService <HandlerConfig>(db); var handelConfig = new HandlerConfig() { AppId = appId, ClassName = className, HandlerName = handleName }; var exists = await service.GetAll().AnyAsync(s => s.AppId == appId); if (exists) { throw new ArgumentException("该公众号appid已经存在"); } db.HandlerConfig.Add(handelConfig); await db.SaveChangesAsync(); return(handelConfig.Id); } }