|
@@ -9,6 +9,9 @@ using System.Security.Claims;
|
|
|
|
|
|
namespace Electric.Domain.Entities.Identity
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// 用户
|
|
|
+ /// </summary>
|
|
|
public class EleUser : AuditedAggregateRoot<Guid>
|
|
|
{
|
|
|
/// <summary>
|
|
@@ -116,10 +119,22 @@ namespace Electric.Domain.Entities.Identity
|
|
|
/// </summary>
|
|
|
public List<EleUserToken> Tokens { get; protected set; }
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 无参构造函数
|
|
|
+ /// </summary>
|
|
|
protected EleUser()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 构造函数二
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <param name="userName">用户名</param>
|
|
|
+ /// <param name="email">邮箱</param>
|
|
|
+ /// <param name="fullName">全名</param>
|
|
|
+ /// <param name="remark">备注</param>
|
|
|
+
|
|
|
public EleUser(Guid id, string userName, string email = null, string fullName = null, string remark = null) : base(id)
|
|
|
{
|
|
|
Check.NotNull(userName, nameof(userName));
|
|
@@ -141,6 +156,10 @@ namespace Electric.Domain.Entities.Identity
|
|
|
Tokens = new List<EleUserToken>();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 添加角色
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="roleId"></param>
|
|
|
public void AddRole(Guid roleId)
|
|
|
{
|
|
|
Check.NotNull(roleId, nameof(roleId));
|
|
@@ -165,11 +184,19 @@ namespace Electric.Domain.Entities.Identity
|
|
|
Roles.RemoveAll(r => r.RoleId == roleId);
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 移除角色
|
|
|
+ /// </summary>
|
|
|
public void RemoveAllRoles()
|
|
|
{
|
|
|
Roles = new List<EleUserRole>();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 是否拥有某个角色
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="roleId"></param>
|
|
|
+ /// <returns></returns>
|
|
|
public bool IsInRole(Guid roleId)
|
|
|
{
|
|
|
Check.NotNull(roleId, nameof(roleId));
|
|
@@ -177,6 +204,10 @@ namespace Electric.Domain.Entities.Identity
|
|
|
return Roles.Any(r => r.RoleId == roleId);
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 设置用户名
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="userName"></param>
|
|
|
public void SetUserName(string userName)
|
|
|
{
|
|
|
Check.NotNull(userName, nameof(userName));
|
|
@@ -185,12 +216,20 @@ namespace Electric.Domain.Entities.Identity
|
|
|
NormalizedUserName = userName.ToUpperInvariant();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 设置邮箱
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="email"></param>
|
|
|
public void SetEmail(string email)
|
|
|
{
|
|
|
Email = email;
|
|
|
NormalizedEmail = email?.ToUpperInvariant();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 设置密码
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="passwordHash"></param>
|
|
|
public void SetPasswordHash(string passwordHash)
|
|
|
{
|
|
|
Check.NotNull(passwordHash, nameof(passwordHash));
|
|
@@ -198,6 +237,11 @@ namespace Electric.Domain.Entities.Identity
|
|
|
PasswordHash = passwordHash;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 添加声明
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="claim"></param>
|
|
|
+
|
|
|
public void AddClaim(Claim claim)
|
|
|
{
|
|
|
Check.NotNull(claim, nameof(claim));
|
|
@@ -208,6 +252,10 @@ namespace Electric.Domain.Entities.Identity
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 移除声明
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="claim"></param>
|
|
|
public void RemoveClaim(Claim claim)
|
|
|
{
|
|
|
Check.NotNull(claim, nameof(claim));
|
|
@@ -215,6 +263,10 @@ namespace Electric.Domain.Entities.Identity
|
|
|
Claims.RemoveAll(x => x.UserId == Id && x.ClaimType == claim.Type);
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 添加登录信息
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="login"></param>
|
|
|
public void AddLogin(UserLoginInfo login)
|
|
|
{
|
|
|
Check.NotNull(login, nameof(login));
|
|
@@ -222,6 +274,12 @@ namespace Electric.Domain.Entities.Identity
|
|
|
Logins.Add(new EleUserLogin(login, Id));
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 一处登录信息
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="loginProvider"></param>
|
|
|
+ /// <param name="providerKey"></param>
|
|
|
+
|
|
|
public void RemoveLogin(string loginProvider, string providerKey)
|
|
|
{
|
|
|
Check.NotNull(loginProvider, nameof(loginProvider));
|
|
@@ -231,13 +289,23 @@ namespace Electric.Domain.Entities.Identity
|
|
|
userLogin.LoginProvider == loginProvider && userLogin.ProviderKey == providerKey);
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 检索令牌
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="loginProvider"></param>
|
|
|
+ /// <param name="name"></param>
|
|
|
+ /// <returns></returns>
|
|
|
public EleUserToken FindToken(string loginProvider, string name)
|
|
|
{
|
|
|
-#pragma warning disable CS8603 // 可能返回 null 引用。
|
|
|
return Tokens.FirstOrDefault(t => t.LoginProvider == loginProvider && t.Name == name);
|
|
|
-#pragma warning restore CS8603 // 可能返回 null 引用。
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 设置令牌
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="loginProvider"></param>
|
|
|
+ /// <param name="name"></param>
|
|
|
+ /// <param name="value"></param>
|
|
|
public void SetToken(string loginProvider, string name, string value)
|
|
|
{
|
|
|
var token = FindToken(loginProvider, name);
|
|
@@ -251,6 +319,11 @@ namespace Electric.Domain.Entities.Identity
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 移除令牌
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="loginProvider"></param>
|
|
|
+ /// <param name="name"></param>
|
|
|
public void RemoveToken(string loginProvider, string name)
|
|
|
{
|
|
|
Tokens.RemoveAll(t => t.LoginProvider == loginProvider && t.Name == name);
|