PluralKit/PluralKit.API/Controllers/v1/MemberController.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2019-07-09 22:19:18 +00:00
using System.Threading.Tasks;
2020-06-29 12:54:11 +00:00
using Dapper;
using Microsoft.AspNetCore.Authorization;
2019-07-09 22:19:18 +00:00
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
2019-07-09 22:19:18 +00:00
using PluralKit.Core;
namespace PluralKit.API
2019-07-09 22:19:18 +00:00
{
[ApiController]
[ApiVersion("1.0")]
2019-07-09 22:19:18 +00:00
[Route("m")]
[Route( "v{version:apiVersion}/m" )]
2019-07-09 22:19:18 +00:00
public class MemberController: ControllerBase
{
2020-06-29 12:15:30 +00:00
private IDatabase _db;
private IAuthorizationService _auth;
2019-07-09 22:19:18 +00:00
2020-06-29 12:54:11 +00:00
public MemberController(IAuthorizationService auth, IDatabase db)
2019-07-09 22:19:18 +00:00
{
_auth = auth;
2020-06-29 12:15:30 +00:00
_db = db;
2019-07-09 22:19:18 +00:00
}
[HttpGet("{hid}")]
public async Task<ActionResult<JObject>> GetMember(string hid)
2019-07-09 22:19:18 +00:00
{
2020-06-29 12:54:11 +00:00
var member = await _db.Execute(conn => conn.QueryMemberByHid(hid));
2019-07-09 22:19:18 +00:00
if (member == null) return NotFound("Member not found.");
return Ok(member.ToJson(User.ContextFor(member)));
2019-07-09 22:19:18 +00:00
}
[HttpPost]
[Authorize]
public async Task<ActionResult<JObject>> PostMember([FromBody] JObject properties)
{
var system = User.CurrentSystem();
if (!properties.ContainsKey("name"))
return BadRequest("Member name must be specified.");
2020-06-29 12:54:11 +00:00
await using var conn = await _db.Obtain();
// Enforce per-system member limit
2020-06-29 12:54:11 +00:00
var memberCount = await conn.QuerySingleAsync<int>("select count(*) from members where system = @System", new {System = system});
if (memberCount >= Limits.MaxMemberCount)
return BadRequest($"Member limit reached ({memberCount} / {Limits.MaxMemberCount}).");
2020-06-29 12:54:11 +00:00
var member = await conn.CreateMember(system, properties.Value<string>("name"));
2020-06-29 12:15:30 +00:00
MemberPatch patch;
try
{
2020-06-29 12:15:30 +00:00
patch = JsonModelExt.ToMemberPatch(properties);
}
catch (JsonModelParseError e)
{
return BadRequest(e.Message);
}
2020-06-29 12:54:11 +00:00
member = await conn.UpdateMember(member.Id, patch);
return Ok(member.ToJson(User.ContextFor(member)));
}
2019-07-09 22:19:18 +00:00
[HttpPatch("{hid}")]
[Authorize]
public async Task<ActionResult<JObject>> PatchMember(string hid, [FromBody] JObject changes)
2019-07-09 22:19:18 +00:00
{
2020-06-29 12:54:11 +00:00
await using var conn = await _db.Obtain();
var member = await conn.QueryMemberByHid(hid);
2019-07-09 22:19:18 +00:00
if (member == null) return NotFound("Member not found.");
var res = await _auth.AuthorizeAsync(User, member, "EditMember");
if (!res.Succeeded) return Unauthorized($"Member '{hid}' is not part of your system.");
2019-07-09 22:19:18 +00:00
2020-06-29 12:15:30 +00:00
MemberPatch patch;
try
{
2020-06-29 12:15:30 +00:00
patch = JsonModelExt.ToMemberPatch(changes);
}
catch (JsonModelParseError e)
{
return BadRequest(e.Message);
}
2020-06-29 12:54:11 +00:00
var newMember = await conn.UpdateMember(member.Id, patch);
return Ok(newMember.ToJson(User.ContextFor(newMember)));
2019-07-09 22:19:18 +00:00
}
[HttpDelete("{hid}")]
[Authorize]
public async Task<ActionResult> DeleteMember(string hid)
{
2020-06-29 12:54:11 +00:00
await using var conn = await _db.Obtain();
var member = await conn.QueryMemberByHid(hid);
if (member == null) return NotFound("Member not found.");
var res = await _auth.AuthorizeAsync(User, member, "EditMember");
if (!res.Succeeded) return Unauthorized($"Member '{hid}' is not part of your system.");
2020-06-29 12:54:11 +00:00
await conn.DeleteMember(member.Id);
return Ok();
}
2019-07-09 22:19:18 +00:00
}
}