Esempio n. 1
0
        public async Task <IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _roleRepository.SoftDeleteAsync(id);

            await _rolePermissionRepository.DeleteAsync(a => a.RoleId == id);

            return(ResponseOutput.Result(result));
        }
        public async Task <IResponseOutput> AssignAsync(PermissionAssignInput input)
        {
            //查询角色权限
            var permissionIds = await _rolePermissionRepository.Select.Where(d => d.RoleId == input.RoleId).ToListAsync(m => m.PermissionId);

            //批量删除权限
            var deleteIds = permissionIds.Where(d => !input.PermissionIds.Contains(d.ToInt()));

            if (deleteIds.Count() > 0)
            {
                await _rolePermissionRepository.DeleteAsync(m => m.RoleId == input.RoleId && deleteIds.Contains(m.PermissionId));
            }

            //批量插入权限
            var insertRolePermissions = new List <RolePermissionEntity>();
            var insertPermissionIds   = input.PermissionIds.Where(d => !permissionIds.Contains(d));

            if (insertPermissionIds.Count() > 0)
            {
                foreach (var permissionId in insertPermissionIds)
                {
                    insertRolePermissions.Add(new RolePermissionEntity()
                    {
                        RoleId       = input.RoleId,
                        PermissionId = permissionId,
                    });
                }
                await _rolePermissionRepository.InsertAsync(insertRolePermissions);
            }

            //清除权限
            await _cache.DelByPatternAsync(CacheKey.UserPermissions);

            return(ResponseOutput.Ok());
        }
Esempio n. 3
0
        public async Task <IResultModel> BindMenuPermission(RoleMenuPermissionBindModel model)
        {
            if (!await _roleRepository.ExistsAsync(m => m.Id == model.RoleId))
            {
                return(ResultModel.Failed("½ÇÉ«²»´æÔÚ"));
            }

            var menus       = new List <RoleMenuEntity>();
            var permissions = new List <RolePermissionEntity>();

            if (model.Menus != null && model.Menus.Any())
            {
                menus = model.Menus.Select(m => new RoleMenuEntity {
                    RoleId = model.RoleId, MenuId = m.MenuId, MenuChecked = m.MenuChecked
                }).ToList();
            }

            foreach (var menu in model.Menus)
            {
                foreach (var button in menu.Buttons)
                {
                    if (button.Checked)
                    {
                        var per = _mapper.Map <RolePermissionEntity>(button);
                        per.RoleId = model.RoleId;
                        permissions.Add(per);
                    }
                }
            }

            try
            {
                _dbContext.Db.BeginTran();

                await _roleMenuRepository.DeleteAsync(m => m.RoleId == model.RoleId, _dbContext.Db);

                if (menus.Any())
                {
                    await _roleMenuRepository.InsertRangeAsync(menus, _dbContext.Db);
                }

                await _rolePermissionRepository.DeleteAsync(m => m.RoleId == model.RoleId, _dbContext.Db);

                if (permissions.Any())
                {
                    await _rolePermissionRepository.InsertRangeAsync(permissions, _dbContext.Db);
                }

                _dbContext.Db.CommitTran();
            }
            catch (Exception ex)
            {
                _dbContext.Db.RollbackTran();
                return(ResultModel.Failed(ex.Message));
            }

            return(ResultModel.Success());
        }
Esempio n. 4
0
        public async Task <IResponseOutput> AssignAsync(PermissionAssignInput input)
        {
            //分配权限的时候判断角色是否存在
            var exists = await _roleRepository.Select.DisableGlobalFilter("Tenant").WhereDynamic(input.RoleId).AnyAsync();

            if (!exists)
            {
                return(ResponseOutput.NotOk("该角色不存在或已被删除!"));
            }

            //查询角色权限
            var permissionIds = await _rolePermissionRepository.Select.Where(d => d.RoleId == input.RoleId).ToListAsync(m => m.PermissionId);

            //批量删除权限
            var deleteIds = permissionIds.Where(d => !input.PermissionIds.Contains(d));

            if (deleteIds.Count() > 0)
            {
                await _rolePermissionRepository.DeleteAsync(m => m.RoleId == input.RoleId && deleteIds.Contains(m.PermissionId));
            }

            //批量插入权限
            var insertRolePermissions = new List <RolePermissionEntity>();
            var insertPermissionIds   = input.PermissionIds.Where(d => !permissionIds.Contains(d));

            if (insertPermissionIds.Count() > 0)
            {
                foreach (var permissionId in insertPermissionIds)
                {
                    insertRolePermissions.Add(new RolePermissionEntity()
                    {
                        RoleId       = input.RoleId,
                        PermissionId = permissionId,
                    });
                }
                await _rolePermissionRepository.InsertAsync(insertRolePermissions);
            }

            //清除权限
            await Cache.DelByPatternAsync(CacheKey.UserPermissions);

            return(ResponseOutput.Ok());
        }
Esempio n. 5
0
 /// <summary>
 /// 删除角色权限
 /// </summary>
 /// <param name="Id"></param>
 /// <returns></returns>
 public async Task DeleteRolePermission(int Id)
 {
     await rolePermissionRepository.DeleteAsync(Id);
 }