From a92288cd61025afa64bb9cc134574391088fcf3e Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 20 Jul 2017 22:12:13 +1000 Subject: [PATCH] Fixes implementation of the role store on the BackOfficeUserStore --- .../Security/BackOfficeUserStore.cs | 48 ++++--------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/src/Umbraco.Core/Security/BackOfficeUserStore.cs b/src/Umbraco.Core/Security/BackOfficeUserStore.cs index 05f7020804..686f0c5b07 100644 --- a/src/Umbraco.Core/Security/BackOfficeUserStore.cs +++ b/src/Umbraco.Core/Security/BackOfficeUserStore.cs @@ -398,30 +398,16 @@ namespace Umbraco.Core.Security /// /// public Task AddToRoleAsync(BackOfficeIdentityUser user, string roleName) - { + { ThrowIfDisposed(); if (user == null) throw new ArgumentNullException("user"); + if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be null or whitespace.", "roleName"); - var currentRoles = user.Roles.Select(x => x.RoleId).ToArray(); + var userRole = user.Roles.SingleOrDefault(r => r.RoleId == roleName); - if (currentRoles.InvariantContains(roleName)) return Task.FromResult(0); - - var asInt = user.Id.TryConvertTo(); - if (asInt == false) + if (userRole == null) { - throw new InvalidOperationException("The user id must be an integer to work with the Umbraco"); - } - - var foundUser = _userService.GetUserById(asInt.Result); - - if (foundUser != null) - { - var foundGroup = _userService.GetUserGroupByAlias(roleName); - - if (foundGroup != null) - { - foundUser.AddGroup(foundGroup.ToReadOnlyGroup()); - } + user.AddRole(roleName); } return Task.FromResult(0); @@ -433,30 +419,16 @@ namespace Umbraco.Core.Security /// /// public Task RemoveFromRoleAsync(BackOfficeIdentityUser user, string roleName) - { + { ThrowIfDisposed(); if (user == null) throw new ArgumentNullException("user"); + if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be null or whitespace.", "roleName"); - var currentRoles = user.Roles.Select(x => x.RoleId).ToArray(); + var userRole = user.Roles.SingleOrDefault(r => r.RoleId == roleName); - if (currentRoles.InvariantContains(roleName) == false) return Task.FromResult(0); - - var asInt = user.Id.TryConvertTo(); - if (asInt == false) + if (userRole != null) { - throw new InvalidOperationException("The user id must be an integer to work with the Umbraco"); - } - - var foundUser = _userService.GetUserById(asInt.Result); - - if (foundUser != null) - { - var foundGroup = _userService.GetUserGroupByAlias(roleName); - - if (foundGroup != null) - { - foundUser.RemoveGroup(foundGroup.Alias); - } + user.Roles.Remove(userRole); } return Task.FromResult(0);