[ADD] Adding models, repositories. Implementing business logic.

This commit is contained in:
max 2024-12-22 02:15:34 +01:00
parent 7ebe1e1752
commit 44e64793b7
12 changed files with 147 additions and 41 deletions

View File

@ -1,7 +1,7 @@
namespace DotBased.AspNet.Authority.Attributes; namespace DotBased.AspNet.Authority.Attributes;
/// <summary> /// <summary>
/// Indicates that the property should be protected. /// Indicates to protect the property before saving to the repository.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public class ProtectAttribute : Attribute public class ProtectAttribute : Attribute

View File

@ -19,7 +19,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Authentication\" /> <Folder Include="Authentication\" />
<Folder Include="Models\Security\" /> <Folder Include="Models\Security\" />
<Folder Include="Repositories\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,7 @@
namespace DotBased.AspNet.Authority.Interfaces;
public interface ISecurityVersionRepository<in TRepositoryObject>
{
public Task<long> GetSecurityVersionAsync(TRepositoryObject obj);
}

View File

@ -1,6 +1,8 @@
namespace DotBased.AspNet.Authority.Interfaces; namespace DotBased.AspNet.Authority.Interfaces;
public interface IUserRepository public interface IUserRepository<TUser, TId> : IVersionRepository<TUser>, ISecurityVersionRepository<TUser> where TUser : class where TId : IEquatable<TId>
{ {
public Task<TUser?> GetUserByIdAsync(TId id);
public Task<TId> GetUserIdAsync(TUser user);
} }

View File

@ -0,0 +1,6 @@
namespace DotBased.AspNet.Authority.Interfaces;
public interface IVersionRepository<in TRepositoryObject>
{
public Task<long> GetVersionAsync(TRepositoryObject obj);
}

View File

@ -0,0 +1,25 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityAttribute
{
public AuthorityAttribute(string attributeKey, string bound) : this()
{
AttributeKey = attributeKey;
BoundId = bound;
}
public AuthorityAttribute()
{
}
public string AttributeKey { get; set; } // ClaimType/Authority.attribute.enabled
public string BoundId { get; set; } // Bound to User, Group, Role id
public string? AttributeValue { get; set; }
public string? Type { get; set; } // AspNet.Claim.Role/Property/Data.JSON, Data.Raw, Data.Base64 etc.
public long Version { get; set; }
}

View File

@ -0,0 +1,26 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityGroup : AuthorityGroup<Guid>
{
public AuthorityGroup(string name) : this()
{
Name = name;
}
public AuthorityGroup()
{
Id = Guid.NewGuid();
CreatedDate = DateTime.Now;
}
}
public abstract class AuthorityGroup<TKey> where TKey : IEquatable<TKey>
{
public TKey Id { get; set; }
public string? Name { get; set; }
public long Version { get; set; }
public DateTime CreatedDate { get; set; }
}

View File

@ -0,0 +1,28 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityRole : AuthorityRole<Guid>
{
public AuthorityRole(string name) : this()
{
Name = name;
}
public AuthorityRole()
{
Id = Guid.NewGuid();
CreatedDate = DateTime.Now;
}
}
public abstract class AuthorityRole<TKey> where TKey : IEquatable<TKey>
{
public TKey Id { get; set; }
public string? Name { get; set; }
public long Version { get; set; }
public DateTime CreatedDate { get; set; }
public override string ToString() => Name ?? string.Empty;
}

View File

@ -1,10 +1,52 @@
using DotBased.AspNet.Authority.Attributes;
namespace DotBased.AspNet.Authority.Models.Authority; namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityUser : AuthorityUserBase<Guid> public class AuthorityUser : AuthorityUser<Guid>
{ {
public AuthorityUser(string userName) : this()
{
UserName = userName;
}
public AuthorityUser() public AuthorityUser()
{ {
Id = Guid.NewGuid(); Id = Guid.NewGuid();
CreatedDate = DateTime.Now; CreatedDate = DateTime.Now;
} }
} }
public abstract class AuthorityUser<TKey> where TKey : IEquatable<TKey>
{
public TKey Id { get; set; }
public bool Enabled { get; set; }
public bool Locked { get; set; }
public DateTime LockedDate { get; set; }
public string? UserName { get; set; }
public string? PasswordHash { get; set; }
public DateTime CreatedDate { get; set; }
public bool TwoFactorEnabled { get; set; }
public long Version { get; set; }
public long SecurityVersion { get; set; }
[Protect]
public string? EmailAddress { get; set; }
public bool EmailConfirmed { get; set; }
[Protect]
public string? PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public override string ToString() => UserName ?? EmailAddress ?? string.Empty;
}

View File

@ -1,35 +0,0 @@
using DotBased.AspNet.Authority.Attributes;
namespace DotBased.AspNet.Authority.Models.Authority;
public abstract class AuthorityUserBase<TKey> where TKey : IEquatable<TKey>
{
public TKey Id { get; set; }
public bool Enabled { get; set; }
public bool Locked { get; set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
public DateTime CreatedDate { get; set; }
public bool TwoFactorEnabled { get; set; }
public string ConcurrencyStamp { get; set; }
public string SecurityStamp { get; set; }
[Protect]
public string EmailAddress { get; set; }
public bool EmailConfirmed { get; set; }
[Protect]
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
}

View File

@ -0,0 +1,6 @@
namespace DotBased.AspNet.Authority.Repositories;
public class AuthorityRepository // Inherit the repository interfaces?
{
}

View File

@ -2,5 +2,5 @@ namespace DotBased.AspNet.Authority.Services;
public class AuthorityService public class AuthorityService
{ {
public long GenerateVersion() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
} }