public async Task <OperationResultResponse <bool> > ExecuteAsync(UpdateRoleRightsRequest request)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.Forbidden));
            }

            ValidationResult validationResult = await _requestValidator.ValidateAsync(request);

            if (!validationResult.IsValid)
            {
                return(_responseCreator.CreateFailureResponse <bool>(
                           HttpStatusCode.BadRequest,
                           validationResult.Errors.Select(validationFailure => validationFailure.ErrorMessage).ToList()));
            }

            OperationResultResponse <bool> response = new();

            response.Body = await _roleRepository.EditRoleRightsAsync(
                request.RoleId,
                _dbRoleRightMapper.Map(request.RoleId, request.Rights));

            await UpdateCacheAsync(request.Rights, request.RoleId);

            return(response);
        }
        public async Task <OperationResultResponse <bool> > ExecuteAsync(Guid workspaceId, JsonPatchDocument <EditWorkspaceRequest> request)
        {
            DbWorkspace dbWorkspace = await _repository.GetAsync(workspaceId);

            Guid editorId = _httpContextAccessor.HttpContext.GetUserId();

            if (dbWorkspace.CreatedBy != editorId &&
                (await _userRepository.GetAdminsAsync(workspaceId)).FirstOrDefault(wa => wa.UserId == editorId) == null &&
                !await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.Forbidden));
            }

            if (!_validator.ValidateCustom(request, out List <string> errors))
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.BadRequest, errors));
            }

            OperationResultResponse <bool> response = new();

            response.Body = await _repository.EditAsync(dbWorkspace, await _mapper.MapAsync(request));

            response.Status = OperationResultStatusType.FullSuccess;

            if (!response.Body)
            {
                response = _responseCreator.CreateFailureResponse <bool>(HttpStatusCode.BadRequest);
            }

            return(response);
        }
        public async Task <OperationResultResponse <Guid> > ExecuteAsync(CreateRoleRequest request)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <Guid>(HttpStatusCode.Forbidden));
            }

            ValidationResult validationResult = await _validator.ValidateAsync(request);

            if (!validationResult.IsValid)
            {
                return(_responseCreator.CreateFailureResponse <Guid>(
                           HttpStatusCode.BadRequest,
                           validationResult.Errors.Select(validationFailure => validationFailure.ErrorMessage).ToList()));
            }

            OperationResultResponse <Guid> response = new();

            response.Body = await _roleRepository.CreateAsync(_mapper.Map(request));

            _httpContextAccessor.HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

            await UpdateCacheAsync(request.Rights, response.Body);

            return(response);
        }
Esempio n. 4
0
        public async Task <OperationResultResponse <bool> > ExecuteAsync(Guid userId, IEnumerable <int> rightsIds)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.Forbidden));
            }

            ValidationResult validationResult = await _validator.ValidateAsync(rightsIds);

            if (!validationResult.IsValid)
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.BadRequest, validationResult.Errors.Select(vf => vf.ErrorMessage).ToList()));
            }

            bool result = await _repository.RemoveUserRightsAsync(userId, rightsIds);

            if (result)
            {
                await UpdateCacheAsync(userId, rightsIds);
            }

            return(new()
            {
                Status = result ? OperationResultStatusType.FullSuccess : OperationResultStatusType.Failed,
                Body = result
            });
        }
        public async Task <OperationResultResponse <bool> > ExecuteAsync(Guid roleLocalizationId, JsonPatchDocument <EditRoleLocalizationRequest> request)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.Forbidden));
            }

            if (!_validator.ValidateCustom(request, out List <string> errors))
            {
                return(_responseCreator.CreateFailureResponse <bool>(
                           HttpStatusCode.BadRequest,
                           errors));
            }

            Operation <EditRoleLocalizationRequest> isActiveOperation = request.Operations.FirstOrDefault(
                o => o.path.EndsWith(nameof(EditRoleLocalizationRequest.IsActive), StringComparison.OrdinalIgnoreCase));
            Operation <EditRoleLocalizationRequest> nameOperation = request.Operations.FirstOrDefault(
                o => o.path.EndsWith(nameof(EditRoleLocalizationRequest.Name), StringComparison.OrdinalIgnoreCase));

            DbRoleLocalization roleLocalization = await _roleLocalizationRepository.GetAsync(roleLocalizationId);

            if (isActiveOperation != default)
            {
                bool isActive = bool.Parse(isActiveOperation.value.ToString().Trim());

                if (roleLocalization.IsActive == isActive)
                {
                    return(_responseCreator.CreateFailureResponse <bool>(
                               HttpStatusCode.BadRequest,
                               new List <string> {
                        "Role localization already has this status."
                    }));
                }

                if (isActive &&
                    await _roleLocalizationRepository.DoesLocaleExistAsync(roleLocalization.RoleId, roleLocalization.Locale))
                {
                    return(_responseCreator.CreateFailureResponse <bool>(
                               HttpStatusCode.BadRequest,
                               new List <string> {
                        "Role must have only one localization per locale."
                    }));
                }
            }

            if (nameOperation != default &&
                await _roleLocalizationRepository.DoesNameExistAsync(roleLocalization.Locale, nameOperation.value.ToString().Trim()))
            {
                return(_responseCreator.CreateFailureResponse <bool>(
                           HttpStatusCode.BadRequest,
                           new List <string> {
                    "Name already exists."
                }));
            }

            return(new OperationResultResponse <bool>()
            {
                Body = await _roleLocalizationRepository.EditRoleLocalizationAsync(roleLocalizationId, _roleLocalizationMapper.Map(request))
            });
        }
