2019-10-28 19:15:27 +00:00
|
|
|
using System.Linq;
|
2019-07-09 22:19:18 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.API.Controllers
|
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Route("m")]
|
2019-07-09 22:23:41 +00:00
|
|
|
[Route("v1/m")]
|
2019-07-09 22:19:18 +00:00
|
|
|
public class MemberController: ControllerBase
|
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
private IDataStore _data;
|
2019-07-09 22:19:18 +00:00
|
|
|
private TokenAuthService _auth;
|
|
|
|
|
2019-10-28 19:15:27 +00:00
|
|
|
public MemberController(IDataStore data, TokenAuthService auth)
|
2019-07-09 22:19:18 +00:00
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
_data = data;
|
2019-07-09 22:19:18 +00:00
|
|
|
_auth = auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{hid}")]
|
|
|
|
public async Task<ActionResult<PKMember>> GetMember(string hid)
|
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.GetMemberByHid(hid);
|
2019-07-09 22:19:18 +00:00
|
|
|
if (member == null) return NotFound("Member not found.");
|
|
|
|
|
|
|
|
return Ok(member);
|
|
|
|
}
|
|
|
|
|
2019-07-27 03:37:23 +00:00
|
|
|
[HttpPost]
|
|
|
|
[RequiresSystem]
|
|
|
|
public async Task<ActionResult<PKMember>> PostMember([FromBody] PKMember newMember)
|
|
|
|
{
|
|
|
|
var system = _auth.CurrentSystem;
|
|
|
|
|
|
|
|
if (newMember.Name == null)
|
2019-10-28 19:15:27 +00:00
|
|
|
return BadRequest("Member name cannot be null.");
|
2019-07-27 03:37:23 +00:00
|
|
|
|
2019-10-20 07:16:57 +00:00
|
|
|
// Enforce per-system member limit
|
2019-10-26 17:45:30 +00:00
|
|
|
var memberCount = await _data.GetSystemMemberCount(system);
|
2019-10-20 07:16:57 +00:00
|
|
|
if (memberCount >= Limits.MaxMemberCount)
|
|
|
|
return BadRequest($"Member limit reached ({memberCount} / {Limits.MaxMemberCount}).");
|
|
|
|
|
2019-07-27 03:37:23 +00:00
|
|
|
// Explicit bounds checks
|
|
|
|
if (newMember.Name != null && newMember.Name.Length > Limits.MaxMemberNameLength)
|
|
|
|
return BadRequest($"Member name too long ({newMember.Name.Length} > {Limits.MaxMemberNameLength}.");
|
2019-08-09 15:37:52 +00:00
|
|
|
if (newMember.DisplayName != null && newMember.DisplayName.Length > Limits.MaxMemberNameLength)
|
|
|
|
return BadRequest($"Member display name too long ({newMember.DisplayName.Length} > {Limits.MaxMemberNameLength}.");
|
2019-07-27 03:37:23 +00:00
|
|
|
if (newMember.Pronouns != null && newMember.Pronouns.Length > Limits.MaxPronounsLength)
|
|
|
|
return BadRequest($"Member pronouns too long ({newMember.Pronouns.Length} > {Limits.MaxPronounsLength}.");
|
|
|
|
if (newMember.Description != null && newMember.Description.Length > Limits.MaxDescriptionLength)
|
|
|
|
return BadRequest($"Member descriptions too long ({newMember.Description.Length} > {Limits.MaxDescriptionLength}.");
|
|
|
|
|
|
|
|
// Sanity bounds checks
|
|
|
|
if (newMember.AvatarUrl != null && newMember.AvatarUrl.Length > 1000)
|
|
|
|
return BadRequest();
|
2019-10-28 19:15:27 +00:00
|
|
|
if (newMember.ProxyTags?.Any(tag => tag.Prefix.Length > 1000 || tag.Suffix.Length > 1000) ?? false)
|
2019-07-27 03:37:23 +00:00
|
|
|
return BadRequest();
|
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.CreateMember(system, newMember.Name);
|
2019-07-27 03:37:23 +00:00
|
|
|
|
|
|
|
member.Name = newMember.Name;
|
2019-08-09 15:37:52 +00:00
|
|
|
member.DisplayName = newMember.DisplayName;
|
2019-07-27 03:37:23 +00:00
|
|
|
member.Color = newMember.Color;
|
|
|
|
member.AvatarUrl = newMember.AvatarUrl;
|
|
|
|
member.Birthday = newMember.Birthday;
|
|
|
|
member.Pronouns = newMember.Pronouns;
|
|
|
|
member.Description = newMember.Description;
|
2019-10-28 19:15:27 +00:00
|
|
|
member.ProxyTags = newMember.ProxyTags;
|
2019-10-30 08:26:50 +00:00
|
|
|
member.KeepProxy = newMember.KeepProxy;
|
2019-10-26 17:45:30 +00:00
|
|
|
await _data.SaveMember(member);
|
2019-07-27 03:37:23 +00:00
|
|
|
|
2019-07-28 20:27:54 +00:00
|
|
|
return Ok(member);
|
2019-07-27 03:37:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:19:18 +00:00
|
|
|
[HttpPatch("{hid}")]
|
|
|
|
[RequiresSystem]
|
|
|
|
public async Task<ActionResult<PKMember>> PatchMember(string hid, [FromBody] PKMember newMember)
|
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.GetMemberByHid(hid);
|
2019-07-09 22:19:18 +00:00
|
|
|
if (member == null) return NotFound("Member not found.");
|
|
|
|
|
|
|
|
if (member.System != _auth.CurrentSystem.Id) return Unauthorized($"Member '{hid}' is not part of your system.");
|
|
|
|
|
2019-07-19 16:36:47 +00:00
|
|
|
if (newMember.Name == null)
|
|
|
|
return BadRequest("Member name can not be null.");
|
|
|
|
|
2019-07-09 22:19:18 +00:00
|
|
|
// Explicit bounds checks
|
2019-07-19 16:36:47 +00:00
|
|
|
if (newMember.Name != null && newMember.Name.Length > Limits.MaxMemberNameLength)
|
2019-07-09 22:19:18 +00:00
|
|
|
return BadRequest($"Member name too long ({newMember.Name.Length} > {Limits.MaxMemberNameLength}.");
|
2019-08-09 15:37:52 +00:00
|
|
|
if (newMember.DisplayName != null && newMember.DisplayName.Length > Limits.MaxMemberNameLength)
|
|
|
|
return BadRequest($"Member display name too long ({newMember.DisplayName.Length} > {Limits.MaxMemberNameLength}.");
|
2019-07-19 16:36:47 +00:00
|
|
|
if (newMember.Pronouns != null && newMember.Pronouns.Length > Limits.MaxPronounsLength)
|
2019-07-09 22:19:18 +00:00
|
|
|
return BadRequest($"Member pronouns too long ({newMember.Pronouns.Length} > {Limits.MaxPronounsLength}.");
|
2019-07-19 16:36:47 +00:00
|
|
|
if (newMember.Description != null && newMember.Description.Length > Limits.MaxDescriptionLength)
|
2019-07-09 22:19:18 +00:00
|
|
|
return BadRequest($"Member descriptions too long ({newMember.Description.Length} > {Limits.MaxDescriptionLength}.");
|
|
|
|
|
|
|
|
// Sanity bounds checks
|
2019-11-03 18:16:06 +00:00
|
|
|
if (newMember.ProxyTags?.Any(tag => (tag.Prefix?.Length ?? 0) > 1000 || (tag.Suffix?.Length ?? 0) > 1000) ?? false)
|
2019-07-09 22:19:18 +00:00
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
member.Name = newMember.Name;
|
2019-08-09 15:37:52 +00:00
|
|
|
member.DisplayName = newMember.DisplayName;
|
2019-07-09 22:19:18 +00:00
|
|
|
member.Color = newMember.Color;
|
|
|
|
member.AvatarUrl = newMember.AvatarUrl;
|
|
|
|
member.Birthday = newMember.Birthday;
|
|
|
|
member.Pronouns = newMember.Pronouns;
|
|
|
|
member.Description = newMember.Description;
|
2019-10-28 19:15:27 +00:00
|
|
|
member.ProxyTags = newMember.ProxyTags;
|
2019-10-30 08:26:50 +00:00
|
|
|
member.KeepProxy = newMember.KeepProxy;
|
2019-10-26 17:45:30 +00:00
|
|
|
await _data.SaveMember(member);
|
2019-07-27 03:37:23 +00:00
|
|
|
|
2019-07-28 20:27:54 +00:00
|
|
|
return Ok(member);
|
2019-07-09 22:19:18 +00:00
|
|
|
}
|
2019-07-28 22:03:50 +00:00
|
|
|
|
|
|
|
[HttpDelete("{hid}")]
|
|
|
|
[RequiresSystem]
|
|
|
|
public async Task<ActionResult<PKMember>> DeleteMember(string hid)
|
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.GetMemberByHid(hid);
|
2019-07-28 22:03:50 +00:00
|
|
|
if (member == null) return NotFound("Member not found.");
|
|
|
|
|
|
|
|
if (member.System != _auth.CurrentSystem.Id) return Unauthorized($"Member '{hid}' is not part of your system.");
|
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
await _data.DeleteMember(member);
|
2019-07-28 22:03:50 +00:00
|
|
|
|
|
|
|
return Ok();
|
|
|
|
}
|
2019-07-09 22:19:18 +00:00
|
|
|
}
|
2019-07-28 20:27:54 +00:00
|
|
|
}
|