2022-02-10 06:51:10 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2022-02-24 09:00:44 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2022-02-10 06:51:10 +01:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Data;
|
2024-12-15 10:02:13 +01:00
|
|
|
using BTCPayServer.Services;
|
2022-02-10 06:51:10 +01:00
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-12-02 15:35:33 +01:00
|
|
|
using StoreData = BTCPayServer.Data.StoreData;
|
2022-02-10 06:51:10 +01:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[EnableCors(CorsPolicies.All)]
|
|
|
|
public class GreenfieldStoreUsersController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly StoreRepository _storeRepository;
|
2023-03-03 21:24:27 +09:00
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2024-12-15 10:02:13 +01:00
|
|
|
private readonly UriResolver _uriResolver;
|
2022-02-10 06:51:10 +01:00
|
|
|
|
2024-12-15 10:02:13 +01:00
|
|
|
public GreenfieldStoreUsersController(
|
|
|
|
StoreRepository storeRepository,
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
UriResolver uriResolver)
|
2022-02-10 06:51:10 +01:00
|
|
|
{
|
|
|
|
_storeRepository = storeRepository;
|
2023-03-03 21:24:27 +09:00
|
|
|
_userManager = userManager;
|
2024-12-15 10:02:13 +01:00
|
|
|
_uriResolver = uriResolver;
|
2022-02-10 06:51:10 +01:00
|
|
|
}
|
2024-03-19 14:58:33 +01:00
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2022-02-10 06:51:10 +01:00
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/users")]
|
2024-12-02 15:35:33 +01:00
|
|
|
public async Task<IActionResult> GetStoreUsers()
|
2022-02-10 06:51:10 +01:00
|
|
|
{
|
|
|
|
var store = HttpContext.GetStoreData();
|
2024-12-02 15:35:33 +01:00
|
|
|
return store == null ? StoreNotFound() : Ok(await FromModel(store));
|
2022-02-10 06:51:10 +01:00
|
|
|
}
|
2024-03-19 14:58:33 +01:00
|
|
|
|
2022-02-10 06:51:10 +01:00
|
|
|
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-03-03 21:24:27 +09:00
|
|
|
[HttpDelete("~/api/v1/stores/{storeId}/users/{idOrEmail}")]
|
|
|
|
public async Task<IActionResult> RemoveStoreUser(string storeId, string idOrEmail)
|
2022-02-10 06:51:10 +01:00
|
|
|
{
|
|
|
|
var store = HttpContext.GetStoreData();
|
2024-12-02 15:35:33 +01:00
|
|
|
if (store == null) return StoreNotFound();
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2024-12-02 15:35:33 +01:00
|
|
|
var user = await _userManager.FindByIdOrEmail(idOrEmail);
|
|
|
|
if (user == null) return UserNotFound();
|
|
|
|
|
|
|
|
return await _storeRepository.RemoveStoreUser(storeId, user.Id)
|
|
|
|
? Ok()
|
|
|
|
: this.CreateAPIError(409, "store-user-role-orphaned", "Removing this user would result in the store having no owner.");
|
2022-02-10 06:51:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/users")]
|
2024-12-02 15:35:33 +01:00
|
|
|
[HttpPut("~/api/v1/stores/{storeId}/users/{idOrEmail?}")]
|
|
|
|
public async Task<IActionResult> AddOrUpdateStoreUser(string storeId, StoreUserData request, string idOrEmail = null)
|
2022-02-10 06:51:10 +01:00
|
|
|
{
|
|
|
|
var store = HttpContext.GetStoreData();
|
2024-12-02 15:35:33 +01:00
|
|
|
if (store == null) return StoreNotFound();
|
2023-05-26 16:49:32 +02:00
|
|
|
|
2024-12-02 15:35:33 +01:00
|
|
|
var user = await _userManager.FindByIdOrEmail(idOrEmail ?? request.UserId);
|
|
|
|
if (user == null) return UserNotFound();
|
|
|
|
|
|
|
|
StoreRoleId roleId = null;
|
2023-05-26 16:49:32 +02:00
|
|
|
if (request.Role is not null)
|
|
|
|
{
|
|
|
|
roleId = await _storeRepository.ResolveStoreRoleId(storeId, request.Role);
|
|
|
|
if (roleId is null)
|
|
|
|
ModelState.AddModelError(nameof(request.Role), "The role id provided does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
return this.CreateValidationError(ModelState);
|
|
|
|
|
2024-12-02 15:35:33 +01:00
|
|
|
var result = string.IsNullOrEmpty(idOrEmail)
|
|
|
|
? await _storeRepository.AddStoreUser(storeId, user.Id, roleId)
|
|
|
|
: await _storeRepository.AddOrUpdateStoreUser(storeId, user.Id, roleId);
|
|
|
|
return result
|
|
|
|
? Ok()
|
|
|
|
: this.CreateAPIError(409, "duplicate-store-user-role", "The user is already added to the store");
|
2022-02-10 06:51:10 +01:00
|
|
|
}
|
|
|
|
|
2024-12-02 15:35:33 +01:00
|
|
|
private async Task<IEnumerable<StoreUserData>> FromModel(StoreData data)
|
2022-02-10 06:51:10 +01:00
|
|
|
{
|
2024-12-02 15:35:33 +01:00
|
|
|
var storeUsers = new List<StoreUserData>();
|
|
|
|
foreach (var storeUser in data.UserStores)
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByIdOrEmail(storeUser.ApplicationUserId);
|
|
|
|
var blob = user?.GetBlob();
|
|
|
|
storeUsers.Add(new StoreUserData
|
|
|
|
{
|
|
|
|
UserId = storeUser.ApplicationUserId,
|
|
|
|
Role = storeUser.StoreRoleId,
|
|
|
|
Email = user?.Email,
|
|
|
|
Name = blob?.Name,
|
2024-12-15 10:02:13 +01:00
|
|
|
ImageUrl = blob?.ImageUrl == null ? null : await _uriResolver.Resolve(Request.GetAbsoluteRootUri(), UnresolvedUri.Create(blob.ImageUrl))
|
2024-12-02 15:35:33 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return storeUsers;
|
2022-02-10 06:51:10 +01:00
|
|
|
}
|
2024-12-02 15:35:33 +01:00
|
|
|
|
2022-02-10 06:51:10 +01:00
|
|
|
private IActionResult StoreNotFound()
|
|
|
|
{
|
|
|
|
return this.CreateAPIError(404, "store-not-found", "The store was not found");
|
|
|
|
}
|
2024-12-02 15:35:33 +01:00
|
|
|
|
|
|
|
private IActionResult UserNotFound()
|
|
|
|
{
|
|
|
|
return this.CreateAPIError(404, "user-not-found", "The user was not found");
|
|
|
|
}
|
2022-02-10 06:51:10 +01:00
|
|
|
}
|
|
|
|
}
|