mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-11 15:56:05 -07:00
#456 Update all the requests when we identify that the username changes
This commit is contained in:
parent
3d2272cd91
commit
d5127073bc
7 changed files with 92 additions and 20 deletions
|
@ -8,7 +8,11 @@ namespace PlexRequests.Core
|
||||||
public interface ISecurityExtensions
|
public interface ISecurityExtensions
|
||||||
{
|
{
|
||||||
Response AdminLoginRedirect(Permissions perm, NancyContext context);
|
Response AdminLoginRedirect(Permissions perm, NancyContext context);
|
||||||
|
Response AdminLoginRedirect(NancyContext context, params Permissions[] perm);
|
||||||
bool DoesNotHavePermissions(Permissions perm, IUserIdentity currentUser);
|
bool DoesNotHavePermissions(Permissions perm, IUserIdentity currentUser);
|
||||||
|
|
||||||
|
Response HasAnyPermissionsRedirect(NancyContext context, string routeName, HttpStatusCode code,
|
||||||
|
params Permissions[] perm);
|
||||||
bool DoesNotHavePermissions(int perm, IUserIdentity currentUser);
|
bool DoesNotHavePermissions(int perm, IUserIdentity currentUser);
|
||||||
Func<NancyContext, Response> ForbiddenIfNot(Func<NancyContext, bool> test);
|
Func<NancyContext, Response> ForbiddenIfNot(Func<NancyContext, bool> test);
|
||||||
bool HasAnyPermissions(IUserIdentity user, params Permissions[] perm);
|
bool HasAnyPermissions(IUserIdentity user, params Permissions[] perm);
|
||||||
|
|
|
@ -184,7 +184,31 @@ namespace PlexRequests.Core
|
||||||
|
|
||||||
var r = response(context);
|
var r = response(context);
|
||||||
return r.StatusCode == code
|
return r.StatusCode == code
|
||||||
? new RedirectResponse(url.ToString())
|
? new RedirectResponse(url.ToString())
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
public Response HasAnyPermissionsRedirect(NancyContext context, string routeName, HttpStatusCode code, params Permissions[] perm)
|
||||||
|
{
|
||||||
|
var url = Linker.BuildRelativeUri(context, routeName);
|
||||||
|
|
||||||
|
var response = ForbiddenIfNot(ctx =>
|
||||||
|
{
|
||||||
|
var permissions = GetPermissions(ctx.CurrentUser);
|
||||||
|
var hasPermission = false;
|
||||||
|
foreach (var p in perm)
|
||||||
|
{
|
||||||
|
var result = permissions.HasFlag(p);
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
hasPermission = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hasPermission;
|
||||||
|
});
|
||||||
|
|
||||||
|
var r = response(context);
|
||||||
|
return r.StatusCode == code
|
||||||
|
? new RedirectResponse(url.ToString())
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +218,13 @@ namespace PlexRequests.Core
|
||||||
// This will redirect us to the Login Page if we don't have the correct permission passed in (e.g. Admin with Http 403 status code).
|
// This will redirect us to the Login Page if we don't have the correct permission passed in (e.g. Admin with Http 403 status code).
|
||||||
return HasPermissionsRedirect(perm, context, "LocalLogin", HttpStatusCode.Forbidden);
|
return HasPermissionsRedirect(perm, context, "LocalLogin", HttpStatusCode.Forbidden);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Response AdminLoginRedirect(NancyContext context, params Permissions[] perm)
|
||||||
|
{
|
||||||
|
// This will redirect us to the Login Page if we don't have the correct permission passed in (e.g. Admin with Http 403 status code).
|
||||||
|
return HasAnyPermissionsRedirect(context, "LocalLogin", HttpStatusCode.Forbidden, perm);
|
||||||
|
}
|
||||||
|
|
||||||
// BELOW IS A COPY FROM THE SecurityHooks CLASS!
|
// BELOW IS A COPY FROM THE SecurityHooks CLASS!
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -45,7 +45,8 @@ namespace PlexRequests.Services.Jobs
|
||||||
{
|
{
|
||||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
public PlexUserChecker(IPlexUserRepository plexUsers, IPlexApi plexAPi, IJobRecord rec, ISettingsService<PlexSettings> plexSettings, ISettingsService<PlexRequestSettings> prSettings, ISettingsService<UserManagementSettings> umSettings)
|
public PlexUserChecker(IPlexUserRepository plexUsers, IPlexApi plexAPi, IJobRecord rec, ISettingsService<PlexSettings> plexSettings, ISettingsService<PlexRequestSettings> prSettings, ISettingsService<UserManagementSettings> umSettings,
|
||||||
|
IRequestService requestService)
|
||||||
{
|
{
|
||||||
Repo = plexUsers;
|
Repo = plexUsers;
|
||||||
JobRecord = rec;
|
JobRecord = rec;
|
||||||
|
@ -53,6 +54,7 @@ namespace PlexRequests.Services.Jobs
|
||||||
PlexSettings = plexSettings;
|
PlexSettings = plexSettings;
|
||||||
PlexRequestSettings = prSettings;
|
PlexRequestSettings = prSettings;
|
||||||
UserManagementSettings = umSettings;
|
UserManagementSettings = umSettings;
|
||||||
|
RequestService = requestService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IJobRecord JobRecord { get; }
|
private IJobRecord JobRecord { get; }
|
||||||
|
@ -61,7 +63,7 @@ namespace PlexRequests.Services.Jobs
|
||||||
private ISettingsService<PlexSettings> PlexSettings { get; }
|
private ISettingsService<PlexSettings> PlexSettings { get; }
|
||||||
private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; }
|
private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; }
|
||||||
private ISettingsService<UserManagementSettings> UserManagementSettings { get; }
|
private ISettingsService<UserManagementSettings> UserManagementSettings { get; }
|
||||||
|
private IRequestService RequestService { get; }
|
||||||
|
|
||||||
public void Execute(IJobExecutionContext context)
|
public void Execute(IJobExecutionContext context)
|
||||||
{
|
{
|
||||||
|
@ -76,6 +78,7 @@ namespace PlexRequests.Services.Jobs
|
||||||
}
|
}
|
||||||
var plexUsers = PlexApi.GetUsers(settings.PlexAuthToken);
|
var plexUsers = PlexApi.GetUsers(settings.PlexAuthToken);
|
||||||
var userManagementSettings = UserManagementSettings.GetSettings();
|
var userManagementSettings = UserManagementSettings.GetSettings();
|
||||||
|
var requests = RequestService.GetAll().ToList();
|
||||||
|
|
||||||
var dbUsers = Repo.GetAll().ToList();
|
var dbUsers = Repo.GetAll().ToList();
|
||||||
foreach (var user in plexUsers.User)
|
foreach (var user in plexUsers.User)
|
||||||
|
@ -84,6 +87,7 @@ namespace PlexRequests.Services.Jobs
|
||||||
if (dbUser != null)
|
if (dbUser != null)
|
||||||
{
|
{
|
||||||
var needToUpdate = false;
|
var needToUpdate = false;
|
||||||
|
var usernameChanged = false;
|
||||||
|
|
||||||
// Do we need up update any info?
|
// Do we need up update any info?
|
||||||
if (dbUser.EmailAddress != user.Email)
|
if (dbUser.EmailAddress != user.Email)
|
||||||
|
@ -95,10 +99,30 @@ namespace PlexRequests.Services.Jobs
|
||||||
{
|
{
|
||||||
dbUser.Username = user.Username;
|
dbUser.Username = user.Username;
|
||||||
needToUpdate = true;
|
needToUpdate = true;
|
||||||
|
usernameChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needToUpdate)
|
if (needToUpdate)
|
||||||
{
|
{
|
||||||
|
if (usernameChanged)
|
||||||
|
{
|
||||||
|
// Since the username has changed, we need to update all requests with that username (unless we are using the alias!)
|
||||||
|
if (string.IsNullOrEmpty(dbUser.UserAlias))
|
||||||
|
{
|
||||||
|
// Update all requests
|
||||||
|
var requestsWithThisUser = requests.Where(x => x.RequestedUsers.Contains(user.Username)).ToList();
|
||||||
|
foreach (var r in requestsWithThisUser)
|
||||||
|
{
|
||||||
|
r.RequestedUsers.Remove(user.Username); // Remove old
|
||||||
|
r.RequestedUsers.Add(dbUser.Username); // Add new
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestsWithThisUser.Any())
|
||||||
|
{
|
||||||
|
RequestService.BatchUpdate(requestsWithThisUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Repo.Update(dbUser);
|
Repo.Update(dbUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,21 @@ namespace PlexRequests.Services.Notification
|
||||||
Log.Debug("Notifying Users Count {0}", users.Count);
|
Log.Debug("Notifying Users Count {0}", users.Count);
|
||||||
foreach (var model in modelChanged)
|
foreach (var model in modelChanged)
|
||||||
{
|
{
|
||||||
var selectedUsers = users.Select(x => x.Username).Intersect(model.RequestedUsers, StringComparer.CurrentCultureIgnoreCase);
|
var selectedUsers = new List<string>();
|
||||||
|
|
||||||
|
foreach (var u in users)
|
||||||
|
{
|
||||||
|
var requestUser = model.RequestedUsers.FirstOrDefault(
|
||||||
|
x => x.Equals(u.Username, StringComparison.CurrentCultureIgnoreCase) || x.Equals(u.UserAlias, StringComparison.CurrentCultureIgnoreCase));
|
||||||
|
if (string.IsNullOrEmpty(requestUser))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedUsers.Add(requestUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
//var selectedUsers = users.Select(x => x.Username).Intersect(model.RequestedUsers, StringComparer.CurrentCultureIgnoreCase);
|
||||||
foreach (var user in selectedUsers)
|
foreach (var user in selectedUsers)
|
||||||
{
|
{
|
||||||
Log.Info("Notifying user {0}", user);
|
Log.Info("Notifying user {0}", user);
|
||||||
|
|
|
@ -56,10 +56,8 @@ namespace PlexRequests.UI.Modules
|
||||||
ISettingsService<HeadphonesSettings> hpSettings, IHeadphonesApi hpApi, ISettingsService<PlexRequestSettings> pr, ITransientFaultQueue faultQueue
|
ISettingsService<HeadphonesSettings> hpSettings, IHeadphonesApi hpApi, ISettingsService<PlexRequestSettings> pr, ITransientFaultQueue faultQueue
|
||||||
, ISecurityExtensions security) : base("approval", pr, security)
|
, ISecurityExtensions security) : base("approval", pr, security)
|
||||||
{
|
{
|
||||||
this.RequiresAnyClaim(UserClaims.Admin, UserClaims.PowerUser);
|
|
||||||
|
|
||||||
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
|
Before += (ctx) => Security.AdminLoginRedirect(ctx, Permissions.Administrator,Permissions.ManageRequests);
|
||||||
Before += (ctx) => Security.AdminLoginRedirect(Permissions.ManageRequests, ctx);
|
|
||||||
|
|
||||||
Service = service;
|
Service = service;
|
||||||
CpService = cpService;
|
CpService = cpService;
|
||||||
|
|
|
@ -101,6 +101,9 @@ namespace PlexRequests.UI.Modules
|
||||||
|
|
||||||
|
|
||||||
private string _username;
|
private string _username;
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the Username or UserAlias
|
||||||
|
/// </summary>
|
||||||
protected string Username
|
protected string Username
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
|
@ -180,18 +180,18 @@
|
||||||
<div>
|
<div>
|
||||||
<span>Request status: </span>
|
<span>Request status: </span>
|
||||||
{{#if available}}
|
{{#if available}}
|
||||||
<span class="label label-success">@UI.Search_Available_on_plex</span>
|
<span class="label label-success">@UI.Search_Available_on_plex</span>
|
||||||
{{else}}
|
{{else}}
|
||||||
{{#if approved}}
|
{{#if approved}}
|
||||||
<span class="label label-info">@UI.Search_Processing_Request</span>
|
<span class="label label-info">@UI.Search_Processing_Request</span>
|
||||||
{{else if denied}}
|
{{else if denied}}
|
||||||
<span class="label label-danger">@UI.Search_Request_denied</span>
|
<span class="label label-danger">@UI.Search_Request_denied</span>
|
||||||
{{#if deniedReason}}
|
{{#if deniedReason}}
|
||||||
<span class="customTooltip" title="{{deniedReason}}"><i class="fa fa-info-circle"></i></span>
|
<span class="customTooltip" title="{{deniedReason}}"><i class="fa fa-info-circle"></i></span>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{else}}
|
{{else}}
|
||||||
<span class="label label-warning">@UI.Search_Pending_approval</span>
|
<span class="label label-warning">@UI.Search_Pending_approval</span>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -199,7 +199,6 @@
|
||||||
{{#if denied}}
|
{{#if denied}}
|
||||||
<div>
|
<div>
|
||||||
Denied: <i style="color:red;" class="fa fa-check"></i>
|
Denied: <i style="color:red;" class="fa fa-check"></i>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue