From cd983419042915ab1efe8154cb91c136d7b87c66 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Sun, 12 Aug 2018 18:01:24 +0300 Subject: [PATCH] Added user integration for 3rd party apps. --- .../Account/Controllers/LoginController.cs | 17 +++++++++-------- .../Controllers/Models/AbpLoginResult.cs | 4 +++- .../Abp/Identity/IIdentityUserAppService.cs | 4 ++++ .../Abp/Identity/IdentityUserAppService.cs | 16 +++++++++++++++- .../Abp/Identity/IdentityUserController.cs | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/LoginController.cs b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/LoginController.cs index 67de8fc9e8..435953a2dd 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/LoginController.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/LoginController.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -29,7 +30,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers [HttpPost] [Route("")] - public virtual async Task Login(UserLoginInfo login) + public virtual async Task Login(UserLoginInfo login) { if (login == null) { @@ -55,28 +56,28 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers if (result.IsLockedOut) { - return Json(new AbpLoginResult(LoginResultType.LockedOut)); + return new AbpLoginResult(LoginResultType.LockedOut); } if (result.RequiresTwoFactor) { - return Json(new AbpLoginResult(LoginResultType.RequiresTwoFactor)); + return new AbpLoginResult(LoginResultType.RequiresTwoFactor); } if (result.IsNotAllowed) { - return Json(new AbpLoginResult(LoginResultType.NotAllowed)); + return new AbpLoginResult(LoginResultType.NotAllowed); } if (!result.Succeeded) { - return Json(new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword)); + return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword); } - return Json(new AbpLoginResult(LoginResultType.Success) + return new AbpLoginResult(LoginResultType.Success) { - IdentityCookieToken = GetCookieValueFromResponse(AspNetCoreIdentityCookieName) //todo: cookie name can be retrieved from UseAuthentication options - }); + IdentityCookieToken = GetCookieValueFromResponse(AspNetCoreIdentityCookieName) + }; } private string GetCookieValueFromResponse(string cookieName) diff --git a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/Models/AbpLoginResult.cs b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/Models/AbpLoginResult.cs index b47a7dc9ef..f4e6fcfde8 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/Models/AbpLoginResult.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/Models/AbpLoginResult.cs @@ -1,4 +1,6 @@ -using Volo.Abp.Account.Web.Areas.Account.Controllers.Models; +using System; +using Volo.Abp.Account.Web.Areas.Account.Controllers.Models; +using Volo.Abp.Identity; namespace Volo.Abp.Account.Web.Areas.Account.Controllers { diff --git a/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs b/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs index 424a5c3ca2..50ea282f15 100644 --- a/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs +++ b/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs @@ -15,5 +15,9 @@ namespace Volo.Abp.Identity Task GetPermissionsAsync(Guid id); Task UpdatePermissionsAsync(Guid id, UpdatePermissionsDto input); + + Task FindByUsernameAsync(string username); + + Task FindByEmailAsync(string email); } } diff --git a/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs b/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs index 4ee5644c87..4fced6274b 100644 --- a/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs +++ b/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs @@ -112,7 +112,21 @@ namespace Volo.Abp.Identity { await _permissionAppServiceHelper.UpdateAsync(UserPermissionValueProvider.ProviderName, id.ToString(), input); } - + + public async Task FindByUsernameAsync(string username) + { + return ObjectMapper.Map( + await _userManager.FindByNameAsync(username) + ); + } + + public async Task FindByEmailAsync(string email) + { + return ObjectMapper.Map( + await _userManager.FindByEmailAsync(email) + ); + } + private async Task UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input) { (await _userManager.SetEmailAsync(user, input.Email)).CheckErrors(); diff --git a/modules/identity/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUserController.cs b/modules/identity/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUserController.cs index e5618710cb..eb2e448c9c 100644 --- a/modules/identity/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUserController.cs +++ b/modules/identity/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUserController.cs @@ -63,5 +63,23 @@ namespace Volo.Abp.Identity { return _userAppService.UpdatePermissionsAsync(id, input); } + + [HttpGet] + public virtual Task FindByUsernameAsync(string username) + { + if (CurrentUser.Id.HasValue) + { + return _userAppService.FindByUsernameAsync(username); + } + + Console.WriteLine("Not logged in!"); + throw new UnauthorizedAccessException(); + } + + [HttpGet] + public virtual Task FindByEmailAsync(string email) + { + return _userAppService.FindByEmailAsync(email); + } } }