Esempio n. 6
0
        public async Task <OperationResultResponse <bool> > ExecuteAsync(Guid userId, IEnumerable <int> rightsIds)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.Forbidden));
            }

            ValidationResult validationResult = await _validator.ValidateAsync(rightsIds);

            List <string> errors = validationResult.Errors.Select(vf => vf.ErrorMessage).ToList();

            if (!validationResult.IsValid && !await CheckUserExistenceAsync(userId, errors))
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.BadRequest, errors));
            }

            await _repository.AddUserRightsAsync(userId, rightsIds);

            _httpContextAccessor.HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

            await UpdateCacheAsync(userId, rightsIds);

            return(new OperationResultResponse <bool>
            {
                Body = true,
                Status = OperationResultStatusType.FullSuccess
            });
        }
        public async Task <OperationResultResponse <List <RightInfo> > > ExecuteAsync(string locale)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <List <RightInfo> >(HttpStatusCode.Forbidden));
            }

            return(new()
            {
                Status = Kernel.Enums.OperationResultStatusType.FullSuccess,
                Body = (await _repository.GetRightsListAsync(locale))?.Select(right => _mapper.Map(right)).ToList()
            });
        }
        public async Task <OperationResultResponse <Guid?> > ExecuteAsync(CreateRoleLocalizationRequest request)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <Guid?>(HttpStatusCode.Forbidden));
            }

            if (!request.RoleId.HasValue)
            {
                return(_responseCreator.CreateFailureResponse <Guid?>(
                           HttpStatusCode.BadRequest,
                           new List <string> {
                    "RoleId can't be empty."
                }));
            }

            ValidationResult validationResult = await _requestValidator.ValidateAsync(request);

            if (!validationResult.IsValid)
            {
                return(_responseCreator.CreateFailureResponse <Guid?>(
                           HttpStatusCode.BadRequest,
                           validationResult.Errors.Select(validationFailure => validationFailure.ErrorMessage).ToList()));
            }

            OperationResultResponse <Guid?> response = new();

            response.Body = await _roleLocalizationRepository.CreateAsync(_roleLocalizationMapper.Map(request));

            response.Status = response.Body != null
        ? OperationResultStatusType.FullSuccess
        : OperationResultStatusType.Failed;

            _httpContextAccessor.HttpContext.Response.StatusCode = response.Body != null
        ? (int)HttpStatusCode.Created
        : (int)HttpStatusCode.BadRequest;

            return(response);
        }
        public async Task <OperationResultResponse <bool> > ExecuteAsync(Guid roleId, bool isActive)
        {
            if (!await _accessValidator.IsAdminAsync())
            {
                return(_responseCreator.CreateFailureResponse <bool>(HttpStatusCode.Forbidden));
            }

            DbRole role = await _roleRepository.GetAsync(roleId);

            if (role == null)
            {
                return(_responseCreator.CreateFailureResponse <bool>(
                           HttpStatusCode.NotFound,
                           new List <string> {
                    "Role doesn't exist."
                }));
            }

            (bool isSuccess, string error)check = await CheckEnablePossibilityAsync(role, isActive);

            if (!check.isSuccess)
            {
                return(_responseCreator.CreateFailureResponse <bool>(
                           HttpStatusCode.Conflict,
                           new List <string> {
                    check.error
                }));
            }

            OperationResultResponse <bool> response = new();

            response.Body = await _roleRepository.EditStatusAsync(roleId, isActive);

            await UpdateCacheAsync(roleId, isActive);

            return(response);
        }