151 lines
4.1 KiB
C#
151 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using IdentityServer4.EntityFramework.DbContexts;
|
|
using IdentityServer4.EntityFramework.Mappers;
|
|
using IdentityServer4.Models;
|
|
using IdentityServer4.Stores;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace xIds.Stores
|
|
{
|
|
public class XPersistedGrantStore : IPersistedGrantStore
|
|
{
|
|
private readonly PersistedGrantDbContext dbContext;
|
|
|
|
public XPersistedGrantStore(PersistedGrantDbContext dbContext)
|
|
{
|
|
this.dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
|
|
{
|
|
//
|
|
var result = (await dbContext.PersistedGrants
|
|
.Where(g => g.SubjectId == subjectId)
|
|
.AsNoTracking()
|
|
.Select(res => res.ToModel())
|
|
.ToListAsync()).AsEnumerable();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public async Task<PersistedGrant> GetAsync(string key)
|
|
{
|
|
//
|
|
var result = await dbContext.PersistedGrants
|
|
.FirstOrDefaultAsync(g => g.Key == key);
|
|
|
|
//
|
|
return result.ToModel();
|
|
}
|
|
|
|
public async Task RemoveAllAsync(string subjectId, string clientId)
|
|
{
|
|
//
|
|
var items = await dbContext.PersistedGrants
|
|
.Where(g =>
|
|
g.SubjectId == subjectId &&
|
|
g.ClientId == clientId)
|
|
.ToListAsync();
|
|
|
|
//
|
|
dbContext.PersistedGrants.RemoveRange(items);
|
|
|
|
//
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task RemoveAllAsync(string subjectId, string clientId, string type)
|
|
{
|
|
//
|
|
var items = await dbContext.PersistedGrants
|
|
.Where(g =>
|
|
g.SubjectId == subjectId &&
|
|
g.ClientId == clientId &&
|
|
g.Type == type)
|
|
.ToListAsync();
|
|
|
|
//
|
|
dbContext.PersistedGrants.RemoveRange(items);
|
|
|
|
//
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task RemoveAsync(string key)
|
|
{
|
|
//
|
|
var items = await dbContext.PersistedGrants
|
|
.Where(g => g.Key == key)
|
|
.ToListAsync();
|
|
|
|
//
|
|
dbContext.PersistedGrants.RemoveRange(items);
|
|
|
|
//
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<bool> IsExistsAsync(string key)
|
|
{
|
|
return await this.dbContext.PersistedGrants.AnyAsync(g => g.Key == key);
|
|
}
|
|
|
|
public async Task<bool> IsExistsAsync(PersistedGrant grant)
|
|
{
|
|
return await IsExistsAsync(grant.Key);
|
|
}
|
|
|
|
public async Task AddOrUpdateAsync(PersistedGrant grant)
|
|
{
|
|
//
|
|
var isExists = await IsExistsAsync(grant);
|
|
if (isExists)
|
|
{
|
|
await UpdateAsync(grant);
|
|
}
|
|
else
|
|
{
|
|
await AddAsync(grant);
|
|
}
|
|
}
|
|
|
|
public async Task AddAsync(PersistedGrant grant)
|
|
{
|
|
//
|
|
var item = grant.ToEntity();
|
|
await dbContext.PersistedGrants.AddAsync(item);
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateAsync(PersistedGrant grant)
|
|
{
|
|
//
|
|
var item = grant.ToEntity();
|
|
var entity = dbContext.Attach(item);
|
|
entity.State = EntityState.Modified;
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task StoreAsync(PersistedGrant grant)
|
|
{
|
|
await AddOrUpdateAsync(grant);
|
|
}
|
|
|
|
//
|
|
// TODO: Complete this ...
|
|
public Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter filter)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
//
|
|
// TODO: Complete this ...
|
|
public Task RemoveAllAsync(PersistedGrantFilter filter)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
}
|
|
} |