#456 Update all the requests when we identify that the username changes

pull/733/head
Jamie.Rees 8 years ago
parent 3d2272cd91
commit d5127073bc

@ -8,7 +8,11 @@ namespace PlexRequests.Core
public interface ISecurityExtensions
{
Response AdminLoginRedirect(Permissions perm, NancyContext context);
Response AdminLoginRedirect(NancyContext context, params Permissions[] perm);
bool DoesNotHavePermissions(Permissions perm, IUserIdentity currentUser);
Response HasAnyPermissionsRedirect(NancyContext context, string routeName, HttpStatusCode code,
params Permissions[] perm);
bool DoesNotHavePermissions(int perm, IUserIdentity currentUser);
Func<NancyContext, Response> ForbiddenIfNot(Func<NancyContext, bool> test);
bool HasAnyPermissions(IUserIdentity user, params Permissions[] perm);

@ -184,7 +184,31 @@ namespace PlexRequests.Core
var r = response(context);
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;
}
@ -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).
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!
/// <summary>

@ -45,7 +45,8 @@ namespace PlexRequests.Services.Jobs
{
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;
JobRecord = rec;
@ -53,6 +54,7 @@ namespace PlexRequests.Services.Jobs
PlexSettings = plexSettings;
PlexRequestSettings = prSettings;
UserManagementSettings = umSettings;
RequestService = requestService;
}
private IJobRecord JobRecord { get; }
@ -61,7 +63,7 @@ namespace PlexRequests.Services.Jobs
private ISettingsService<PlexSettings> PlexSettings { get; }
private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; }
private ISettingsService<UserManagementSettings> UserManagementSettings { get; }
private IRequestService RequestService { get; }
public void Execute(IJobExecutionContext context)
{
@ -76,6 +78,7 @@ namespace PlexRequests.Services.Jobs
}
var plexUsers = PlexApi.GetUsers(settings.PlexAuthToken);
var userManagementSettings = UserManagementSettings.GetSettings();
var requests = RequestService.GetAll().ToList();
var dbUsers = Repo.GetAll().ToList();
foreach (var user in plexUsers.User)
@ -84,6 +87,7 @@ namespace PlexRequests.Services.Jobs
if (dbUser != null)
{
var needToUpdate = false;
var usernameChanged = false;
// Do we need up update any info?
if (dbUser.EmailAddress != user.Email)
@ -95,10 +99,30 @@ namespace PlexRequests.Services.Jobs
{
dbUser.Username = user.Username;
needToUpdate = true;
usernameChanged = true;
}
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);
}

@ -72,7 +72,21 @@ namespace PlexRequests.Services.Notification
Log.Debug("Notifying Users Count {0}", users.Count);
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)
{
Log.Info("Notifying user {0}", user);

@ -56,10 +56,8 @@ namespace PlexRequests.UI.Modules
ISettingsService<HeadphonesSettings> hpSettings, IHeadphonesApi hpApi, ISettingsService<PlexRequestSettings> pr, ITransientFaultQueue faultQueue
, ISecurityExtensions security) : base("approval", pr, security)
{
this.RequiresAnyClaim(UserClaims.Admin, UserClaims.PowerUser);
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
Before += (ctx) => Security.AdminLoginRedirect(Permissions.ManageRequests, ctx);
Before += (ctx) => Security.AdminLoginRedirect(ctx, Permissions.Administrator,Permissions.ManageRequests);
Service = service;
CpService = cpService;

@ -101,6 +101,9 @@ namespace PlexRequests.UI.Modules
private string _username;
/// <summary>
/// Returns the Username or UserAlias
/// </summary>
protected string Username
{
get

@ -180,18 +180,18 @@
<div>
<span>Request status: </span>
{{#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}}
{{#if approved}}
<span class="label label-info">@UI.Search_Processing_Request</span>
{{else if denied}}
<span class="label label-danger">@UI.Search_Request_denied</span>
{{#if deniedReason}}
<span class="customTooltip" title="{{deniedReason}}"><i class="fa fa-info-circle"></i></span>
{{/if}}
{{else}}
<span class="label label-warning">@UI.Search_Pending_approval</span>
{{/if}}
{{#if approved}}
<span class="label label-info">@UI.Search_Processing_Request</span>
{{else if denied}}
<span class="label label-danger">@UI.Search_Request_denied</span>
{{#if deniedReason}}
<span class="customTooltip" title="{{deniedReason}}"><i class="fa fa-info-circle"></i></span>
{{/if}}
{{else}}
<span class="label label-warning">@UI.Search_Pending_approval</span>
{{/if}}
{{/if}}
</div>
</div>
@ -199,7 +199,6 @@
{{#if denied}}
<div>
Denied: <i style="color:red;" class="fa fa-check"></i>
</div>
{{/if}}

Loading…
Cancel
Save