Skip to content

Commit

Permalink
fix typos; remove unused private methods; remove redunant qualifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
vova3211 committed Oct 6, 2024
1 parent 91b5068 commit 35f4865
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/AspNetCore.Identity.Mongo/Mongo/ObjectIdConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
Expand Down
16 changes: 8 additions & 8 deletions src/AspNetCore.Identity.Mongo/Stores/RoleStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using AspNetCore.Identity.Mongo.Mongo;
using Microsoft.AspNetCore.Identity;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -16,7 +15,8 @@ namespace AspNetCore.Identity.Mongo.Stores;
/// <summary>
/// Creates a new instance of a persistence store for roles.
/// </summary>
/// <typeparam name="TRole">The type of the class representing a role</typeparam>
/// <typeparam name="TRole">The type of the class representing a role.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a user/role.</typeparam>
public class RoleStore<TRole, TKey> :
IRoleClaimStore<TRole>,
IQueryableRoleStore<TRole>
Expand Down Expand Up @@ -183,14 +183,14 @@ public Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken can
/// <param name="normalizedName">The normalized name to set</param>
/// <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 Task SetNormalizedRoleNameAsync(TRole role, string normalizedRoleName, CancellationToken cancellationToken = default)
public Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();

if (role == null) throw new ArgumentNullException(nameof(role));

role.NormalizedName = normalizedRoleName;
role.NormalizedName = normalizedName;

return Task.CompletedTask;
}
Expand All @@ -201,12 +201,12 @@ public Task SetNormalizedRoleNameAsync(TRole role, string normalizedRoleName, Ca
/// <param name="id">The role ID to look for.</param>
/// <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 result of the look up.</returns>
public Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken = default)
public Task<TRole> FindByIdAsync(string id, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();

return _collection.FirstOrDefaultAsync(x => x.Id.Equals(ConvertIdFromString(roleId)), cancellationToken: cancellationToken);
return _collection.FirstOrDefaultAsync(x => x.Id.Equals(ConvertIdFromString(id)), cancellationToken: cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -309,7 +309,7 @@ public virtual TKey ConvertIdFromString(string id)
{
if (id == null)
{
return default(TKey);
return default;
}
return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id);
}
Expand All @@ -321,7 +321,7 @@ public virtual TKey ConvertIdFromString(string id)
/// <returns>An <see cref="string"/> representation of the provided <paramref name="id"/>.</returns>
public virtual string ConvertIdToString(TKey id)
{
if (object.Equals(id, default(TKey)))
if (Equals(id, default(TKey)))
{
return null;
}
Expand Down
44 changes: 13 additions & 31 deletions src/AspNetCore.Identity.Mongo/Stores/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using AspNetCore.Identity.Mongo.Mongo;
using Microsoft.AspNetCore.Identity;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -430,7 +428,7 @@ public async Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken can
if (user == null) throw new ArgumentNullException(nameof(user));

var dbUser = await ByIdAsync(user.Id, cancellationToken).ConfigureAwait(true);
return dbUser?.Claims?.Select(x => new Claim(x.ClaimType, x.ClaimValue))?.ToList() ?? new List<Claim>();
return dbUser?.Claims?.Select(x => new Claim(x.ClaimType, x.ClaimValue)).ToList() ?? new List<Claim>();
}

/// <summary>
Expand Down Expand Up @@ -615,7 +613,7 @@ public Task<int> GetAccessFailedCountAsync(TUser user, CancellationToken cancell
}

/// <summary>
/// Retrieves a flag indicating whether user lockout can enabled for the specified user.
/// Retrieves a flag indicating whether user lockout is enabled for the specified user.
/// </summary>
/// <param name="user">The user whose ability to be locked out should be returned.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
Expand All @@ -633,7 +631,7 @@ public Task<bool> GetLockoutEnabledAsync(TUser user, CancellationToken cancellat
}

/// <summary>
/// Records that a failed access has occurred, incrementing the failed access count.
/// Records that failed access has occurred, incrementing the failed access count.
/// </summary>
/// <param name="user">The user whose cancellation count should be incremented.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
Expand Down Expand Up @@ -806,7 +804,7 @@ public async Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationT

var dbUser = await ByIdAsync(user.Id, cancellationToken).ConfigureAwait(true);

return dbUser?.Logins?.Select(x => new UserLoginInfo(x.LoginProvider, x.ProviderKey, x.ProviderDisplayName))?.ToList() ?? new List<UserLoginInfo>();
return dbUser?.Logins?.Select(x => new UserLoginInfo(x.LoginProvider, x.ProviderKey, x.ProviderDisplayName)).ToList() ?? new List<UserLoginInfo>();
}

