btcpayserver/BTCPayServer/Services/UserService.cs

67 lines
2.2 KiB
C#
Raw Normal View History

#nullable enable
2021-03-14 13:02:43 -07:00
using System;
using System.Collections.Generic;
2021-03-14 12:24:32 -07:00
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Services.Stores;
2021-12-31 16:59:02 +09:00
using BTCPayServer.Storage.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
2021-03-14 12:24:32 -07:00
namespace BTCPayServer.Services
{
public class UserService
{
private readonly UserManager<ApplicationUser> _userManager;
2021-03-14 13:02:43 -07:00
private readonly IAuthorizationService _authorizationService;
2021-03-14 12:24:32 -07:00
private readonly StoredFileRepository _storedFileRepository;
private readonly FileService _fileService;
private readonly StoreRepository _storeRepository;
public UserService(
UserManager<ApplicationUser> userManager,
2021-03-14 13:02:43 -07:00
IAuthorizationService authorizationService,
2021-03-14 12:24:32 -07:00
StoredFileRepository storedFileRepository,
FileService fileService,
StoreRepository storeRepository
)
{
_userManager = userManager;
2021-03-14 13:02:43 -07:00
_authorizationService = authorizationService;
2021-03-14 12:24:32 -07:00
_storedFileRepository = storedFileRepository;
_fileService = fileService;
_storeRepository = storeRepository;
}
2021-12-31 16:59:02 +09:00
public async Task<bool> IsAdminUser(string userId)
2021-11-04 08:21:01 +01:00
{
2021-12-31 16:59:02 +09:00
return IsRoleAdmin(await _userManager.GetRolesAsync(new ApplicationUser() { Id = userId }));
2021-11-04 08:21:01 +01:00
}
2021-03-14 12:24:32 -07:00
2021-12-31 16:59:02 +09:00
public async Task<bool> IsAdminUser(ApplicationUser user)
2021-04-06 18:19:31 -07:00
{
return IsRoleAdmin(await _userManager.GetRolesAsync(user));
}
2021-12-31 16:59:02 +09:00
public async Task DeleteUserAndAssociatedData(ApplicationUser user)
2021-03-14 12:24:32 -07:00
{
var userId = user.Id;
var files = await _storedFileRepository.GetFiles(new StoredFileRepository.FilesQuery()
{
UserIds = new[] { userId },
});
await Task.WhenAll(files.Select(file => _fileService.RemoveFile(file.Id, userId)));
await _userManager.DeleteAsync(user);
await _storeRepository.CleanUnreachableStores();
}
2021-03-14 13:02:43 -07:00
public bool IsRoleAdmin(IList<string> roles)
{
return roles.Contains(Roles.ServerAdmin, StringComparer.Ordinal);
}
2021-03-14 12:24:32 -07:00
}
}