Change IUserClaimStore to take IEnumerable<Claims>

https://github.com/aspnet/Identity/issues/163
This commit is contained in:
Hao Kung 2014-08-08 11:55:55 -07:00
parent 097925a3e4
commit 2a45a851a5
7 changed files with 128 additions and 60 deletions

View File

@ -422,36 +422,41 @@ namespace Microsoft.AspNet.Identity.EntityFramework
return Task.FromResult((IList<Claim>)result);
}
public Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = new CancellationToken())
public Task AddClaimsAsync(TUser user, IEnumerable<Claim> 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<TKey> { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
}
UserClaims.Add(new IdentityUserClaim<TKey> { 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<Claim> 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);
}

View File

@ -27,19 +27,19 @@ namespace Microsoft.AspNet.Identity
/// Add a new user claim
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <param name="claims"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken));
Task AddClaimsAsync(TUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Remove a user claim
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <param name="claims"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task RemoveClaimAsync(TUser user, Claim claim,
Task RemoveClaimsAsync(TUser user, IEnumerable<Claim> claims,
CancellationToken cancellationToken = default(CancellationToken));
}
}

View File

@ -895,7 +895,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="claim"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> AddClaimAsync(TUser user, Claim claim,
public virtual Task<IdentityResult> 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);
}
/// <summary>
/// Add a user claim
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> AddClaimsAsync(TUser user, IEnumerable<Claim> 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
/// <param name="claim"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> RemoveClaimAsync(TUser user, Claim claim,
public virtual Task<IdentityResult> 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);
}
/// <summary>
/// Remove a user claim
/// </summary>
/// <param name="user"></param>
/// <param name="claims"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> RemoveClaimsAsync(TUser user, IEnumerable<Claim> 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);
}

View File

@ -358,13 +358,13 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
}
/// <summary>
/// Add a claim to a user
/// Add claims to a user
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <param name="claims"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task AddClaimsAsync(TUser user, IEnumerable<Claim> 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);
}
/// <summary>
/// Remove a claim from a user
/// Remove claims from a user
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <param name="claims"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task RemoveClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task RemoveClaimsAsync(TUser user, IEnumerable<Claim> 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 =

View File

@ -57,18 +57,18 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
{
var store = new InMemoryUserStore(new InMemoryContext());
store.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddLoginAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddToRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetClaimsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetLoginsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetRolesAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.IsInRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveLoginAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(
async () => await store.RemoveFromRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByLoginAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByIdAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByNameAsync(null));
@ -95,8 +95,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.CreateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.UpdateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.DeleteAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.AddClaimAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.AddClaimsAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.RemoveClaimsAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetClaimsAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetLoginsAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetRolesAsync(null));
@ -115,10 +115,10 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetSecurityStampAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await store.SetSecurityStampAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("claim",
async () => await store.AddClaimAsync(new InMemoryUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("claim",
async () => await store.RemoveClaimAsync(new InMemoryUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("claims",
async () => await store.AddClaimsAsync(new InMemoryUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("claims",
async () => await store.RemoveClaimsAsync(new InMemoryUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("login",
async () => await store.AddLoginAsync(new InMemoryUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("login",

View File

@ -171,18 +171,18 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
{
var store = new UserStore(new IdentityDbContext());
store.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddLoginAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.AddToRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetClaimsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetLoginsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetRolesAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.IsInRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveLoginAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(
async () => await store.RemoveFromRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.RemoveClaimsAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByLoginAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByIdAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByNameAsync(null));
@ -209,8 +209,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.CreateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.UpdateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.DeleteAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.AddClaimAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.AddClaimsAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.RemoveClaimsAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetClaimsAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetLoginsAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetRolesAsync(null));
@ -229,10 +229,10 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetSecurityStampAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await store.SetSecurityStampAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("claim",
async () => await store.AddClaimAsync(new IdentityUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("claim",
async () => await store.RemoveClaimAsync(new IdentityUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("claims",
async () => await store.AddClaimsAsync(new IdentityUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("claims",
async () => await store.RemoveClaimsAsync(new IdentityUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("login",
async () => await store.AddLoginAsync(new IdentityUser("fake"), null));
await Assert.ThrowsAsync<ArgumentNullException>("login",

View File

@ -39,20 +39,26 @@ namespace Microsoft.AspNet.Identity.InMemory
return Task.FromResult<IList<Claim>>(claims);
}
public Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
public Task AddClaimsAsync(TUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default(CancellationToken))
{
user.Claims.Add(new IdentityUserClaim<string> { ClaimType = claim.Type, ClaimValue = claim.Value, UserId = user.Id });
foreach (var claim in claims)
{
user.Claims.Add(new IdentityUserClaim<string> { 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<Claim> 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);
}