From c2d200f5598b71a3056df4b0914f22cd9b80abf8 Mon Sep 17 00:00:00 2001 From: Judah Himango CW Date: Tue, 25 Jun 2019 22:36:54 -0500 Subject: [PATCH] Fixed issue #10, role names are consistent between user.Roles and role.Name. --- RavenDB.Identity/RoleStore.cs | 3 ++- RavenDB.Identity/UserStore.cs | 24 +++++++++-------- Samples/Mvc/Common/RavenExtensions.cs | 28 +++++++++++++++++++- Samples/Mvc/Controllers/AccountController.cs | 4 +-- Samples/Mvc/Models/AppUser.cs | 4 +-- Samples/Mvc/Startup.cs | 7 +++-- 6 files changed, 51 insertions(+), 19 deletions(-) diff --git a/RavenDB.Identity/RoleStore.cs b/RavenDB.Identity/RoleStore.cs index 46ef498..9883eb2 100644 --- a/RavenDB.Identity/RoleStore.cs +++ b/RavenDB.Identity/RoleStore.cs @@ -413,8 +413,9 @@ namespace Raven.Identity internal static string GetRavenIdFromRoleName(string role, IDocumentStore docStore) { + var roleCollection = docStore.Conventions.GetCollectionName(typeof(TRole)); var partSeparator = docStore.Conventions.IdentityPartsSeparator; - return "IdentityRole" + partSeparator + role; + return roleCollection + partSeparator + role; } } } diff --git a/RavenDB.Identity/UserStore.cs b/RavenDB.Identity/UserStore.cs index f5d0043..7108d9a 100644 --- a/RavenDB.Identity/UserStore.cs +++ b/RavenDB.Identity/UserStore.cs @@ -386,17 +386,12 @@ namespace Raven.Identity /// public async Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken) { - ThrowIfNullDisposedCancelled(user, cancellationToken); - - var roleNameLowered = roleName.ToLowerInvariant(); - if (!user.Roles.Contains(roleNameLowered, StringComparer.OrdinalIgnoreCase)) - { - user.GetRolesList().Add(roleNameLowered); - } + ThrowIfNullDisposedCancelled(user, cancellationToken); - // See if we have an IdentityRole with that. + // See if we have an IdentityRole with that name. var identityUserPrefix = DbSession.Advanced.DocumentStore.Conventions.GetCollectionName(typeof(IdentityRole)); var identityPartSeperator = DbSession.Advanced.DocumentStore.Conventions.IdentityPartsSeparator; + var roleNameLowered = roleName.ToLowerInvariant(); var roleId = identityUserPrefix + identityPartSeperator + roleNameLowered; var existingRoleOrNull = await this.DbSession.LoadAsync(roleId, cancellationToken); if (existingRoleOrNull == null) @@ -406,7 +401,14 @@ namespace Raven.Identity await this.DbSession.StoreAsync(existingRoleOrNull, roleId, cancellationToken); } - if (!existingRoleOrNull.Users.Contains(user.Id, StringComparer.OrdinalIgnoreCase)) + // Use the real name (not normalized/uppered/lowered) of the role, as specified by the user. + var roleRealName = existingRoleOrNull.Name; + if (!user.Roles.Contains(roleRealName, StringComparer.InvariantCultureIgnoreCase)) + { + user.GetRolesList().Add(roleRealName); + } + + if (!existingRoleOrNull.Users.Contains(user.Id, StringComparer.InvariantCultureIgnoreCase)) { existingRoleOrNull.Users.Add(user.Id); } @@ -417,7 +419,7 @@ namespace Raven.Identity { ThrowIfNullDisposedCancelled(user, cancellationToken); - user.GetRolesList().RemoveAll(r => string.Equals(r, roleName, StringComparison.OrdinalIgnoreCase)); + user.GetRolesList().RemoveAll(r => string.Equals(r, roleName, StringComparison.InvariantCultureIgnoreCase)); var roleId = RoleStore.GetRavenIdFromRoleName(roleName, DbSession.Advanced.DocumentStore); var roleOrNull = await DbSession.LoadAsync(roleId, cancellationToken); @@ -443,7 +445,7 @@ namespace Raven.Identity throw new ArgumentNullException(nameof(roleName)); } - return Task.FromResult(user.Roles.Contains(roleName, StringComparer.OrdinalIgnoreCase)); + return Task.FromResult(user.Roles.Contains(roleName, StringComparer.InvariantCultureIgnoreCase)); } /// diff --git a/Samples/Mvc/Common/RavenExtensions.cs b/Samples/Mvc/Common/RavenExtensions.cs index 68b527d..68d6472 100644 --- a/Samples/Mvc/Common/RavenExtensions.cs +++ b/Samples/Mvc/Common/RavenExtensions.cs @@ -1,4 +1,5 @@ -using Raven.Client.Documents; +using Microsoft.AspNetCore.Identity; +using Raven.Client.Documents; using Sample.Mvc.Models; using System; using System.Collections.Generic; @@ -28,5 +29,30 @@ namespace Sample.Mvc.Common return store; } + + public static IDocumentStore EnsureRolesExist(this IDocumentStore docStore, List roleNames) + { + using (var dbSession = docStore.OpenSession()) + { + var roleIds = roleNames.Select(r => "IdentityRoles/" + r); + var roles = dbSession.Load(roleIds); + foreach (var idRolePair in roles) + { + if (idRolePair.Value == null) + { + var id = idRolePair.Key; + var roleName = id.Replace("IdentityRoles/", string.Empty); + dbSession.Store(new Raven.Identity.IdentityRole(roleName), id); + } + } + + if (roles.Any(i => i.Value == null)) + { + dbSession.SaveChanges(); + } + } + + return docStore; + } } } diff --git a/Samples/Mvc/Controllers/AccountController.cs b/Samples/Mvc/Controllers/AccountController.cs index 9a5cafc..e8b8dc9 100644 --- a/Samples/Mvc/Controllers/AccountController.cs +++ b/Samples/Mvc/Controllers/AccountController.cs @@ -66,8 +66,8 @@ namespace Sample.Mvc.Controllers // Create the user. var appUser = new AppUser { - UserName = model.Email, - Email = model.Email + Email = model.Email, + UserName = model.Email }; var createUserResult = await this.userManager.CreateAsync(appUser, model.Password); if (!createUserResult.Succeeded) diff --git a/Samples/Mvc/Models/AppUser.cs b/Samples/Mvc/Models/AppUser.cs index d9c7e94..eadadcc 100644 --- a/Samples/Mvc/Models/AppUser.cs +++ b/Samples/Mvc/Models/AppUser.cs @@ -7,8 +7,8 @@ namespace Sample.Mvc.Models { public class AppUser : Raven.Identity.IdentityUser { - public const string AdminRole = "admin"; - public const string ManagerRole = "manager"; + public const string AdminRole = "Admin"; + public const string ManagerRole = "Manager"; /// /// The user's full name. diff --git a/Samples/Mvc/Startup.cs b/Samples/Mvc/Startup.cs index 6e2413b..f9498ae 100644 --- a/Samples/Mvc/Startup.cs +++ b/Samples/Mvc/Startup.cs @@ -65,8 +65,11 @@ namespace Sample.Mvc app.UseCookiePolicy(); // Create the database if it doesn't exist. - app.ApplicationServices.GetRequiredService().EnsureExists(); - + // Also, create our roles if they don't exist. Needed because we're doing some role-based auth in this demo. + var docStore = app.ApplicationServices.GetRequiredService(); + docStore.EnsureExists(); + docStore.EnsureRolesExist(new List { AppUser.AdminRole, AppUser.ManagerRole }); + app.UseMvc(routes => { routes.MapRoute(