Skip to content

Commit

Permalink
chore: update log & code style
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Sep 3, 2023
1 parent 866f2e9 commit 6e79ff9
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 123 deletions.
22 changes: 11 additions & 11 deletions src/GZCTF/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ public async Task<IActionResult> Register([FromBody] RegisterModel model, Cancel
await signInManager.SignInAsync(user, true);

logger.Log("用户成功注册", user, TaskStatus.Success);
return Ok(new RequestResponse<RegisterStatus>("注册成功", RegisterStatus.LoggedIn, 200));
return Ok(new RequestResponse<RegisterStatus>("注册成功", RegisterStatus.LoggedIn, StatusCodes.Status200OK));
}

if (!accountPolicy.Value.EmailConfirmationRequired)
{
logger.Log("用户成功注册,待审核", user, TaskStatus.Success);
return Ok(new RequestResponse<RegisterStatus>("注册成功,等待管理员审核",
RegisterStatus.AdminConfirmationRequired, 200));
RegisterStatus.AdminConfirmationRequired, StatusCodes.Status200OK));
}

logger.Log("发送用户邮箱验证邮件", user, TaskStatus.Pending);
Expand All @@ -112,7 +112,7 @@ public async Task<IActionResult> Register([FromBody] RegisterModel model, Cancel
}

return Ok(new RequestResponse<RegisterStatus>("注册成功,等待邮箱验证",
RegisterStatus.EmailConfirmationRequired, 200));
RegisterStatus.EmailConfirmationRequired, StatusCodes.Status200OK));
}

/// <summary>
Expand All @@ -138,10 +138,10 @@ public async Task<IActionResult> Recovery([FromBody] RecoveryModel model, Cancel

var user = await userManager.FindByEmailAsync(model.Email!);
if (user is null)
return NotFound(new RequestResponse("用户不存在", 404));
return NotFound(new RequestResponse("用户不存在", StatusCodes.Status404NotFound));

if (!user.EmailConfirmed)
return NotFound(new RequestResponse("账户未激活,请重新注册", 404));
return NotFound(new RequestResponse("账户未激活,请重新注册", StatusCodes.Status404NotFound));

if (!accountPolicy.Value.EmailConfirmationRequired)
return BadRequest(new RequestResponse("请联系管理员重置密码"));
Expand All @@ -161,7 +161,7 @@ public async Task<IActionResult> Recovery([FromBody] RecoveryModel model, Cancel
return BadRequest(new RequestResponse("邮件无法发送,请联系管理员"));
}

return Ok(new RequestResponse("邮件发送成功", 200));
return Ok(new RequestResponse("邮件发送成功", StatusCodes.Status200OK));
}

/// <summary>
Expand Down Expand Up @@ -219,7 +219,7 @@ public async Task<IActionResult> Verify([FromBody] AccountVerifyModel model)
var result = await userManager.ConfirmEmailAsync(user, Codec.Base64.Decode(model.Token));

if (!result.Succeeded)
return Unauthorized(new RequestResponse("邮箱验证失败", 401));
return Unauthorized(new RequestResponse("邮箱验证失败", StatusCodes.Status401Unauthorized));

logger.Log("通过邮箱验证", user, TaskStatus.Success);
await signInManager.SignInAsync(user, true);
Expand Down Expand Up @@ -260,10 +260,10 @@ public async Task<IActionResult> LogIn([FromBody] LoginModel model, Cancellation
user ??= await userManager.FindByEmailAsync(model.UserName);

if (user is null)
return Unauthorized(new RequestResponse("用户名或密码错误", 401));
return Unauthorized(new RequestResponse("用户名或密码错误", StatusCodes.Status401Unauthorized));

if (user.Role == Role.Banned)
return Unauthorized(new RequestResponse("用户已被禁用", 401));
return Unauthorized(new RequestResponse("用户已被禁用", StatusCodes.Status401Unauthorized));

user.LastSignedInUTC = DateTimeOffset.UtcNow;
user.UpdateByHttpContext(HttpContext);
Expand All @@ -273,7 +273,7 @@ public async Task<IActionResult> LogIn([FromBody] LoginModel model, Cancellation
var result = await signInManager.PasswordSignInAsync(user, model.Password, true, false);

if (!result.Succeeded)
return Unauthorized(new RequestResponse("用户名或密码错误", 401));
return Unauthorized(new RequestResponse("用户名或密码错误", StatusCodes.Status401Unauthorized));

logger.Log("用户成功登录", user, TaskStatus.Success);

Expand Down Expand Up @@ -397,7 +397,7 @@ public async Task<IActionResult> ChangeEmail([FromBody] MailChangeModel model)
return BadRequest(new RequestResponse("邮件无法发送,请联系管理员"));
}

return Ok(new RequestResponse<bool>("邮箱待验证", true, 200));
return Ok(new RequestResponse<bool>("邮箱待验证", true, StatusCodes.Status200OK));
}

