From e4f9c2916d52028bd15061a0f5ba42a3bf6c58cd Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Wed, 12 Dec 2018 14:27:20 +0100 Subject: [PATCH] Use "groups", not "roles" + return richer group models from API --- .../src/common/resources/content.resource.js | 8 ++--- .../content/content.protect.controller.js | 27 +++++++++-------- .../src/views/content/protect.html | 16 +++++----- src/Umbraco.Web.UI/Umbraco/config/lang/da.xml | 8 ++--- src/Umbraco.Web.UI/Umbraco/config/lang/en.xml | 8 ++--- .../Umbraco/config/lang/en_us.xml | 8 ++--- src/Umbraco.Web/Editors/ContentController.cs | 29 ++++++++++--------- .../Models/ContentEditing/PublicAccess.cs | 7 ++--- 8 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js index 75e1877be8..b807a4dc31 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js @@ -990,21 +990,21 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) { * * * @param {Int} contentId The content Id - * @param {Array} roles The roles that should have access (if using role based protection) + * @param {Array} groups The names of the groups that should have access (if using group based protection) * @param {Array} usernames The usernames of the members that should have access (if using member based protection) * @param {Int} loginPageId The Id of the login page * @param {Int} errorPageId The Id of the error page * @returns {Promise} resourcePromise object containing the public access protection * */ - updatePublicAccess: function (contentId, roles, usernames, loginPageId, errorPageId) { + updatePublicAccess: function (contentId, groups, usernames, loginPageId, errorPageId) { var publicAccess = { contentId: contentId, loginPageId: loginPageId, errorPageId: errorPageId }; - if (angular.isArray(roles) && roles.length) { - publicAccess.roles = roles; + if (angular.isArray(groups) && groups.length) { + publicAccess.groups = groups; } else if (angular.isArray(usernames) && usernames.length) { publicAccess.usernames = usernames; diff --git a/src/Umbraco.Web.UI.Client/src/views/content/content.protect.controller.js b/src/Umbraco.Web.UI.Client/src/views/content/content.protect.controller.js index 988cd10739..8d80f308ab 100644 --- a/src/Umbraco.Web.UI.Client/src/views/content/content.protect.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/content/content.protect.controller.js @@ -36,7 +36,7 @@ // init the current settings for public access (if any) vm.loginPage = publicAccess.loginPage; vm.errorPage = publicAccess.errorPage; - vm.roles = publicAccess.roles || []; + vm.groups = publicAccess.groups || []; vm.members = publicAccess.members || []; vm.canRemove = true; @@ -44,8 +44,8 @@ vm.type = "member"; next(); } - else if (vm.roles.length) { - vm.type = "role"; + else if (vm.groups.length) { + vm.type = "group"; next(); } else { @@ -55,16 +55,14 @@ } function next() { - if (vm.type === "role") { + if (vm.type === "group") { vm.loading = true; - // Get all member groups + // get all existing member groups for lookup upon selection + // NOTE: if/when member groups support infinite editing, we can't rely on using a cached lookup list of valid groups anymore memberGroupResource.getGroups().then(function (groups) { vm.step = vm.type; vm.allGroups = groups; vm.hasGroups = groups.length > 0; - vm.groups = _.filter(groups, function(group) { - return _.contains(vm.roles, group.name); - }); vm.loading = false; }); } @@ -83,7 +81,7 @@ if (!vm.loginPage || !vm.errorPage) { return false; } - if (vm.type === "role") { + if (vm.type === "group") { return vm.groups && vm.groups.length > 0; } if (vm.type === "member") { @@ -94,9 +92,9 @@ function save() { vm.buttonState = "busy"; - var roles = _.map(vm.groups, function (group) { return group.name; }); + var groups = _.map(vm.groups, function (group) { return group.name; }); var usernames = _.map(vm.members, function (member) { return member.username; }); - contentResource.updatePublicAccess(id, roles, usernames, vm.loginPage.id, vm.errorPage.id).then( + contentResource.updatePublicAccess(id, groups, usernames, vm.loginPage.id, vm.errorPage.id).then( function () { localizationService.localize("publicAccess_paIsProtected", [$scope.currentNode.name]).then(function (value) { vm.success = { @@ -130,9 +128,10 @@ ? model.selectedMemberGroups : [model.selectedMemberGroup]; _.each(selectedGroupIds, - function(groupId) { + function (groupId) { + // find the group in the lookup list and add it if it isn't already var group = _.find(vm.allGroups, function(g) { return g.id === parseInt(groupId); }); - if (group && !_.contains(vm.groups, group)) { + if (group && !_.find(vm.groups, function (g) { return g.id === group.id })) { vm.groups.push(group); } }); @@ -147,7 +146,7 @@ } function removeGroup(group) { - vm.groups = _.without(vm.groups, group); + vm.groups = _.reject(vm.groups, function(g) { return g.id === group.id }); } function pickMember() { diff --git a/src/Umbraco.Web.UI.Client/src/views/content/protect.html b/src/Umbraco.Web.UI.Client/src/views/content/protect.html index f179f1c63c..ae4a15e8c1 100644 --- a/src/Umbraco.Web.UI.Client/src/views/content/protect.html +++ b/src/Umbraco.Web.UI.Client/src/views/content/protect.html @@ -29,11 +29,11 @@
- + -
@@ -55,12 +55,12 @@ -
-

You need to create a membergroup before you can use role-based authentication

+
+

You need to create a member group before you can use group based authentication

-
-

Pick the roles who have access to this page

+
+

Select the groups that should have access to this page

Indsæt, men fjern formattering som ikke bør være på en webside (Anbefales) - Rollebaseret beskyttelse - Hvis du ønsker at kontrollere adgang til siden ved hjælp af rollebaseret godkendelse via Umbracos medlemsgrupper. - Du skal oprette en medlemsgruppe før du kan bruge rollebaseret godkendelse + Gruppebaseret beskyttelse + Hvis du ønsker at give adgang til alle medlemmer af specifikke medlemsgrupper + Du skal oprette en medlemsgruppe før du kan bruge gruppebaseret beskyttelse Fejlside Brugt når folk er logget ind, men ingen adgang %0% skal beskyttes]]> @@ -885,7 +885,7 @@ Mange hilsner fra Umbraco robotten Fjern beskyttelse... %0%?]]> Vælg siderne der indeholder log ind-formularer og fejlmeddelelser - %0%]]> + %0%]]> %0%]]> Adgang til enkelte medlemmer Hvis du ønsker at give adgang til enkelte medlemmer diff --git a/src/Umbraco.Web.UI/Umbraco/config/lang/en.xml b/src/Umbraco.Web.UI/Umbraco/config/lang/en.xml index f61186a98f..51ea51685f 100644 --- a/src/Umbraco.Web.UI/Umbraco/config/lang/en.xml +++ b/src/Umbraco.Web.UI/Umbraco/config/lang/en.xml @@ -1124,9 +1124,9 @@ To manage your website, simply open the Umbraco back office and start adding con Paste, but remove formatting (Recommended) - Role based protection - If you wish to control access to the page using role-based authentication, using Umbraco's member groups - You need to create a membergroup before you can use role-based authentication + Group based protection + If you want to grant access to all members of specific member groups + You need to create a member group before you can use group based authentication Error Page Used when people are logged on, but do not have access %0%]]> @@ -1137,7 +1137,7 @@ To manage your website, simply open the Umbraco back office and start adding con Remove protection... %0%?]]> Select the pages that contain login form and error messages - %0%]]> + %0%]]> %0%]]> Specific members protection If you wish to grant access to specific members diff --git a/src/Umbraco.Web.UI/Umbraco/config/lang/en_us.xml b/src/Umbraco.Web.UI/Umbraco/config/lang/en_us.xml index 1953a351be..b5efba5a32 100644 --- a/src/Umbraco.Web.UI/Umbraco/config/lang/en_us.xml +++ b/src/Umbraco.Web.UI/Umbraco/config/lang/en_us.xml @@ -1146,9 +1146,9 @@ To manage your website, simply open the Umbraco back office and start adding con Paste, but remove formatting (Recommended) - Role based protection - If you wish to control access to the page using role-based authentication, using Umbraco's member groups - You need to create a membergroup before you can use role-based authentication + Group based protection + If you want to grant access to all members of specific member groups + You need to create a member group before you can use group based authentication Error Page Used when people are logged on, but do not have access %0%]]> @@ -1159,7 +1159,7 @@ To manage your website, simply open the Umbraco back office and start adding con Remove protection... %0%?]]> Select the pages that contain login form and error messages - %0%]]> + %0%]]> %0%]]> Specific members protection If you wish to grant access to specific members diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index 3f595f9cbc..d1cb15e76d 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -2201,15 +2201,18 @@ namespace Umbraco.Web.Editors break; } - var roles = entry.Rules + var allGroups = Services.MemberGroupService.GetAll().ToArray(); + var groups = entry.Rules .Where(rule => rule.RuleType == Constants.Conventions.PublicAccess.MemberRoleRuleType) - .Select(rule => rule.RuleValue) + .Select(rule => allGroups.FirstOrDefault(g => g.Name == rule.RuleValue)) + .Where(memberGroup => memberGroup != null) + .Select(Mapper.Map) .ToArray(); return Request.CreateResponse(HttpStatusCode.OK, new PublicAccess { Members = members, - Roles = roles, + Groups = groups, LoginPage = loginPageEntity != null ? Mapper.Map(loginPageEntity) : null, ErrorPage = errorPageEntity != null ? Mapper.Map(errorPageEntity) : null }); @@ -2218,9 +2221,9 @@ namespace Umbraco.Web.Editors // set up public access using role based access [EnsureUserPermissionForContent("contentId", ActionProtect.ActionLetter)] [HttpPost] - public HttpResponseMessage PostPublicAccess(int contentId, [FromUri]string[] roles, [FromUri]string[] usernames, int loginPageId, int errorPageId) + public HttpResponseMessage PostPublicAccess(int contentId, [FromUri]string[] groups, [FromUri]string[] usernames, int loginPageId, int errorPageId) { - if ((roles == null || roles.Any() == false) && (usernames == null || usernames.Any() == false)) + if ((groups == null || groups.Any() == false) && (usernames == null || usernames.Any() == false)) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest)); } @@ -2233,11 +2236,11 @@ namespace Umbraco.Web.Editors throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest)); } - var isRoleBased = roles != null && roles.Any(); - var candidateRuleValues = isRoleBased - ? roles + var isGroupBased = groups != null && groups.Any(); + var candidateRuleValues = isGroupBased + ? groups : usernames; - var newRuleType = isRoleBased + var newRuleType = isGroupBased ? Constants.Conventions.PublicAccess.MemberRoleRuleType : Constants.Conventions.PublicAccess.MemberUsernameRuleType; @@ -2247,9 +2250,9 @@ namespace Umbraco.Web.Editors { entry = new PublicAccessEntry(content, loginPage, errorPage, new List()); - foreach (var role in candidateRuleValues) + foreach (var ruleValue in candidateRuleValues) { - entry.AddRule(role, newRuleType); + entry.AddRule(ruleValue, newRuleType); } } else @@ -2272,9 +2275,9 @@ namespace Umbraco.Web.Editors { entry.RemoveRule(rule); } - foreach (var role in newRuleValues) + foreach (var ruleValue in newRuleValues) { - entry.AddRule(role, newRuleType); + entry.AddRule(ruleValue, newRuleType); } } diff --git a/src/Umbraco.Web/Models/ContentEditing/PublicAccess.cs b/src/Umbraco.Web/Models/ContentEditing/PublicAccess.cs index b8035c9f25..dcf2dcae92 100644 --- a/src/Umbraco.Web/Models/ContentEditing/PublicAccess.cs +++ b/src/Umbraco.Web/Models/ContentEditing/PublicAccess.cs @@ -5,11 +5,8 @@ namespace Umbraco.Web.Models.ContentEditing [DataContract(Name = "publicAccess", Namespace = "")] public class PublicAccess { - //[DataMember(Name = "userName")] - //public string UserName { get; set; } - - [DataMember(Name = "roles")] - public string[] Roles { get; set; } + [DataMember(Name = "groups")] + public MemberGroupDisplay[] Groups { get; set; } [DataMember(Name = "loginPage")] public EntityBasic LoginPage { get; set; }