Compare commits
12 Commits
e2140d83f2
...
a07f5f5869
Author | SHA1 | Date | |
---|---|---|---|
a07f5f5869 | |||
d8b98ac5cc | |||
4afb3b0c54 | |||
588abd2712 | |||
d5a7ffc596 | |||
71cabbd548 | |||
7ae98f4bb4 | |||
58cf1cd74c | |||
0cdc5ebb20 | |||
18ab0b592f | |||
1dd4d3dca7 | |||
d58eb8d973 |
@ -1,41 +1,43 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class ColorAuthentication : IColorAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<ColorAuthentication> _logger;
|
||||
public ColorAuthentication(ILogger<ColorAuthentication> logger)
|
||||
public ColorAuthentication(ILogger<ColorAuthentication> logger, GrantService grantService)
|
||||
{
|
||||
_logger = logger;
|
||||
_grantService = grantService;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
return _grantService.hasGrant(user.permissionId, ColorGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(Color model, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
return _grantService.hasGrant(user.permissionId, ColorGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, ColorGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(ColorDTO item, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
return _grantService.hasGrant(user.permissionId, ColorGrantNames.CanAdd);
|
||||
}
|
||||
public bool canUpdate(Color model, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
return _grantService.hasGrant(user.permissionId, ColorGrantNames.CanUpdateAny) ||
|
||||
_grantService.getULongValues(user.permissionId, ColorGrantNames.CanUpdate).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canDelete(Color model, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
return _grantService.hasGrant(user.permissionId, ColorGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, ColorGrantNames.CanDelete).Exists(x => x == model.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
44
API/Authentication/EventAuthentication.cs
Normal file
44
API/Authentication/EventAuthentication.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class EventAuthentication : IEventAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<EventAuthentication> _logger;
|
||||
|
||||
public EventAuthentication(GrantService grantService, ILogger<EventAuthentication> logger)
|
||||
{
|
||||
_grantService = grantService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, EventGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(Event model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, EventGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, EventGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(EventDTO item, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, EventGrantNames.CanAdd);
|
||||
}
|
||||
public bool canUpdate(Event model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, EventGrantNames.CanUpdateAny) ||
|
||||
_grantService.getULongValues(user.permissionId, EventGrantNames.CanUpdate).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canDelete(Event model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, EventGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, EventGrantNames.CanDelete).Exists(x => x == model.id);
|
||||
}
|
||||
}
|
||||
}
|
46
API/Authentication/GrantAuthentication.cs
Normal file
46
API/Authentication/GrantAuthentication.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class GrantAuthentication : IGrantAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<GrantAuthentication> _logger;
|
||||
|
||||
public GrantAuthentication(GrantService grantService, ILogger<GrantAuthentication> logger)
|
||||
{
|
||||
_grantService = grantService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, GrantGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(Grant model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, GrantGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, GrantGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(GrantDTO item, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, GrantGrantNames.CanAdd) &&
|
||||
_grantService.hasGrant(user.permissionId, item.name);
|
||||
}
|
||||
public bool canUpdate(Grant model, User user)
|
||||
{
|
||||
// Doesn't make sense to update the name of a grant. The updater can just delete and remake.
|
||||
return false;
|
||||
}
|
||||
public bool canDelete(Grant model, User user)
|
||||
{
|
||||
return (_grantService.hasGrant(user.permissionId, GrantGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, GrantGrantNames.CanDelete).Exists(x => x == model.id))
|
||||
&& _grantService.hasGrant(user.permissionId, model.name);
|
||||
}
|
||||
}
|
||||
}
|
14
API/Authentication/GrantNames/ColorGrantNames.cs
Normal file
14
API/Authentication/GrantNames/ColorGrantNames.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class ColorGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.color.get.all";
|
||||
public const string CanGetAny = "api.color.get.any";
|
||||
public const string CanGet = "api.color.get";
|
||||
public const string CanAdd = "api.color.add";
|
||||
public const string CanUpdateAny = "api.color.update.any";
|
||||
public const string CanUpdate = "api.color.update";
|
||||
public const string CanDeleteAny = "api.color.delete.any";
|
||||
public const string CanDelete = "api.color.delete";
|
||||
}
|
||||
}
|
14
API/Authentication/GrantNames/EventGrantNames.cs
Normal file
14
API/Authentication/GrantNames/EventGrantNames.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class EventGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.event.get.all";
|
||||
public const string CanGetAny = "api.event.get.any";
|
||||
public const string CanGet = "api.event.get";
|
||||
public const string CanAdd = "api.event.add";
|
||||
public const string CanUpdateAny = "api.event.update.any";
|
||||
public const string CanUpdate = "api.event.update";
|
||||
public const string CanDeleteAny = "api.event.delete.any";
|
||||
public const string CanDelete = "api.event.delete";
|
||||
}
|
||||
}
|
14
API/Authentication/GrantNames/GrantGrantNames.cs
Normal file
14
API/Authentication/GrantNames/GrantGrantNames.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class GrantGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.grant.get.all";
|
||||
public const string CanGetAny = "api.grant.get.any";
|
||||
public const string CanGet = "api.grant.get";
|
||||
public const string CanAdd = "api.grant.add";
|
||||
public const string CanUpdateAny = "api.grant.update.any";
|
||||
public const string CanUpdate = "api.grant.update";
|
||||
public const string CanDeleteAny = "api.grant.delete.any";
|
||||
public const string CanDelete = "api.grant.delete";
|
||||
}
|
||||
}
|
14
API/Authentication/GrantNames/ImageGrantNames.cs
Normal file
14
API/Authentication/GrantNames/ImageGrantNames.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class ImageGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.image.get.all";
|
||||
public const string CanGetAny = "api.image.get.any";
|
||||
public const string CanGet = "api.image.get";
|
||||
public const string CanAdd = "api.image.add";
|
||||
public const string CanUpdateAny = "api.image.update.any";
|
||||
public const string CanUpdate = "api.image.update";
|
||||
public const string CanDeleteAny = "api.image.delete.any";
|
||||
public const string CanDelete = "api.image.delete";
|
||||
}
|
||||
}
|
14
API/Authentication/GrantNames/PermissionGrantNames.cs
Normal file
14
API/Authentication/GrantNames/PermissionGrantNames.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class PermissionGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.permission.get.all";
|
||||
public const string CanGetAny = "api.permission.get.any";
|
||||
public const string CanGet = "api.permission.get";
|
||||
public const string CanAdd = "api.permission.add";
|
||||
public const string CanUpdateAny = "api.permission.update.any";
|
||||
public const string CanUpdate = "api.permission.update";
|
||||
public const string CanDeleteAny = "api.permission.delete.any";
|
||||
public const string CanDelete = "api.permission.delete";
|
||||
}
|
||||
}
|
14
API/Authentication/GrantNames/SavedEventGrantNames.cs
Normal file
14
API/Authentication/GrantNames/SavedEventGrantNames.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class SavedEventGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.savedEvent.get.all";
|
||||
public const string CanGetAny = "api.savedEvent.get.any";
|
||||
public const string CanGet = "api.savedEvent.get";
|
||||
public const string CanAdd = "api.savedEvent.add";
|
||||
public const string CanUpdateAny = "api.savedEvent.update.any";
|
||||
public const string CanUpdate = "api.savedEvent.update";
|
||||
public const string CanDeleteAny = "api.savedEvent.delete.any";
|
||||
public const string CanDelete = "api.savedEvent.delete";
|
||||
}
|
||||
}
|
18
API/Authentication/GrantNames/UserGrantNames.cs
Normal file
18
API/Authentication/GrantNames/UserGrantNames.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace API.Authentication.GrantNames
|
||||
{
|
||||
public static class UserGrantNames
|
||||
{
|
||||
public const string CanGetAll = "api.user.get.all";
|
||||
public const string CanGetAny = "api.user.get.any";
|
||||
public const string CanGet = "api.user.get";
|
||||
public const string CanAdd = "api.user.add";
|
||||
public const string CanUpdateAny = "api.user.update.any";
|
||||
public const string CanUpdate = "api.user.update";
|
||||
public const string CanUpdateSelf = "api.user.update.self";
|
||||
public const string CanUpdateNames = "api.user.update.names";
|
||||
public const string CanUpdatePhoneNumber = "api.user.update.phoneNumber";
|
||||
public const string CanUpdatePermission = "api.user.update.permission";
|
||||
public const string CanDeleteAny = "api.user.delete.any";
|
||||
public const string CanDelete = "api.user.delete";
|
||||
}
|
||||
}
|
43
API/Authentication/ImageAuthentication.cs
Normal file
43
API/Authentication/ImageAuthentication.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class ImageAuthentication : IImageAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<ImageAuthentication> _logger;
|
||||
public ImageAuthentication(ILogger<ImageAuthentication> logger, GrantService grantService)
|
||||
{
|
||||
_logger = logger;
|
||||
_grantService = grantService;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(Image model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, ImageGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(ImageDTO item, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanAdd);
|
||||
}
|
||||
public bool canUpdate(Image model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanUpdateAny) ||
|
||||
_grantService.getULongValues(user.permissionId, ImageGrantNames.CanUpdate).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canDelete(Image model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, ImageGrantNames.CanDelete).Exists(x => x == model.id);
|
||||
}
|
||||
}
|
||||
}
|
9
API/Authentication/Interfaces/IEventAuthentication.cs
Normal file
9
API/Authentication/Interfaces/IEventAuthentication.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using API.DTO.Base;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface IEventAuthentication : IGenericAuthentication<EventDTO, Event>
|
||||
{
|
||||
}
|
||||
}
|
9
API/Authentication/Interfaces/IGrantAuthentication.cs
Normal file
9
API/Authentication/Interfaces/IGrantAuthentication.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using API.DTO.Base;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface IGrantAuthentication : IGenericAuthentication<GrantDTO, Grant>
|
||||
{
|
||||
}
|
||||
}
|
9
API/Authentication/Interfaces/IImageAuthentication.cs
Normal file
9
API/Authentication/Interfaces/IImageAuthentication.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using API.DTO.Base;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface IImageAuthentication : IGenericAuthentication<ImageDTO, Image>
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using API.DTO.Base;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface IPermissionAuthentication : IGenericAuthentication<PermissionDTO, Permission>
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using API.DTO.Base;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface ISavedEventAuthentication : IGenericAuthentication<SavedEventDTO, SavedEvent>
|
||||
{
|
||||
}
|
||||
}
|
9
API/Authentication/Interfaces/IUserAuthentication.cs
Normal file
9
API/Authentication/Interfaces/IUserAuthentication.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using API.DTO.Base;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface IUserAuthentication : IGenericAuthentication<UserDTO, User>
|
||||
{
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
namespace API.Authentication.Interfaces
|
||||
{
|
||||
public interface IYesAuthentication : IGenericAuthentication<object, object>
|
||||
{
|
||||
}
|
||||
}
|
44
API/Authentication/PermissionAuthentication.cs
Normal file
44
API/Authentication/PermissionAuthentication.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class PermissionAuthentication : IPermissionAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<PermissionAuthentication> _logger;
|
||||
public PermissionAuthentication(ILogger<PermissionAuthentication> logger, GrantService grantService)
|
||||
{
|
||||
_logger = logger;
|
||||
_grantService = grantService;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, PermissionGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(Permission model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, PermissionGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, PermissionGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(PermissionDTO item, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, PermissionGrantNames.CanAdd);
|
||||
}
|
||||
public bool canUpdate(Permission model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, PermissionGrantNames.CanUpdateAny) ||
|
||||
_grantService.getULongValues(user.permissionId, PermissionGrantNames.CanUpdate).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canDelete(Permission model, User user)
|
||||
{
|
||||
return (_grantService.hasGrant(user.permissionId, PermissionGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, PermissionGrantNames.CanDelete).Exists(x => x == model.id))
|
||||
&& model.id != user.permissionId;
|
||||
}
|
||||
}
|
||||
}
|
43
API/Authentication/SavedEventAuthentication.cs
Normal file
43
API/Authentication/SavedEventAuthentication.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class SavedEventAuthentication : ISavedEventAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<SavedEventAuthentication> _logger;
|
||||
public SavedEventAuthentication(ILogger<SavedEventAuthentication> logger, GrantService grantService)
|
||||
{
|
||||
_logger = logger;
|
||||
_grantService = grantService;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(SavedEvent model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, SavedEventGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(SavedEventDTO item, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanAdd);
|
||||
}
|
||||
public bool canUpdate(SavedEvent model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanUpdateAny) ||
|
||||
_grantService.getULongValues(user.permissionId, SavedEventGrantNames.CanUpdate).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canDelete(SavedEvent model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, SavedEventGrantNames.CanDelete).Exists(x => x == model.id);
|
||||
}
|
||||
}
|
||||
}
|
87
API/Authentication/UserAuthentication.cs
Normal file
87
API/Authentication/UserAuthentication.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using API.Authentication.GrantNames;
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.Services;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class UserAuthentication : IUserAuthentication
|
||||
{
|
||||
private readonly GrantService _grantService;
|
||||
private readonly ILogger<UserAuthentication> _logger;
|
||||
private readonly UserService _userService;
|
||||
public UserAuthentication(ILogger<UserAuthentication> logger, GrantService grantService, UserService userService)
|
||||
{
|
||||
_logger = logger;
|
||||
_grantService = grantService;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, UserGrantNames.CanGetAll);
|
||||
}
|
||||
public bool canGet(User model, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, UserGrantNames.CanGetAny) ||
|
||||
_grantService.getULongValues(user.permissionId, UserGrantNames.CanGet).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canAdd(UserDTO item, User user)
|
||||
{
|
||||
return _grantService.hasGrant(user.permissionId, UserGrantNames.CanAdd);
|
||||
}
|
||||
|
||||
// todo this needs to be made much better
|
||||
public bool canUpdate(User model, User user)
|
||||
{
|
||||
User origUser;
|
||||
if (model.id == user.id)
|
||||
{
|
||||
if (!_grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdateSelf)
|
||||
|| !_grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdateAny)
|
||||
|| !_grantService.getULongValues(user.permissionId, UserGrantNames.CanUpdate).Exists(x => x == model.id)
|
||||
)
|
||||
return false;
|
||||
|
||||
// Don't let the user change their own permissionId
|
||||
if (model.permissionId != user.permissionId)
|
||||
return false;
|
||||
origUser = user;
|
||||
}
|
||||
else
|
||||
{
|
||||
origUser = _userService.getNoAuthentication(model.id) ?? throw new InvalidOperationException("Model is null.");
|
||||
}
|
||||
|
||||
if (origUser.permissionId != model.permissionId)
|
||||
{
|
||||
if (!_grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdatePermission))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (origUser.firstName != user.firstName || origUser.lastName != user.lastName)
|
||||
{
|
||||
if (!_grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdateNames))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (origUser.phoneNumber != user.phoneNumber)
|
||||
{
|
||||
if (!_grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdatePhoneNumber))
|
||||
return false;
|
||||
}
|
||||
|
||||
return _grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdateAny)
|
||||
|| model.id == user.id &&
|
||||
_grantService.hasGrant(user.permissionId, UserGrantNames.CanUpdateSelf)
|
||||
|| _grantService.getULongValues(user.permissionId, UserGrantNames.CanUpdate).Exists(x => x == model.id);
|
||||
}
|
||||
public bool canDelete(User model, User user)
|
||||
{
|
||||
return (_grantService.hasGrant(user.permissionId, UserGrantNames.CanDeleteAny) ||
|
||||
_grantService.getULongValues(user.permissionId, UserGrantNames.CanDelete).Exists(x => x == model.id))
|
||||
&& model.id != user.id;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
using API.Authentication.Interfaces;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Authentication
|
||||
{
|
||||
public class YesAuthentication : IYesAuthentication
|
||||
{
|
||||
private readonly ILogger<YesAuthentication> _logger;
|
||||
public YesAuthentication(ILogger<YesAuthentication> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
}
|
||||
|
||||
public bool canGetAll(User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
}
|
||||
public bool canGet(object model, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
}
|
||||
public bool canAdd(object item, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
}
|
||||
public bool canUpdate(object model, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
}
|
||||
public bool canDelete(object model, User user)
|
||||
{
|
||||
_logger.Log(LogLevel.Warning, "Yes Authentication being used.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
using API.DTO.Base;
|
||||
using API.DTO.Login;
|
||||
using API.Errors;
|
||||
using API.Services;
|
||||
using API.Services.Interfaces;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
@ -14,17 +16,19 @@ namespace API.Controllers
|
||||
{
|
||||
private readonly ILogger<AuthController> _logger;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly UserService _userService;
|
||||
|
||||
public AuthController(ILogger<AuthController> logger, IUserManager userManager)
|
||||
public AuthController(ILogger<AuthController> logger, IUserManager userManager, UserService userService)
|
||||
{
|
||||
_logger = logger;
|
||||
_userManager = userManager;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public ActionResult<UserDTO> login(UserLoginDTO userLogin)
|
||||
{
|
||||
UserDTO? user = _userManager.AuthenticateUser(userLogin);
|
||||
UserDTO? user = _userManager.authenticateUser(userLogin);
|
||||
if (user == null)
|
||||
return new UnauthorizedResult();
|
||||
|
||||
@ -42,5 +46,18 @@ namespace API.Controllers
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public ActionResult<UserDTO> register(UserRegisterDTO registerDTO)
|
||||
{
|
||||
UserDTO? user = _userManager.registerUser(registerDTO);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return Conflict(Strings.UserExists);
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class EventController : CRUDBase<EventController, EventDTO, EventUpdateDTO, Event, AuditEvent, IYesAuthentication, EventService>
|
||||
public class EventController : CRUDBase<EventController, EventDTO, EventUpdateDTO, Event, AuditEvent, IEventAuthentication, EventService>
|
||||
{
|
||||
public EventController(ILogger<EventController> logger, UserService userService, EventService service) : base(logger, userService, service)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class GrantController : CRUDBase<GrantController, GrantDTO, GrantUpdateDTO, Grant, AuditGrant, IYesAuthentication, GrantService>
|
||||
public class GrantController : CRUDBase<GrantController, GrantDTO, GrantUpdateDTO, Grant, AuditGrant, IGrantAuthentication, GrantService>
|
||||
{
|
||||
public GrantController(ILogger<GrantController> logger, UserService userService, GrantService service) : base(logger, userService, service)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class ImageController : CRUDBase<ImageController, ImageDTO, ImageUpdateDTO, Image, AuditImage, IYesAuthentication, ImageService>
|
||||
public class ImageController : CRUDBase<ImageController, ImageDTO, ImageUpdateDTO, Image, AuditImage, IImageAuthentication, ImageService>
|
||||
{
|
||||
public ImageController(ILogger<ImageController> logger, UserService userService, ImageService service) : base(logger, userService, service)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class PermissionController : CRUDBase<PermissionController, PermissionDTO, PermissionUpdateDTO, Permission, AuditPermission, IYesAuthentication, PermissionService>
|
||||
public class PermissionController : CRUDBase<PermissionController, PermissionDTO, PermissionUpdateDTO, Permission, AuditPermission, IPermissionAuthentication, PermissionService>
|
||||
{
|
||||
public PermissionController(ILogger<PermissionController> logger, UserService userService, PermissionService service) : base(logger, userService, service)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class SavedEventController : CRUDBase<SavedEventController, SavedEventDTO, SavedEventUpdateDTO, SavedEvent, AuditSavedEvent, IYesAuthentication, SavedEventService>
|
||||
public class SavedEventController : CRUDBase<SavedEventController, SavedEventDTO, SavedEventUpdateDTO, SavedEvent, AuditSavedEvent, ISavedEventAuthentication, SavedEventService>
|
||||
{
|
||||
public SavedEventController(ILogger<SavedEventController> logger, UserService userService, SavedEventService service) : base(logger, userService, service)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class UserController : CRUDBase<UserController, UserDTO, UserUpdateDTO, User, AuditUser, IYesAuthentication, UserService>
|
||||
public class UserController : CRUDBase<UserController, UserDTO, UserUpdateDTO, User, AuditUser, IUserAuthentication, UserService>
|
||||
{
|
||||
public UserController(ILogger<UserController> logger, UserService userService, UserService service) : base(logger, userService, service)
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ namespace API.DTO.Base
|
||||
|
||||
public DateTime updated { get; set; }
|
||||
|
||||
public ulong updater { get; set; }
|
||||
public ulong? updater { get; set; }
|
||||
|
||||
public User adaptToModel()
|
||||
{
|
||||
|
19
API/DTO/Login/UserRegisterDTO.cs
Normal file
19
API/DTO/Login/UserRegisterDTO.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using DAL.Values;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace API.DTO.Login
|
||||
{
|
||||
public class UserRegisterDTO
|
||||
{
|
||||
[MaxLength(64)]
|
||||
public string firstName { get; set; } = null!;
|
||||
|
||||
[MaxLength(64)]
|
||||
public string lastName { get; set; } = null!;
|
||||
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
[MaxLength(1000)]
|
||||
public string password { get; set; } = null!;
|
||||
}
|
||||
}
|
7
API/Errors/Strings.cs
Normal file
7
API/Errors/Strings.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace API.Errors
|
||||
{
|
||||
public static class Strings
|
||||
{
|
||||
public const string UserExists = "User with that phone number or first and last name already exists.\nIf you would like to change your phone number please login.";
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ using API.Services;
|
||||
using API.Services.Interfaces;
|
||||
using DAL.Contexts;
|
||||
using DAL.Models;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Serilog;
|
||||
using System.Reflection;
|
||||
@ -40,9 +41,23 @@ namespace API
|
||||
builder.Services.AddTransient<ImageService>();
|
||||
builder.Services.AddTransient<PermissionService>();
|
||||
builder.Services.AddTransient<SavedEventService>();
|
||||
builder.Services.AddTransient<UserService>();
|
||||
builder.Services.AddTransient<UserService>(options =>
|
||||
{
|
||||
ILogger<UserService> logger = options.GetRequiredService<ILogger<UserService>>();
|
||||
SASGContext context = options.GetRequiredService<SASGContext>();
|
||||
IUserAuthentication authentication = options.GetRequiredService<IUserAuthentication>();
|
||||
PermissionService permissionService = options.GetRequiredService<PermissionService>();
|
||||
|
||||
builder.Services.AddTransient<IYesAuthentication, YesAuthentication>();
|
||||
ulong defaultUserPermission = UInt64.Parse(builder.Configuration["defaultUserPermission"] ?? throw new InvalidOperationException("defaultUserPermission is null"));
|
||||
|
||||
return new UserService(logger, context, authentication, permissionService, defaultUserPermission);
|
||||
});
|
||||
|
||||
builder.Services.AddTransient<IColorAuthentication, ColorAuthentication>();
|
||||
builder.Services.AddTransient<IEventAuthentication, EventAuthentication>();
|
||||
builder.Services.AddTransient<IGrantAuthentication, GrantAuthentication>();
|
||||
builder.Services.AddTransient<IImageAuthentication, ImageAuthentication>();
|
||||
builder.Services.AddTransient<IColorAuthentication, ColorAuthentication>();
|
||||
builder.Services.AddTransient<IColorAuthentication, ColorAuthentication>();
|
||||
|
||||
builder.Services.AddTransient<IHashingFactory, HashingFactory>();
|
||||
@ -62,6 +77,9 @@ namespace API
|
||||
return new UserManager(userService, hashingFactory, logger, hashingType);
|
||||
});
|
||||
|
||||
|
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
@ -75,6 +93,8 @@ namespace API
|
||||
MinimumSameSitePolicy = SameSiteMode.Strict
|
||||
});
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
|
@ -6,9 +6,9 @@ using DAL.Models.Audits;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class EventService : ServiceBase<EventService, EventDTO, Event, AuditEvent, IYesAuthentication>
|
||||
public class EventService : ServiceBase<EventService, EventDTO, Event, AuditEvent, IEventAuthentication>
|
||||
{
|
||||
public EventService(ILogger<EventService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
|
||||
public EventService(ILogger<EventService> logger, SASGContext context, IEventAuthentication auth) : base(logger, context, auth)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,71 @@ using DAL.Models.Audits;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class GrantService : ServiceBase<GrantService, GrantDTO, Grant, AuditGrant, IYesAuthentication>
|
||||
public class GrantService : ServiceBase<GrantService, GrantDTO, Grant, AuditGrant, IGrantAuthentication>
|
||||
{
|
||||
public GrantService(ILogger<GrantService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
|
||||
public GrantService(ILogger<GrantService> logger, SASGContext context, IGrantAuthentication auth) : base(logger, context, auth)
|
||||
{
|
||||
}
|
||||
|
||||
public bool hasGrant(ulong permissionId, string grantName)
|
||||
{
|
||||
return getNoAuthentication(x => x.permissionId == permissionId && x.name.Equals(grantName)).Any();
|
||||
}
|
||||
|
||||
public List<string> getValues(ulong permissionId, string grantName)
|
||||
{
|
||||
List<Grant> grants = getNoAuthentication(x => x.permissionId == permissionId && x.name.StartsWith(grantName + ".")).ToList();
|
||||
|
||||
List<string> values = [];
|
||||
foreach (Grant grant in grants)
|
||||
{
|
||||
string value = grant.name.Substring(grantName.Length);
|
||||
if (value.Contains('.'))
|
||||
// Were not looking at a value and instead another grant
|
||||
continue;
|
||||
|
||||
values.Add(value);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public List<string> getStringValues(ulong permissionId, string grantName)
|
||||
{
|
||||
List<string> values = getValues(permissionId, grantName);
|
||||
|
||||
// Get rid of numbers
|
||||
values = values.Where(x => !Int32.TryParse(x, out int _)).ToList();
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public List<int> getIntValues(ulong permissionId, string grantName)
|
||||
{
|
||||
List<string> values = getValues(permissionId, grantName);
|
||||
List<int> intValues = [];
|
||||
|
||||
Parallel.ForEach(values, x =>
|
||||
{
|
||||
if (Int32.TryParse(x, out int parsed))
|
||||
intValues.Add(parsed);
|
||||
});
|
||||
|
||||
return intValues;
|
||||
}
|
||||
|
||||
public List<ulong> getULongValues(ulong permissionId, string grantName)
|
||||
{
|
||||
List<string> values = getValues(permissionId, grantName);
|
||||
List<ulong> uLongValues = [];
|
||||
|
||||
Parallel.ForEach(values, x =>
|
||||
{
|
||||
if (UInt64.TryParse(x, out ulong parsed))
|
||||
uLongValues.Add(parsed);
|
||||
});
|
||||
|
||||
return uLongValues;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,9 @@ using DAL.Models.Audits;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class ImageService : ServiceBase<ImageService, ImageDTO, Image, AuditImage, IYesAuthentication>
|
||||
public class ImageService : ServiceBase<ImageService, ImageDTO, Image, AuditImage, IImageAuthentication>
|
||||
{
|
||||
public ImageService(ILogger<ImageService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
|
||||
public ImageService(ILogger<ImageService> logger, SASGContext context, IImageAuthentication auth) : base(logger, context, auth)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ namespace API.Services.Interfaces
|
||||
{
|
||||
public interface IUserManager
|
||||
{
|
||||
UserDTO? AuthenticateUser(UserLoginDTO loginDTO);
|
||||
UserDTO? authenticateUser(UserLoginDTO loginDTO);
|
||||
|
||||
UserDTO? registerUser(UserRegisterDTO registerDTO);
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ using DAL.Models.Audits;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class PermissionService : ServiceBase<PermissionService, PermissionDTO, Permission, AuditPermission, IYesAuthentication>
|
||||
public class PermissionService : ServiceBase<PermissionService, PermissionDTO, Permission, AuditPermission, IPermissionAuthentication>
|
||||
{
|
||||
|
||||
public PermissionService(ILogger<PermissionService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
|
||||
public PermissionService(ILogger<PermissionService> logger, SASGContext context, IPermissionAuthentication auth) : base(logger, context, auth)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ using DAL.Models.Audits;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class SavedEventService : ServiceBase<SavedEventService, SavedEventDTO, SavedEvent, AuditSavedEvent, IYesAuthentication>
|
||||
public class SavedEventService : ServiceBase<SavedEventService, SavedEventDTO, SavedEvent, AuditSavedEvent, ISavedEventAuthentication>
|
||||
{
|
||||
|
||||
public SavedEventService(ILogger<SavedEventService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
|
||||
public SavedEventService(ILogger<SavedEventService> logger, SASGContext context, ISavedEventAuthentication auth) : base(logger, context, auth)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -15,19 +15,19 @@ namespace API.Services
|
||||
where TDTO : IAdaptable<TModel>
|
||||
{
|
||||
private readonly TAuthentication _auth;
|
||||
private readonly SASGContext _context;
|
||||
private readonly ILogger<TLoggerCategory> _logger;
|
||||
public readonly SASGContext Context;
|
||||
|
||||
public ServiceBase(ILogger<TLoggerCategory> logger, SASGContext context, TAuthentication auth)
|
||||
{
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
Context = context;
|
||||
_auth = auth;
|
||||
}
|
||||
|
||||
public TModel? get(ulong id, User user)
|
||||
{
|
||||
TModel? result = _context.Set<TModel>().Find(id);
|
||||
TModel? result = Context.Set<TModel>().Find(id);
|
||||
if (result == null)
|
||||
return null;
|
||||
|
||||
@ -39,17 +39,17 @@ namespace API.Services
|
||||
if (!_auth.canGetAll(user))
|
||||
return null;
|
||||
|
||||
return whereClause != null ? _context.Set<TModel>().Where(whereClause) : _context.Set<TModel>();
|
||||
return whereClause != null ? Context.Set<TModel>().Where(whereClause) : Context.Set<TModel>();
|
||||
}
|
||||
|
||||
public TModel? getNoAuthentication(ulong id)
|
||||
{
|
||||
return _context.Set<TModel>().Find(id);
|
||||
return Context.Set<TModel>().Find(id);
|
||||
}
|
||||
|
||||
public IEnumerable<TModel> getNoAuthentication(Expression<Func<TModel, bool>>? whereClause = null)
|
||||
{
|
||||
return whereClause != null ? _context.Set<TModel>().Where(whereClause) : _context.Set<TModel>();
|
||||
return whereClause != null ? Context.Set<TModel>().Where(whereClause) : Context.Set<TModel>();
|
||||
}
|
||||
|
||||
public TModel? add(TDTO item, User user)
|
||||
@ -61,8 +61,8 @@ namespace API.Services
|
||||
|
||||
model.updater = user.id;
|
||||
model.updated = DateTime.Now;
|
||||
_context.Add(model);
|
||||
_context.SaveChanges();
|
||||
Context.Add(model);
|
||||
Context.SaveChanges();
|
||||
|
||||
return model;
|
||||
}
|
||||
@ -72,7 +72,7 @@ namespace API.Services
|
||||
if (!_auth.canUpdate(model, user))
|
||||
return null;
|
||||
|
||||
TModel? origModel = _context.Set<TModel>().Find(model.id);
|
||||
TModel? origModel = Context.Set<TModel>().Find(model.id);
|
||||
if (origModel == null)
|
||||
return null;
|
||||
|
||||
@ -83,7 +83,7 @@ namespace API.Services
|
||||
origModel.updated = DateTime.Now;
|
||||
origModel.updater = user.id;
|
||||
|
||||
_context.SaveChanges();
|
||||
Context.SaveChanges();
|
||||
|
||||
return origModel;
|
||||
}
|
||||
@ -93,7 +93,7 @@ namespace API.Services
|
||||
if (!_auth.canDelete(model, user))
|
||||
return null;
|
||||
|
||||
TModel? origModel = _context.Set<TModel>().Find(model.id);
|
||||
TModel? origModel = Context.Set<TModel>().Find(model.id);
|
||||
if (origModel == null)
|
||||
return null;
|
||||
|
||||
@ -104,15 +104,15 @@ namespace API.Services
|
||||
|
||||
copyToAudit(origModel);
|
||||
|
||||
_context.Remove(origModel);
|
||||
_context.SaveChanges();
|
||||
Context.Remove(origModel);
|
||||
Context.SaveChanges();
|
||||
|
||||
return origModel.adaptToAudit();
|
||||
}
|
||||
|
||||
private void copyToAudit(TModel model)
|
||||
{
|
||||
_context.Set<TAudit>().Add(model.adaptToAudit());
|
||||
Context.Set<TAudit>().Add(model.adaptToAudit());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ namespace API.Services
|
||||
_preferredHashingType = preferredHashingType;
|
||||
}
|
||||
|
||||
public UserDTO? AuthenticateUser(UserLoginDTO loginDTO)
|
||||
public UserDTO? authenticateUser(UserLoginDTO loginDTO)
|
||||
{
|
||||
User? user = _userService.getNoAuthentication(x => x.phoneNumber.Equals(loginDTO.phoneNumber)).FirstOrDefault();
|
||||
|
||||
@ -53,5 +53,33 @@ namespace API.Services
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
public UserDTO? registerUser(UserRegisterDTO registerDTO)
|
||||
{
|
||||
if (_userService.getNoAuthentication(x =>
|
||||
x.phoneNumber.Equals(registerDTO.phoneNumber) ||
|
||||
x.firstName.Equals(registerDTO.firstName) && x.lastName.Equals(registerDTO.lastName))
|
||||
.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
IHashingAlgorithm? hashingAlgorithm = _hashingFactory.getAlgorithm(_preferredHashingType);
|
||||
if (hashingAlgorithm == null)
|
||||
{
|
||||
_logger.Log(LogLevel.Error, "Preferred hashing type '{hashingType}' that isn't recognized by factory '{factory}'.", _preferredHashingType, nameof(_hashingFactory));
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] salt;
|
||||
string hashedPassword = hashingAlgorithm.hash(registerDTO.password, out salt);
|
||||
|
||||
User user = _userService.add(registerDTO, hashedPassword, salt);
|
||||
|
||||
UserDTO dto = new UserDTO();
|
||||
dto.adaptFromModel(user);
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,47 @@
|
||||
using API.Authentication.Interfaces;
|
||||
using API.DTO.Base;
|
||||
using API.DTO.Login;
|
||||
using DAL.Contexts;
|
||||
using DAL.Models;
|
||||
using DAL.Models.Audits;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class UserService : ServiceBase<UserService, UserDTO, User, AuditUser, IYesAuthentication>
|
||||
public class UserService : ServiceBase<UserService, UserDTO, User, AuditUser, IUserAuthentication>
|
||||
{
|
||||
|
||||
public UserService(ILogger<UserService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
|
||||
private readonly ulong _defaultUserPermission;
|
||||
private readonly PermissionService _permissionService;
|
||||
public UserService(ILogger<UserService> logger, SASGContext context, IUserAuthentication auth, PermissionService permissionService, ulong defaultUserPermission) : base(logger, context, auth)
|
||||
{
|
||||
_permissionService = permissionService;
|
||||
_defaultUserPermission = defaultUserPermission;
|
||||
}
|
||||
|
||||
public User add(UserRegisterDTO registerDTO, string hashedPassword, byte[] salt)
|
||||
{
|
||||
Permission? defaultPermission = _permissionService.getNoAuthentication(_defaultUserPermission);
|
||||
|
||||
if (defaultPermission == null)
|
||||
throw new InvalidOperationException("defaultUserPermission doesn't exist.");
|
||||
|
||||
User model = new User
|
||||
{
|
||||
firstName = registerDTO.firstName,
|
||||
lastName = registerDTO.lastName,
|
||||
phoneNumber = registerDTO.phoneNumber,
|
||||
|
||||
password = hashedPassword,
|
||||
salt = salt,
|
||||
|
||||
permissionId = defaultPermission.id,
|
||||
|
||||
updated = DateTime.Now
|
||||
};
|
||||
|
||||
Context.Add(model);
|
||||
Context.SaveChanges();
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,9 @@ namespace DAL.Models.Audits
|
||||
[Column("permissionId")]
|
||||
public ulong permissionId { get; set; }
|
||||
|
||||
[Column("updater")]
|
||||
public new ulong? updater { get; set; }
|
||||
|
||||
public override User adaptToModel()
|
||||
{
|
||||
return new User
|
||||
|
@ -27,11 +27,10 @@ namespace DAL.Models
|
||||
public string lastName { get; set; } = null!;
|
||||
|
||||
[Column("phoneNumber")]
|
||||
[MaxLength(32)]
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
[Column("password")]
|
||||
[MaxLength(1000)]
|
||||
[MaxLength(2048)]
|
||||
public string password { get; set; } = null!;
|
||||
|
||||
[Column("salt")]
|
||||
@ -45,6 +44,11 @@ namespace DAL.Models
|
||||
[Column("permissionId")]
|
||||
public ulong permissionId { get; set; }
|
||||
|
||||
[Column("updater")]
|
||||
public new ulong? updater { get; set; }
|
||||
|
||||
public new User? updaterRelation { get; set; } = null!;
|
||||
|
||||
|
||||
public override AuditUser adaptToAudit()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user