adding more comments and deleting unused methods.

This commit is contained in:
alexandre-spieser
2017-10-29 18:15:09 +00:00
parent d99ce293f0
commit 9bbf5c577e
6 changed files with 210 additions and 303 deletions
+145 -160
View File
@@ -69,51 +69,53 @@ namespace AspNetCore.Identity.MongoDbCore.Models
}
}
/// <summary>
/// A document representing an <see cref="IdentityUser{TKey}"/> document.
/// </summary>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
public class MongoIdentityUser<TKey> : IdentityUser<TKey>, IDocument<TKey>, IClaimHolder
where TKey : IEquatable<TKey>
{
/// <summary>
/// The version of the schema do the <see cref="MongoIdentityUser{TKey}"/> document.
/// </summary>
public int Version { get; set; }
/// <summary>
/// The date and time at which this user was created, in UTC.
/// </summary>
public DateTime CreatedOn { get; private set; }
public DateTime? LockoutEndDate { get; private set; }
public DateTime? DeletedOn { get; private set; }
/// <summary>
/// The claims this user has.
/// </summary>
public List<MongoClaim> Claims { get; set; }
/// <summary>
/// The role Ids of the roles that this user has.
/// </summary>
public List<TKey> Roles { get; set; }
/// <summary>
/// The list of <see cref="UserLoginInfo"/>s that this user has.
/// </summary>
public List<UserLoginInfo> Logins { get; set; }
/// <summary>
/// The list of <see cref="Token"/>s that this user has.
/// </summary>
public List<Token> Tokens { get; set; }
private void InitializeFields()
{
Claims = new List<MongoClaim>();
Logins = new List<UserLoginInfo>();
Roles = new List<TKey>();
Tokens = new List<Token>();
Guid guidValue = Guid.NewGuid();
var idTypeName = typeof(TKey).Name;
switch (idTypeName)
{
case "Guid":
Id = (TKey)(object)guidValue;
break;
case "Int32":
Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue);
break;
case "String":
Id = (TKey)(object)guidValue.ToString();
break;
}
}
/// <summary>
/// The constructor for a <see cref="MongoIdentityUser{TKey}"/>, taking a username and an email address.
/// </summary>
public MongoIdentityUser()
{
CreatedOn = DateTime.UtcNow;
SetVersion(1);
InitializeFields();
}
/// <summary>
/// The constructor for a <see cref="MongoIdentityUser{TKey}"/>, taking a username and an email address.
/// </summary>
/// <param name="userName">The name of the user.</param>
/// <param name="email">The email address of the user.</param>
public MongoIdentityUser(string userName, string email) : this(userName)
{
if (email != null)
@@ -122,118 +124,40 @@ namespace AspNetCore.Identity.MongoDbCore.Models
}
}
/// <summary>
/// The constructor for a <see cref="MongoIdentityUser{TKey}"/>, taking a username.
/// </summary>
/// <param name="userName">The name of the user.</param>
public MongoIdentityUser(string userName)
{
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
CreatedOn = DateTime.UtcNow;
SetVersion(1);
InitializeFields();
Roles = new List<TKey>();
}
public virtual MongoIdentityUser<TKey> SetId(TKey key)
{
Id = key;
return this;
}
/// <summary>
/// Sets the version of the schema for the <see cref="MongoIdentityUser{TKey}"/> document.
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
public virtual MongoIdentityUser<TKey> SetVersion(int version)
{
Version = 1;
return this;
}
public virtual void EnableTwoFactorAuthentication()
{
TwoFactorEnabled = true;
}
public virtual void DisableTwoFactorAuthentication()
{
TwoFactorEnabled = false;
}
public virtual void EnableLockout()
{
LockoutEnabled = true;
}
public virtual void DisableLockout()
{
LockoutEnabled = false;
}
public virtual void SetEmail(string email)
{
Email = email ?? throw new ArgumentNullException(nameof(email));
}
public virtual void SetNormalizedUserName(string normalizedUserName)
{
NormalizedUserName = normalizedUserName ?? throw new ArgumentNullException(nameof(normalizedUserName));
}
public virtual void SetPhoneNumber(string phoneNumber)
{
PhoneNumber = phoneNumber;
}
public virtual void SetPasswordHash(string passwordHash)
{
PasswordHash = passwordHash;
}
public virtual void SetSecurityStamp(string securityStamp)
{
SecurityStamp = securityStamp;
}
public virtual void SetAccessFailedCount(int accessFailedCount)
{
AccessFailedCount = accessFailedCount;
}
public virtual void ResetAccessFailedCount()
{
AccessFailedCount = 0;
}
public virtual void LockUntil(DateTime lockoutEndDate)
{
LockoutEndDate = lockoutEndDate;
}
public void Delete()
{
if (DeletedOn != null)
{
throw new InvalidOperationException($"User '{Id}' has already been deleted.");
}
DeletedOn = DateTime.UtcNow;
}
#region Role Management
public virtual IdentityUserRole<TKey> GetUserRole(TKey roleId)
{
var foundRoleId = Roles.FirstOrDefault(e => e.Equals(roleId));
if (!foundRoleId.Equals(default(TKey)))
{
return new IdentityUserRole<TKey>
{
UserId = Id,
RoleId = foundRoleId
};
}
return default(IdentityUserRole<TKey>);
}
/// <summary>
/// Removes a role.
/// </summary>
/// <param name="roleId">The Id of the role you want to remove.</param>
/// <returns>True if the removal was successful.</returns>
public virtual bool RemoveRole(TKey roleId)
{
var roleClaim = Roles.FirstOrDefault(e => e.Equals(roleId));
if (!roleClaim.Equals(default(TKey)))
if (roleClaim != null && !roleClaim.Equals(default(TKey)))
{
Roles.Remove(roleId);
return true;
@@ -241,8 +165,18 @@ namespace AspNetCore.Identity.MongoDbCore.Models
return false;
}
/// <summary>
/// Add a role to the user.
/// </summary>
/// <param name="roleId">The Id of the role you want to add.</param>
/// <returns>True if the addition was successful.</returns>
public virtual bool AddRole(TKey roleId)
{
if(roleId == null || roleId.Equals(default(TKey)))
{
throw new ArgumentNullException(nameof(roleId));
}
if (!Roles.Contains(roleId))
{
Roles.Add(roleId);
@@ -255,35 +189,60 @@ namespace AspNetCore.Identity.MongoDbCore.Models
#region Login Management
public virtual bool AddLogin(UserLoginInfo mongoUserLogin)
/// <summary>
/// Adds a user login to the user.
/// </summary>
/// <param name="userLoginInfo">The <see cref="UserLoginInfo"/> you want to add.</param>
/// <returns>True if the addition was successful.</returns>
public virtual bool AddLogin(UserLoginInfo userLoginInfo)
{
if (mongoUserLogin == null)
if (userLoginInfo == null)
{
throw new ArgumentNullException(nameof(mongoUserLogin));
throw new ArgumentNullException(nameof(userLoginInfo));
}
if (HasLogin(mongoUserLogin))
if (HasLogin(userLoginInfo))
{
return false;
}
Logins.Add(mongoUserLogin);
Logins.Add(userLoginInfo);
return true;
}
public virtual bool HasLogin(UserLoginInfo login)
/// <summary>
/// Checks if the user has the given <see cref="UserLoginInfo"/>.
/// </summary>
/// <param name="userLoginInfo">The <see cref="UserLoginInfo"/> we are looking for.</param>
/// <returns>True if the user has the given <see cref="UserLoginInfo"/>.</returns>
public virtual bool HasLogin(UserLoginInfo userLoginInfo)
{
return Logins.Any(e => e.LoginProvider == login.LoginProvider && e.ProviderKey == e.ProviderKey);
return Logins.Any(e => e.LoginProvider == userLoginInfo.LoginProvider && e.ProviderKey == e.ProviderKey);
}
public virtual void RemoveLogin(UserLoginInfo mongoUserLogin)
/// <summary>
/// Removes a <see cref="UserLoginInfo"/> from the user.
/// </summary>
/// <param name="userLoginInfo"></param>
public virtual bool RemoveLogin(UserLoginInfo userLoginInfo)
{
if (mongoUserLogin == null)
if (userLoginInfo == null)
{
throw new ArgumentNullException(nameof(mongoUserLogin));
}
Logins.Remove(mongoUserLogin);
throw new ArgumentNullException(nameof(userLoginInfo));
}
var loginToremove = Logins.FirstOrDefault(e => e.LoginProvider == userLoginInfo.LoginProvider && e.ProviderKey == e.ProviderKey);
if (loginToremove != null)
{
Logins.Remove(loginToremove);
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="loginProvider"></param>
/// <param name="providerKey"></param>
/// <returns></returns>
public virtual IdentityUserLogin<TKey> GetUserLogin(string loginProvider, string providerKey)
{
@@ -307,12 +266,11 @@ namespace AspNetCore.Identity.MongoDbCore.Models
/// <summary>
/// Replaces a claim on a claim holder, implementing <see cref="IClaimHolder"/>.
/// Sets the token to a new value.
/// </summary>
/// <param name="claimHolder">The object holding claims.</param>
/// <param name="claim">The claim you want to replace.</param>
/// <param name="newClaim">The new claim you want to set.</param>
/// <returns>Returns true if the claim was replaced.</returns>
/// <param name="tokenToset">The token you want to set you want to set.</param>
/// <param name="value">The value you want to set the token to.</param>
/// <returns>Returns true if the token was successfully set.</returns>
public bool SetToken(IdentityUserToken<TKey> tokenToset, string value)
{
var token = Tokens.FirstOrDefault(e => e.LoginProvider == tokenToset.LoginProvider && e.Name == tokenToset.Name);
@@ -324,6 +282,12 @@ namespace AspNetCore.Identity.MongoDbCore.Models
return false;
}
/// <summary>
/// Gets a token given the login provider and the name.
/// </summary>
/// <param name="loginProvider">The value for the login provider.</param>
/// <param name="name">The name of the token.</param>
/// <returns>An <see cref="IdentityUser{TKey}"/> if found, or null.</returns>
public IdentityUserToken<TKey> GetToken(string loginProvider, string name)
{
var token = Tokens.FirstOrDefault(e => e.LoginProvider == loginProvider && e.Name == name);
@@ -340,6 +304,11 @@ namespace AspNetCore.Identity.MongoDbCore.Models
return default(IdentityUserToken<TKey>);
}
/// <summary>
/// Checks if a user has the given token.
/// </summary>
/// <param name="token">The token you are looking for.</param>
/// <returns>True if the user has the given token</returns>
public bool HasToken(IdentityUserToken<TKey> token)
{
return Tokens.Any(e => e.LoginProvider == token.LoginProvider
@@ -347,26 +316,12 @@ namespace AspNetCore.Identity.MongoDbCore.Models
&& e.Value == token.Value);
}
public bool AddOrSet(IdentityUserToken<TKey> token)
{
var exists = GetToken(token.LoginProvider, token.Name);
if (exists != null && exists.Value != token.Value)
{
return SetToken(exists, token.Value);
}
if (exists == null)
{
Tokens.Add(new Token
{
LoginProvider = token.LoginProvider,
Name = token.Name,
Value = token.Value
});
return true;
}
return false;
}
/// <summary>
/// Adds a token to the user.
/// </summary>
/// <typeparam name="TUserToken">The type of the token.</typeparam>
/// <param name="token">The token you want to add.</param>
/// <returns>True if the addition was successful.</returns>
public bool AddUserToken<TUserToken>(TUserToken token) where TUserToken : IdentityUserToken<TKey>
{
if (HasToken(token))
@@ -383,9 +338,16 @@ namespace AspNetCore.Identity.MongoDbCore.Models
return true;
}
/// <summary>
/// Removes a token from the user.
/// </summary>
/// <typeparam name="TUserToken">The type of the token.</typeparam>
/// <param name="token">The token you want to remove.</param>
/// <returns>True if the removal was successful.</returns>
public bool RemoveUserToken<TUserToken>(TUserToken token) where TUserToken : IdentityUserToken<TKey>
{
var exists = Tokens.FirstOrDefault(e => e.LoginProvider == token.LoginProvider && e.Name == token.Name);
var exists = Tokens.FirstOrDefault(e => e.LoginProvider == token.LoginProvider
&& e.Name == token.Name);
if (exists == null)
{
return false;
@@ -396,7 +358,30 @@ namespace AspNetCore.Identity.MongoDbCore.Models
#endregion Token Management
private void InitializeFields()
{
CreatedOn = DateTime.UtcNow;
Claims = new List<MongoClaim>();
Logins = new List<UserLoginInfo>();
Roles = new List<TKey>();
Tokens = new List<Token>();
Guid guidValue = Guid.NewGuid();
var idTypeName = typeof(TKey).Name;
switch (idTypeName)
{
case "Guid":
Id = (TKey)(object)guidValue;
break;
case "Int32":
Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue);
break;
case "String":
Id = (TKey)(object)guidValue.ToString();
break;
}
}
}
}
+1
View File
@@ -11,6 +11,7 @@
public string LoginProvider { get; set; }
/// <summary>
/// Gets or sets the name of the token.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the token value.
-89
View File
@@ -1,89 +0,0 @@
using AspNetCore.Identity.MongoDbCore.Infrastructure;
using AspNetCore.Identity.MongoDbCore.Models;
using MongoDB.Driver;
using MongoDbGenericRepository;
using System;
namespace AspNetCore.Identity.MongoDbCore
{
public class MongoIdentityDbContext : MongoDbContext
{
public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName)
{
}
public IMongoRepository MongoRepository { get; }
public IMongoCollection<MongoIdentityUser> Users
{
get
{
return GetCollection<MongoIdentityUser>();
}
}
public IMongoCollection<MongoIdentityRole> Roles
{
get
{
return GetCollection<MongoIdentityRole>();
}
}
}
public class MongoIdentityDbContext<TUser> : MongoDbContext
where TUser : MongoIdentityUser
{
public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName)
{
}
public IMongoRepository MongoRepository { get; }
public IMongoCollection<TUser> Users
{
get
{
return GetCollection<TUser>();
}
}
public IMongoCollection<MongoIdentityRole> Roles
{
get
{
return GetCollection<MongoIdentityRole>();
}
}
}
public class MongoIdentityDbContext<TUser, TRole, TKey> : MongoDbContext
where TUser : MongoIdentityUser<TKey>
where TRole : MongoIdentityRole<TKey>
where TKey : IEquatable<TKey>
{
public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName)
{
}
public IMongoCollection<TUser> Users
{
get
{
return GetCollection<TUser>();
}
}
public IMongoCollection<TRole> Roles
{
get
{
return GetCollection<TRole>();
}
}
}
}
+2
View File
@@ -381,6 +381,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// </summary>
public void Dispose() => _disposed = true;
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Get the claims associated with the specified <paramref name="role"/> as an asynchronous operation.
/// </summary>
@@ -388,6 +389,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a role.</returns>
public async virtual Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
{
ThrowIfDisposed();
if (role == null)
+6
View File
@@ -297,6 +297,7 @@ namespace AspNetCore.Identity.MongoDbCore
return Task.FromResult(default(TUserLogin));
}
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation.
/// </summary>
@@ -304,6 +305,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns>
public async override Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
{
ThrowIfDisposed();
if (user == null)
@@ -435,6 +437,7 @@ namespace AspNetCore.Identity.MongoDbCore
return Task.FromResult(false);
}
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Removes the <paramref name="loginProvider"/> given from the specified <paramref name="user"/>.
/// </summary>
@@ -444,6 +447,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
public override async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
@@ -459,6 +463,7 @@ namespace AspNetCore.Identity.MongoDbCore
}
}
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Retrieves the associated logins for the specified <param ref="user"/>.
/// </summary>
@@ -468,6 +473,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// The <see cref="Task"/> for the asynchronous operation, containing a list of <see cref="UserLoginInfo"/> for the specified <paramref name="user"/>, if any.
/// </returns>
public async override Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
+6 -4
View File
@@ -475,6 +475,7 @@ namespace AspNetCore.Identity.MongoDbCore
return false;
}
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation.
/// </summary>
@@ -482,6 +483,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns>
public async override Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
{
ThrowIfDisposed();
if (user == null)
@@ -613,6 +615,7 @@ namespace AspNetCore.Identity.MongoDbCore
return Task.FromResult(false);
}
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Removes the <paramref name="loginProvider"/> given from the specified <paramref name="user"/>.
/// </summary>
@@ -622,6 +625,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
public override async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
@@ -637,6 +641,7 @@ namespace AspNetCore.Identity.MongoDbCore
}
}
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
/// <summary>
/// Retrieves the associated logins for the specified <param ref="user"/>.
/// </summary>
@@ -646,6 +651,7 @@ namespace AspNetCore.Identity.MongoDbCore
/// The <see cref="Task"/> for the asynchronous operation, containing a list of <see cref="UserLoginInfo"/> for the specified <paramref name="user"/>, if any.
/// </returns>
public async override Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
@@ -745,10 +751,6 @@ namespace AspNetCore.Identity.MongoDbCore
#region Token Management
public class TokenProjection
{
public List<Token> Tokens { get; set; }
}
/// <summary>
/// Find a user token if it exists.
/// </summary>