added ApplicationController and Application enum
This commit is contained in:
		@ -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<Iterable<Applications>> getAuthorizedApps(@RequestHeader("Authorization") String token){
 | 
			
		||||
 | 
			
		||||
        return new ResponseEntity<>(getAuthorizedApplications(token), HttpStatus.OK);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/apps/{identifier}")
 | 
			
		||||
    public ResponseEntity<Boolean> 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<Applications> getAuthorizedApplications(String token){
 | 
			
		||||
        Role posterRole = authServ.getUserFromToken(token).getRole();
 | 
			
		||||
        ArrayList<Applications> 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user