Files
mixtape/zero.Core/Identity/UserStore.Role.cs
T

61 lines
1.8 KiB
C#
Raw Normal View History

2020-04-14 20:16:04 +02:00
using Microsoft.AspNetCore.Identity;
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using zero.Core.Entities;
namespace zero.Core.Identity
{
// TODO this class still searches for role names in role IDs, which is not correct.
// we need a join here
// guess this code will only be used in certain Authorize attributes
2020-11-04 16:16:36 +01:00
public partial class RavenUserStore<TUser, TRole> : IUserRoleStore<TUser>
where TUser : class, IIdentityUserWithRoles
where TRole : class, IIdentityUserRole
{
/// <inheritdoc />
public Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
{
user.RoleIds.Add(roleName);
2020-11-04 16:16:36 +01:00
return Task.CompletedTask;
}
2020-04-14 20:16:04 +02:00
2020-11-04 16:16:36 +01:00
/// <inheritdoc />
public Task<IList<string>> GetRolesAsync(TUser user, CancellationToken cancellationToken)
{
return Task.FromResult((IList<string>)user.RoleIds.ToList());
2020-11-04 16:16:36 +01:00
}
2020-04-14 20:16:04 +02:00
2020-11-04 16:16:36 +01:00
/// <inheritdoc />
public async Task<IList<TUser>> GetUsersInRoleAsync(string roleName, CancellationToken cancellationToken)
{
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
{
return await session.Query<TUser>().Where(x => roleName.In(x.RoleIds)).ToListAsync();
2020-11-04 16:16:36 +01:00
}
}
2020-04-14 20:16:04 +02:00
2020-11-04 16:16:36 +01:00
/// <inheritdoc />
public Task<bool> IsInRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
{
return Task.FromResult(user.RoleIds.Contains(roleName, StringComparer.InvariantCultureIgnoreCase));
2020-11-04 16:16:36 +01:00
}
2020-04-14 20:16:04 +02:00
2020-11-04 16:16:36 +01:00
/// <inheritdoc />
public Task RemoveFromRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
{
user.RoleIds.Remove(roleName);
2020-11-04 16:16:36 +01:00
return Task.CompletedTask;
}
}
2020-04-14 20:16:04 +02:00
}