| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Api.Repositories; |
| | | 3 | | using Chronicis.Shared.Extensions; |
| | | 4 | | using Chronicis.Shared.Models; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Api.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Service implementation for managing resource providers with authorization. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ResourceProviderService : IResourceProviderService |
| | | 13 | | { |
| | | 14 | | private readonly IResourceProviderRepository _repository; |
| | | 15 | | private readonly ChronicisDbContext _context; |
| | | 16 | | private readonly ILogger<ResourceProviderService> _logger; |
| | | 17 | | |
| | 0 | 18 | | public ResourceProviderService( |
| | 0 | 19 | | IResourceProviderRepository repository, |
| | 0 | 20 | | ChronicisDbContext context, |
| | 0 | 21 | | ILogger<ResourceProviderService> logger) |
| | | 22 | | { |
| | 0 | 23 | | _repository = repository; |
| | 0 | 24 | | _context = context; |
| | 0 | 25 | | _logger = logger; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | public async Task<List<ResourceProvider>> GetAllProvidersAsync() |
| | | 30 | | { |
| | 0 | 31 | | return await _repository.GetAllProvidersAsync(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public async Task<List<(ResourceProvider Provider, bool IsEnabled)>> GetWorldProvidersAsync(Guid worldId, Guid userI |
| | | 36 | | { |
| | | 37 | | // Verify world exists and user has access |
| | 0 | 38 | | var world = await _context.Worlds |
| | 0 | 39 | | .AsNoTracking() |
| | 0 | 40 | | .FirstOrDefaultAsync(w => w.Id == worldId); |
| | | 41 | | |
| | 0 | 42 | | if (world == null) |
| | | 43 | | { |
| | 0 | 44 | | _logger.LogWarning("World {WorldId} not found", worldId); |
| | 0 | 45 | | throw new KeyNotFoundException($"World {worldId} not found"); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // Check if user is owner or member |
| | 0 | 49 | | var isOwner = world.OwnerId == userId; |
| | 0 | 50 | | var isMember = await _context.WorldMembers |
| | 0 | 51 | | .AnyAsync(wm => wm.WorldId == worldId && wm.UserId == userId); |
| | | 52 | | |
| | 0 | 53 | | if (!isOwner && !isMember) |
| | | 54 | | { |
| | 0 | 55 | | _logger.LogWarning("User {UserId} unauthorized to access world {WorldId}", userId, worldId); |
| | 0 | 56 | | throw new UnauthorizedAccessException($"User does not have access to world {worldId}"); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return await _repository.GetWorldProvidersAsync(worldId); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc/> |
| | | 63 | | public async Task<bool> SetProviderEnabledAsync(Guid worldId, string providerCode, bool enabled, Guid userId) |
| | | 64 | | { |
| | | 65 | | // Verify world exists |
| | 0 | 66 | | var world = await _context.Worlds |
| | 0 | 67 | | .AsNoTracking() |
| | 0 | 68 | | .FirstOrDefaultAsync(w => w.Id == worldId); |
| | | 69 | | |
| | 0 | 70 | | if (world == null) |
| | | 71 | | { |
| | 0 | 72 | | _logger.LogWarning("World {WorldId} not found", worldId); |
| | 0 | 73 | | throw new KeyNotFoundException($"World {worldId} not found"); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // Check if user is the owner (only owners can modify settings) |
| | 0 | 77 | | if (world.OwnerId != userId) |
| | | 78 | | { |
| | 0 | 79 | | _logger.LogWarning("User {UserId} is not owner of world {WorldId}", userId, worldId); |
| | 0 | 80 | | throw new UnauthorizedAccessException($"Only the world owner can modify resource provider settings"); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Attempt to enable/disable the provider |
| | 0 | 84 | | var result = await _repository.SetProviderEnabledAsync(worldId, providerCode, enabled, userId); |
| | | 85 | | |
| | 0 | 86 | | if (!result) |
| | | 87 | | { |
| | 0 | 88 | | _logger.LogWarning("Provider {ProviderCode} not found or inactive", providerCode); |
| | 0 | 89 | | throw new KeyNotFoundException($"Resource provider '{providerCode}' not found or inactive"); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | _logger.LogDebugSanitized( |
| | 0 | 93 | | "User {UserId} {Action} provider {ProviderCode} for world {WorldId}", |
| | 0 | 94 | | userId, |
| | 0 | 95 | | enabled ? "enabled" : "disabled", |
| | 0 | 96 | | providerCode, |
| | 0 | 97 | | worldId); |
| | | 98 | | |
| | 0 | 99 | | return true; |
| | 0 | 100 | | } |
| | | 101 | | } |