From 62ee9d8cdbf227c4f6c6b3899fe5e8d5fd7e8a1a Mon Sep 17 00:00:00 2001 From: Jamie Date: Thu, 18 Jan 2018 08:23:45 +0000 Subject: [PATCH] Hide the password field if it's not needed #1815 --- .../Authentication/OmbiUserManager.cs | 32 ++++++++++++------- src/Ombi/ClientApp/app/auth/auth.service.ts | 4 +++ .../ClientApp/app/login/login.component.html | 4 +-- .../ClientApp/app/login/login.component.ts | 30 +++++++++++------ src/Ombi/Controllers/SettingsController.cs | 1 + src/Ombi/Controllers/TokenController.cs | 21 ++++++++++++ 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/Ombi.Core/Authentication/OmbiUserManager.cs b/src/Ombi.Core/Authentication/OmbiUserManager.cs index 522ffb899..b9453749d 100644 --- a/src/Ombi.Core/Authentication/OmbiUserManager.cs +++ b/src/Ombi.Core/Authentication/OmbiUserManager.cs @@ -64,19 +64,11 @@ namespace Ombi.Core.Authentication public override async Task CheckPasswordAsync(OmbiUser user, string password) { - var authSettings = await _authSettings.GetSettingsAsync(); - if (authSettings.AllowNoPassword) + var requiresPassword = await RequiresPassword(user); + if (!requiresPassword) { - // Check their roles - var roles = await GetRolesAsync(user); - if (roles.Contains(OmbiRoles.Admin) || roles.Contains(OmbiRoles.PowerUser)) - { - // Do nothing, let it continue to check the password - } - else - { - return true; - } + // Let them through! + return true; } if (user.UserType == UserType.LocalUser) { @@ -93,6 +85,22 @@ namespace Ombi.Core.Authentication return false; } + public async Task RequiresPassword(OmbiUser user) + { + var authSettings = await _authSettings.GetSettingsAsync(); + if (authSettings.AllowNoPassword) + { + var roles = await GetRolesAsync(user); + if (roles.Contains(OmbiRoles.Admin) || roles.Contains(OmbiRoles.PowerUser)) + { + // We require a password + return true; + } + return false; + } + return true; + } + /// /// Sign the user into plex and make sure we can get the authentication token. /// We do not check if the user is in the owners "friends" since they must have a local user account to get this far diff --git a/src/Ombi/ClientApp/app/auth/auth.service.ts b/src/Ombi/ClientApp/app/auth/auth.service.ts index 6249edc5b..b9899c9a4 100644 --- a/src/Ombi/ClientApp/app/auth/auth.service.ts +++ b/src/Ombi/ClientApp/app/auth/auth.service.ts @@ -18,6 +18,10 @@ export class AuthService extends ServiceHelpers { return this.http.post(`${this.url}/`, JSON.stringify(login), {headers: this.headers}); } + public requiresPassword(login: IUserLogin): Observable { + return this.http.post(`${this.url}/requirePassword`, JSON.stringify(login), {headers: this.headers}); + } + public loggedIn() { const token: string = this.jwtHelperService.tokenGetter(); diff --git a/src/Ombi/ClientApp/app/login/login.component.html b/src/Ombi/ClientApp/app/login/login.component.html index a000971ce..80c48cabd 100644 --- a/src/Ombi/ClientApp/app/login/login.component.html +++ b/src/Ombi/ClientApp/app/login/login.component.html @@ -12,11 +12,11 @@ include the remember me checkbox

-
/// [HttpGet("authentication")] + [AllowAnonymous] public async Task AuthenticationsSettings() { return await Get(); diff --git a/src/Ombi/Controllers/TokenController.cs b/src/Ombi/Controllers/TokenController.cs index a8d47f524..93e8a025e 100644 --- a/src/Ombi/Controllers/TokenController.cs +++ b/src/Ombi/Controllers/TokenController.cs @@ -15,6 +15,7 @@ using Ombi.Models; using Ombi.Models.Identity; using Ombi.Store.Entities; using Ombi.Store.Repository; +using StackExchange.Profiling.Helpers; namespace Ombi.Controllers { @@ -129,6 +130,26 @@ namespace Ombi.Controllers throw new NotImplementedException(); } + [HttpPost("requirePassword")] + public async Task DoesUserRequireAPassword([FromBody] UserAuthModel model) + { + var user = await _userManager.FindByNameAsync(model.Username); + + if (user == null) + { + // Could this be an email login? + user = await _userManager.FindByEmailAsync(model.Username); + + if (user == null) + { + return true; + } + } + + var requires = await _userManager.RequiresPassword(user); + return requires; + } + public class TokenRefresh { public string Token { get; set; }