diff --git a/src/Models/MongoIdentityUser.cs b/src/Models/MongoIdentityUser.cs index 4043eac..cf00272 100644 --- a/src/Models/MongoIdentityUser.cs +++ b/src/Models/MongoIdentityUser.cs @@ -69,51 +69,53 @@ namespace AspNetCore.Identity.MongoDbCore.Models } } - public class MongoIdentityUser : IdentityUser, IDocument, IClaimHolder + /// + /// A document representing an document. + /// + /// The type of the primary key. + public class MongoIdentityUser : IdentityUser, IDocument, IClaimHolder where TKey : IEquatable { + /// + /// The version of the schema do the document. + /// public int Version { get; set; } - + /// + /// The date and time at which this user was created, in UTC. + /// public DateTime CreatedOn { get; private set; } - public DateTime? LockoutEndDate { get; private set; } - public DateTime? DeletedOn { get; private set; } + /// + /// The claims this user has. + /// public List Claims { get; set; } + /// + /// The role Ids of the roles that this user has. + /// public List Roles { get; set; } + /// + /// The list of s that this user has. + /// public List Logins { get; set; } + /// + /// The list of s that this user has. + /// public List Tokens { get; set; } - private void InitializeFields() - { - Claims = new List(); - Logins = new List(); - Roles = new List(); - Tokens = new List(); - 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; - } - - } - + /// + /// The constructor for a , taking a username and an email address. + /// public MongoIdentityUser() { - CreatedOn = DateTime.UtcNow; SetVersion(1); InitializeFields(); } + /// + /// The constructor for a , taking a username and an email address. + /// + /// The name of the user. + /// The email address of the user. public MongoIdentityUser(string userName, string email) : this(userName) { if (email != null) @@ -122,118 +124,40 @@ namespace AspNetCore.Identity.MongoDbCore.Models } } + /// + /// The constructor for a , taking a username. + /// + /// The name of the user. public MongoIdentityUser(string userName) { UserName = userName ?? throw new ArgumentNullException(nameof(userName)); - CreatedOn = DateTime.UtcNow; - SetVersion(1); InitializeFields(); Roles = new List(); } - public virtual MongoIdentityUser SetId(TKey key) - { - Id = key; - return this; - } - + /// + /// Sets the version of the schema for the document. + /// + /// + /// public virtual MongoIdentityUser 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 GetUserRole(TKey roleId) - { - var foundRoleId = Roles.FirstOrDefault(e => e.Equals(roleId)); - if (!foundRoleId.Equals(default(TKey))) - { - return new IdentityUserRole - { - UserId = Id, - RoleId = foundRoleId - }; - } - return default(IdentityUserRole); - } - + /// + /// Removes a role. + /// + /// The Id of the role you want to remove. + /// True if the removal was successful. 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; } + + /// + /// Add a role to the user. + /// + /// The Id of the role you want to add. + /// True if the addition was successful. 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) + /// + /// Adds a user login to the user. + /// + /// The you want to add. + /// True if the addition was successful. + 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) + /// + /// Checks if the user has the given . + /// + /// The we are looking for. + /// True if the user has the given . + 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) + /// + /// Removes a from the user. + /// + /// + public virtual bool RemoveLogin(UserLoginInfo userLoginInfo) { - if (mongoUserLogin == null) + if (userLoginInfo == null) { - throw new ArgumentNullException(nameof(mongoUserLogin)); + throw new ArgumentNullException(nameof(userLoginInfo)); } - - Logins.Remove(mongoUserLogin); + var loginToremove = Logins.FirstOrDefault(e => e.LoginProvider == userLoginInfo.LoginProvider && e.ProviderKey == e.ProviderKey); + if (loginToremove != null) + { + Logins.Remove(loginToremove); + return true; + } + return false; } + /// + /// + /// + /// + /// + /// public virtual IdentityUserLogin GetUserLogin(string loginProvider, string providerKey) { @@ -307,12 +266,11 @@ namespace AspNetCore.Identity.MongoDbCore.Models /// - /// Replaces a claim on a claim holder, implementing . + /// Sets the token to a new value. /// - /// The object holding claims. - /// The claim you want to replace. - /// The new claim you want to set. - /// Returns true if the claim was replaced. + /// The token you want to set you want to set. + /// The value you want to set the token to. + /// Returns true if the token was successfully set. public bool SetToken(IdentityUserToken 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; } + /// + /// Gets a token given the login provider and the name. + /// + /// The value for the login provider. + /// The name of the token. + /// An if found, or null. public IdentityUserToken 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); } + /// + /// Checks if a user has the given token. + /// + /// The token you are looking for. + /// True if the user has the given token public bool HasToken(IdentityUserToken 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 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; - } - + /// + /// Adds a token to the user. + /// + /// The type of the token. + /// The token you want to add. + /// True if the addition was successful. public bool AddUserToken(TUserToken token) where TUserToken : IdentityUserToken { if (HasToken(token)) @@ -383,9 +338,16 @@ namespace AspNetCore.Identity.MongoDbCore.Models return true; } + /// + /// Removes a token from the user. + /// + /// The type of the token. + /// The token you want to remove. + /// True if the removal was successful. public bool RemoveUserToken(TUserToken token) where TUserToken : IdentityUserToken { - 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(); + Logins = new List(); + Roles = new List(); + Tokens = new List(); + 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; + } + + } } } \ No newline at end of file diff --git a/src/Models/Token.cs b/src/Models/Token.cs index 729981c..1ab2459 100644 --- a/src/Models/Token.cs +++ b/src/Models/Token.cs @@ -11,6 +11,7 @@ public string LoginProvider { get; set; } /// /// Gets or sets the name of the token. + /// public string Name { get; set; } /// /// Gets or sets the token value. diff --git a/src/MongoIdentityDbContext.cs b/src/MongoIdentityDbContext.cs deleted file mode 100644 index 21d5b0c..0000000 --- a/src/MongoIdentityDbContext.cs +++ /dev/null @@ -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 Users - { - get - { - return GetCollection(); - } - } - - public IMongoCollection Roles - { - get - { - return GetCollection(); - } - } - } - - public class MongoIdentityDbContext : MongoDbContext - where TUser : MongoIdentityUser - { - - public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName) - { - } - - public IMongoRepository MongoRepository { get; } - - public IMongoCollection Users - { - get - { - return GetCollection(); - } - } - - public IMongoCollection Roles - { - get - { - return GetCollection(); - } - } - } - - public class MongoIdentityDbContext : MongoDbContext - where TUser : MongoIdentityUser - where TRole : MongoIdentityRole - where TKey : IEquatable - { - - public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName) - { - } - - public IMongoCollection Users - { - get - { - return GetCollection(); - } - } - - public IMongoCollection Roles - { - get - { - return GetCollection(); - } - } - } -} diff --git a/src/MongoRoleStore.cs b/src/MongoRoleStore.cs index 81a5d6b..bfe9ed3 100644 --- a/src/MongoRoleStore.cs +++ b/src/MongoRoleStore.cs @@ -381,13 +381,15 @@ namespace AspNetCore.Identity.MongoDbCore /// public void Dispose() => _disposed = true; - /// - /// Get the claims associated with the specified as an asynchronous operation. - /// - /// The role whose claims should be retrieved. - /// The used to propagate notifications that the operation should be canceled. - /// A that contains the claims granted to a role. +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Get the claims associated with the specified as an asynchronous operation. + /// + /// The role whose claims should be retrieved. + /// The used to propagate notifications that the operation should be canceled. + /// A that contains the claims granted to a role. public async virtual Task> 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) diff --git a/src/MongoUserOnlyStore.cs b/src/MongoUserOnlyStore.cs index 6f0ef23..571d918 100644 --- a/src/MongoUserOnlyStore.cs +++ b/src/MongoUserOnlyStore.cs @@ -297,13 +297,15 @@ namespace AspNetCore.Identity.MongoDbCore return Task.FromResult(default(TUserLogin)); } - /// - /// Get the claims associated with the specified as an asynchronous operation. - /// - /// The user whose claims should be retrieved. - /// The used to propagate notifications that the operation should be canceled. - /// A that contains the claims granted to a user. +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Get the claims associated with the specified as an asynchronous operation. + /// + /// The user whose claims should be retrieved. + /// The used to propagate notifications that the operation should be canceled. + /// A that contains the claims granted to a user. public async override Task> 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,15 +437,17 @@ namespace AspNetCore.Identity.MongoDbCore return Task.FromResult(false); } - /// - /// Removes the given from the specified . - /// - /// The user to remove the login from. - /// The login to remove from the user. - /// The key provided by the to identify a user. - /// The used to propagate notifications that the operation should be canceled. - /// The that represents the asynchronous operation. +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Removes the given from the specified . + /// + /// The user to remove the login from. + /// The login to remove from the user. + /// The key provided by the to identify a user. + /// The used to propagate notifications that the operation should be canceled. + /// The that represents the asynchronous operation. 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,15 +463,17 @@ namespace AspNetCore.Identity.MongoDbCore } } - /// - /// Retrieves the associated logins for the specified . - /// - /// The user whose associated logins to retrieve. - /// The used to propagate notifications that the operation should be canceled. - /// - /// The for the asynchronous operation, containing a list of for the specified , if any. - /// +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Retrieves the associated logins for the specified . + /// + /// The user whose associated logins to retrieve. + /// The used to propagate notifications that the operation should be canceled. + /// + /// The for the asynchronous operation, containing a list of for the specified , if any. + /// public async override Task> 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(); diff --git a/src/MongoUserStore.cs b/src/MongoUserStore.cs index 2717203..c7125b5 100644 --- a/src/MongoUserStore.cs +++ b/src/MongoUserStore.cs @@ -475,13 +475,15 @@ namespace AspNetCore.Identity.MongoDbCore return false; } - /// - /// Get the claims associated with the specified as an asynchronous operation. - /// - /// The user whose claims should be retrieved. - /// The used to propagate notifications that the operation should be canceled. - /// A that contains the claims granted to a user. +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Get the claims associated with the specified as an asynchronous operation. + /// + /// The user whose claims should be retrieved. + /// The used to propagate notifications that the operation should be canceled. + /// A that contains the claims granted to a user. public async override Task> 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,15 +615,17 @@ namespace AspNetCore.Identity.MongoDbCore return Task.FromResult(false); } - /// - /// Removes the given from the specified . - /// - /// The user to remove the login from. - /// The login to remove from the user. - /// The key provided by the to identify a user. - /// The used to propagate notifications that the operation should be canceled. - /// The that represents the asynchronous operation. +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Removes the given from the specified . + /// + /// The user to remove the login from. + /// The login to remove from the user. + /// The key provided by the to identify a user. + /// The used to propagate notifications that the operation should be canceled. + /// The that represents the asynchronous operation. 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,15 +641,17 @@ namespace AspNetCore.Identity.MongoDbCore } } - /// - /// Retrieves the associated logins for the specified . - /// - /// The user whose associated logins to retrieve. - /// The used to propagate notifications that the operation should be canceled. - /// - /// The for the asynchronous operation, containing a list of for the specified , if any. - /// +#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone + /// + /// Retrieves the associated logins for the specified . + /// + /// The user whose associated logins to retrieve. + /// The used to propagate notifications that the operation should be canceled. + /// + /// The for the asynchronous operation, containing a list of for the specified , if any. + /// public async override Task> 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 Tokens { get; set; } - } /// /// Find a user token if it exists. ///