From 233c76813cd3b0429c76fbe8b27c2ec80333c77d Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 25 Aug 2017 22:06:42 +0100 Subject: [PATCH] Small changes around the roles #865 --- src/Ombi.Core/Claims/OmbiRoles.cs | 1 + src/Ombi.Core/Senders/TvSender.cs | 2 +- src/Ombi.Store/Context/OmbiContext.cs | 1 - src/Ombi/Controllers/IdentityController.cs | 24 ++++++++++++++++++---- src/Ombi/Controllers/TokenController.cs | 6 ++++++ 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Ombi.Core/Claims/OmbiRoles.cs b/src/Ombi.Core/Claims/OmbiRoles.cs index 15ce52f9a..5d809e405 100644 --- a/src/Ombi.Core/Claims/OmbiRoles.cs +++ b/src/Ombi.Core/Claims/OmbiRoles.cs @@ -8,5 +8,6 @@ public const string PowerUser = nameof(PowerUser); public const string RequestTv = nameof(RequestTv); public const string RequestMovie = nameof(RequestMovie); + public const string Disabled = nameof(Disabled); } } \ No newline at end of file diff --git a/src/Ombi.Core/Senders/TvSender.cs b/src/Ombi.Core/Senders/TvSender.cs index e20e8c54a..d2fc19683 100644 --- a/src/Ombi.Core/Senders/TvSender.cs +++ b/src/Ombi.Core/Senders/TvSender.cs @@ -185,7 +185,7 @@ namespace Ombi.Core.Senders { var sonarrSeason = sonarrEpisodes.Where(x => x.seasonNumber == season.SeasonNumber); var sonarrEpCount = sonarrSeason.Count(); - var ourRequestCount = season.Episodes.Count(); + var ourRequestCount = season.Episodes.Count; if (sonarrEpCount == ourRequestCount) { diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index 823e56021..7c1fea71f 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -81,7 +81,6 @@ namespace Ombi.Store.Context SaveChanges(); } - //Check if templates exist var templates = NotificationTemplates.ToList(); //if (templates.Any()) diff --git a/src/Ombi/Controllers/IdentityController.cs b/src/Ombi/Controllers/IdentityController.cs index 8a82d2d19..fc329ecf4 100644 --- a/src/Ombi/Controllers/IdentityController.cs +++ b/src/Ombi/Controllers/IdentityController.cs @@ -89,16 +89,32 @@ namespace Ombi.Controllers var result = await UserManager.CreateAsync(userToCreate, user.Password); if (result.Succeeded) { - if (!await RoleManager.RoleExistsAsync(OmbiRoles.Admin)) - { - await RoleManager.CreateAsync(new IdentityRole(OmbiRoles.Admin)); - } + await CreateRoles(); await UserManager.AddToRoleAsync(userToCreate, OmbiRoles.Admin); } return true; } + private async Task CreateRoles() + { + await CreateRole(OmbiRoles.AutoApproveMovie); + await CreateRole(OmbiRoles.Admin); + await CreateRole(OmbiRoles.AutoApproveTv); + await CreateRole(OmbiRoles.PowerUser); + await CreateRole(OmbiRoles.RequestMovie); + await CreateRole(OmbiRoles.RequestTv); + await CreateRole(OmbiRoles.Disabled); + } + + private async Task CreateRole(string role) + { + if (!await RoleManager.RoleExistsAsync(role)) + { + await RoleManager.CreateAsync(new IdentityRole(role)); + } + } + /// /// Gets all users. /// diff --git a/src/Ombi/Controllers/TokenController.cs b/src/Ombi/Controllers/TokenController.cs index fa10ecf77..fc7af8ae8 100644 --- a/src/Ombi/Controllers/TokenController.cs +++ b/src/Ombi/Controllers/TokenController.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; +using Ombi.Core.Claims; using Ombi.Models; using Ombi.Models.Identity; using Ombi.Store.Entities; @@ -58,6 +59,11 @@ namespace Ombi.Controllers { var roles = await _userManager.GetRolesAsync(user); + if (roles.Contains(OmbiRoles.Disabled)) + { + return new UnauthorizedResult(); + } + var claims = new List { new Claim(JwtRegisteredClaimNames.Sub, user.UserName),