From 7294f942d9db2d8c5836fb1915b698565f671a59 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sun, 16 Apr 2017 01:38:56 +0100 Subject: [PATCH] Finished the wizard #865 (For Plex Anyway) --- Ombi/Ombi.Core/Claims/OmbiClaims.cs | 34 ++++++++++ .../IdentityResolver/UserIdentityManager.cs | 2 + Ombi/Ombi/Controllers/IdentityController.cs | 38 ++++++++++- Ombi/Ombi/Controllers/StatusController.cs | 65 +++++++++++++++++++ Ombi/Ombi/Ombi.csproj | 23 +++++-- Ombi/Ombi/wwwroot/app/app.component.html | 2 +- Ombi/Ombi/wwwroot/app/app.component.ts | 10 ++- Ombi/Ombi/wwwroot/app/app.module.ts | 7 +- .../Ombi/wwwroot/app/login/login.component.ts | 10 ++- .../wwwroot/app/services/identity.service.ts | 17 +++++ .../wwwroot/app/services/status.service.ts | 16 +++++ .../createadmin/createadmin.component.ts | 32 +++++++-- Ombi/Ombi/wwwroot/app/wizard/wizard.module.ts | 9 ++- 13 files changed, 248 insertions(+), 17 deletions(-) create mode 100644 Ombi/Ombi.Core/Claims/OmbiClaims.cs create mode 100644 Ombi/Ombi/Controllers/StatusController.cs create mode 100644 Ombi/Ombi/wwwroot/app/services/identity.service.ts create mode 100644 Ombi/Ombi/wwwroot/app/services/status.service.ts diff --git a/Ombi/Ombi.Core/Claims/OmbiClaims.cs b/Ombi/Ombi.Core/Claims/OmbiClaims.cs new file mode 100644 index 000000000..fcfc682ec --- /dev/null +++ b/Ombi/Ombi.Core/Claims/OmbiClaims.cs @@ -0,0 +1,34 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: OmbiClaims.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace Ombi.Core.Claims +{ + public static class OmbiClaims + { + public const string Admin = nameof(Admin); + + } +} \ No newline at end of file diff --git a/Ombi/Ombi.Core/IdentityResolver/UserIdentityManager.cs b/Ombi/Ombi.Core/IdentityResolver/UserIdentityManager.cs index 4b40a8788..942d1b0e9 100644 --- a/Ombi/Ombi.Core/IdentityResolver/UserIdentityManager.cs +++ b/Ombi/Ombi.Core/IdentityResolver/UserIdentityManager.cs @@ -51,6 +51,8 @@ namespace Ombi.Core.IdentityResolver public async Task CredentialsValid(string username, string password) { var user = await UserRepository.GetUser(username); + if (user == null) return false; + var hash = HashPassword(password, user.Salt); return hash.HashedPass.Equals(user.Password); diff --git a/Ombi/Ombi/Controllers/IdentityController.cs b/Ombi/Ombi/Controllers/IdentityController.cs index 30b47310c..6598f5310 100644 --- a/Ombi/Ombi/Controllers/IdentityController.cs +++ b/Ombi/Ombi/Controllers/IdentityController.cs @@ -1,8 +1,13 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Ombi.Core.Claims; using Ombi.Core.IdentityResolver; using Ombi.Core.Models; +using Ombi.Models; namespace Ombi.Controllers { @@ -21,6 +26,37 @@ namespace Ombi.Controllers { return await IdentityManager.GetUser(this.HttpContext.User.Identity.Name); } + + + /// + /// This is what the Wizard will call when creating the user for the very first time. + /// This should never be called after this. + /// The reason why we return false if users exists is that this method doesn't have any + /// authorization and could be called from anywhere. + /// + /// + /// + [HttpPost("Wizard")] + [AllowAnonymous] + public async Task CreateWizardUser([FromBody] UserAuthModel user) + { + var users = await IdentityManager.GetUsers(); + if (users.Any()) + { + // No one should be calling this. Only the wizard + return false; + } + + await IdentityManager.CreateUser(new UserDto + { + Username = user.Username, + UserType = UserType.LocalUser, + Claims = new List() {new Claim(ClaimTypes.Role, OmbiClaims.Admin)}, + Password = user.Password, + }); + + return true; + } } } diff --git a/Ombi/Ombi/Controllers/StatusController.cs b/Ombi/Ombi/Controllers/StatusController.cs new file mode 100644 index 000000000..85d0bd1a7 --- /dev/null +++ b/Ombi/Ombi/Controllers/StatusController.cs @@ -0,0 +1,65 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: StatusController.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Ombi.Core.Settings; +using Ombi.Core.Settings.Models; + +namespace Ombi.Controllers +{ + + public class StatusController : BaseV1ApiController + { + public StatusController(ISettingsService ombi) + { + Ombi = ombi; + } + + private ISettingsService Ombi { get; } + + [AllowAnonymous] + [HttpGet] + public HttpStatusCode GetStatus() + { + return HttpStatusCode.OK; + } + + + [AllowAnonymous] + [HttpGet("Wizard")] + public async Task WizardStatus() + { + var settings = await Ombi.GetSettingsAsync(); + + return new { Result = settings?.Wizard ?? false}; + } + + } +} \ No newline at end of file diff --git a/Ombi/Ombi/Ombi.csproj b/Ombi/Ombi/Ombi.csproj index a94d5f0ff..23e4dc7d6 100644 --- a/Ombi/Ombi/Ombi.csproj +++ b/Ombi/Ombi/Ombi.csproj @@ -20,6 +20,20 @@ + + identity - Copy.service.ts + PreserveNewest + + + identity - Copy.service.js + PreserveNewest + + + PreserveNewest + + + + @@ -28,6 +42,10 @@ + + + + @@ -59,11 +77,6 @@ - - PreserveNewest - - - PreserveNewest diff --git a/Ombi/Ombi/wwwroot/app/app.component.html b/Ombi/Ombi/wwwroot/app/app.component.html index b3ea68d54..1bbc54d2c 100644 --- a/Ombi/Ombi/wwwroot/app/app.component.html +++ b/Ombi/Ombi/wwwroot/app/app.component.html @@ -1,6 +1,6 @@  -