From e0cb6233fdc21411983a479d23506c5f76a888c5 Mon Sep 17 00:00:00 2001 From: GZTime Date: Tue, 26 Nov 2024 20:45:06 +0800 Subject: [PATCH] feat(team): allow transfer when team locked --- src/GZCTF/Controllers/TeamController.cs | 62 ++++++++++++++----------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/src/GZCTF/Controllers/TeamController.cs b/src/GZCTF/Controllers/TeamController.cs index 756460ef1..928aba071 100644 --- a/src/GZCTF/Controllers/TeamController.cs +++ b/src/GZCTF/Controllers/TeamController.cs @@ -177,32 +177,41 @@ public async Task 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; + } } /// @@ -299,20 +308,21 @@ public async Task UpdateInviteToken([FromRoute] int id, Cancellat public async Task 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)]));