Skip to content

Task 1-1: Guard CheatController với SUPER_ADMIN Role Only

Phase: 1 - Security Priority: Critical Module: admin Depends on: Không có Reference: SPEC.md — xem mục 4 (ADM-F-02 RBAC) và mục 5 (Acceptance Criteria: "Admin với wrong role → 403")

Background

CheatController có endpoint addCoin cho phép thêm coin vào tài khoản user bất kỳ. Nếu bất kỳ admin nào có quyền gọi endpoint này, đây là rủi ro nghiêm trọng.

Tasks

File: admin/controller/cheat/CheatController.java

  • [ ] Xác nhận annotation security hiện tại (có @PreAuthorize không?)
  • [ ] Thêm SUPER_ADMIN restriction:

    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @PostMapping("/add-coin")
    public Result<?> addCoin(@RequestBody AddCoinUserRequest request) {
        LOGGER.warn("[CHEAT_AUDIT] SUPER_ADMIN {} adding {} coin to userId={}",
            getCurrentAdminId(), request.getAmount(), request.getUserId());
        // ... existing logic ...
    }
    

    Injection note: getCurrentAdminId() phải được cung cấp bởi base controller class (ví dụ BaseAdminController) hoặc inject SecurityContextHolder / AdminSecurityUtils. Kiểm tra xem CheatController extend base class nào, hoặc thêm helper method:

    private String getCurrentAdminId() {
        return ((AdminUserDetails) SecurityContextHolder.getContext()
            .getAuthentication().getPrincipal()).getAdminId();
    }
    

  • [ ] Verify RoleEnum.SUPER_ADMIN tồn tại và đúng admin accounts được assign

  • [ ] Thêm Slack notification khi endpoint được gọi:
    slackNotificationService.sendAlert(
        "[CHEAT] addCoin called by admin=" + adminId +
        " userId=" + request.getUserId() + " amount=" + request.getAmount()
    );
    

    Injection note: SlackNotificationService phải được inject vào CheatController nếu chưa có:

    @Autowired
    private SlackNotificationService slackNotificationService;
    
    Xác nhận SlackNotificationService tồn tại trong module admin (search SlackNotificationService.java trong admin/src). Nếu chỉ tồn tại ở module khác, cần thêm dependency hoặc tạo interface tương ứng.

Verification / Acceptance Criteria

  • [ ] Code compiles: CheatController.java không có import errors; SlackNotificationServiceRoleEnum.SUPER_ADMIN đều resolve đúng
  • [ ] HTTP 403 cho non-SUPER_ADMIN: Gọi POST /api/admin/cheats/add-coin với JWT của admin có role OPERATOR hoặc ADMIN → response 403 Forbidden
  • [ ] HTTP 200 cho SUPER_ADMIN: Gọi cùng endpoint với JWT của SUPER_ADMIN → success
  • [ ] Slack alert gửi: Sau mỗi lần addCoin thành công, Slack channel nhận message chứa adminId và userId
  • [ ] Audit log: Log line [CHEAT_AUDIT] xuất hiện trong server logs với đúng adminId, amount, userId
  • [ ] Test class tham khảo: CheatControllerTest (nếu tồn tại) — hoặc xem task-3-2.md cho integration test RBAC

Files to Modify

  • admin/controller/cheat/CheatController.java