Skip to content

Commit

Permalink
feat(team): allow transfer when team locked
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Nov 26, 2024
1 parent 2003fec commit e0cb623
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/GZCTF/Controllers/TeamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,41 @@ public async Task<IActionResult> Transfer([FromRoute] int id, [FromBody] TeamTra
CancellationToken token)
{
UserInfo? user = await userManager.GetUserAsync(User);
Team? team = await teamRepository.GetTeamById(id, token);

if (team is null)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NotFound)]));
IDbContextTransaction trans = await teamRepository.BeginTransactionAsync(token);

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };
try
{
Team? team = await teamRepository.GetTeamById(id, token);

if (team.Locked && await teamRepository.AnyActiveGame(team, token))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_Locked)]));
if (team is null)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NotFound)]));

UserInfo? newCaptain = await userManager.Users.SingleOrDefaultAsync(u => u.Id == model.NewCaptainId, token);
if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

if (newCaptain is null)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NewCaptainNotFound)]));
UserInfo? newCaptain = await userManager.Users.SingleOrDefaultAsync(u => u.Id == model.NewCaptainId, token);

Team[] newCaptainTeams = await teamRepository.GetUserTeams(newCaptain, token);
if (newCaptain is null)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NewCaptainNotFound)]));

if (newCaptainTeams.Count(t => t.CaptainId == newCaptain.Id) >= 3)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NewCaptainTeamTooMany)]));
Team[] newCaptainTeams = await teamRepository.GetUserTeams(newCaptain, token);

await teamRepository.Transfer(team, newCaptain, token);
if (newCaptainTeams.Count(t => t.CaptainId == newCaptain.Id) >= 3)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NewCaptainTeamTooMany)]));

return Ok(TeamInfoModel.FromTeam(team));
await teamRepository.Transfer(team, newCaptain, token);
await trans.CommitAsync(token);

return Ok(TeamInfoModel.FromTeam(team));
}
catch
{
await trans.RollbackAsync(token);
throw;
}
}

/// <summary>
Expand Down Expand Up @@ -299,20 +308,21 @@ public async Task<IActionResult> UpdateInviteToken([FromRoute] int id, Cancellat
public async Task<IActionResult> KickUser([FromRoute] int id, [FromRoute] Guid userId, CancellationToken token)
{
UserInfo? user = await userManager.GetUserAsync(User);
Team? team = await teamRepository.GetTeamById(id, token);

if (team is null)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NotFound)]));

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

IDbContextTransaction trans = await teamRepository.BeginTransactionAsync(token);

try
{
Team? team = await teamRepository.GetTeamById(id, token);

if (team is null)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_NotFound)]));

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

if (team.Locked && await teamRepository.AnyActiveGame(team, token))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_Locked)]));

Expand Down

0 comments on commit e0cb623

Please sign in to comment.