btcpayserver/BTCPayServer/Controllers/GreenField/GreenfieldTestApiKeyController.cs

84 lines
3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
2020-03-19 19:11:15 +09:00
using BTCPayServer.Client;
using BTCPayServer.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
2022-01-14 13:05:23 +09:00
namespace BTCPayServer.Controllers.Greenfield
{
/// <summary>
/// this controller serves as a testing endpoint for our api key unit tests
/// </summary>
[Route("api/test/apikey")]
[ApiController]
2020-03-23 14:23:23 +01:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[EnableCors(CorsPolicies.All)]
2022-01-07 12:17:59 +09:00
public class GreenfieldTestApiKeyController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly GreenfieldStoresController _greenfieldStoresController;
public GreenfieldTestApiKeyController(
UserManager<ApplicationUser> userManager,
GreenfieldStoresController greenfieldStoresController)
{
_userManager = userManager;
_greenfieldStoresController = greenfieldStoresController;
}
[HttpGet("me/id")]
2020-03-23 14:23:23 +01:00
[Authorize(Policy = Policies.CanViewProfile, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public string GetCurrentUserId()
{
return _userManager.GetUserId(User);
}
[HttpGet("me")]
2020-03-23 14:23:23 +01:00
[Authorize(Policy = Policies.CanViewProfile, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public async Task<ApplicationUser> GetCurrentUser()
{
return await _userManager.GetUserAsync(User);
}
[HttpGet("me/is-admin")]
2020-03-23 14:23:23 +01:00
[Authorize(Policy = Policies.CanModifyServerSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public bool AmIAnAdmin()
{
2019-10-12 20:35:30 +09:00
return true;
}
[HttpGet("me/stores")]
2020-03-23 14:23:23 +01:00
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public async Task<BTCPayServer.Client.Models.StoreData[]> GetCurrentUserStores()
{
var storesData = HttpContext.GetStoresData();
var stores = new List<Client.Models.StoreData>();
foreach (var storeData in storesData)
{
stores.Add(await _greenfieldStoresController.FromModel(storeData));
}
return stores.ToArray();
}
2020-03-19 19:11:15 +09:00
[HttpGet("me/stores/{storeId}/can-view")]
[Authorize(Policy = Policies.CanViewStoreSettings,
2020-03-23 14:23:23 +01:00
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
2020-03-19 19:11:15 +09:00
public bool CanViewStore(string storeId)
{
return true;
}
[HttpGet("me/stores/{storeId}/can-edit")]
[Authorize(Policy = Policies.CanModifyStoreSettings,
2020-03-23 14:23:23 +01:00
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
2020-03-19 19:11:15 +09:00
public bool CanEditStore(string storeId)
{
return true;
}
}
}