/// <summary>
Expand Down
22 changes: 11 additions & 11 deletions src/GZCTF/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public async Task<IActionResult> UpdateUserInfo(string userid, [FromBody] AdminU
var user = await userManager.FindByIdAsync(userid);

if (user is null)
return NotFound(new RequestResponse("用户未找到", 404));
return NotFound(new RequestResponse("用户未找到", StatusCodes.Status404NotFound));

user.UpdateUserInfo(model);
await userManager.UpdateAsync(user);
Expand All @@ -314,12 +314,12 @@ public async Task<IActionResult> UpdateUserInfo(string userid, [FromBody] AdminU
[HttpDelete("Users/{userid}/Password")]
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status404NotFound)]
public async Task<IActionResult> ResetPassword(string userid, CancellationToken token = default)
public async Task<IActionResult> ResetPassword(string userid)
{
var user = await userManager.FindByIdAsync(userid);

if (user is null)
return NotFound(new RequestResponse("用户未找到", 404));
return NotFound(new RequestResponse("用户未找到", StatusCodes.Status404NotFound));

var pwd = Codec.RandomPassword(16);
var code = await userManager.GeneratePasswordResetTokenAsync(user);
Expand Down Expand Up @@ -351,7 +351,7 @@ public async Task<IActionResult> DeleteUser(string userid, CancellationToken tok
user = await userManager.FindByIdAsync(userid);

if (user is null)
return NotFound(new RequestResponse("用户未找到", 404));
return NotFound(new RequestResponse("用户未找到", StatusCodes.Status404NotFound));

if (await teamRepository.CheckIsCaptain(user, token))
return BadRequest(new RequestResponse("不可以删除队长"));
Expand Down Expand Up @@ -379,7 +379,7 @@ public async Task<IActionResult> DeleteTeam(int id, CancellationToken token = de
var team = await teamRepository.GetTeamById(id, token);

if (team is null)
return NotFound(new RequestResponse("队伍未找到", 404));
return NotFound(new RequestResponse("队伍未找到", StatusCodes.Status404NotFound));

await teamRepository.DeleteTeam(team, token);

Expand All @@ -403,7 +403,7 @@ public async Task<IActionResult> UserInfo(string userid)
var user = await userManager.FindByIdAsync(userid);

if (user is null)
return NotFound(new RequestResponse("用户未找到", 404));
return NotFound(new RequestResponse("用户未找到", StatusCodes.Status404NotFound));

return Ok(ProfileUserInfoModel.FromUserInfo(user));
}
Expand Down Expand Up @@ -440,7 +440,7 @@ public async Task<IActionResult> Participation(int id, ParticipationStatus statu
var participation = await participationRepository.GetParticipationById(id, token);

if (participation is null)
return NotFound(new RequestResponse("参与状态未找到", 404));
return NotFound(new RequestResponse("参与状态未找到", StatusCodes.Status404NotFound));

await participationRepository.UpdateParticipationStatus(participation, status, token);

Expand All @@ -465,7 +465,7 @@ public async Task<IActionResult> Writeups(int id, CancellationToken token = defa
var game = await gameRepository.GetGameById(id, token);

if (game is null)
return NotFound(new RequestResponse("比赛未找到", 404));
return NotFound(new RequestResponse("比赛未找到", StatusCodes.Status404NotFound));

return Ok(await participationRepository.GetWriteups(game, token));
}
Expand All @@ -488,7 +488,7 @@ public async Task<IActionResult> DownloadAllWriteups(int id, CancellationToken t
var game = await gameRepository.GetGameById(id, token);

if (game is null)
return NotFound(new RequestResponse("比赛未找到", 404));
return NotFound(new RequestResponse("比赛未找到", StatusCodes.Status404NotFound));

var wps = await participationRepository.GetWriteups(game, token);
var filename = $"Writeups-{game.Title}-{DateTimeOffset.UtcNow:yyyyMMdd-HH.mm.ssZ}";
Expand Down Expand Up @@ -532,12 +532,12 @@ public async Task<IActionResult> DestroyInstance(string id, CancellationToken to
var container = await containerRepository.GetContainerById(id, token);

if (container is null)
return NotFound(new RequestResponse("容器实例未找到", 404));
return NotFound(new RequestResponse("容器实例未找到", StatusCodes.Status404NotFound));

if (await instanceRepository.DestroyContainer(container, token))
return Ok();
else
return BadRequest(new RequestResponse("容器实例销毁失败", 404));
return BadRequest(new RequestResponse("容器实例销毁失败"));
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions src/GZCTF/Controllers/AssetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ public IActionResult GetFile([RegularExpression("[0-9a-f]{64}")] string hash, st

if (!System.IO.File.Exists(path))
{
logger.Log($"尝试获取不存在的文件 [{hash[..8]}] {filename}", HttpContext.Connection?.RemoteIpAddress?.ToString() ?? "0.0.0.0", TaskStatus.NotFound, LogLevel.Warning);
return NotFound(new RequestResponse("文件不存在", 404));
var ip = HttpContext.Connection?.RemoteIpAddress?.ToString() ?? "0.0.0.0";
logger.Log($"尝试获取不存在的文件 [{hash[..8]}] {filename}", ip, TaskStatus.NotFound, LogLevel.Warning);
return NotFound(new RequestResponse("文件不存在", StatusCodes.Status404NotFound));
}

if (!_extProvider.TryGetContentType(filename, out string? contentType))
contentType = MediaTypeNames.Application.Octet;

HttpContext.Response.Headers.CacheControl = $"public, max-age={60 * 60 * 24 * 7}";

return new PhysicalFileResult(path, contentType)
{
FileDownloadName = filename
FileDownloadName = filename,
};
}

Expand Down
Loading

0 comments on commit 6e79ff9

Please sign in to comment.