From 1f14890d435bc8bd3d0350616708dab09f777e4f Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Fri, 15 Mar 2024 18:54:53 +0100 Subject: [PATCH] added ApplicationController and Application enum --- .../EndPoints/ApplicationsController.java | 62 +++++++++++++++++++ .../herisson/Clyde/Tables/Applications.java | 24 +++++++ 2 files changed, 86 insertions(+) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java new file mode 100644 index 0000000..d044bf7 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java @@ -0,0 +1,62 @@ +package ovh.herisson.Clyde.EndPoints; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; +import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Tables.Applications; +import ovh.herisson.Clyde.Tables.Role; + +import java.util.ArrayList; + +@RestController +public class ApplicationsController { + + AuthenticatorService authServ; + + public ApplicationsController(AuthenticatorService authServ){ + this.authServ = authServ; + } + + + /** return a list of authorized applications. + * depends on the token + */ + @GetMapping("/apps") + public ResponseEntity> getAuthorizedApps(@RequestHeader("Authorization") String token){ + + return new ResponseEntity<>(getAuthorizedApplications(token), HttpStatus.OK); + } + + @GetMapping("/apps/{identifier}") + public ResponseEntity getAppAuthorization(@PathVariable Applications identifier, @RequestHeader("Authorization") String token){ + + if (getAuthorizedApplications(token).contains(identifier)){ + return new ResponseEntity<>(true, HttpStatus.OK); + } + return new ResponseEntity<>(false, HttpStatus.OK); + } + + public ArrayList getAuthorizedApplications(String token){ + Role posterRole = authServ.getUserFromToken(token).getRole(); + ArrayList authorizedApps = new ArrayList<>(); + + authorizedApps.add(Applications.LOGIN); + authorizedApps.add(Applications.PROFILE); + authorizedApps.add(Applications.MSG); + authorizedApps.add(Applications.FORUM); + authorizedApps.add(Applications.RDV); + + if (posterRole == Role.Student || posterRole == Role.Admin) return authorizedApps; + + if (posterRole == Role.Teacher || posterRole == Role.Secretary || posterRole == Role.Admin) authorizedApps.add(Applications.MANAGECOURSES); + + if (posterRole == Role.InscriptionService || posterRole == Role.Admin) authorizedApps.add(Applications.INSCRIPTION); + + return authorizedApps; + } + +} \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java new file mode 100644 index 0000000..445a928 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java @@ -0,0 +1,24 @@ +package ovh.herisson.Clyde.Tables; + +public enum Applications { + // without any token + LOGIN, + + // with any token + PROFILE, + + + // Students and higher authorization + MSG, + FORUM, + RDV, + + // teachers and Secretary authorization + MANAGECOURSES, + + // InscriptionService authorization + INSCRIPTION; + + + +}