Merge pull request #740 from tidusjar/dev

Dev
This commit is contained in:
Jamie 2016-12-08 14:25:49 +00:00 committed by GitHub
commit 3782cf7029
9 changed files with 30 additions and 54 deletions

View file

@ -58,7 +58,6 @@ namespace PlexRequests.Core.SettingModels
public int MovieWeeklyRequestLimit { get; set; } public int MovieWeeklyRequestLimit { get; set; }
public int TvWeeklyRequestLimit { get; set; } public int TvWeeklyRequestLimit { get; set; }
public int AlbumWeeklyRequestLimit { get; set; } public int AlbumWeeklyRequestLimit { get; set; }
public string NoApprovalUsers { get; set; }
public bool CollectAnalyticData { get; set; } public bool CollectAnalyticData { get; set; }
public bool IgnoreNotifyForAutoApprovedRequests { get; set; } public bool IgnoreNotifyForAutoApprovedRequests { get; set; }
public bool Wizard { get; set; } public bool Wizard { get; set; }
@ -75,26 +74,5 @@ namespace PlexRequests.Core.SettingModels
public string ThemeName { get; set; } public string ThemeName { get; set; }
public string ApiKey { get; set; } public string ApiKey { get; set; }
[JsonIgnore]
public List<string> ApprovalWhiteList
{
get
{
var users = new List<string>();
if (string.IsNullOrEmpty(NoApprovalUsers))
{
return users;
}
var splitUsers = NoApprovalUsers.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var user in splitUsers)
{
if (!string.IsNullOrWhiteSpace(user))
users.Add(user.Trim());
}
return users;
}
}
} }
} }

View file

@ -54,7 +54,7 @@ namespace PlexRequests.Helpers.Analytics
Track(HitType.@event, username, cat, act, label, clientId, value); Track(HitType.@event, username, cat, act, label, clientId, value);
} }
public async Task TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null) public async void TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null)
{ {
var cat = category.ToString(); var cat = category.ToString();
var act = action.ToString(); var act = action.ToString();
@ -146,7 +146,7 @@ namespace PlexRequests.Helpers.Analytics
request.Method = RequestMethod; request.Method = RequestMethod;
// set the Content-Length header to the correct value // set the Content-Length header to the correct value
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString); request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
#if !DEBUG
// write the request body to the request // write the request body to the request
using (var writer = new StreamWriter(request.GetRequestStream())) using (var writer = new StreamWriter(request.GetRequestStream()))
{ {
@ -165,6 +165,7 @@ namespace PlexRequests.Helpers.Analytics
{ {
Log.Error(ex, "Analytics tracking failed"); Log.Error(ex, "Analytics tracking failed");
} }
#endif
} }
private async Task SendRequestAsync(string postDataString) private async Task SendRequestAsync(string postDataString)
{ {
@ -173,6 +174,7 @@ namespace PlexRequests.Helpers.Analytics
// set the Content-Length header to the correct value // set the Content-Length header to the correct value
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString); request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
#if !DEBUG
// write the request body to the request // write the request body to the request
using (var writer = new StreamWriter(await request.GetRequestStreamAsync())) using (var writer = new StreamWriter(await request.GetRequestStreamAsync()))
{ {
@ -191,6 +193,7 @@ namespace PlexRequests.Helpers.Analytics
{ {
Log.Error(ex, "Analytics tracking failed"); Log.Error(ex, "Analytics tracking failed");
} }
#endif
} }
private Dictionary<string, string> BuildRequestData(HitType type, string username, string category, string action, string clientId, string label, int? value, string exceptionDescription, int? fatal) private Dictionary<string, string> BuildRequestData(HitType type, string username, string category, string action, string clientId, string label, int? value, string exceptionDescription, int? fatal)

View file

@ -38,6 +38,7 @@ namespace PlexRequests.Helpers.Analytics
Issues, Issues,
UserLogin, UserLogin,
Services, Services,
Navbar Navbar,
UserManagement
} }
} }

View file

@ -51,7 +51,7 @@ namespace PlexRequests.Helpers.Analytics
/// <param name="clientId">The client identifier.</param> /// <param name="clientId">The client identifier.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
/// <returns></returns> /// <returns></returns>
Task TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null); void TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null);
/// <summary> /// <summary>
/// Tracks the page view. /// Tracks the page view.

View file

