Exemple #1
0
        public async Task <OperationResult> UpdateAsync(
            ulong userId,
            UserUpdateModel updateModel,
            CancellationToken cancellationToken)
        {
            using var logScope = OperationLogMessages.BeginOperationScope(_logger, this);
            OperationLogMessages.OperationPerforming(_logger);

            OperationLogMessages.OperationAuthorizing(_logger);
            var authResult = await _authorizationService.RequirePermissionsAsync(
                new[] { (int)AdministrationPermission.ManageRoles },
                cancellationToken);

            if (authResult.IsFailure)
            {
                OperationLogMessages.OperationNotAuthorized(_logger);
                return(authResult.Error);
            }
            OperationLogMessages.OperationAuthorized(_logger);

            var performedById = _authenticationService.CurrentTicket !.UserId;

            var result = await _usersService.UpdateAsync(userId, updateModel, performedById, cancellationToken);

            OperationLogMessages.OperationPerformed(_logger, result);

            return(result);
        }
 public static void UserUpdating(
     ILogger logger,
     ulong userId,
     UserUpdateModel updateModel,
     ulong performedById)
 => _userUpdating.Invoke(
     logger,
     userId,
     updateModel,
     performedById);
        public async Task <OperationResult> UpdateAsync(
            ulong userId,
            UserUpdateModel updateModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            UsersLogMessages.UserUpdating(_logger, userId, updateModel, performedById);

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

            var permissionIds = Enumerable.Union(
                updateModel.GrantedPermissionIds,
                updateModel.DeniedPermissionIds)
                                .ToArray();

            UsersLogMessages.PermissionIdsValidating(_logger, permissionIds);
            var permissionIdsValidationResult = await _permissionsService.ValidateIdsAsync(
                permissionIds,
                cancellationToken);

            if (permissionIdsValidationResult.IsFailure)
            {
                UsersLogMessages.PermissionIdsValidationFailed(_logger, permissionIdsValidationResult);
                return(permissionIdsValidationResult);
            }
            UsersLogMessages.PermissionIdsValidationSucceeded(_logger);

            UsersLogMessages.RoleIdsValidating(_logger, updateModel.AssignedRoleIds);
            var assignedRoleIdsValidationResult = await _rolesService.ValidateIdsAsync(updateModel.AssignedRoleIds, cancellationToken);

            if (assignedRoleIdsValidationResult.IsFailure)
            {
                UsersLogMessages.RoleIdsValidationFailed(_logger, assignedRoleIdsValidationResult);
                return(assignedRoleIdsValidationResult);
            }
            UsersLogMessages.RoleIdsValidationSucceeded(_logger);

            var now = _systemClock.UtcNow;

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)UserManagementAdministrationActionType.UserModified,
                now,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var anyChanges = false;

            UsersLogMessages.UserPermissionMappingIdentitiesFetching(_logger, userId);
            var permissionMappings = await _usersRepository.AsyncEnumeratePermissionMappingIdentities(
                userId : userId,
                isDeleted : false)
                                     .ToArrayAsync(cancellationToken);

            UsersLogMessages.UserPermissionMappingIdentitiesFetched(_logger, userId);

            anyChanges |= await HandleRemovedPermissionMappings(
                userId,
                permissionMappings,
                updateModel.GrantedPermissionIds,
                updateModel.DeniedPermissionIds,
                actionId,
                cancellationToken);

            anyChanges |= await HandleAddedGrantedPermissions(
                permissionMappings,
                updateModel.GrantedPermissionIds,
                userId,
                actionId,
                cancellationToken);

            anyChanges |= await HandleAddedDeniedPermissions(
                permissionMappings,
                updateModel.DeniedPermissionIds,
                userId,
                actionId,
                cancellationToken);

            UsersLogMessages.UserRoleMappingIdentitiesFetching(_logger, userId);
            var roleMappings = await _usersRepository.AsyncEnumerateRoleMappingIdentities(
                userId : userId,
                isDeleted : false)
                               .ToArrayAsync(cancellationToken);

            UsersLogMessages.UserRoleMappingIdentitiesFetched(_logger, userId);

            anyChanges |= await HandleRemovedRoleMappings(
                userId,
                roleMappings,
                updateModel.AssignedRoleIds,
                actionId,
                cancellationToken);

            anyChanges |= await HandleAddedRoles(
                roleMappings,
                updateModel.AssignedRoleIds,
                userId,
                actionId,
                cancellationToken);

            if (!anyChanges)
            {
                UsersLogMessages.UserUpdateNoChangesGiven(_logger, userId);
                return(new NoChangesGivenError($"User ID {userId}"));
            }

            UsersLogMessages.UserUpdatingNotificationPublishing(_logger, userId);
            await _messenger.PublishNotificationAsync(
                new UserUpdatingNotification(
                    userId,
                    actionId),
                cancellationToken);

            UsersLogMessages.UserUpdatingNotificationPublished(_logger, userId);

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

            UsersLogMessages.UserUpdated(_logger, userId);
            return(OperationResult.Success);
        }