Skip to content

Commit

Permalink
#5 basic query functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmelendez committed Sep 21, 2020
1 parent e941ce6 commit 5e09622
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ElCamino.AspNetCore.Identity.AzureTable/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static class RowKeyConstants

#region Identity Role
public const string PreFixIdentityRole = "R_";
public const string PreFixIdentityRoleUpperBound = "S_";
public const string PreFixIdentityRoleClaim = "C_";
public const string FormatterIdentityRole = PreFixIdentityRole + "{0}";
public const string FormatterIdentityRoleClaim = PreFixIdentityRoleClaim + "{0}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public abstract class BaseKeyHelper : IKeyHelper

public virtual string PreFixIdentityRole => Constants.RowKeyConstants.PreFixIdentityRole;

public virtual string PreFixIdentityRoleUpperBound => Constants.RowKeyConstants.PreFixIdentityRoleUpperBound;

public virtual string PreFixIdentityRoleClaim => Constants.RowKeyConstants.PreFixIdentityRoleClaim;

public virtual string FormatterIdentityRole => Constants.RowKeyConstants.FormatterIdentityRole;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface IKeyHelper

#region Identity Role
string PreFixIdentityRole { get; }
string PreFixIdentityRoleUpperBound { get; }
string PreFixIdentityRoleClaim { get; }
string FormatterIdentityRole { get; }
string FormatterIdentityRoleClaim { get; }
Expand Down
15 changes: 14 additions & 1 deletion src/ElCamino.AspNetCore.Identity.AzureTable/RoleStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ public class RoleStore<TRole, TKey, TUserRole, TRoleClaim, TContext> :
private CloudTable _roleTable;
private IdentityErrorDescriber _errorDescriber = new IdentityErrorDescriber();
protected IKeyHelper _keyHelper;
private readonly string FilterString;

public RoleStore(TContext context, IKeyHelper keyHelper) : base(new IdentityErrorDescriber())
{
Context = context ?? throw new ArgumentNullException(nameof(context));
_roleTable = context.RoleTable;
_keyHelper = keyHelper;

FilterString = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.GreaterThanOrEqual, _keyHelper.PreFixIdentityRole),
TableOperators.And,
TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.LessThan, _keyHelper.PreFixIdentityRoleUpperBound));

}

public Task<bool> CreateTableIfNotExistsAsync()
Expand Down Expand Up @@ -261,12 +268,18 @@ await Task.WhenAll(

public TContext Context { get; private set; }

/// <summary>
/// Queries will be slow unless they include Partition and/or Row keys
/// </summary>
public override IQueryable<TRole> Roles
{
get
{
throw new NotImplementedException();
TableQuery<TRole> tableQuery = _roleTable.CreateQuery<TRole>();
tableQuery.FilterString = FilterString;
return tableQuery.AsQueryable();
}
}

}
}
25 changes: 24 additions & 1 deletion src/ElCamino.AspNetCore.Identity.AzureTable/UserOnlyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,39 @@ public class UserOnlyStore<TUser, TContext, TKey, TUserClaim, TUserLogin, TUserT

private IdentityConfiguration _config = null;

private readonly string FilterString;

public UserOnlyStore(TContext context, IKeyHelper keyHelper, IdentityConfiguration config) : base(new IdentityErrorDescriber())
{
Context = context ?? throw new ArgumentNullException(nameof(context));
_userTable = context.UserTable;
_indexTable = context.IndexTable;
_keyHelper = keyHelper;
_config = config;

string partitionFilter = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.GreaterThanOrEqual, _keyHelper.PreFixIdentityUserId),
TableOperators.And,
TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.LessThan, _keyHelper.PreFixIdentityUserIdUpperBound));
string rowFilter = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.GreaterThanOrEqual, _keyHelper.PreFixIdentityUserId),
TableOperators.And,
TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.LessThan, _keyHelper.PreFixIdentityUserIdUpperBound));
FilterString = TableQuery.CombineFilters(partitionFilter, TableOperators.And, rowFilter);
}

public override IQueryable<TUser> Users => throw new NotImplementedException();
/// <summary>
/// Queries will be slow unless they include Partition and/or Row keys
/// </summary>
public override IQueryable<TUser> Users
{
get
{
TableQuery<TUser> tableQuery = _userTable.CreateQuery<TUser>();
tableQuery.FilterString = FilterString;
return tableQuery.AsQueryable();
}
}

public virtual async Task<bool> CreateTablesIfNotExistsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@ public virtual async Task FindUserByName()
output.WriteLine("FindByNameAsync: {0} seconds", sw.Elapsed.TotalSeconds);

Assert.Equal(user.UserName, result.UserName);

sw.Reset();
sw.Start();
var result1 = manager.Users.Where(w => w.UserName == user.UserName).ToList().FirstOrDefault();
sw.Stop();
output.WriteLine("Users where UserName: {0} seconds", sw.Elapsed.TotalSeconds);
Assert.Equal(user.UserName, result1.UserName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ public async Task FindRoleByName()

Assert.NotNull(result);
Assert.Equal(role.Name, result.Name);

sw.Reset();
sw.Start();
var result1 = manager.Roles.Where(r => r.Name == role.Name).ToList();
sw.Stop();
output.WriteLine("RoleManager.Roles where name: {0} seconds", sw.Elapsed.TotalSeconds);

Assert.NotNull(result1.SingleOrDefault());
Assert.Equal(role.Name, result1.SingleOrDefault().Name);
}
}
}
Expand Down

0 comments on commit 5e09622

Please sign in to comment.