Move create/update to the provider.

pull/4979/head
Halil İbrahim Kalkan 5 years ago
parent e98084fdf6
commit cc94779107

@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.Identity.AspNetCore
{
@ -14,7 +13,7 @@ namespace Volo.Abp.Identity.AspNetCore
protected AbpIdentityAspNetCoreOptions AbpOptions { get; }
public AbpSignInManager(
UserManager<IdentityUser> userManager,
IdentityUserManager userManager,
IHttpContextAccessor contextAccessor,
IUserClaimsPrincipalFactory<IdentityUser> claimsFactory,
IOptions<IdentityOptions> optionsAccessor,
@ -51,15 +50,10 @@ namespace Volo.Abp.Identity.AspNetCore
if (user == null)
{
user = await externalLoginProvider.CreateUserAsync(userName);
//TODO: +TenantId, LoginProvider, Password, +NormalizeNames
//TODO: Set default roles
await UserManager.CreateAsync(user);
}
else
{
await externalLoginProvider.UpdateUserAsync(user);
//TODO: LoginProvider
await UserManager.UpdateAsync(user);
}
return await SignInOrTwoFactorAsync(user, isPersistent);

@ -32,5 +32,10 @@ namespace Volo.Abp.Identity
/// Default value: 256
/// </summary>
public static int MaxSecurityStampLength { get; set; } = 256;
/// <summary>
/// Default value: 16
/// </summary>
public static int MaxLoginProviderLength { get; set; } = 16;
}
}

@ -66,6 +66,8 @@ namespace Volo.Abp.Identity
[DisableAuditing]
public virtual string SecurityStamp { get; protected internal set; }
public virtual string LoginProvider { get; protected set; }
/// <summary>
/// Gets or sets a telephone number for the user.
/// </summary>
@ -133,7 +135,11 @@ namespace Volo.Abp.Identity
{
}
public IdentityUser(Guid id, [NotNull] string userName, [NotNull] string email, Guid? tenantId = null)
public IdentityUser(
Guid id,
[NotNull] string userName,
[NotNull] string email,
Guid? tenantId = null)
{
Check.NotNull(userName, nameof(userName));
Check.NotNull(email, nameof(email));
@ -254,7 +260,8 @@ namespace Volo.Abp.Identity
Check.NotNull(loginProvider, nameof(loginProvider));
Check.NotNull(providerKey, nameof(providerKey));
Logins.RemoveAll(userLogin => userLogin.LoginProvider == loginProvider && userLogin.ProviderKey == providerKey);
Logins.RemoveAll(userLogin =>
userLogin.LoginProvider == loginProvider && userLogin.ProviderKey == providerKey);
}
[CanBeNull]
@ -316,9 +323,43 @@ namespace Volo.Abp.Identity
);
}
public virtual void SetLoginProvider([CanBeNull] string loginProvider)
{
LoginProvider = Check.Length(loginProvider, nameof(loginProvider),
IdentityUserConsts.MaxLoginProviderLength);
}
/// <summary>
/// Use <see cref="IdentityUserManager.ConfirmEmailAsync"/> for regular email confirmation.
/// Using this skips the confirmation process and directly sets the <see cref="EmailConfirmed"/>.
/// </summary>
public virtual void SetEmailConfirmed(bool confirmed)
{
EmailConfirmed = confirmed;
}
public virtual void SetPhoneNumberConfirmed(bool confirmed)
{
PhoneNumberConfirmed = confirmed;
}
public override string ToString()
{
return $"{base.ToString()}, UserName = {UserName}";
}
/// <summary>
/// Normally use <see cref="IdentityUserManager.ChangePhoneNumberAsync"/> to change the phone number
/// in the application code.
/// This method is to directly set it with a confirmation information.
/// </summary>
/// <param name="phoneNumber"></param>
/// <param name="confirmed"></param>
/// <exception cref="NotImplementedException"></exception>
public void SetPhoneNumber(string phoneNumber, bool confirmed)
{
PhoneNumber = phoneNumber;
PhoneNumberConfirmed = !phoneNumber.IsNullOrWhiteSpace() && confirmed;
}
}
}

