mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-12 00:06:05 -07:00
Fixed the issue where it was not picking up roles until the JWT was refreshed
This commit is contained in:
parent
826517cbff
commit
cf9bb889ed
3 changed files with 34 additions and 22 deletions
|
@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using Ombi.Core.Rule.Rules;
|
using Ombi.Core.Rule.Rules;
|
||||||
|
using Ombi.Core.Rule.Rules.Request;
|
||||||
using Ombi.Helpers;
|
using Ombi.Helpers;
|
||||||
using Ombi.Store.Entities.Requests;
|
using Ombi.Store.Entities.Requests;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Ombi.Core.Authentication;
|
||||||
using Ombi.Core.Models.Requests;
|
using Ombi.Core.Models.Requests;
|
||||||
using Ombi.Core.Rule.Interfaces;
|
using Ombi.Core.Rule.Interfaces;
|
||||||
using Ombi.Helpers;
|
using Ombi.Helpers;
|
||||||
|
@ -10,28 +12,31 @@ namespace Ombi.Core.Rule.Rules.Request
|
||||||
{
|
{
|
||||||
public class AutoApproveRule : BaseRequestRule, IRules<BaseRequest>
|
public class AutoApproveRule : BaseRequestRule, IRules<BaseRequest>
|
||||||
{
|
{
|
||||||
public AutoApproveRule(IPrincipal principal)
|
public AutoApproveRule(IPrincipal principal, OmbiUserManager um)
|
||||||
{
|
{
|
||||||
User = principal;
|
User = principal;
|
||||||
|
_manager = um;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IPrincipal User { get; }
|
private IPrincipal User { get; }
|
||||||
|
private readonly OmbiUserManager _manager;
|
||||||
|
|
||||||
public Task<RuleResult> Execute(BaseRequest obj)
|
public async Task<RuleResult> Execute(BaseRequest obj)
|
||||||
{
|
{
|
||||||
if (User.IsInRole(OmbiRoles.Admin))
|
var user = await _manager.Users.FirstOrDefaultAsync(x => x.UserName == User.Identity.Name);
|
||||||
|
if (await _manager.IsInRoleAsync(user, OmbiRoles.Admin))
|
||||||
{
|
{
|
||||||
obj.Approved = true;
|
obj.Approved = true;
|
||||||
return Task.FromResult(Success());
|
return Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.RequestType == RequestType.Movie && User.IsInRole(OmbiRoles.AutoApproveMovie))
|
if (obj.RequestType == RequestType.Movie && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMovie))
|
||||||
obj.Approved = true;
|
obj.Approved = true;
|
||||||
if (obj.RequestType == RequestType.TvShow && User.IsInRole(OmbiRoles.AutoApproveTv))
|
if (obj.RequestType == RequestType.TvShow && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveTv))
|
||||||
obj.Approved = true;
|
obj.Approved = true;
|
||||||
if (obj.RequestType == RequestType.Album && User.IsInRole(OmbiRoles.AutoApproveMusic))
|
if (obj.RequestType == RequestType.Album && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMusic))
|
||||||
obj.Approved = true;
|
obj.Approved = true;
|
||||||
return Task.FromResult(Success()); // We don't really care, we just don't set the obj to approve
|
return Success(); // We don't really care, we just don't set the obj to approve
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,46 +1,52 @@
|
||||||
using Ombi.Store.Entities;
|
using System.Security.Claims;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Ombi.Core.Authentication;
|
||||||
using Ombi.Core.Rule.Interfaces;
|
using Ombi.Core.Rule.Interfaces;
|
||||||
using Ombi.Helpers;
|
using Ombi.Helpers;
|
||||||
|
using Ombi.Store.Entities;
|
||||||
using Ombi.Store.Entities.Requests;
|
using Ombi.Store.Entities.Requests;
|
||||||
|
|
||||||
namespace Ombi.Core.Rule.Rules
|
namespace Ombi.Core.Rule.Rules.Request
|
||||||
{
|
{
|
||||||
public class CanRequestRule : BaseRequestRule, IRules<BaseRequest>
|
public class CanRequestRule : BaseRequestRule, IRules<BaseRequest>
|
||||||
{
|
{
|
||||||
public CanRequestRule(IPrincipal principal)
|
public CanRequestRule(IPrincipal principal, OmbiUserManager manager)
|
||||||
{
|
{
|
||||||
User = principal;
|
User = principal;
|
||||||
|
_manager = manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IPrincipal User { get; }
|
private IPrincipal User { get; }
|
||||||
|
private readonly OmbiUserManager _manager;
|
||||||
|
|
||||||
public Task<RuleResult> Execute(BaseRequest obj)
|
public async Task<RuleResult> Execute(BaseRequest obj)
|
||||||
{
|
{
|
||||||
if (User.IsInRole(OmbiRoles.Admin))
|
var user = await _manager.Users.FirstOrDefaultAsync(x => x.UserName == User.Identity.Name);
|
||||||
return Task.FromResult(Success());
|
if (await _manager.IsInRoleAsync(user, OmbiRoles.Admin))
|
||||||
|
return Success();
|
||||||
|
|
||||||
if (obj.RequestType == RequestType.Movie)
|
if (obj.RequestType == RequestType.Movie)
|
||||||
{
|
{
|
||||||
if (User.IsInRole(OmbiRoles.RequestMovie) || User.IsInRole(OmbiRoles.AutoApproveMovie))
|
if (await _manager.IsInRoleAsync(user, OmbiRoles.RequestMovie) || await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMovie))
|
||||||
return Task.FromResult(Success());
|
return Success();
|
||||||
return Task.FromResult(Fail("You do not have permissions to Request a Movie"));
|
return Fail("You do not have permissions to Request a Movie");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.RequestType == RequestType.TvShow)
|
if (obj.RequestType == RequestType.TvShow)
|
||||||
{
|
{
|
||||||
if (User.IsInRole(OmbiRoles.RequestTv) || User.IsInRole(OmbiRoles.AutoApproveTv))
|
if (await _manager.IsInRoleAsync(user, OmbiRoles.RequestTv) || await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveTv))
|
||||||
return Task.FromResult(Success());
|
return Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.RequestType == RequestType.Album)
|
if (obj.RequestType == RequestType.Album)
|
||||||
{
|
{
|
||||||
if (User.IsInRole(OmbiRoles.RequestMusic) || User.IsInRole(OmbiRoles.AutoApproveMusic))
|
if (await _manager.IsInRoleAsync(user, OmbiRoles.RequestMusic) || await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMusic))
|
||||||
return Task.FromResult(Success());
|
return Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.FromResult(Fail("You do not have permissions to Request a TV Show"));
|
return Fail("You do not have permissions to Request a TV Show");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue