Fixed issue #10, role names are consistent between user.Roles and role.Name.

This commit is contained in:
Judah Himango CW
2019-06-25 22:36:54 -05:00
parent b85b2ad846
commit c2d200f559
6 changed files with 51 additions and 19 deletions
+2 -1
View File
@@ -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;
}
}
}
+13 -11
View File
@@ -386,17 +386,12 @@ namespace Raven.Identity
/// <inheritdoc />
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<IdentityRole>(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<IdentityRole>.GetRavenIdFromRoleName(roleName, DbSession.Advanced.DocumentStore);
var roleOrNull = await DbSession.LoadAsync<IdentityRole>(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));
}
/// <inheritdoc />
+27 -1
View File
@@ -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<string> roleNames)
{
using (var dbSession = docStore.OpenSession())
{
var roleIds = roleNames.Select(r => "IdentityRoles/" + r);
var roles = dbSession.Load<Raven.Identity.IdentityRole>(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;
}
}
}
+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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";
/// <summary>
/// The user's full name.
+5 -2
View File
@@ -65,8 +65,11 @@ namespace Sample.Mvc
app.UseCookiePolicy();
// Create the database if it doesn't exist.
app.ApplicationServices.GetRequiredService<IDocumentStore>().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<IDocumentStore>();
docStore.EnsureExists();
docStore.EnsureRolesExist(new List<string> { AppUser.AdminRole, AppUser.ManagerRole });
app.UseMvc(routes =>
{
routes.MapRoute(