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
|
|
|
|
|
{
|
2020-04-15 01:42:06 +02:00
|
|
|
public partial class UserStore<TUser> : IUserRoleStore<TUser> where TUser : class, IUser
|
2020-04-14 20:16:04 +02:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 01:42:06 +02:00
|
|
|
public Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
|
2020-04-14 20:16:04 +02:00
|
|
|
{
|
2020-04-16 12:56:17 +02:00
|
|
|
user.Roles.Add(roleName);
|
2020-04-15 01:42:06 +02:00
|
|
|
return Task.CompletedTask;
|
2020-04-14 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 01:42:06 +02:00
|
|
|
public Task<IList<string>> GetRolesAsync(TUser user, CancellationToken cancellationToken)
|
2020-04-14 20:16:04 +02:00
|
|
|
{
|
2020-04-16 12:56:17 +02:00
|
|
|
return Task.FromResult((IList<string>)user.Roles.ToList());
|
2020-04-14 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 01:42:06 +02:00
|
|
|
public async Task<IList<TUser>> GetUsersInRoleAsync(string roleName, CancellationToken cancellationToken)
|
2020-04-14 20:16:04 +02:00
|
|
|
{
|
|
|
|
|
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
|
|
|
|
|
{
|
2020-04-16 12:56:17 +02:00
|
|
|
return await session.Query<TUser>().Where(x => roleName.In(x.Roles)).ToListAsync();
|
2020-04-14 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 01:42:06 +02:00
|
|
|
public Task<bool> IsInRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
|
2020-04-14 20:16:04 +02:00
|
|
|
{
|
2020-04-16 12:56:17 +02:00
|
|
|
return Task.FromResult(user.Roles.Contains(roleName, StringComparer.InvariantCultureIgnoreCase));
|
2020-04-14 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 01:42:06 +02:00
|
|
|
public Task RemoveFromRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
|
2020-04-14 20:16:04 +02:00
|
|
|
{
|
2020-04-16 12:56:17 +02:00
|
|
|
user.Roles.Remove(roleName);
|
2020-04-15 01:42:06 +02:00
|
|
|
return Task.CompletedTask;
|
2020-04-14 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|