Feature #26: User Flags
Flags can be associated with Users. Includes minor updates to Job Queues and improved visibility of user information.
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Users.UserFlags;
|
||||
using Disco.Services.Web;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class UserFlagAssignmentController : AuthorizedDatabaseController
|
||||
{
|
||||
const string pComments = "comments";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var userFlagAssignment = Database.UserFlagAssignments.FirstOrDefault(a => a.Id == id);
|
||||
if (userFlagAssignment != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pComments:
|
||||
UpdateComments(userFlagAssignment, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid User Flag Assignment Id");
|
||||
}
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return Redirect(string.Format("{0}#UserDetailTab-Flags", Url.Action(MVC.User.Show(userFlagAssignment.UserId))));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
[DiscoAuthorizeAny(Claims.User.Actions.EditFlags)]
|
||||
public virtual ActionResult UpdateComments(int id, string Comments = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pComments, Comments, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateComments(UserFlagAssignment userFlagAssignment, string Comments)
|
||||
{
|
||||
if (!userFlagAssignment.CanEditComments())
|
||||
throw new InvalidOperationException("Editing comments for user flags is denied");
|
||||
|
||||
userFlagAssignment.OnEditComments(Comments);
|
||||
Database.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
|
||||
[DiscoAuthorizeAny(Claims.User.Actions.AddFlags)]
|
||||
public virtual ActionResult AddUser(int id, string UserId, string Comments)
|
||||
{
|
||||
var userFlag = UserFlagService.GetUserFlag(id);
|
||||
if (userFlag == null)
|
||||
throw new ArgumentException("Invalid User Flag Id", "id");
|
||||
|
||||
var user = Database.Users.Include("UserFlagAssignments").FirstOrDefault(u => u.UserId == UserId);
|
||||
if (user == null)
|
||||
throw new ArgumentException("Invalid User Id", "UserId");
|
||||
|
||||
if (!user.CanAddUserFlag(userFlag))
|
||||
throw new InvalidOperationException("Adding user flag is denied");
|
||||
|
||||
var userFlagAssignment = user.OnAddUserFlag(Database, userFlag, CurrentUser, Comments);
|
||||
Database.SaveChanges();
|
||||
|
||||
return Redirect(string.Format("{0}#UserDetailTab-Flags", Url.Action(MVC.User.Show(user.UserId))));
|
||||
}
|
||||
|
||||
[DiscoAuthorizeAny(Claims.User.Actions.RemoveFlags)]
|
||||
public virtual ActionResult RemoveUser(int id)
|
||||
{
|
||||
var userFlagAssignment = Database.UserFlagAssignments.FirstOrDefault(a => a.Id == id);
|
||||
if (userFlagAssignment == null)
|
||||
throw new ArgumentException("Invalid User Flag Assignment Id", "id");
|
||||
|
||||
if (!userFlagAssignment.CanRemove())
|
||||
throw new InvalidOperationException("Removing user flag assignment is denied");
|
||||
|
||||
userFlagAssignment.OnRemove(CurrentUser);
|
||||
Database.SaveChanges();
|
||||
|
||||
return Redirect(string.Format("{0}#UserDetailTab-Flags", Url.Action(MVC.User.Show(userFlagAssignment.UserId))));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Users.UserFlags;
|
||||
using Disco.Services.Web;
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class UserFlagController : AuthorizedDatabaseController
|
||||
{
|
||||
const string pName = "name";
|
||||
const string pDescription = "description";
|
||||
const string pIcon = "icon";
|
||||
const string pIconColour = "iconcolour";
|
||||
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Configure)]
|
||||
public virtual ActionResult Update(int id, string key, string value = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
Authorization.Require(Claims.Config.UserFlag.Configure);
|
||||
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var flag = Database.UserFlags.Find(id);
|
||||
if (flag != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pName:
|
||||
UpdateName(flag, value);
|
||||
break;
|
||||
case pDescription:
|
||||
UpdateDescription(flag, value);
|
||||
break;
|
||||
case pIcon:
|
||||
UpdateIcon(flag, value);
|
||||
break;
|
||||
case pIconColour:
|
||||
UpdateIconColour(flag, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid User Flag Id");
|
||||
}
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.UserFlag.Index(flag.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Configure)]
|
||||
public virtual ActionResult UpdateName(int id, string FlagName = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pName, FlagName, redirect);
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Configure)]
|
||||
public virtual ActionResult UpdateDescription(int id, string Description = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Configure)]
|
||||
public virtual ActionResult UpdateIcon(int id, string Icon = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pIcon, Icon, redirect);
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Configure)]
|
||||
public virtual ActionResult UpdateIconColour(int id, string IconColour = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pIconColour, IconColour, redirect);
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Configure)]
|
||||
public virtual ActionResult UpdateIconAndColour(int id, string Icon = null, string IconColour = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
|
||||
var UserFlag = Database.UserFlags.Find(id);
|
||||
if (UserFlag != null)
|
||||
{
|
||||
UpdateIconAndColour(UserFlag, Icon, IconColour);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Invalid User Flag Id", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.UserFlag.Index(UserFlag.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateIconAndColour(UserFlag UserFlag, string Icon, string IconColour)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Icon))
|
||||
throw new ArgumentNullException("Icon");
|
||||
if (string.IsNullOrWhiteSpace(IconColour))
|
||||
throw new ArgumentNullException("IconColour");
|
||||
|
||||
UserFlag.Icon = Icon;
|
||||
UserFlag.IconColour = IconColour;
|
||||
UserFlagService.Update(Database, UserFlag);
|
||||
}
|
||||
private void UpdateIcon(UserFlag UserFlag, string Icon)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Icon))
|
||||
throw new ArgumentNullException("Icon");
|
||||
|
||||
UserFlag.Icon = Icon;
|
||||
UserFlagService.Update(Database, UserFlag);
|
||||
}
|
||||
private void UpdateIconColour(UserFlag UserFlag, string IconColour)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(IconColour))
|
||||
throw new ArgumentNullException("IconColour");
|
||||
|
||||
UserFlag.IconColour = IconColour;
|
||||
UserFlagService.Update(Database, UserFlag);
|
||||
}
|
||||
|
||||
private void UpdateName(UserFlag UserFlag, string Name)
|
||||
{
|
||||
UserFlag.Name = Name;
|
||||
UserFlagService.Update(Database, UserFlag);
|
||||
}
|
||||
|
||||
private void UpdateDescription(UserFlag UserFlag, string Description)
|
||||
{
|
||||
UserFlag.Description = Description;
|
||||
UserFlagService.Update(Database, UserFlag);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
[DiscoAuthorize(Claims.Config.UserFlag.Delete)]
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jq = Database.UserFlags.Find(id);
|
||||
if (jq != null)
|
||||
{
|
||||
|
||||
var status = UserFlagDeleteTask.ScheduleNow(id);
|
||||
status.SetFinishedUrl(Url.Action(MVC.Config.UserFlag.Index(null)));
|
||||
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.Logging.TaskStatus(status.SessionId));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid User Flag Id");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user