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());
        }
Exemple #2
0
        public async Task <OperationResult> UpdateAsync(
            long guildId,
            long divisionId,
            CharacterGuildDivisionUpdateModel updateModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildDivisionUpdating(_logger, guildId, divisionId, updateModel, performedById);

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

            var guildIdValidationResult = await ValidateGuildIdAsync(guildId, cancellationToken);

            if (guildIdValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildIdValidationFailed(_logger, guildId, guildIdValidationResult);
                return(guildIdValidationResult);
            }
            CharactersLogMessages.CharacterGuildIdValidationSucceeded(_logger, guildId);

            var nameValidationResult = await ValidateDivisionNameAsync(guildId, updateModel.Name, divisionId, cancellationToken);

            if (nameValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildDivisionNameValidationFailed(_logger, updateModel.Name, nameValidationResult);
                return(nameValidationResult);
            }
            CharactersLogMessages.CharacterGuildDivisionNameValidationSucceeded(_logger, updateModel.Name);

            var now = _systemClock.UtcNow;

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)CharacterManagementAdministrationActionType.DivisionModified,
                now,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var updateResult = await _characterGuildDivisionsRepository.UpdateAsync(
                divisionId : divisionId,
                actionId : actionId,
                name : updateModel.Name,
                cancellationToken : cancellationToken);

            if (updateResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildDivisionUpdateFailed(_logger, guildId, divisionId, updateResult);
                return(updateResult);
            }
            CharactersLogMessages.CharacterGuildDivisionUpdated(_logger, guildId, divisionId);

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

            return(OperationResult.Success);
        }
Exemple #3
0
        public async Task <OperationResult <long> > CreateAsync(
            long guildId,
            CharacterGuildDivisionCreationModel creationModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildDivisionCreating(_logger, guildId, creationModel, performedById);

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

            var guildIdValidationResult = await ValidateGuildIdAsync(guildId, cancellationToken);

            if (guildIdValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildIdValidationFailed(_logger, guildId, guildIdValidationResult);
                return(guildIdValidationResult.Error);
            }
            CharactersLogMessages.CharacterGuildIdValidationSucceeded(_logger, guildId);

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

            if (nameValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildDivisionNameValidationFailed(_logger, creationModel.Name, nameValidationResult);
                return(nameValidationResult.Error);
            }
            CharactersLogMessages.CharacterGuildDivisionNameValidationSucceeded(_logger, creationModel.Name);

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

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var divisionId = await _characterGuildDivisionsRepository.CreateAsync(
                guildId,
                creationModel.Name,
                actionId,
                cancellationToken);

            CharactersLogMessages.CharacterGuildDivisionCreated(_logger, guildId, divisionId);

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

            return(divisionId.ToSuccess());
        }
Exemple #4
0
        public async Task <OperationResult> DeleteAsync(
            long roleId,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            RolesLogMessages.RoleDeleting(_logger, roleId, performedById);

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

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

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var deleteResult = await _rolesRepository.UpdateAsync(
                roleId : roleId,
                actionId : actionId,
                isDeleted : true,
                cancellationToken : cancellationToken);

            if (deleteResult.IsSuccess)
            {
                RolesLogMessages.RoleDeleted(_logger, roleId, deleteResult.Value);

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

                RolesLogMessages.RoleDeletingNotificationPublishing(_logger, roleId);
                await _messenger.PublishNotificationAsync(
                    new RoleDeletingNotification(
                        roleId,
                        actionId),
                    cancellationToken);

                RolesLogMessages.RoleDeletingNotificationPublished(_logger, roleId);

                transactionScope.Complete();
                TransactionsLogMessages.TransactionScopeCommitted(_logger);
            }
            else
            {
                RolesLogMessages.RoleDeleteFailed(_logger, roleId, deleteResult);
            }

            return(deleteResult);
        }
        public async Task <OperationResult> DeleteAsync(
            long guildId,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildDeleting(_logger, guildId, performedById);

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

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

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var updateResult = await _characterGuildsRepository.UpdateAsync(
                guildId : guildId,
                actionId : actionId,
                isDeleted : true,
                cancellationToken : cancellationToken);

            if (updateResult.IsSuccess)
            {
                CharactersLogMessages.CharacterGuildDeleted(_logger, guildId);
                transactionScope.Complete();
                TransactionsLogMessages.TransactionScopeCommitted(_logger);
            }
            else
            {
                CharactersLogMessages.CharacterGuildDeleteFailed(_logger, guildId, updateResult);
            }

            return(updateResult);
        }
