public async Task <IActionResult> UpdatePermission(int id, [FromBody] PermissionUpdateDto permission)
        {
            try
            {
                if (permission == null)
                {
                    _logger.LogError("Permission object sent from client is null.");
                    return(BadRequest("Permission object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid permission object sent from client.");
                    return(BadRequest("Invalid model object"));
                }
                var permissionEntity = await _repository.Permission.GetByIdAsync(id);

                if (permissionEntity == null)
                {
                    _logger.LogError($"Permission with id: {id}, hasn't been found in db.");
                    return(NotFound());
                }
                _mapper.Map(permission, permissionEntity);
                _repository.Permission.Update(permissionEntity);
                await _repository.SaveAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdatePermission action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Example #2
0
        public static Permission ToPermission(this PermissionUpdateDto model)
        {
            var permission = new Permission
            {
                Name = model.Name,
                Id   = model.Id
            };

            return(permission);
        }
Example #3
0
        public async Task <IActionResult> UpdatePermission(
            PermissionUpdateDto model
            )
        {
            try
            {
                var responsePayload = await _permissionsService.UpdatePermissionAsync(model);

                return(Ok(responsePayload));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { ex.Message }));
            }
        }
Example #4
0
        public async Task <ActionResult> UpdatePermissions(string id, PermissionUpdateDto model)
        {
            // 除了平台的超级管理员,其他管理员只能管理所属 Client 的资源
            bool isSuper = User.IsSuperAdmin();
            IEnumerable <string> allowedClientIds = null;

            if (!isSuper)
            {
                allowedClientIds = User.FindAll(JwtClaimTypes.ClientId).Select(itm => itm.Value);
            }

            await _permissionService.UpdateAsync(id, model, allowedClientIds);

            return(Ok());
        }
        public async Task <PermissionReturnDto> UpdatePermissionAsync(PermissionUpdateDto model)
        {
            var permissionOld =
                await _permissionService.GetByIdAsync(model.Id);

            if (permissionOld == null)
            {
                throw new ObjectNotFoundException("Permission not found.");
            }

            if ((await _permissionService.GetAllAsync(d => d.Name == model.Name && d.Id != model.Id)).Any())
            {
                throw new ObjectAlreadyExistException("Permission with such name already added.");
            }

            var permission =
                await _permissionService.UpdateAsync(model.ToPermission(permissionOld));

            return(permission.ToPermissionReturnDto());
        }
Example #6
0
        public async Task UpdateAsync(string id, PermissionUpdateDto model, IEnumerable <string> allowedClientIds = null)
        {
            var perm = await _permissionRepo.GetAsync(id, isReadonly : false);

            if (perm == null)
            {
                throw new IamException(HttpStatusCode.NotFound, "权限不存在");
            }

            if (allowedClientIds != null && allowedClientIds.Contains(perm.ClientId))
            {
                throw new IamException(HttpStatusCode.BadRequest, "无权操作");
            }

            if (model.Type == PermissionType.View)
            {
                if (String.IsNullOrWhiteSpace(model.Url))
                {
                    throw new IamException(HttpStatusCode.BadRequest, "Url 不能为空");
                }
            }

            if (!String.IsNullOrWhiteSpace(model.ParentId))
            {
                var parent = await _permissionRepo.GetAsync(model.ParentId);

                if (parent == null)
                {
                    throw new IamException(HttpStatusCode.BadRequest, "父级不存在!");
                }

                if (!String.IsNullOrWhiteSpace(parent.ClientId) && parent.ClientId != perm.ClientId)
                {
                    throw new IamException(HttpStatusCode.BadRequest, $"父级并不属于客户端({perm.ClientId})!");
                }
            }
            perm.Update(model.Name, model.Desc, model.Type, model.ParentId, model.Url, model.Icon, model.Order, model.Level);
        }
Example #7
0
 public static Permission ToPermission(this PermissionUpdateDto model, Permission permissionOld)
 {
     permissionOld.Name = model.Name;
     return(permissionOld);
 }