files restored
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
namespace WatchIt.WebAPI.Services.Utility.User;
|
||||||
|
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
ClaimsPrincipal GetRawUser();
|
||||||
|
string? GetRawToken();
|
||||||
|
UserValidator GetValidator();
|
||||||
|
Guid GetJti();
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
|
using WatchIt.Database;
|
||||||
|
|
||||||
|
namespace WatchIt.WebAPI.Services.Utility.User;
|
||||||
|
|
||||||
|
public class UserValidator
|
||||||
|
{
|
||||||
|
#region FIELDS
|
||||||
|
|
||||||
|
protected readonly DatabaseContext _database;
|
||||||
|
protected readonly ClaimsPrincipal _claimsPrincipal;
|
||||||
|
protected readonly List<string> _validationErrors;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region PROPERTIES
|
||||||
|
|
||||||
|
public bool IsValid { get; protected set; }
|
||||||
|
public IEnumerable<string> ValidationErrors => _validationErrors;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region CONSTRUCTORS
|
||||||
|
|
||||||
|
internal UserValidator(DatabaseContext database, ClaimsPrincipal claimsPrincipal)
|
||||||
|
{
|
||||||
|
_database = database;
|
||||||
|
_claimsPrincipal = claimsPrincipal;
|
||||||
|
_validationErrors = new List<string>();
|
||||||
|
|
||||||
|
IsValid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region PUBLIC METHODS
|
||||||
|
|
||||||
|
public UserValidator MustBeAdmin()
|
||||||
|
{
|
||||||
|
Claim adminClaim = _claimsPrincipal.FindFirst(x => x.Type == "admin")!;
|
||||||
|
if (adminClaim.Value == bool.FalseString)
|
||||||
|
{
|
||||||
|
IsValid = false;
|
||||||
|
_validationErrors.Add("User is not admin");
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user