Skip to content

Commit

Permalink
feat: #39 role-based limits
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Nov 3, 2024
1 parent d470e1e commit c0adf4b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
1 change: 0 additions & 1 deletion rag-2-backend/Infrastructure/Common/Model/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ public enum Role
{
Student,
Teacher,
Special,
Admin
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Mvc;
using rag_2_backend.Infrastructure.Common.Model;
using rag_2_backend.Infrastructure.Dao;
using rag_2_backend.Infrastructure.Module.Administration.Dto;
using rag_2_backend.Infrastructure.Module.User.Dto;

#endregion
Expand All @@ -13,7 +14,9 @@ namespace rag_2_backend.Infrastructure.Module.Administration;

[ApiController]
[Route("api/[controller]")]
public class AdministrationController(AdministrationService administrationService) : ControllerBase
public class AdministrationController(
AdministrationService administrationService,
IConfiguration config) : ControllerBase
{
/// <summary>Change ban status for any user by user ID despite admins (Admin)</summary>
/// <response code="404">User not found</response>
Expand Down Expand Up @@ -44,6 +47,19 @@ public UserResponse GetUserDetails([Required] int userId)
return administrationService.GetUserDetails(UserDao.GetPrincipalEmail(User), userId);
}

/// <summary>Get current limits for roles (Auth)</summary>
/// <response code="403">Cannot view limits</response>
[HttpGet("limits")]
[Authorize]
public LimitsResponse GetCurrentLimits()
{
return new LimitsResponse
{
StudentLimitMb = int.Parse(config["StudentDataLimitMb"] ?? "30"),
TeacherLimitMb = int.Parse(config["TeacherDataLimitMb"] ?? "30")
};
}

/// <summary>Get all users list (Admin, Teacher)</summary>
[HttpGet("users")]
[Authorize(Roles = "Admin, Teacher")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public UserResponse GetUserDetails(string principalEmail, int userId)
{
var principal = userDao.GetUserByEmailOrThrow(principalEmail);

if (principal.Role is Role.Student or Role.Special && userId != principal.Id)
if (principal.Role is Role.Student && userId != principal.Id)
throw new ForbiddenException("Cannot view details");

return UserMapper.Map(userDao.GetUserByIdOrThrow(userId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace rag_2_backend.Infrastructure.Module.Administration.Dto;

public class LimitsResponse
{
public required int StudentLimitMb { get; set; }
public required int TeacherLimitMb { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ public void AddGameRecord(RecordedGameRequest request, string email)
throw new BadRequestException("Value state cannot be empty");

var user = userDao.GetUserByEmailOrThrow(email);
if (GetSizeByUser(user.Id, request.Values.Count) > configuration.GetValue<int>("UserDataLimitMb"))
throw new BadRequestException("Space limit exceeded");

switch (user.Role)
{
case Role.Student when GetSizeByUser(user.Id, request.Values.Count) >
configuration.GetValue<int>("StudentDataLimitMb"):
throw new BadRequestException("Space limit exceeded");
case Role.Teacher when GetSizeByUser(user.Id, request.Values.Count) >
configuration.GetValue<int>("TeacherDataLimitMb"):
throw new BadRequestException("Space limit exceeded");
}

var game = context.Games.SingleOrDefault(g => Equals(g.Name.ToLower(), request.GameName.ToLower()))
?? throw new NotFoundException("Game not found");
Expand Down
3 changes: 2 additions & 1 deletion rag-2-backend/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"MailConfirmationURL": "http://localhost:4200/register/confirm/?token=",
"PasswordResetURL": "http://localhost:4200/login/reset-password/?token="
},
"UserDataLimitMb": 10,
"StudentDataLimitMb": 10,
"TeacherDataLimitMb": 20,
"MailSettings": {
"Password": "fzozhrthfueuuorm"
}
Expand Down

0 comments on commit c0adf4b

Please sign in to comment.