btcpayserver/BTCPayServer/Services/UserService.cs

94 lines
3.3 KiB
C#
Raw Normal View History

#nullable enable
2021-03-14 21:02:43 +01:00
using System;
using System.Collections.Generic;
2021-03-14 20:24:32 +01:00
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
2021-03-14 20:24:32 +01:00
using BTCPayServer.Data;
using BTCPayServer.Services.Stores;
2021-12-31 08:59:02 +01:00
using BTCPayServer.Storage.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
2021-03-14 20:24:32 +01:00
namespace BTCPayServer.Services
{
public class UserService
{
private readonly UserManager<ApplicationUser> _userManager;
2021-03-14 21:02:43 +01:00
private readonly IAuthorizationService _authorizationService;
2021-03-14 20:24:32 +01:00
private readonly StoredFileRepository _storedFileRepository;
private readonly FileService _fileService;
private readonly StoreRepository _storeRepository;
private readonly ApplicationDbContextFactory _applicationDbContextFactory;
2021-03-14 20:24:32 +01:00
public UserService(
UserManager<ApplicationUser> userManager,
2021-03-14 21:02:43 +01:00
IAuthorizationService authorizationService,
2021-03-14 20:24:32 +01:00
StoredFileRepository storedFileRepository,
FileService fileService,
StoreRepository storeRepository,
ApplicationDbContextFactory applicationDbContextFactory
2021-03-14 20:24:32 +01:00
)
{
_userManager = userManager;
2021-03-14 21:02:43 +01:00
_authorizationService = authorizationService;
2021-03-14 20:24:32 +01:00
_storedFileRepository = storedFileRepository;
_fileService = fileService;
_storeRepository = storeRepository;
_applicationDbContextFactory = applicationDbContextFactory;
}
public async Task<List<ApplicationUserData>> GetUsersWithRoles()
{
await using var context = _applicationDbContextFactory.CreateContext();
return await (context.Users.Select(p => FromModel(p, p.UserRoles.Join(context.Roles, userRole => userRole.RoleId, role => role.Id,
(userRole, role) => role.Name).ToArray()))).ToListAsync();
}
public static ApplicationUserData FromModel(ApplicationUser data, string[] roles)
{
return new ApplicationUserData()
{
Id = data.Id,
Email = data.Email,
EmailConfirmed = data.EmailConfirmed,
RequiresEmailConfirmation = data.RequiresEmailConfirmation,
Created = data.Created,
Roles = roles
};
2021-03-14 20:24:32 +01:00
}
2021-12-31 08:59:02 +01:00
public async Task<bool> IsAdminUser(string userId)
2021-11-04 08:21:01 +01:00
{
2021-12-31 08:59:02 +01:00
return IsRoleAdmin(await _userManager.GetRolesAsync(new ApplicationUser() { Id = userId }));
2021-11-04 08:21:01 +01:00
}
2021-03-14 20:24:32 +01:00
2021-12-31 08:59:02 +01:00
public async Task<bool> IsAdminUser(ApplicationUser user)
2021-04-07 03:19:31 +02:00
{
return IsRoleAdmin(await _userManager.GetRolesAsync(user));
}
2021-12-31 08:59:02 +01:00
public async Task DeleteUserAndAssociatedData(ApplicationUser user)
2021-03-14 20:24:32 +01: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 21:02:43 +01:00
public bool IsRoleAdmin(IList<string> roles)
{
return roles.Contains(Roles.ServerAdmin, StringComparer.Ordinal);
}
2021-03-14 20:24:32 +01:00
}
}