@ -302,11 +302,6 @@ namespace PlexRequests.Services.Jobs
public bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings, List<string> username) public bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings, List<string> username)
{ {
if (prSettings.ApprovalWhiteList.Intersect(username).Any())
{
return true;
}
foreach (var user in username) foreach (var user in username)
{ {
var admin = Security.HasPermissions(user, Permissions.Administrator); var admin = Security.HasPermissions(user, Permissions.Administrator);

View file

@ -37,8 +37,8 @@ using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Core.StatusChecker; using PlexRequests.Core.StatusChecker;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using PlexRequests.Helpers.Analytics;
using PlexRequests.Helpers.Permissions; using PlexRequests.Helpers.Permissions;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Models; using PlexRequests.UI.Models;
using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions; using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions;
@ -46,10 +46,11 @@ namespace PlexRequests.UI.Modules.Admin
{ {
public class SystemStatusModule : BaseModule public class SystemStatusModule : BaseModule
{ {
public SystemStatusModule(ISettingsService<PlexRequestSettings> settingsService, ICacheProvider cache, ISettingsService<SystemSettings> ss, ISecurityExtensions security) : base("admin", settingsService, security) public SystemStatusModule(ISettingsService<PlexRequestSettings> settingsService, ICacheProvider cache, ISettingsService<SystemSettings> ss, ISecurityExtensions security, IAnalytics a) : base("admin", settingsService, security)
{ {
Cache = cache; Cache = cache;
SystemSettings = ss; SystemSettings = ss;
Analytics = a;
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx); Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
@ -61,6 +62,7 @@ namespace PlexRequests.UI.Modules.Admin
private ICacheProvider Cache { get; } private ICacheProvider Cache { get; }
private ISettingsService<SystemSettings> SystemSettings { get; } private ISettingsService<SystemSettings> SystemSettings { get; }
private IAnalytics Analytics { get; }
private async Task<Negotiator> Status() private async Task<Negotiator> Status()
{ {
@ -99,8 +101,10 @@ namespace PlexRequests.UI.Modules.Admin
private async Task<Response> Save() private async Task<Response> Save()
{ {
var settings = this.Bind<SystemSettings>(); var settings = this.Bind<SystemSettings>();
Analytics.TrackEventAsync(Category.Admin, PlexRequests.Helpers.Analytics.Action.Update, $"Updated Branch {EnumHelper<Branches>.GetDisplayValue(settings.Branch)}", Username, CookieHelper.GetAnalyticClientId(Cookies));
await SystemSettings.SaveSettingsAsync(settings); await SystemSettings.SaveSettingsAsync(settings);
// Clear the cache // Clear the cache
@ -111,6 +115,8 @@ namespace PlexRequests.UI.Modules.Admin
private Response AutoUpdate() private Response AutoUpdate()
{ {
Analytics.TrackEventAsync(Category.Admin, PlexRequests.Helpers.Analytics.Action.Update, "AutoUpdate", Username, CookieHelper.GetAnalyticClientId(Cookies));
var url = Request.Form["url"]; var url = Request.Form["url"];
var startInfo = Type.GetType("Mono.Runtime") != null var startInfo = Type.GetType("Mono.Runtime") != null

View file

@ -1234,7 +1234,7 @@ namespace PlexRequests.UI.Modules
if (IsAdmin) if (IsAdmin)
return true; return true;
if (s.ApprovalWhiteList.Contains(Username)) if (ShouldAutoApprove(type,s,Username))
return true; return true;
var requestLimit = GetRequestLimitForType(type, s); var requestLimit = GetRequestLimitForType(type, s);
@ -1376,8 +1376,8 @@ namespace PlexRequests.UI.Modules
public bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings, string username) public bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings, string username)
{ {
var admin = Security.HasPermissions(Context.CurrentUser, Permissions.Administrator); var admin = Security.HasPermissions(Context.CurrentUser, Permissions.Administrator);
// if the user is an admin or they are whitelisted, they go ahead and allow auto-approval // if the user is an admin, they go ahead and allow auto-approval
if (admin || prSettings.ApprovalWhiteList.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase))) return true; if (admin) return true;
// check by request type if the category requires approval or not // check by request type if the category requires approval or not
switch (requestType) switch (requestType)

View file

@ -13,12 +13,13 @@ using PlexRequests.Core;
using PlexRequests.Core.Models; using PlexRequests.Core.Models;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using PlexRequests.Helpers.Analytics;
using PlexRequests.Helpers.Permissions; using PlexRequests.Helpers.Permissions;
using PlexRequests.Store; using PlexRequests.Store;
using PlexRequests.Store.Models; using PlexRequests.Store.Models;
using PlexRequests.Store.Repository; using PlexRequests.Store.Repository;
using PlexRequests.UI.Models; using PlexRequests.UI.Models;
using Action = PlexRequests.Helpers.Analytics.Action;
using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions; using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions;
namespace PlexRequests.UI.Modules namespace PlexRequests.UI.Modules
@ -26,7 +27,7 @@ namespace PlexRequests.UI.Modules
public class UserManagementModule : BaseModule public class UserManagementModule : BaseModule
{ {
public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex, IRepository<UserLogins> userLogins, IPlexUserRepository plexRepo public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex, IRepository<UserLogins> userLogins, IPlexUserRepository plexRepo
, ISecurityExtensions security, IRequestService req) : base("usermanagement", pr, security) , ISecurityExtensions security, IRequestService req, IAnalytics ana) : base("usermanagement", pr, security)
{ {
#if !DEBUG #if !DEBUG
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx); Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
@ -38,11 +39,12 @@ namespace PlexRequests.UI.Modules
PlexUsersRepository = plexRepo; PlexUsersRepository = plexRepo;
PlexRequestSettings = pr; PlexRequestSettings = pr;
RequestService = req; RequestService = req;
Analytics = ana;
Get["/"] = x => Load(); Get["/"] = x => Load();
Get["/users", true] = async (x, ct) => await LoadUsers(); Get["/users", true] = async (x, ct) => await LoadUsers();
Post["/createuser"] = x => CreateUser(); Post["/createuser", true] = async (x,ct) => await CreateUser();
Get["/local/{id}"] = x => LocalDetails((Guid)x.id); Get["/local/{id}"] = x => LocalDetails((Guid)x.id);
Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id); Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id);
Get["/permissions"] = x => GetEnum<Permissions>(); Get["/permissions"] = x => GetEnum<Permissions>();
@ -58,6 +60,7 @@ namespace PlexRequests.UI.Modules
private IPlexUserRepository PlexUsersRepository { get; } private IPlexUserRepository PlexUsersRepository { get; }
private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; } private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; }
private IRequestService RequestService { get; } private IRequestService RequestService { get; }
private IAnalytics Analytics { get; }
private Negotiator Load() private Negotiator Load()
{ {
@ -103,8 +106,9 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(model); return Response.AsJson(model);
} }
private Response CreateUser() private async Task<Response> CreateUser()
{ {
Analytics.TrackEventAsync(Category.UserManagement, Action.Create, "Created User", Username, CookieHelper.GetAnalyticClientId(Cookies));
var body = Request.Body.AsString(); var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body)) if (string.IsNullOrEmpty(body))
{ {
@ -122,7 +126,7 @@ namespace PlexRequests.UI.Modules
}); });
} }
var users = UserMapper.GetUsers(); var users = await UserMapper.GetUsersAsync();
if (users.Any(x => x.UserName.Equals(model.Username, StringComparison.CurrentCultureIgnoreCase))) if (users.Any(x => x.UserName.Equals(model.Username, StringComparison.CurrentCultureIgnoreCase)))
{ {
return Response.AsJson(new JsonResponseModel return Response.AsJson(new JsonResponseModel
@ -158,6 +162,7 @@ namespace PlexRequests.UI.Modules
private async Task<Response> UpdateUser() private async Task<Response> UpdateUser()
{ {
Analytics.TrackEventAsync(Category.UserManagement, Action.Update, "Updated User", Username, CookieHelper.GetAnalyticClientId(Cookies));
var body = Request.Body.AsString(); var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body)) if (string.IsNullOrEmpty(body))
{ {
@ -276,6 +281,7 @@ namespace PlexRequests.UI.Modules
private Response DeleteUser() private Response DeleteUser()
{ {
Analytics.TrackEventAsync(Category.UserManagement, Action.Delete, "Deleted User", Username, CookieHelper.GetAnalyticClientId(Cookies));
var body = Request.Body.AsString(); var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body)) if (string.IsNullOrEmpty(body))
{ {

View file

@ -199,19 +199,6 @@
</div> </div>
</div> </div>
<p class="form-group">A comma separated list of users whose requests do not require approval (These users also do not have a request limit).</p>
<div class="form-group">
<label for="NoApprovalUsers" class="control-label">Approval White listed Users</label>
<div>
<input type="text" class="form-control-custom form-control " id="NoApprovalUsers" name="NoApprovalUsers" placeholder="e.g. John, Bobby" value="@Model.NoApprovalUsers">
</div>
</div>
<p class="form-group">If the request limits are set to 0 then no request limit is applied.</p> <p class="form-group">If the request limits are set to 0 then no request limit is applied.</p>
<div class="form-group"> <div class="form-group">
<label for="MovieWeeklyRequestLimit" class="control-label">Movie Weekly Request Limit</label> <label for="MovieWeeklyRequestLimit" class="control-label">Movie Weekly Request Limit</label>