Exemple #6
0
        public async Task <OperationResult> UpdateAsync(
            long roleId,
            RoleUpdateModel updateModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            RolesLogMessages.RoleUpdating(_logger, roleId, updateModel, performedById);

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

            var nameValidationResult = await ValidateNameAsync(updateModel.Name, roleId, cancellationToken);

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

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

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

            var now = _systemClock.UtcNow;

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)RoleManagementAdministrationActionType.RoleModified,
                now,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var updateResult = await _rolesRepository.UpdateAsync(
                roleId : roleId,
                actionId : actionId,
                name : updateModel.Name,
                cancellationToken : cancellationToken);

            if (updateResult.IsFailure && !(updateResult.Error is NoChangesGivenError))
            {
                RolesLogMessages.RoleUpdateFailed(_logger, roleId, updateResult);
                return(updateResult);
            }
            RolesLogMessages.RoleUpdated(_logger, roleId, updateResult);

            var anyChanges = updateResult.IsSuccess;

            RolesLogMessages.RolePermissionMappingIdentitiesFetching(_logger, roleId);
            var permissionMappings = await _rolesRepository.AsyncEnumeratePermissionMappingIdentities(
                roleId : roleId,
                isDeleted : false)
                                     .ToArrayAsync(cancellationToken);

            RolesLogMessages.RolePermissionMappingIdentitiesFetched(_logger, roleId);

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

            anyChanges |= await HandleAddedPermissions(
                permissionMappings,
                updateModel.GrantedPermissionIds,
                roleId,
                actionId,
                cancellationToken);

            if (!anyChanges)
            {
                RolesLogMessages.RoleUpdateNoChangesGiven(_logger, roleId);
                return(new NoChangesGivenError($"Role ID {roleId}"));
            }

            RolesLogMessages.RoleUpdatingNotificationPublishing(_logger, roleId);
            await _messenger.PublishNotificationAsync(
                new RoleUpdatingNotification(
                    roleId,
                    actionId),
                cancellationToken);

            RolesLogMessages.RoleUpdatingNotificationPublished(_logger, roleId);

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

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

            return(OperationResult.Success);
        }
        public async Task <OperationResult> UpdateExperienceDiffsAsync(
            IReadOnlyList <int> experienceDiffs,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterLevelDefinitionsUpdating(_logger);

            var totalExperience     = 0;
            var proposedDefinitions = experienceDiffs
                                      .Select((experienceDiff, index) => (
                                                  level: index + 2,
                                                  previousExperienceThreshold: totalExperience,
                                                  experienceThreshold: totalExperience += experienceDiff))
                                      .Prepend((
                                                   level: 0,
                                                   previousExperienceThreshold: -1,
                                                   experienceThreshold: 0))
                                      .ToArray();

            foreach (var(level, previousExperienceThreshold, experienceThreshold) in proposedDefinitions)
            {
                CharactersLogMessages.CharacterLevelDefinitionProposed(_logger, level, experienceThreshold, previousExperienceThreshold);
                if (experienceThreshold <= previousExperienceThreshold)
                {
                    CharactersLogMessages.CharacterLevelDefinitionValidationFailed(_logger, level, experienceThreshold, previousExperienceThreshold);
                    return(new InvalidLevelDefinitionError(
                               level,
                               experienceThreshold,
                               previousExperienceThreshold));
                }
            }

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

            var currentDefinitions = await GetCurrentDefinitionsAsync(cancellationToken);

            CharactersLogMessages.CharacterLevelDefinitionsFetchedCurrent(_logger);

            var sequenceLength   = Math.Max(experienceDiffs.Count + 1, currentDefinitions.Count);
            var pairwiseSequence = Enumerable.Zip(
                currentDefinitions
                .PadEnd <CharacterLevelDefinitionViewModel?>(sequenceLength, null),
                proposedDefinitions
                .Select(x => x.ToNullable())
                .PadEnd(sequenceLength, null),
                (current, proposed) => (current, proposed));

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

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var anyChangesMade = false;

            foreach (var(current, proposed) in pairwiseSequence)
            {
                if (proposed is null)
                {
                    CharactersLogMessages.CharacterLevelDefinitionDeleting(_logger, current !.Level);
                    await _characterLevelsRepository.MergeDefinitionAsync(
                        current !.Level,
                        current !.ExperienceThreshold,
                        true,
                        actionId,
                        cancellationToken);

                    CharactersLogMessages.CharacterLevelDefinitionDeleted(_logger, current !.Level);

                    anyChangesMade = true;
                }
                else if ((current is null) || (current.ExperienceThreshold != proposed.Value.experienceThreshold))
                {
                    CharactersLogMessages.CharacterLevelDefinitionUpdating(_logger, proposed !.Value.level, proposed !.Value.experienceThreshold);
                    await _characterLevelsRepository.MergeDefinitionAsync(
                        proposed !.Value.level,
                        proposed !.Value.experienceThreshold,
                        false,
                        actionId,
                        cancellationToken);

                    CharactersLogMessages.CharacterLevelDefinitionUpdated(_logger, proposed !.Value.level, proposed !.Value.experienceThreshold);

                    anyChangesMade = true;
                }
            }

            if (!anyChangesMade)
            {
                CharactersLogMessages.CharacterLevelDefinitionsNoChangesGiven(_logger);
                return(new NoChangesGivenError("Character Level Definitions"));
            }

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

            _memoryCache.Remove(_getCurrentDefinitionsCacheKey);
            CharactersLogMessages.CharacterLevelDefinitionsCacheCleared(_logger);

            return(OperationResult.Success);
        }