Software_Engineering_Field_.../SiteManagementSystem(Softwa.../Entity/User.cs

61 lines
1.6 KiB
C#
Raw Normal View History

2024-11-03 11:21:47 +08:00
using SiteManagementSystem_SoftwareEngineering_.Model;
using SiteManagementSystem_SoftwareEngineering_.Service;
using System.ComponentModel.DataAnnotations;
2024-10-30 17:52:22 +08:00
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Claims;
namespace SiteManagementSystem_SoftwareEngineering_.Entity
{
public class User : ICloneable
{
public User() { }
public User(UserModel model)
{
Name = model.Name;
RoleName = model.RoleName;
Secret = model.Secret;
}
public User(User user)
{
Id = user.Id;
RoleName = user.RoleName;
Role = user.Role;
Name = user.Name;
Secret = user.Secret;
HashSecret = user.HashSecret;
}
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
[NotMapped]
public string RoleName { get; set; } = null!;
public IdentityPolicyNames Role
{
get
{
if (Enum.TryParse<IdentityPolicyNames>(RoleName, out var result))
return result;
else
return IdentityPolicyNames.NotSupport;
}
set => RoleName = Enum.GetName(value)!;
}
[StringLength(50)]
public string Name { get; set; } = null!;
[NotMapped]
public string Secret { get; set; } = null!;
public string HashSecret { get; set; } = null!;
public object Clone() => new User(this);
public IEnumerable<Claim> GetUserClaims() =>
[new Claim(nameof(Name), Name), new Claim(ClaimTypes.Role, RoleName)];
}
}