From 2a45a851a52811d78350fa38e7eec4236e4abf78 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Fri, 8 Aug 2014 11:55:55 -0700 Subject: [PATCH] Change IUserClaimStore to take IEnumerable https://github.com/aspnet/Identity/issues/163 --- .../UserStore.cs | 27 +++++---- .../IUserClaimStore.cs | 8 +-- src/Microsoft.AspNet.Identity/UserManager.cs | 59 +++++++++++++++++-- .../InMemoryUserStore.cs | 36 ++++++----- .../InMemoryUserStoreTest.cs | 18 +++--- .../UserStoreTest.cs | 18 +++--- .../InMemoryUserStore.cs | 22 ++++--- 7 files changed, 128 insertions(+), 60 deletions(-) diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs b/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs index 173dbf30a4..5071adfb4e 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs @@ -422,36 +422,41 @@ namespace Microsoft.AspNet.Identity.EntityFramework return Task.FromResult((IList)result); } - public Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = new CancellationToken()) + public Task AddClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = new CancellationToken()) { ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException("user"); } - if (claim == null) + if (claims == null) { - throw new ArgumentNullException("claim"); + throw new ArgumentNullException("claims"); + } + foreach (var claim in claims) + { + UserClaims.Add(new IdentityUserClaim { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value }); } - UserClaims.Add(new IdentityUserClaim { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value }); return Task.FromResult(0); } - public Task RemoveClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = new CancellationToken()) + public Task RemoveClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = new CancellationToken()) { ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException("user"); } - if (claim == null) + if (claims == null) { - throw new ArgumentNullException("claim"); + throw new ArgumentNullException("claims"); } - var claims = UserClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList(); - foreach (var c in claims) - { - UserClaims.Remove(c); + foreach (var claim in claims) { + var matchedClaims = UserClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList(); + foreach (var c in matchedClaims) + { + UserClaims.Remove(c); + } } return Task.FromResult(0); } diff --git a/src/Microsoft.AspNet.Identity/IUserClaimStore.cs b/src/Microsoft.AspNet.Identity/IUserClaimStore.cs index 1a50c112bd..ae483223fc 100644 --- a/src/Microsoft.AspNet.Identity/IUserClaimStore.cs +++ b/src/Microsoft.AspNet.Identity/IUserClaimStore.cs @@ -27,19 +27,19 @@ namespace Microsoft.AspNet.Identity /// Add a new user claim /// /// - /// + /// /// /// - Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)); + Task AddClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = default(CancellationToken)); /// /// Remove a user claim /// /// - /// + /// /// /// - Task RemoveClaimAsync(TUser user, Claim claim, + Task RemoveClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = default(CancellationToken)); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Identity/UserManager.cs b/src/Microsoft.AspNet.Identity/UserManager.cs index c7df9db3d8..f2845161d3 100644 --- a/src/Microsoft.AspNet.Identity/UserManager.cs +++ b/src/Microsoft.AspNet.Identity/UserManager.cs @@ -895,7 +895,7 @@ namespace Microsoft.AspNet.Identity /// /// /// - public virtual async Task AddClaimAsync(TUser user, Claim claim, + public virtual Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); @@ -908,7 +908,30 @@ namespace Microsoft.AspNet.Identity { throw new ArgumentNullException("user"); } - await claimStore.AddClaimAsync(user, claim, cancellationToken); + return AddClaimsAsync(user, new Claim[] { claim }, cancellationToken); + } + + /// + /// Add a user claim + /// + /// + /// + /// + /// + public virtual async Task AddClaimsAsync(TUser user, IEnumerable claims, + CancellationToken cancellationToken = default(CancellationToken)) + { + ThrowIfDisposed(); + var claimStore = GetClaimStore(); + if (claims == null) + { + throw new ArgumentNullException("claims"); + } + if (user == null) + { + throw new ArgumentNullException("user"); + } + await claimStore.AddClaimsAsync(user, claims, cancellationToken); return await UpdateAsync(user, cancellationToken); } @@ -919,7 +942,7 @@ namespace Microsoft.AspNet.Identity /// /// /// - public virtual async Task RemoveClaimAsync(TUser user, Claim claim, + public virtual Task RemoveClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); @@ -928,7 +951,35 @@ namespace Microsoft.AspNet.Identity { throw new ArgumentNullException("user"); } - await claimStore.RemoveClaimAsync(user, claim, cancellationToken); + if (claim == null) + { + throw new ArgumentNullException("claim"); + } + return RemoveClaimsAsync(user, new Claim[] { claim }, cancellationToken); + } + + + /// + /// Remove a user claim + /// + /// + /// + /// + /// + public virtual async Task RemoveClaimsAsync(TUser user, IEnumerable claims, + CancellationToken cancellationToken = default(CancellationToken)) + { + ThrowIfDisposed(); + var claimStore = GetClaimStore(); + if (user == null) + { + throw new ArgumentNullException("user"); + } + if (claims == null) + { + throw new ArgumentNullException("claims"); + } + await claimStore.RemoveClaimsAsync(user, claims, cancellationToken); return await UpdateAsync(user, cancellationToken); } diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStore.cs b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStore.cs index 636ffe2d96..202b51dbcb 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStore.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStore.cs @@ -358,13 +358,13 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test } /// - /// Add a claim to a user + /// Add claims to a user /// /// - /// + /// /// /// - public virtual Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)) + public virtual Task AddClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); @@ -372,22 +372,25 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test { throw new ArgumentNullException("user"); } - if (claim == null) + if (claims == null) { - throw new ArgumentNullException("claim"); + throw new ArgumentNullException("claims"); + } + foreach (var claim in claims) + { + user.Claims.Add(new TUserClaim { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value }); } - user.Claims.Add(new TUserClaim { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value }); return Task.FromResult(0); } /// - /// Remove a claim from a user + /// Remove claims from a user /// /// - /// + /// /// /// - public virtual Task RemoveClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)) + public virtual Task RemoveClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); @@ -395,15 +398,18 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test { throw new ArgumentNullException("user"); } - if (claim == null) + if (claims == null) { - throw new ArgumentNullException("claim"); + throw new ArgumentNullException("claims"); } - var claims = - user.Claims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList(); - foreach (var c in claims) + foreach (var claim in claims) { - user.Claims.Remove(c); + var matchingClaims = + user.Claims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList(); + foreach (var c in matchingClaims) + { + user.Claims.Remove(c); + } } // TODO:these claims might not exist in the dbset //var query = diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStoreTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStoreTest.cs index c92b9f3592..e8557a046b 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryUserStoreTest.cs @@ -57,18 +57,18 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test { var store = new InMemoryUserStore(new InMemoryContext()); store.Dispose(); - await Assert.ThrowsAsync(async () => await store.AddClaimAsync(null, null)); + await Assert.ThrowsAsync(async () => await store.AddClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.AddLoginAsync(null, null)); await Assert.ThrowsAsync(async () => await store.AddToRoleAsync(null, null)); await Assert.ThrowsAsync(async () => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync(async () => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync(async () => await store.GetRolesAsync(null)); await Assert.ThrowsAsync(async () => await store.IsInRoleAsync(null, null)); - await Assert.ThrowsAsync(async () => await store.RemoveClaimAsync(null, null)); + await Assert.ThrowsAsync(async () => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.RemoveLoginAsync(null, null)); await Assert.ThrowsAsync( async () => await store.RemoveFromRoleAsync(null, null)); - await Assert.ThrowsAsync(async () => await store.RemoveClaimAsync(null, null)); + await Assert.ThrowsAsync(async () => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.FindByLoginAsync(null)); await Assert.ThrowsAsync(async () => await store.FindByIdAsync(null)); await Assert.ThrowsAsync(async () => await store.FindByNameAsync(null)); @@ -95,8 +95,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test await Assert.ThrowsAsync("user", async () => await store.CreateAsync(null)); await Assert.ThrowsAsync("user", async () => await store.UpdateAsync(null)); await Assert.ThrowsAsync("user", async () => await store.DeleteAsync(null)); - await Assert.ThrowsAsync("user", async () => await store.AddClaimAsync(null, null)); - await Assert.ThrowsAsync("user", async () => await store.RemoveClaimAsync(null, null)); + await Assert.ThrowsAsync("user", async () => await store.AddClaimsAsync(null, null)); + await Assert.ThrowsAsync("user", async () => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync("user", async () => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync("user", async () => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync("user", async () => await store.GetRolesAsync(null)); @@ -115,10 +115,10 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test await Assert.ThrowsAsync("user", async () => await store.GetSecurityStampAsync(null)); await Assert.ThrowsAsync("user", async () => await store.SetSecurityStampAsync(null, null)); - await Assert.ThrowsAsync("claim", - async () => await store.AddClaimAsync(new InMemoryUser("fake"), null)); - await Assert.ThrowsAsync("claim", - async () => await store.RemoveClaimAsync(new InMemoryUser("fake"), null)); + await Assert.ThrowsAsync("claims", + async () => await store.AddClaimsAsync(new InMemoryUser("fake"), null)); + await Assert.ThrowsAsync("claims", + async () => await store.RemoveClaimsAsync(new InMemoryUser("fake"), null)); await Assert.ThrowsAsync("login", async () => await store.AddLoginAsync(new InMemoryUser("fake"), null)); await Assert.ThrowsAsync("login", diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs index e0b71cd17f..e6418cd42d 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs @@ -171,18 +171,18 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test { var store = new UserStore(new IdentityDbContext()); store.Dispose(); - await Assert.ThrowsAsync(async () => await store.AddClaimAsync(null, null)); + await Assert.ThrowsAsync(async () => await store.AddClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.AddLoginAsync(null, null)); await Assert.ThrowsAsync(async () => await store.AddToRoleAsync(null, null)); await Assert.ThrowsAsync(async () => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync(async () => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync(async () => await store.GetRolesAsync(null)); await Assert.ThrowsAsync(async () => await store.IsInRoleAsync(null, null)); - await Assert.ThrowsAsync(async () => await store.RemoveClaimAsync(null, null)); + await Assert.ThrowsAsync(async () => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.RemoveLoginAsync(null, null)); await Assert.ThrowsAsync( async () => await store.RemoveFromRoleAsync(null, null)); - await Assert.ThrowsAsync(async () => await store.RemoveClaimAsync(null, null)); + await Assert.ThrowsAsync(async () => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.FindByLoginAsync(null)); await Assert.ThrowsAsync(async () => await store.FindByIdAsync(null)); await Assert.ThrowsAsync(async () => await store.FindByNameAsync(null)); @@ -209,8 +209,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test await Assert.ThrowsAsync("user", async () => await store.CreateAsync(null)); await Assert.ThrowsAsync("user", async () => await store.UpdateAsync(null)); await Assert.ThrowsAsync("user", async () => await store.DeleteAsync(null)); - await Assert.ThrowsAsync("user", async () => await store.AddClaimAsync(null, null)); - await Assert.ThrowsAsync("user", async () => await store.RemoveClaimAsync(null, null)); + await Assert.ThrowsAsync("user", async () => await store.AddClaimsAsync(null, null)); + await Assert.ThrowsAsync("user", async () => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync("user", async () => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync("user", async () => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync("user", async () => await store.GetRolesAsync(null)); @@ -229,10 +229,10 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test await Assert.ThrowsAsync("user", async () => await store.GetSecurityStampAsync(null)); await Assert.ThrowsAsync("user", async () => await store.SetSecurityStampAsync(null, null)); - await Assert.ThrowsAsync("claim", - async () => await store.AddClaimAsync(new IdentityUser("fake"), null)); - await Assert.ThrowsAsync("claim", - async () => await store.RemoveClaimAsync(new IdentityUser("fake"), null)); + await Assert.ThrowsAsync("claims", + async () => await store.AddClaimsAsync(new IdentityUser("fake"), null)); + await Assert.ThrowsAsync("claims", + async () => await store.RemoveClaimsAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync("login", async () => await store.AddLoginAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync("login", diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/InMemoryUserStore.cs b/test/Microsoft.AspNet.Identity.InMemory.Test/InMemoryUserStore.cs index 85a3f4ad4a..c9923bcf9d 100644 --- a/test/Microsoft.AspNet.Identity.InMemory.Test/InMemoryUserStore.cs +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/InMemoryUserStore.cs @@ -39,20 +39,26 @@ namespace Microsoft.AspNet.Identity.InMemory return Task.FromResult>(claims); } - public Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)) + public Task AddClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = default(CancellationToken)) { - user.Claims.Add(new IdentityUserClaim { ClaimType = claim.Type, ClaimValue = claim.Value, UserId = user.Id }); + foreach (var claim in claims) + { + user.Claims.Add(new IdentityUserClaim { ClaimType = claim.Type, ClaimValue = claim.Value, UserId = user.Id }); + } return Task.FromResult(0); } - public Task RemoveClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken)) + public Task RemoveClaimsAsync(TUser user, IEnumerable claims, CancellationToken cancellationToken = default(CancellationToken)) { - var entity = - user.Claims.FirstOrDefault( - uc => uc.UserId == user.Id && uc.ClaimType == claim.Type && uc.ClaimValue == claim.Value); - if (entity != null) + foreach (var claim in claims) { - user.Claims.Remove(entity); + var entity = + user.Claims.FirstOrDefault( + uc => uc.UserId == user.Id && uc.ClaimType == claim.Type && uc.ClaimValue == claim.Value); + if (entity != null) + { + user.Claims.Remove(entity); + } } return Task.FromResult(0); }