runtime-role-matrix-live-20.../src/main/java/com/example/demo/controller/DeveloperController.java

37 lines
1.2 KiB
Java

package com.example.demo.controller;
import com.example.demo.service.DeveloperService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/roles")
public class DeveloperController {
private final DeveloperService developerService;
public DeveloperController(DeveloperService developerService) {
this.developerService = developerService;
}
@GetMapping
public ResponseEntity<List<String>> getAllRoles() {
return ResponseEntity.ok(developerService.getAllRoles());
}
@GetMapping("/{role}")
public ResponseEntity<Map<String, String>> getRolePermission(@PathVariable String role) {
String permission = developerService.getRolePermission(role);
return ResponseEntity.ok(Map.of("role", role, "permission", permission));
}
@GetMapping("/{role}/check")
public ResponseEntity<Map<String, Boolean>> checkPermission(
@PathVariable String role,
@RequestParam String action) {
boolean allowed = developerService.hasPermission(role, action);
return ResponseEntity.ok(Map.of("action", action, "allowed", allowed));
}
}