2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2017-09-13 15:47:34 +09:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-06-28 17:55:27 +09:00
|
|
|
using BTCPayServer.Data;
|
2019-08-30 00:24:42 +09:00
|
|
|
using BTCPayServer.Migrations;
|
2019-10-04 17:17:11 +09:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-06-28 17:55:27 +09:00
|
|
|
using NBitcoin;
|
|
|
|
using NBitcoin.DataEncoders;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-09-15 16:06:57 +09:00
|
|
|
namespace BTCPayServer.Services.Stores
|
2017-09-13 15:47:34 +09:00
|
|
|
{
|
2017-10-27 17:53:04 +09:00
|
|
|
public class StoreRepository
|
|
|
|
{
|
2020-06-28 22:07:48 -05:00
|
|
|
private readonly ApplicationDbContextFactory _ContextFactory;
|
2019-05-30 07:02:52 +00:00
|
|
|
|
2019-06-04 10:17:26 +09:00
|
|
|
public StoreRepository(ApplicationDbContextFactory contextFactory)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
|
|
|
_ContextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
public async Task<StoreData> FindStore(string storeId)
|
|
|
|
{
|
2018-04-28 02:51:20 +09:00
|
|
|
if (storeId == null)
|
|
|
|
return null;
|
2017-10-27 17:53:04 +09:00
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
2020-06-28 17:55:27 +09:00
|
|
|
var result = await ctx.FindAsync<StoreData>(storeId).ConfigureAwait(false);
|
2019-06-04 10:17:26 +09:00
|
|
|
return result;
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
public async Task<StoreData> FindStore(string storeId, string userId)
|
|
|
|
{
|
|
|
|
if (userId == null)
|
|
|
|
throw new ArgumentNullException(nameof(userId));
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
return (await ctx
|
2019-10-04 22:55:11 +09:00
|
|
|
.UserStore
|
2017-10-27 17:53:04 +09:00
|
|
|
.Where(us => us.ApplicationUserId == userId && us.StoreDataId == storeId)
|
|
|
|
.Select(us => new
|
|
|
|
{
|
|
|
|
Store = us.StoreData,
|
|
|
|
Role = us.Role
|
|
|
|
}).ToArrayAsync())
|
|
|
|
.Select(us =>
|
|
|
|
{
|
|
|
|
us.Store.Role = us.Role;
|
2019-06-04 10:17:26 +09:00
|
|
|
return us.Store;
|
2017-10-27 17:53:04 +09:00
|
|
|
}).FirstOrDefault();
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 23:50:36 +09:00
|
|
|
|
2018-03-23 16:24:57 +09:00
|
|
|
public class StoreUser
|
|
|
|
{
|
|
|
|
public string Id { get; set; }
|
|
|
|
public string Email { get; set; }
|
|
|
|
public string Role { get; set; }
|
|
|
|
}
|
|
|
|
public async Task<StoreUser[]> GetStoreUsers(string storeId)
|
|
|
|
{
|
|
|
|
if (storeId == null)
|
|
|
|
throw new ArgumentNullException(nameof(storeId));
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
return await ctx
|
2019-10-04 22:55:11 +09:00
|
|
|
.UserStore
|
2018-03-23 16:24:57 +09:00
|
|
|
.Where(u => u.StoreDataId == storeId)
|
|
|
|
.Select(u => new StoreUser()
|
|
|
|
{
|
|
|
|
Id = u.ApplicationUserId,
|
|
|
|
Email = u.ApplicationUser.Email,
|
|
|
|
Role = u.Role
|
|
|
|
}).ToArrayAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 14:36:15 +01:00
|
|
|
public async Task<StoreData[]> GetStoresByUserId(string userId, IEnumerable<string> storeIds = null)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
2019-10-04 22:55:11 +09:00
|
|
|
return (await ctx.UserStore
|
2020-02-24 14:36:15 +01:00
|
|
|
.Where(u => u.ApplicationUserId == userId && (storeIds == null || storeIds.Contains(u.StoreDataId)))
|
2018-03-23 16:24:57 +09:00
|
|
|
.Select(u => new { u.StoreData, u.Role })
|
|
|
|
.ToArrayAsync())
|
|
|
|
.Select(u =>
|
|
|
|
{
|
|
|
|
u.StoreData.Role = u.Role;
|
2019-06-04 10:17:26 +09:00
|
|
|
return u.StoreData;
|
2018-03-23 16:24:57 +09:00
|
|
|
}).ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<bool> AddStoreUser(string storeId, string userId, string role)
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId, Role = role };
|
|
|
|
ctx.UserStore.Add(userStore);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-04 17:17:11 +09:00
|
|
|
catch (Microsoft.EntityFrameworkCore.DbUpdateException)
|
2018-03-23 16:24:57 +09:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:24:19 +09:00
|
|
|
public async Task CleanUnreachableStores()
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
if (!ctx.Database.SupportDropForeignKey())
|
|
|
|
return;
|
2020-01-12 15:32:26 +09:00
|
|
|
foreach (var store in await ctx.Stores.Where(s => !s.UserStores.Where(u => u.Role == StoreRoles.Owner).Any()).ToArrayAsync())
|
2018-07-20 15:24:19 +09:00
|
|
|
{
|
|
|
|
ctx.Stores.Remove(store);
|
|
|
|
}
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:24:57 +09:00
|
|
|
public async Task RemoveStoreUser(string storeId, string userId)
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId };
|
|
|
|
ctx.UserStore.Add(userStore);
|
2019-10-04 17:17:11 +09:00
|
|
|
ctx.Entry<UserStore>(userStore).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
|
2018-03-23 16:24:57 +09:00
|
|
|
await ctx.SaveChangesAsync();
|
2018-07-19 22:46:55 +09:00
|
|
|
|
|
|
|
}
|
|
|
|
await DeleteStoreIfOrphan(storeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task DeleteStoreIfOrphan(string storeId)
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
if (ctx.Database.SupportDropForeignKey())
|
|
|
|
{
|
2020-01-12 15:32:26 +09:00
|
|
|
if (!await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).AnyAsync())
|
2018-07-19 22:46:55 +09:00
|
|
|
{
|
|
|
|
var store = await ctx.Stores.FindAsync(storeId);
|
|
|
|
if (store != null)
|
|
|
|
{
|
|
|
|
ctx.Stores.Remove(store);
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 23:50:36 +09:00
|
|
|
|
2020-04-30 16:43:16 +02:00
|
|
|
public async Task CreateStore(string ownerId, StoreData storeData)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2020-04-30 16:43:16 +02:00
|
|
|
if (!string.IsNullOrEmpty(storeData.Id))
|
|
|
|
throw new ArgumentException("id should be empty", nameof(storeData.StoreName));
|
|
|
|
if (string.IsNullOrEmpty(storeData.StoreName))
|
|
|
|
throw new ArgumentException("name should not be empty", nameof(storeData.StoreName));
|
2019-01-10 13:47:21 +09:00
|
|
|
if (ownerId == null)
|
|
|
|
throw new ArgumentNullException(nameof(ownerId));
|
2017-10-27 17:53:04 +09:00
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
2020-04-30 16:43:16 +02:00
|
|
|
storeData.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(32));
|
2017-10-27 17:53:04 +09:00
|
|
|
var userStore = new UserStore
|
|
|
|
{
|
2020-04-30 16:43:16 +02:00
|
|
|
StoreDataId = storeData.Id,
|
2017-10-27 17:53:04 +09:00
|
|
|
ApplicationUserId = ownerId,
|
2020-04-30 16:43:16 +02:00
|
|
|
Role = StoreRoles.Owner
|
2017-10-27 17:53:04 +09:00
|
|
|
};
|
2020-04-30 16:43:16 +02:00
|
|
|
ctx.Add(storeData);
|
2017-11-12 23:37:21 +09:00
|
|
|
ctx.Add(userStore);
|
2020-04-30 16:43:16 +02:00
|
|
|
await ctx.SaveChangesAsync();
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2020-04-30 16:43:16 +02:00
|
|
|
public async Task<StoreData> CreateStore(string ownerId, string name)
|
|
|
|
{
|
2020-06-28 17:55:27 +09:00
|
|
|
var store = new StoreData() { StoreName = name };
|
|
|
|
await CreateStore(ownerId, store);
|
2020-04-30 16:43:16 +02:00
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
public async Task RemoveStore(string storeId, string userId)
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
2019-10-04 22:55:11 +09:00
|
|
|
var storeUser = await ctx.UserStore.AsQueryable().FirstOrDefaultAsync(o => o.StoreDataId == storeId && o.ApplicationUserId == userId);
|
2017-10-27 17:53:04 +09:00
|
|
|
if (storeUser == null)
|
|
|
|
return;
|
|
|
|
ctx.UserStore.Remove(storeUser);
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
}
|
2018-07-19 22:46:55 +09:00
|
|
|
await DeleteStoreIfOrphan(storeId);
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2017-10-23 22:55:46 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
public async Task UpdateStore(StoreData store)
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
var existing = await ctx.FindAsync<StoreData>(store.Id);
|
|
|
|
ctx.Entry(existing).CurrentValues.SetValues(store);
|
|
|
|
await ctx.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
2018-07-19 19:31:17 +09:00
|
|
|
|
|
|
|
public async Task<bool> DeleteStore(string storeId)
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
2018-07-19 21:32:33 +09:00
|
|
|
if (!ctx.Database.SupportDropForeignKey())
|
|
|
|
return false;
|
2018-07-19 19:31:17 +09:00
|
|
|
var store = await ctx.Stores.FindAsync(storeId);
|
|
|
|
if (store == null)
|
|
|
|
return false;
|
|
|
|
ctx.Stores.Remove(store);
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-19 22:23:14 +09:00
|
|
|
|
|
|
|
public bool CanDeleteStores()
|
|
|
|
{
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
{
|
|
|
|
return ctx.Database.SupportDropForeignKey();
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
}
|