package ciai.security; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Service; import ciai.model.Account; import ciai.model.Edition; import ciai.model.Professor; import ciai.model.Student; import ciai.service.AppService; @Service public class SecurityService { @Autowired AppService service; public boolean isStudentPage(final int number) { User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); try { Student student = service.getStudentByNumber(number); Account a = student.getAccount(); if (a.getUsername().equals(user.getUsername())) return true; return false; } catch (Exception e) { return false; } } public boolean isProfessorPage(final Long id) { User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); try { Professor prof = service.getProfessor(id); Account a = prof.getAccount(); if (a.getUsername().equals(user.getUsername())){ return true; } return false; } catch (Exception e) { return false; } } public boolean isProfessorOfEdition(final Long eid){ User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); try{ Account a = service.getAccountByUsername(user.getUsername()); Professor prof = service.getProfessorByAccount(a); for(Edition e : prof.getEditions()){ if(e.getId() == eid) return true; } return false; }catch(Exception e){ return false; } } }