public async Task ExecuteAsync(RegisterDefinedRolesCommand command, IExecutionContext executionContext) { DetectDuplicateRoles(); var existingRoles = await _dbContext .Roles .Include(r => r.RolePermissions) .ThenInclude(p => p.Permission) .ToListAsync(); var rolesWithSpecialistCodes = existingRoles .Where(r => !string.IsNullOrEmpty(r.RoleCode)) .ToDictionary(r => r.RoleCode.ToUpperInvariant()); var requiresUpdate = command.UpdateExistingRoles || _roleDefinitions.Any(d => !rolesWithSpecialistCodes.ContainsKey(d.RoleCode.ToUpperInvariant())); if (!requiresUpdate) { return; } var allDbPermissions = await _dbContext .Permissions .ToListAsync(); await EnsureUserAreaExistsAndValidatePermission(existingRoles, executionContext); var allPermissions = _permissionRepository.GetAll(); foreach (var roleDefinition in _roleDefinitions) { var dbRole = rolesWithSpecialistCodes.GetOrDefault(roleDefinition.RoleCode.ToUpperInvariant()); if (dbRole == null) { ValidateRole(existingRoles, roleDefinition); dbRole = MapAndAddRole(roleDefinition); await UpdatePermissions(dbRole, roleDefinition, command, allPermissions, allDbPermissions); } else if (command.UpdateExistingRoles) { await UpdatePermissions(dbRole, roleDefinition, command, allPermissions, allDbPermissions); } } await _dbContext.SaveChangesAsync(); _roleCache.Clear(); }
public async Task ExecuteAsync(UpdateRoleCommand command, IExecutionContext executionContext) { ValidatePermissions(command); var role = await QueryRole(command).SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(role, command.RoleId); var isUnique = await _queryExecutor.ExecuteAsync(GetUniqueQuery(command, role)); ValidateIsUnique(isUnique); MapRole(command, role); await MergePermissions(command, role); await _dbContext.SaveChangesAsync(); _roleCache.Clear(role.RoleId); }
public async Task ExecuteAsync(DeleteRoleCommand command, IExecutionContext executionContext) { var role = await _dbContext .Roles .FilterById(command.RoleId) .SingleOrDefaultAsync(); if (role != null) { ValidateCanDelete(role, command); _dbContext.Roles.Remove(role); await _dbContext.SaveChangesAsync(); _roleCache.Clear(role.RoleId); } }
public async Task ExecuteAsync(DeleteRoleCommand command, IExecutionContext executionContext) { var role = await _dbContext .Roles .FilterById(command.RoleId) .SingleOrDefaultAsync(); if (role != null) { ValidateCanDelete(role, command); _dbContext.Roles.Remove(role); await _dbContext.SaveChangesAsync(); _transactionScopeFactory.QueueCompletionTask(_dbContext, () => _roleCache.Clear(command.RoleId)); } }