Skip to content

Commit

Permalink
Merge pull request #3173 from abpframework/Cotur-Virtualization-Account
Browse files Browse the repository at this point in the history
Make Account module services easily overridable by inheritance
  • Loading branch information
hikalkan authored Mar 18, 2020
2 parents 819aebe + 8b84a49 commit 237a353
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Volo.Abp.Account
{
public class AccountAppService : ApplicationService, IAccountAppService
{
private readonly IIdentityRoleRepository _roleRepository;
protected IIdentityRoleRepository RoleRepository { get; }
protected IdentityUserManager UserManager { get; }

public AccountAppService(
IdentityUserManager userManager,
IIdentityRoleRepository roleRepository)
{
_roleRepository = roleRepository;
RoleRepository = roleRepository;
UserManager = userManager;
}

Expand All @@ -43,4 +43,4 @@ protected virtual async Task CheckSelfRegistrationAsync()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ namespace Volo.Abp.Account
[Route("api/account")]
public class AccountController : AbpController, IAccountAppService
{
private readonly IAccountAppService _accountAppService;
protected IAccountAppService AccountAppService { get; }

public AccountController(IAccountAppService accountAppService)
{
_accountAppService = accountAppService;
AccountAppService = accountAppService;
}

[HttpPost]
[Route("register")]
public Task<IdentityUserDto> RegisterAsync(RegisterDto input)
public virtual Task<IdentityUserDto> RegisterAsync(RegisterDto input)
{
return _accountAppService.RegisterAsync(input);
return AccountAppService.RegisterAsync(input);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public override async Task<IActionResult> OnGetAsync()
return Page();
}

var schemes = await _schemeProvider.GetAllSchemesAsync();
var schemes = await SchemeProvider.GetAllSchemesAsync();

var providers = schemes
.Where(x => x.DisplayName != null || x.Name.Equals(_accountOptions.WindowsAuthenticationSchemeName, StringComparison.OrdinalIgnoreCase))
.Where(x => x.DisplayName != null || x.Name.Equals(AccountOptions.WindowsAuthenticationSchemeName, StringComparison.OrdinalIgnoreCase))
.Select(x => new ExternalProviderModel
{
DisplayName = x.DisplayName,
Expand Down Expand Up @@ -174,7 +174,7 @@ public override async Task<IActionResult> OnPostAsync(string action)
[UnitOfWork]
public override async Task<IActionResult> OnPostExternalLogin(string provider)
{
if (_accountOptions.WindowsAuthenticationSchemeName == provider)
if (AccountOptions.WindowsAuthenticationSchemeName == provider)
{
return await ProcessWindowsLoginAsync();
}
Expand All @@ -184,22 +184,22 @@ public override async Task<IActionResult> OnPostExternalLogin(string provider)

private async Task<IActionResult> ProcessWindowsLoginAsync()
{
var result = await HttpContext.AuthenticateAsync(_accountOptions.WindowsAuthenticationSchemeName);
var result = await HttpContext.AuthenticateAsync(AccountOptions.WindowsAuthenticationSchemeName);
if (!(result?.Principal is WindowsPrincipal windowsPrincipal))
{
return Challenge(_accountOptions.WindowsAuthenticationSchemeName);
return Challenge(AccountOptions.WindowsAuthenticationSchemeName);
}

var props = new AuthenticationProperties
{
RedirectUri = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash }),
Items =
{
{"scheme", _accountOptions.WindowsAuthenticationSchemeName},
{"scheme", AccountOptions.WindowsAuthenticationSchemeName},
}
};

var identity = new ClaimsIdentity(_accountOptions.WindowsAuthenticationSchemeName);
var identity = new ClaimsIdentity(AccountOptions.WindowsAuthenticationSchemeName);
identity.AddClaim(new Claim(JwtClaimTypes.Subject, windowsPrincipal.Identity.Name));
identity.AddClaim(new Claim(JwtClaimTypes.Name, windowsPrincipal.Identity.Name));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Volo.Abp.Account.Web
{
public class AbpAccountUserMenuContributor : IMenuContributor
{
public Task ConfigureMenuAsync(MenuConfigurationContext context)
public virtual Task ConfigureMenuAsync(MenuConfigurationContext context)
{
if (context.Menu.Name != StandardMenus.User)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Volo.Abp.Account.Web
{
public class AccountModuleToolbarContributor : IToolbarContributor
{
public Task ConfigureToolbarAsync(IToolbarConfigurationContext context)
public virtual Task ConfigureToolbarAsync(IToolbarConfigurationContext context)
{
if (context.Toolbar.Name != StandardToolbars.Main)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
[Route("api/account")]
public class AccountController : AbpController
{
private readonly SignInManager<IdentityUser> _signInManager;
private readonly IdentityUserManager _userManager;
private readonly ISettingProvider _settingProvider;
protected SignInManager<IdentityUser> SignInManager { get; }
protected IdentityUserManager UserManager { get; }
protected ISettingProvider SettingProvider { get; }

public AccountController(SignInManager<IdentityUser> signInManager, IdentityUserManager userManager, ISettingProvider settingProvider)
{
LocalizationResource = typeof(AccountResource);

_signInManager = signInManager;
_userManager = userManager;
_settingProvider = settingProvider;
SignInManager = signInManager;
UserManager = userManager;
SettingProvider = settingProvider;
}

[HttpPost]
Expand All @@ -45,7 +45,7 @@ public virtual async Task<AbpLoginResult> Login(UserLoginInfo login)

await ReplaceEmailToUsernameOfInputIfNeeds(login);

return GetAbpLoginResult(await _signInManager.PasswordSignInAsync(
return GetAbpLoginResult(await SignInManager.PasswordSignInAsync(
login.UserNameOrEmailAddress,
login.Password,
login.RememberMe,
Expand All @@ -57,7 +57,7 @@ public virtual async Task<AbpLoginResult> Login(UserLoginInfo login)
[Route("logout")]
public virtual async Task Logout()
{
await _signInManager.SignOutAsync();
await SignInManager.SignOutAsync();
}

[HttpPost]
Expand All @@ -68,14 +68,14 @@ public virtual async Task<AbpLoginResult> CheckPassword(UserLoginInfo login)

await ReplaceEmailToUsernameOfInputIfNeeds(login);

var identityUser = await _userManager.FindByNameAsync(login.UserNameOrEmailAddress);
var identityUser = await UserManager.FindByNameAsync(login.UserNameOrEmailAddress);

if (identityUser == null)
{
return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword);
}

return GetAbpLoginResult(await _signInManager.CheckPasswordSignInAsync(identityUser, login.Password, true));
return GetAbpLoginResult(await SignInManager.CheckPasswordSignInAsync(identityUser, login.Password, true));
}

protected virtual async Task ReplaceEmailToUsernameOfInputIfNeeds(UserLoginInfo login)
Expand All @@ -85,13 +85,13 @@ protected virtual async Task ReplaceEmailToUsernameOfInputIfNeeds(UserLoginInfo
return;
}

var userByUsername = await _userManager.FindByNameAsync(login.UserNameOrEmailAddress);
var userByUsername = await UserManager.FindByNameAsync(login.UserNameOrEmailAddress);
if (userByUsername != null)
{
return;
}

var userByEmail = await _userManager.FindByEmailAsync(login.UserNameOrEmailAddress);
var userByEmail = await UserManager.FindByEmailAsync(login.UserNameOrEmailAddress);
if (userByEmail == null)
{
return;
Expand Down Expand Up @@ -125,7 +125,7 @@ private static AbpLoginResult GetAbpLoginResult(SignInResult result)
return new AbpLoginResult(LoginResultType.Success);
}

private void ValidateLoginInfo(UserLoginInfo login)
protected virtual void ValidateLoginInfo(UserLoginInfo login)
{
if (login == null)
{
Expand All @@ -143,9 +143,9 @@ private void ValidateLoginInfo(UserLoginInfo login)
}
}

private async Task CheckLocalLoginAsync()
protected virtual async Task CheckLocalLoginAsync()
{
if (!await _settingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin))
if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin))
{
throw new UserFriendlyException(L["LocalLoginDisabledMessage"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Volo.Abp.Account.Web.Modules.Account.Components.Toolbar.UserLoginLink
{
public class UserLoginLinkViewComponent : AbpViewComponent
{
public IViewComponentResult Invoke()
public virtual IViewComponentResult Invoke()
{
return View("~/Modules/Account/Components/Toolbar/UserLoginLink/Default.cshtml");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ protected AccountPageModel()
ObjectMapperContext = typeof(AbpAccountWebModule);
}

protected RedirectResult RedirectSafely(string returnUrl, string returnUrlHash = null)
protected virtual RedirectResult RedirectSafely(string returnUrl, string returnUrlHash = null)
{
return Redirect(GetRedirectUrl(returnUrl, returnUrlHash));
}

protected void CheckIdentityErrors(IdentityResult identityResult)
protected virtual void CheckIdentityErrors(IdentityResult identityResult)
{
if (!identityResult.Succeeded)
{
Expand All @@ -36,7 +36,7 @@ protected void CheckIdentityErrors(IdentityResult identityResult)
//identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp
}

private string GetRedirectUrl(string returnUrl, string returnUrlHash = null)
protected virtual string GetRedirectUrl(string returnUrl, string returnUrlHash = null)
{
returnUrl = NormalizeReturnUrl(returnUrl);

Expand All @@ -48,7 +48,7 @@ private string GetRedirectUrl(string returnUrl, string returnUrlHash = null)
return returnUrl;
}

private string NormalizeReturnUrl(string returnUrl)
protected virtual string NormalizeReturnUrl(string returnUrl)
{
if (returnUrl.IsNullOrEmpty())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ public class LoginModel : AccountPageModel
//public IClientStore ClientStore { get; set; }
//public IEventService IdentityServerEvents { get; set; }

protected IAuthenticationSchemeProvider _schemeProvider;
protected AbpAccountOptions _accountOptions;
protected IAuthenticationSchemeProvider SchemeProvider { get; }
protected AbpAccountOptions AccountOptions { get; }

public LoginModel(
IAuthenticationSchemeProvider schemeProvider,
IOptions<AbpAccountOptions> accountOptions)
{
_schemeProvider = schemeProvider;
_accountOptions = accountOptions.Value;
SchemeProvider = schemeProvider;
AccountOptions = accountOptions.Value;
}

public virtual async Task<IActionResult> OnGetAsync()
{
LoginInput = new LoginInputModel();

var schemes = await _schemeProvider.GetAllSchemesAsync();
var schemes = await SchemeProvider.GetAllSchemesAsync();

var providers = schemes
.Where(x => x.DisplayName != null || x.Name.Equals(_accountOptions.WindowsAuthenticationSchemeName, StringComparison.OrdinalIgnoreCase))
.Where(x => x.DisplayName != null || x.Name.Equals(AccountOptions.WindowsAuthenticationSchemeName, StringComparison.OrdinalIgnoreCase))
.Select(x => new ExternalProviderModel
{
DisplayName = x.DisplayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public virtual async Task<IActionResult> OnGetAsync()

return RedirectToPage("/Account/Login");
}

public virtual Task OnPostAsync()
{
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ public class ManageModel : AccountPageModel

public PersonalSettingsInfoModel PersonalSettingsInfoModel { get; set; }

private readonly IProfileAppService _profileAppService;
protected IProfileAppService ProfileAppService { get; }

public ManageModel(IProfileAppService profileAppService)
{
_profileAppService = profileAppService;
ProfileAppService = profileAppService;
}

public async Task OnGetAsync()
public virtual async Task OnGetAsync()
{
var user = await _profileAppService.GetAsync();
var user = await ProfileAppService.GetAsync();

PersonalSettingsInfoModel = ObjectMapper.Map<ProfileDto, PersonalSettingsInfoModel>(user);
}

public virtual Task OnPostAsync()
{
return Task.CompletedTask;
}
}

public class ChangePasswordInfoModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Volo.Abp.Account.Web.Pages.Account
{
public class RegisterModel : AccountPageModel
{
private readonly IAccountAppService _accountAppService;
protected IAccountAppService AccountAppService { get; }

[BindProperty(SupportsGet = true)]
public string ReturnUrl { get; set; }
Expand All @@ -28,7 +28,7 @@ public class RegisterModel : AccountPageModel

public RegisterModel(IAccountAppService accountAppService)
{
_accountAppService = accountAppService;
AccountAppService = accountAppService;
}

public virtual async Task OnGetAsync()
Expand All @@ -51,7 +51,7 @@ public virtual async Task<IActionResult> OnPostAsync()
UserName = Input.UserName
};

var userDto = await _accountAppService.RegisterAsync(registerDto);
var userDto = await AccountAppService.RegisterAsync(registerDto);
var user = await UserManager.GetByIdAsync(userDto.Id);

await UserManager.SetEmailAsync(user, Input.EmailAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SendSecurityCodeModel : AccountPageModel
{
public List<SelectListItem> Providers { get; set; }

public async Task<IActionResult> OnGetAsync()
public virtual async Task<IActionResult> OnGetAsync()
{
var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
if (user == null)
Expand Down Expand Up @@ -41,5 +41,9 @@ public async Task<IActionResult> OnGetAsync()
//);
}

public virtual Task OnPostAsync()
{
return Task.CompletedTask;
}
}
}

0 comments on commit 237a353

Please sign in to comment.