From ab71fa3101cbd2a190145cf9325c15ba4e79ae9d Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 27 Jun 2017 15:44:55 +1000 Subject: [PATCH] adds the IsCurrentUser flag to the UserDisplay model --- .../IsCurrentUserModelFilterAttribute.cs | 56 +++++++++++++++++++ src/Umbraco.Web/Editors/UsersController.cs | 2 + .../Models/ContentEditing/UserDisplay.cs | 8 +++ src/Umbraco.Web/Umbraco.Web.csproj | 1 + 4 files changed, 67 insertions(+) create mode 100644 src/Umbraco.Web/Editors/IsCurrentUserModelFilterAttribute.cs diff --git a/src/Umbraco.Web/Editors/IsCurrentUserModelFilterAttribute.cs b/src/Umbraco.Web/Editors/IsCurrentUserModelFilterAttribute.cs new file mode 100644 index 0000000000..86676b8b4e --- /dev/null +++ b/src/Umbraco.Web/Editors/IsCurrentUserModelFilterAttribute.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Net.Http; +using System.Web.Http.Filters; +using Umbraco.Web.Models.ContentEditing; + +namespace Umbraco.Web.Editors +{ + /// + /// This sets the IsCurrentUser property on any outgoing model or any collection of models + /// + internal class IsCurrentUserModelFilterAttribute : ActionFilterAttribute + { + public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) + { + if (actionExecutedContext.Response == null) return; + + var user = UmbracoContext.Current.Security.CurrentUser; + if (user == null) return; + + var objectContent = actionExecutedContext.Response.Content as ObjectContent; + if (objectContent != null) + { + var model = objectContent.Value as UserDisplay; + if (model != null) + { + model.IsCurrentUser = (int) model.Id == user.Id; + } + else + { + var collection = objectContent.Value as IEnumerable; + if (collection != null) + { + foreach (var userDisplay in collection) + { + userDisplay.IsCurrentUser = (int) userDisplay.Id == user.Id; + } + } + else + { + var paged = objectContent.Value as UsersController.PagedUserResult; + if (paged != null) + { + foreach (var userDisplay in paged.Items) + { + userDisplay.IsCurrentUser = (int)userDisplay.Id == user.Id; + } + } + } + } + + } + + base.OnActionExecuted(actionExecutedContext); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/UsersController.cs b/src/Umbraco.Web/Editors/UsersController.cs index d42bd4f7e9..cb91852a9a 100644 --- a/src/Umbraco.Web/Editors/UsersController.cs +++ b/src/Umbraco.Web/Editors/UsersController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.IO; using System.Linq; using System.Net; @@ -37,6 +38,7 @@ namespace Umbraco.Web.Editors [PluginController("UmbracoApi")] [UmbracoApplicationAuthorize(Constants.Applications.Users)] [PrefixlessBodyModelValidator] + [IsCurrentUserModelFilter] public class UsersController : UmbracoAuthorizedJsonController { /// diff --git a/src/Umbraco.Web/Models/ContentEditing/UserDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/UserDisplay.cs index b747c7f348..095a0fe13b 100644 --- a/src/Umbraco.Web/Models/ContentEditing/UserDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/UserDisplay.cs @@ -39,6 +39,14 @@ namespace Umbraco.Web.Models.ContentEditing /// If the password is reset on save, this value will be populated /// [DataMember(Name = "resetPasswordValue")] + [ReadOnly(true)] public string ResetPasswordValue { get; set; } + + /// + /// This is an info flag to denote if this object is the equivalent of the currently logged in user + /// + [DataMember(Name = "isCurrentUser")] + [ReadOnly(true)] + public bool IsCurrentUser { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 51819e114f..7ccac65e44 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -331,6 +331,7 @@ +