Exemple #1
0
        public async Task <OperationResult <long> > CreateAsync(
            RoleCreationModel creationModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            RolesLogMessages.RoleCreating(_logger, creationModel, performedById);

            using var transactionScope = _transactionScopeFactory.CreateScope();
            TransactionsLogMessages.TransactionScopeCreated(_logger);

            var nameValidationResult = await ValidateNameAsync(creationModel.Name, null, cancellationToken);

            if (nameValidationResult.IsFailure)
            {
                RolesLogMessages.RoleNameValidationFailed(_logger, creationModel.Name, nameValidationResult);
                return(nameValidationResult.Error);
            }
            RolesLogMessages.RoleNameValidationSucceeded(_logger, creationModel.Name);

            var grantedPermissionIdsValidationResult = await _permissionsService.ValidateIdsAsync(creationModel.GrantedPermissionIds, cancellationToken);

            if (grantedPermissionIdsValidationResult.IsFailure)
            {
                RolesLogMessages.PermissionIdsValidationFailed(_logger, creationModel.GrantedPermissionIds, grantedPermissionIdsValidationResult);
                return(grantedPermissionIdsValidationResult.Error);
            }
            RolesLogMessages.PermissionIdsValidationSucceeded(_logger, creationModel.GrantedPermissionIds);

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)RoleManagementAdministrationActionType.RoleCreated,
                _systemClock.UtcNow,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var roleId = await _rolesRepository.CreateAsync(
                creationModel.Name,
                actionId,
                cancellationToken);

            RolesLogMessages.RoleCreated(_logger, roleId);

            var mappingIds = await _rolesRepository.CreatePermissionMappingsAsync(
                roleId,
                creationModel.GrantedPermissionIds,
                actionId,
                cancellationToken);

            RolesLogMessages.RolePermissionMappingsCreated(_logger, roleId, mappingIds);

            _memoryCache.Remove(_getCurrentIdentitiesCacheKey);
            RolesLogMessages.RoleIdentitiesCacheCleared(_logger);

            transactionScope.Complete();
            TransactionsLogMessages.TransactionScopeCommitted(_logger);

            return(roleId.ToSuccess());
        }