@ -291,7 +291,7 @@ namespace Volo.Abp.Identity
/// </summary>
/// <param name="user">The user to retrieve the password hash 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}"/> containing a flag indicating if the specified user has a password. If the
/// <returns>A <see cref="Task{TResult}"/> containing a flag indicating if the specified user has a password. If the
/// user has a password the returned value with be true, otherwise it will be false.</returns>
public virtual Task<bool> HasPasswordAsync([NotNull] IdentityUser user, CancellationToken cancellationToken = default)
{
@ -326,7 +326,7 @@ namespace Volo.Abp.Identity
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Role {0} does not exist!", normalizedRoleName));
}
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken);
user.AddRole(role.Id);
@ -353,7 +353,7 @@ namespace Volo.Abp.Identity
}
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken);
user.RemoveRole(role.Id);
}
@ -384,15 +384,15 @@ namespace Volo.Abp.Identity
/// <param name="user">The user whose role membership should be checked.</param>
/// <param name="normalizedRoleName">The role to check membership of</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}"/> containing a flag indicating if the specified user is a member of the given group. If the
/// <returns>A <see cref="Task{TResult}"/> containing a flag indicating if the specified user is a member of the given group. If the
/// user is a member of the group the returned value with be true, otherwise it will be false.</returns>
public virtual async Task<bool> IsInRoleAsync(
[NotNull] IdentityUser user,
[NotNull] IdentityUser user,
[NotNull] string normalizedRoleName,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
Check.NotNull(user, nameof(user));
Check.NotNullOrWhiteSpace(normalizedRoleName, nameof(normalizedRoleName));
@ -589,7 +589,7 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user));
user.EmailConfirmed = confirmed;
user.SetEmailConfirmed(confirmed);
return Task.CompletedTask;
}
@ -864,7 +864,7 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user));
user.PhoneNumberConfirmed = confirmed;
user.SetPhoneNumberConfirmed(confirmed);
return Task.CompletedTask;
}
@ -928,7 +928,7 @@ namespace Volo.Abp.Identity
/// <param name="user">The user whose two factor authentication enabled status should be 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, containing a flag indicating whether the specified
/// The <see cref="Task"/> that represents the asynchronous operation, containing a flag indicating whether the specified
/// <paramref name="user"/> has two factor authentication enabled or not.
/// </returns>
public virtual Task<bool> GetTwoFactorEnabledAsync([NotNull] IdentityUser user, CancellationToken cancellationToken = default)
@ -946,7 +946,7 @@ namespace Volo.Abp.Identity
/// <param name="claim">The claim whose users should be retrieved.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Task"/> contains a list of users, if any, that contain the specified claim.
/// The <see cref="Task"/> contains a list of users, if any, that contain the specified claim.
/// </returns>
public virtual async Task<IList<IdentityUser>> GetUsersForClaimAsync([NotNull] Claim claim, CancellationToken cancellationToken = default)
{
@ -963,7 +963,7 @@ namespace Volo.Abp.Identity
/// <param name="normalizedRoleName">The role whose users should be retrieved.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Task"/> contains a list of users, if any, that are in the specified role.
/// The <see cref="Task"/> contains a list of users, if any, that are in the specified role.
/// </returns>
public virtual async Task<IList<IdentityUser>> GetUsersInRoleAsync([NotNull] string normalizedRoleName, CancellationToken cancellationToken = default)
{

@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
@ -10,10 +11,14 @@ namespace Volo.Abp.Identity.AspNetCore
public const string Name = "Fake";
private readonly ICurrentTenant _currentTenant;
private readonly IdentityUserManager _userManager;
private RandomPasswordGenerator _randomPasswordGenerator;
public FakeExternalLoginProvider(ICurrentTenant currentTenant)
public FakeExternalLoginProvider(ICurrentTenant currentTenant, IdentityUserManager userManager, RandomPasswordGenerator randomPasswordGenerator)
{
_currentTenant = currentTenant;
_userManager = userManager;
_randomPasswordGenerator = randomPasswordGenerator;
}
public override Task<bool> TryAuthenticateAsync(string userName, string plainPassword)
@ -23,16 +28,28 @@ namespace Volo.Abp.Identity.AspNetCore
);
}
public override Task<IdentityUser> CreateUserAsync(string userName)
public override async Task<IdentityUser> CreateUserAsync(string userName)
{
return Task.FromResult(
new IdentityUser(
Guid.NewGuid(),
userName,
"test@abp.io",
tenantId: _currentTenant.Id
)
var user = new IdentityUser(
Guid.NewGuid(),
userName,
"test@abp.io",
tenantId: _currentTenant.Id //Setting TenantId is responsibility of the provider!
);
user.SetLoginProvider(Name);
user.SetEmailConfirmed(true); //Setting this is responsibility of the provider!
user.SetPhoneNumber("123123", true);
(await _userManager.CreateAsync(user, await _randomPasswordGenerator.CreateAsync())).CheckErrors();
(await _userManager.AddDefaultRolesAsync(user)).CheckErrors();
return user;
}
public override async Task UpdateUserAsync(IdentityUser user)
{
(await _userManager.SetEmailAsync(user, "test-updated@abp.io")).CheckErrors();
}
}
}

Loading…
Cancel
Save