
Interface Definition: done Server-Side-Implementation: partly done (CR missing, potential Refactor, ...) Client-Side-Implementation: started: (UI: done, Service: started, but works with SSR) Missing: Permissions / Roles to restrict access to an event. Missing: Fields on Event. Missing: Good Styling Time-Took: about 12 Hours. Learning: Commit in smaller packets, rest will be discussed at CR
218 lines
9.6 KiB
C#
218 lines
9.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Oqtane.Enums;
|
|
using Oqtane.Extensions;
|
|
using Oqtane.Infrastructure;
|
|
using Oqtane.Models;
|
|
using Oqtane.Security;
|
|
using Oqtane.Shared;
|
|
using SZUAbsolventenverein.Module.EventRegistration.Models;
|
|
using SZUAbsolventenverein.Module.EventRegistration.Repository;
|
|
|
|
namespace SZUAbsolventenverein.Module.EventRegistration.Services
|
|
{
|
|
public class ServerEventRegistrationService : IEventRegistrationService
|
|
{
|
|
private readonly IEventRepository _EventRepository;
|
|
private readonly IResponseRepository _ResponseRepository;
|
|
private readonly IUserPermissions _userPermissions;
|
|
private readonly ILogManager _logger;
|
|
private readonly IHttpContextAccessor _accessor;
|
|
private readonly Alias _alias;
|
|
|
|
public ServerEventRegistrationService(IEventRepository EventRepository, IResponseRepository ResponseRepository, IUserPermissions userPermissions, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
|
|
{
|
|
_EventRepository = EventRepository;
|
|
_ResponseRepository = ResponseRepository;
|
|
_userPermissions = userPermissions;
|
|
_logger = logger;
|
|
_accessor = accessor;
|
|
_alias = tenantManager.GetAlias();
|
|
}
|
|
|
|
public Task<Event> AddEventAsync(Event NewEvent)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, NewEvent.ModuleId, PermissionNames.Edit))
|
|
{
|
|
NewEvent = _EventRepository.AddEvent(NewEvent);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {NewEvent}", NewEvent);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {NewEvent}", NewEvent);
|
|
NewEvent = null;
|
|
}
|
|
return Task.FromResult(NewEvent);
|
|
}
|
|
|
|
public Task<Response> AddResponseAsync(Response Response)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, Response.ModuleId, PermissionNames.View))
|
|
{
|
|
Response = _ResponseRepository.AddResponse(Response);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {NewEvent}", Response);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {NewEvent}", Response);
|
|
Response = null;
|
|
}
|
|
return Task.FromResult(Response);
|
|
}
|
|
|
|
public Task<Response> UpdateResponseAsync(Response Response)
|
|
{
|
|
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, Response.ModuleId, PermissionNames.View))
|
|
{
|
|
Response = _ResponseRepository.UpdateResponse(Response);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {NewEvent}", Response);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {NewEvent}", Response);
|
|
Response = null;
|
|
}
|
|
return Task.FromResult(Response);
|
|
}
|
|
|
|
public Task DeleteEventAsync(int EventId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit))
|
|
{
|
|
_EventRepository.DeleteEvent(EventId);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Event Deleted {EventId}", EventId);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Delete Attempt {EventId} {ModuleId}", EventId, ModuleId);
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<Event> GetEventAsync(int EventId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
return Task.FromResult(_EventRepository.GetEvent(EventId, true));
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Get Attempt {ModuleId}", ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<(Event, Response)> GetEventDetails(int EventId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
Event currentEvent = _EventRepository.GetEvent(EventId);
|
|
Response rsvp = _ResponseRepository.GetResponse(EventId, _accessor.HttpContext.User.UserId());
|
|
return Task.FromResult((currentEvent, rsvp));
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Get Attempt {ModuleId}", ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<List<Response>> GetEventResponses(int EventId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit))
|
|
{
|
|
return Task.FromResult(_ResponseRepository.GetResponses(EventId, ModuleId).ToList());
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Response Get Attempt {ModuleId}", ModuleId);
|
|
return null;
|
|
}
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
public Task<List<Event>> GetEventsAsync(int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
return Task.FromResult(_EventRepository.GetEvents(ModuleId).ToList());
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Events Get Attempt {ModuleId}", ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<Event> UpdateEventAsync(Event NewEvent)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, NewEvent.ModuleId, PermissionNames.Edit))
|
|
{
|
|
NewEvent = _EventRepository.UpdateEvent(NewEvent);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Event Updated {NewEvent}", NewEvent);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Update Attempt {NewEvent}", NewEvent);
|
|
NewEvent = null;
|
|
}
|
|
return Task.FromResult(NewEvent);
|
|
}
|
|
|
|
// TODO: Implement the methods for EventResponses
|
|
|
|
/*
|
|
|
|
public Task<Models.Event> GetEventRegistrationAsync(int EventRegistrationId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
return Task.FromResult(_EventRegistrationRepository.GetEventRegistration(EventRegistrationId));
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Get Attempt {EventRegistrationId} {ModuleId}", EventRegistrationId, ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<Models.Event> AddEventRegistrationAsync(Models.Event EventRegistration)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, EventRegistration.ModuleId, PermissionNames.Edit))
|
|
{
|
|
EventRegistration = _EventRegistrationRepository.AddEventRegistration(EventRegistration);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {EventRegistration}", EventRegistration);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {EventRegistration}", EventRegistration);
|
|
EventRegistration = null;
|
|
}
|
|
return Task.FromResult(EventRegistration);
|
|
}
|
|
|
|
public Task<Models.Event> UpdateEventRegistrationAsync(Models.Event EventRegistration)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, EventRegistration.ModuleId, PermissionNames.Edit))
|
|
{
|
|
EventRegistration = _EventRegistrationRepository.UpdateEventRegistration(EventRegistration);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Update, "EventRegistration Updated {EventRegistration}", EventRegistration);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Update Attempt {EventRegistration}", EventRegistration);
|
|
EventRegistration = null;
|
|
}
|
|
return Task.FromResult(EventRegistration);
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
}
|
|
}
|