/// <summary>
Expand Down Expand Up @@ -946,7 +944,7 @@ public async Task AddToRoleAsync(TUser user, string normalizedRoleName, Cancella
ThrowIfDisposed();

if (user == null) throw new ArgumentNullException(nameof(user));
if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException("Value cannot be null or empty.", nameof(normalizedRoleName));
if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException(nameof(normalizedRoleName));

var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken);
if (roleEntity == null)
Expand All @@ -970,7 +968,7 @@ public async Task RemoveFromRoleAsync(TUser user, string normalizedRoleName, Can
ThrowIfDisposed();

if (user == null) throw new ArgumentNullException(nameof(user));
if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException("Value cannot be null or empty.", nameof(normalizedRoleName));
if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException(nameof(normalizedRoleName));

var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken);
if (roleEntity != null)
Expand Down Expand Up @@ -1119,7 +1117,7 @@ public async Task<bool> RedeemCodeAsync(TUser user, string code, CancellationTok
if (user == null) throw new ArgumentNullException(nameof(user));
if (code == null) throw new ArgumentNullException(nameof(code));

var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? "";
var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? string.Empty;
var splitCodes = mergedCodes.Split(';');
if (splitCodes.Contains(code))
{
Expand All @@ -1137,15 +1135,15 @@ public async Task<bool> RedeemCodeAsync(TUser user, string code, CancellationTok
/// </summary>
/// <param name="user">The user who owns the recovery code.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The number of valid recovery codes for the user..</returns>
/// <returns>The number of valid recovery codes for the user.</returns>
public async Task<int> CountCodesAsync(TUser user, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();

if (user == null) throw new ArgumentNullException(nameof(user));

var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? "";
var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? string.Empty;
if (mergedCodes.Length > 0)
{
return mergedCodes.Split(';').Length;
Expand Down Expand Up @@ -1221,8 +1219,9 @@ public virtual TKey ConvertIdFromString(string id)
{
if (id == null)
{
return default(TKey);
return default;
}

return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id);
}

Expand All @@ -1233,10 +1232,11 @@ public virtual TKey ConvertIdFromString(string id)
/// <returns>An <see cref="string"/> representation of the provided <paramref name="id"/>.</returns>
public virtual string ConvertIdToString(TKey id)
{
if (object.Equals(id, default(TKey)))
if (Equals(id, default(TKey)))
{
return null;
}

return id.ToString();
}

Expand All @@ -1258,24 +1258,6 @@ private async Task<IdentityUserToken<string>> FindTokenAsync(TUser user, string
return dbUser?.Tokens?.FirstOrDefault(x => x.LoginProvider == loginProvider && x.Name == name);
}

private async Task UpdateAsync<TFieldValue>(TUser user, Expression<Func<TUser, TFieldValue>> expression, TFieldValue value, CancellationToken cancellationToken = default)
{
if (user == null) throw new ArgumentNullException(nameof(user));

var updateDefinition = Builders<TUser>.Update.Set(expression, value);

await _userCollection.UpdateOneAsync(x => x.Id.Equals(user.Id), updateDefinition, cancellationToken: cancellationToken).ConfigureAwait(false);
}

private async Task AddAsync<TFieldValue>(TUser user, Expression<Func<TUser, IEnumerable<TFieldValue>>> expression, TFieldValue value, CancellationToken cancellationToken = default)
{
if (user == null) throw new ArgumentNullException(nameof(user));

var addDefinition = Builders<TUser>.Update.AddToSet(expression, value);

await _userCollection.UpdateOneAsync(x => x.Id.Equals(user.Id), addDefinition, cancellationToken: cancellationToken).ConfigureAwait(false);
}

private Task<TUser> ByIdAsync(TKey id, CancellationToken cancellationToken = default)
{
return _userCollection.FirstOrDefaultAsync(x => x.Id.Equals(id), cancellationToken);
Expand Down

0 comments on commit 35f4865

Please # to comment.