2020-04-14 20:16:04 +02:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Raven.Client.Documents;
|
|
|
|
|
using Raven.Client.Documents.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-11-15 17:07:27 +01:00
|
|
|
using zero.Core.Database;
|
2021-05-04 17:23:52 +02:00
|
|
|
using zero.Core.Entities;
|
2020-11-15 20:11:27 +01:00
|
|
|
using zero.Core.Options;
|
2020-04-14 20:16:04 +02:00
|
|
|
|
|
|
|
|
namespace zero.Core.Identity
|
|
|
|
|
{
|
2020-11-11 01:01:32 +01:00
|
|
|
// TODO we can't inject IZeroContext here for app-context as the IApplicationContext itself
|
|
|
|
|
// relies on UserManager and therefore this UserStore, i.e. circular dependency
|
|
|
|
|
|
|
|
|
|
public partial class RavenUserStore<TUser, TRole> : RavenUserStore<TUser>,
|
|
|
|
|
IUserRoleStore<TUser>
|
2021-05-04 17:23:52 +02:00
|
|
|
where TUser : ZeroIdentityUser
|
|
|
|
|
where TRole : ZeroIdentityRole
|
2020-11-04 16:16:36 +01:00
|
|
|
{
|
2021-10-28 12:11:07 +02:00
|
|
|
public RavenUserStore(IZeroStore store, IZeroOptions options, bool global = false) : base(store, options, global) { }
|
2020-11-11 01:01:32 +01:00
|
|
|
|
|
|
|
|
|
2020-11-04 16:16:36 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
|
2020-11-11 01:01:32 +01:00
|
|
|
{
|
2020-11-05 00:27:02 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2020-11-05 00:27:02 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2021-10-28 12:11:07 +02:00
|
|
|
return await ScopeQuery(Store.Session(Global).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)
|
|
|
|
|
{
|
2020-11-05 00:27:02 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2020-11-05 00:27:02 +01:00
|
|
|
user.RoleIds.Remove(roleName);
|
2020-11-04 16:16:36 +01:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 20:16:04 +02:00
|
|
|
}
|