Exemplo n.º 1
0
        public async Task <Result> Assign([FromBody] AssignIdArrayRequestDto <long> dto)
        {
            var  server    = _serviceProvider.GetRequiredService <DepartmentService>();
            long?tenancyId = HttpContext.User.IsSuperRole() ? null : Convert.ToInt64(HttpContext.User.TenancyId());
            var  result    = await server.Get(dto.Id, tenancyId);

            if (!result.Success)
            {
                return(Result.ReFailure(result.Message, result.Status));
            }
            // 除总超级管理员之外其他不允许操作其他租户信息
            if (tenancyId != null && result.Data.TenancyId != tenancyId)
            {
                return(Result.ReFailure(ResultCodes.TenancyMatchExist));
            }
            if (dto.Ids == null || dto.Ids.Count == 0)
            {
                return(Result.ReFailure(ResultCodes.RoleIdNotExist));
            }
            result.Data.DepartmentRole.Clear();
            foreach (var item in dto.Ids)
            {
                result.Data.DepartmentRole.Add(new DepartmentRole()
                {
                    RoleId       = item,
                    DepartmentId = dto.Id
                });
            }
            return(await server.Modify(result.Data));
        }
Exemplo n.º 2
0
        public async Task <Result> AssignPermission([FromBody] AssignIdArrayRequestDto <long> dto)
        {
            var  server    = _serviceProvider.GetRequiredService <RoleService>();
            long?tenancyId = HttpContext.User.IsSuperRole() ? null : Convert.ToInt64(HttpContext.User.TenancyId());
            var  result    = await server.Get(dto.Id, tenancyId);

            if (!result.Success)
            {
                return(Result.ReFailure(result.Message, result.Status));
            }
            // 除总超级管理员之外其他不允许操作其他租户信息
            if (tenancyId != null && result.Data.TenancyId != tenancyId)
            {
                return(Result.ReFailure(ResultCodes.TenancyMatchExist));
            }
            if (dto.Ids == null || dto.Ids.Count == 0)
            {
                return(Result.ReFailure(ResultCodes.PermissionIdNotExist));
            }
            result.Data.CreateId = Convert.ToInt64(HttpContext.User.UserId());

            result.Data.PermissionRole.Clear();
            foreach (long value in dto.Ids.ToList())
            {
                if (result.Data.PermissionRole.Count(f => f.PermissionId == value) == 0)
                {
                    result.Data.PermissionRole.Add(new PermissionRole()
                    {
                        PermissionId = value,
                        RoleId       = dto.Id
                    });
                }
            }
            return(await server.Modify(result.Data));
        }
Exemplo n.º 3
0
        public async Task <Result> AssignRole([FromBody] AssignIdArrayRequestDto <long> dto)
        {
            var  server    = _serviceProvider.GetRequiredService <AccountService>();
            long?tenancyId = HttpContext.User.IsSuperRole() ? null : Convert.ToInt64(HttpContext.User.TenancyId());
            var  result    = await server.GetAccountByRole(dto.Id, tenancyId);

            if (!result.Success)
            {
                return(Result.ReFailure(result.Message, result.Status));
            }
            // 除总超级管理员之外其他不允许操作其他租户信息
            if (tenancyId != null && result.Data.TenancyId != tenancyId)
            {
                return(Result.ReFailure(ResultCodes.TenancyMatchExist));
            }
            if (dto.Ids == null || dto.Ids.Count == 0)
            {
                return(Result.ReFailure(ResultCodes.RoleIdNotExist));
            }

            result.Data.CreateId = Convert.ToInt64(HttpContext.User.UserId());
            // 判断是否存在想用的角色。如果存在就忽略分配相同的
            result.Data.AccountRole.Clear();
            foreach (var item in dto.Ids)
            {
                // 判断部门不为空。并且部门角色不包含相同角色添加。 否则直接添加
                if (result.Data.Department != null && result.Data.Department.Role.Count > 0)
                {
                    if (result.Data.Department.Role.Any(f => f.Id != item))
                    {
                        result.Data.AccountRole.Add(new AccountRole()
                        {
                            RoleId    = item,
                            AccountId = result.Data.Id
                        });
                    }
                }
                else
                {
                    result.Data.AccountRole.Add(new AccountRole()
                    {
                        RoleId    = item,
                        AccountId = result.Data.Id
                    });
                }
            }
            return(await server.Modify(result.Data));
        }