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..1629baa --- /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); + + if (posterRole == Role.Teacher || posterRole == Role.Student || posterRole == Role.Admin){ + authorizedApps.add(Applications.Msg); + authorizedApps.add(Applications.Forum); + authorizedApps.add(Applications.Rdv); + } + + 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; + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java index bc83924..8e9b256 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -43,28 +43,4 @@ public class CurriculumController { public ResponseEntity> findAll(){ return new ResponseEntity<>(curriculumCourseServ.findAll(),HttpStatus.OK); } - - /**@PostMapping("/curriculum") - public ResponseEntity postCurriculum(@RequestHeader("Authorization") String token,@RequestBody Curriculum curriculum){ - - if (!isSecretaryOrAdmin(token)){ - return new UnauthorizedResponse<>("you're not allowed to post a Curriculum"); - } - - CurriculumServ.save(Curriculum); - - return new ResponseEntity<>("created !",HttpStatus.CREATED); - }**/ - - - - private boolean isSecretaryOrAdmin(String authorization){ - if (authorization ==null) - return false; - - User poster = authServ.getUserFromToken(authorization); - if (poster == null) return false; - - return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin; - } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java index 3347c8e..36946b5 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -32,7 +32,7 @@ public class InscriptionController { @GetMapping("/requests/register") public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ - if (!isSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} + if (authServ.isNotSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} Iterable inscriptionRequests = inscriptionServ.getAll(); ArrayList> toReturn = new ArrayList<>(); @@ -64,7 +64,7 @@ public class InscriptionController { @RequestHeader("Authorize") String token, @RequestBody RequestState requestState) { - if (!isSecretaryOrAdmin(token)) return new UnauthorizedResponse<>(null); + if (authServ.isNotSecretaryOrAdmin(token)) return new UnauthorizedResponse<>(null); inscriptionServ.modifyState(id, requestState); return null; } @@ -83,15 +83,4 @@ public class InscriptionController { toReturn.put("state", inscriptionRequest.getState()); return toReturn; } - - - private boolean isSecretaryOrAdmin(String authorization){ - if (authorization ==null) - return false; - - User poster = authServ.getUserFromToken(authorization); - if (poster == null) return false; - - return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin; - } -} \ No newline at end of file +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java index a47603b..1e761ec 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -49,4 +49,4 @@ public class LoginController { authServ.register(inscriptionRequest); return new ResponseEntity<>("Is OK", HttpStatus.OK); } -} \ No newline at end of file +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 611ad82..23fb5db 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -51,6 +51,7 @@ public class MockController { User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), null,Role.Student,passwordEncoder.encode("student")); User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Secretary,passwordEncoder.encode("secretary")); User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher")); + User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.InscriptionService,passwordEncoder.encode("inscriptionService")); mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke)); userRepo.saveAll(mockUsers); diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 0fc894c..84fd34a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -38,7 +38,7 @@ public class UserController { @PostMapping("/user") public ResponseEntity postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){ - if (!isSecretaryOrAdmin(authorization)) + if (authServ.isNotSecretaryOrAdmin(authorization)) return new UnauthorizedResponse<>(null); userService.save(user); @@ -48,7 +48,7 @@ public class UserController { @GetMapping("/users") public ResponseEntity>> getAllUsers(@RequestHeader("Authorization") String authorization){ - if (!isSecretaryOrAdmin(authorization)) + if (authServ.isNotSecretaryOrAdmin(authorization)) return new UnauthorizedResponse<>(null); Iterable users = userService.getAll(); @@ -78,7 +78,6 @@ public class UserController { */ private HashMap userWithoutPassword(User user){ HashMap toReturn = new HashMap<>(); - toReturn.put("regNo",user.getRegNo()); toReturn.put("firstName",user.getFirstName()); toReturn.put("lastName",user.getLastName()); @@ -90,15 +89,5 @@ public class UserController { return toReturn; } - - private boolean isSecretaryOrAdmin(String authorization){ - if (authorization ==null) - return false; - - User poster = authServ.getUserFromToken(authorization); - if (poster == null) return false; - - return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin; - } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index 60dc6bc..a3301c1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -1,9 +1,8 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; -import ovh.herisson.Clyde.EndPoints.LoginController; -import ovh.herisson.Clyde.Repositories.InscriptionRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; +import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; @@ -39,4 +38,17 @@ public class AuthenticatorService { public void register(InscriptionRequest inscriptionRequest) { inscriptionService.save(inscriptionRequest); } + + + public boolean isNotSecretaryOrAdmin(String authorization){ + if (authorization ==null) + return true; + + User poster = getUserFromToken(authorization); + if (poster == null) return true; + + return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin; + } + } + diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java index f1bd092..6130fe8 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -31,4 +31,4 @@ public class InscriptionService { inscriptionRequest.setState(requestState); save(inscriptionRequest); } -} \ No newline at end of file +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index c7f8d1b..fb04f68 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -4,10 +4,8 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import ovh.herisson.Clyde.Repositories.FileRepository; import ovh.herisson.Clyde.Tables.*; - import java.io.File; import java.io.IOException; - import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java index 50ddcbf..2f746ce 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java @@ -5,16 +5,15 @@ import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; - import java.io.UnsupportedEncodingException; import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Base64; import java.util.Calendar; -import java.util.Date; @Service public class TokenService { - TokenRepository tokenRepo; + private final TokenRepository tokenRepo; public TokenService(TokenRepository tokenRepo){ this.tokenRepo = tokenRepo; @@ -30,13 +29,10 @@ public class TokenService { new SecureRandom().nextBytes(bytes); for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) (((bytes[i]+256)%256 %95+ 32)); - while ((char)bytes[i] == ';'){ - bytes[i] = new SecureRandom().generateSeed(1)[0]; - } } // will never end up in the catch because of the way that SecureRandom.nextBytes is implemented try { - return new String(bytes,"ISO_8859_1"); + return new String(Base64.getEncoder().encode(bytes),"ISO_8859_1"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } 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..6ad6567 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java @@ -0,0 +1,21 @@ +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 +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java index f0345c1..d52f1c9 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java @@ -3,5 +3,5 @@ package ovh.herisson.Clyde.Tables; public enum RequestState { Accepted, Refused, - Pending; + Pending } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java index 4e4469b..f6f8967 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java @@ -5,5 +5,5 @@ public enum Role { Student, Admin, InscriptionService, - Secretary; -} + Secretary +} \ No newline at end of file diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 42f1b38..e9de01f 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -2,6 +2,7 @@ import { toast } from 'vue3-toastify'; import { ref, computed } from 'vue' import i18n, { setLang } from './i18n.js' + import { isLogged } from '@/rest/Users.js' // Liste des apps @@ -23,6 +24,7 @@ const currentPath = ref(window.location.hash) window.addEventListener('hashchange', () => { + Logged.value = isLogged(); currentPath.value = window.location.hash }) @@ -35,7 +37,9 @@ const settings=ref(i18n("app.settings")) const login=ref(i18n("app.login")) const active=ref(false) - + + const Logged = ref(isLogged()); +