From e8fc2a766b66e9ac0e1399f5fcf6f3b9d9367231 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 5 May 2020 11:33:19 +0200 Subject: [PATCH] Implemented GetUsersInRoleAsync and reintroduced email check --- .../Install/InstallSteps/NewInstallStep.cs | 9 +++--- .../Security/BackOfficeUserStore.cs | 31 ++++++++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs b/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs index b83d0b3a93..fedb93d6f1 100644 --- a/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs @@ -56,6 +56,11 @@ namespace Umbraco.Web.Install.InstallSteps { throw new InvalidOperationException("Could not find the super user!"); } + admin.Email = user.Email.Trim(); + admin.Name = user.Name.Trim(); + admin.Username = user.Email.Trim(); + + _userService.Save(admin); var userManager = _httpContextAccessor.GetRequiredHttpContext().GetOwinContext().GetBackOfficeUserManager(); var membershipUser = await userManager.FindByIdAsync(Constants.Security.SuperUserId.ToString()); @@ -74,11 +79,7 @@ namespace Umbraco.Web.Install.InstallSteps if (!resetResult.Succeeded) throw new InvalidOperationException("Could not reset password: " + string.Join(", ", resetResult.Errors.ToErrorMessage())); - admin.Email = user.Email.Trim(); - admin.Name = user.Name.Trim(); - admin.Username = user.Email.Trim(); - _userService.Save(admin); if (user.SubscribeToNewsLetter) { diff --git a/src/Umbraco.Web/Security/BackOfficeUserStore.cs b/src/Umbraco.Web/Security/BackOfficeUserStore.cs index a62e9cc89f..6f401163aa 100644 --- a/src/Umbraco.Web/Security/BackOfficeUserStore.cs +++ b/src/Umbraco.Web/Security/BackOfficeUserStore.cs @@ -66,7 +66,7 @@ namespace Umbraco.Web.Security cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) throw new ArgumentNullException(nameof(user)); - + return Task.FromResult(user.Id.ToString()); } @@ -152,7 +152,7 @@ namespace Umbraco.Web.Security cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) throw new ArgumentNullException(nameof(user)); - + var found = _userService.GetUserById(user.Id); if (found != null) { @@ -184,7 +184,7 @@ namespace Umbraco.Web.Security cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) throw new ArgumentNullException(nameof(user)); - + var found = _userService.GetUserById(user.Id); if (found != null) { @@ -293,7 +293,7 @@ namespace Umbraco.Web.Security cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) throw new ArgumentNullException(nameof(user)); - // if (email.IsNullOrWhiteSpace()) throw new ArgumentNullException("email"); + if (email.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(email)); user.Email = email; @@ -540,9 +540,24 @@ namespace Umbraco.Web.Security return Task.FromResult(user.Roles.Select(x => x.RoleId).InvariantContains(normalizedRoleName)); } - public Task> GetUsersInRoleAsync(string normalizedRoleName, CancellationToken cancellationToken) + /// + /// Lists all users of a given role. + /// + /// + /// Identity Role names are equal to Umbraco UserGroup alias. + /// + public Task> GetUsersInRoleAsync(string normalizedRoleName, CancellationToken cancellationToken = default(CancellationToken)) { - throw new NotImplementedException(); + cancellationToken.ThrowIfCancellationRequested(); + ThrowIfDisposed(); + if (normalizedRoleName == null) throw new ArgumentNullException(nameof(normalizedRoleName)); + + var userGroup = _userService.GetUserGroupByAlias(normalizedRoleName); + + var users = _userService.GetAllInGroup(userGroup.Id); + IList backOfficeIdentityUsers = users.Select(x => _mapper.Map(x)).ToList(); + + return Task.FromResult(backOfficeIdentityUsers); } /// @@ -875,7 +890,7 @@ namespace Umbraco.Web.Security return anythingChanged; } - + private void ThrowIfDisposed() { if (_disposed) throw new ObjectDisposedException(GetType().Name); @@ -891,7 +906,7 @@ namespace Umbraco.Web.Security return Task.FromResult(false); } - + private static int UserIdToInt(string userId) { var attempt = userId.TryConvertTo();