fix: check member/group limits before creating

This commit is contained in:
spiral 2021-11-03 02:42:37 -04:00
parent 29b28ba742
commit e1a5310a3a
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
3 changed files with 15 additions and 2 deletions

View File

@ -56,6 +56,12 @@ namespace PluralKit.API
{
var system = await ResolveSystem("@me");
// Check group cap
var existingGroupCount = await _repo.GetSystemGroupCount(system.Id);
var groupLimit = system.GroupLimitOverride ?? Limits.MaxGroupCount;
if (existingGroupCount >= groupLimit)
throw Errors.GroupLimitReached;
var patch = GroupPatch.FromJson(data);
patch.AssertIsValid();
if (!patch.Name.IsPresent)

View File

@ -40,6 +40,13 @@ namespace PluralKit.API
[HttpPost("members")]
public async Task<IActionResult> MemberCreate([FromBody] JObject data)
{
var system = await ResolveSystem("@me");
var memberCount = await _repo.GetSystemMemberCount(system.Id);
var memberLimit = system.MemberLimitOverride ?? Limits.MaxMemberCount;
if (memberCount >= memberLimit)
throw Errors.MemberLimitReached;
var patch = MemberPatch.FromJSON(data);
patch.AssertIsValid();
if (!patch.Name.IsPresent)
@ -47,8 +54,6 @@ namespace PluralKit.API
if (patch.Errors.Count > 0)
throw new ModelParseError(patch.Errors);
var system = await ResolveSystem("@me");
using var conn = await _db.Obtain();
using var tx = await conn.BeginTransactionAsync();

View File

@ -94,6 +94,8 @@ namespace PluralKit.API
public static PKError SameSwitchMembersError = new(400, 40004, "Member list identical to current fronter list.");
public static PKError SameSwitchTimestampError = new(400, 40005, "Switch with provided timestamp already exists.");
public static PKError InvalidSwitchId = new(400, 40006, "Invalid switch ID.");
public static PKError MemberLimitReached = new(400, 40007, "Member limit reached.");
public static PKError GroupLimitReached = new(400, 40008, "Group limit reached.");
public static PKError Unimplemented = new(501, 50001, "Unimplemented");
}