trop de truc
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
package ovh.herisson.Clyde.Services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ovh.herisson.Clyde.Tables.InscriptionRequest;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.Token;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
import ovh.herisson.Clyde.Tables.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Service
|
||||
public class AuthenticatorService {
|
||||
@@ -35,22 +33,11 @@ public class AuthenticatorService {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void register(InscriptionRequest inscriptionRequest) {
|
||||
inscriptionService.save(inscriptionRequest);
|
||||
public InscriptionRequest register(InscriptionRequest inscriptionRequest) {
|
||||
return 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;
|
||||
}
|
||||
|
||||
public boolean IsNotIn(Role[] roles, String token){
|
||||
public boolean isNotIn(Role[] roles, String token){
|
||||
if (token == null)
|
||||
return true;
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import ovh.herisson.Clyde.Repositories.CourseRepository;
|
||||
import ovh.herisson.Clyde.Tables.Course;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@@ -18,6 +17,8 @@ public class CourseService {
|
||||
}
|
||||
|
||||
public Course save(Course course){
|
||||
if (course.getOwner().getRole() != Role.Teacher)
|
||||
return null;
|
||||
return courseRepo.save(course);
|
||||
}
|
||||
|
||||
@@ -25,18 +26,37 @@ public class CourseService {
|
||||
return courseRepo.findById(id);
|
||||
}
|
||||
|
||||
public Course modifyData(long id, Map<String, Object> updates, Role role) {
|
||||
|
||||
public Iterable<Course> findAll() {
|
||||
return courseRepo.findAll();
|
||||
}
|
||||
|
||||
|
||||
public Iterable<Course> findOwnedCourses(User userFromToken) {
|
||||
return courseRepo.findAllOwnedCoures(userFromToken);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean modifyData(long id, Map<String, Object> updates, Role role) {
|
||||
Course target = courseRepo.findById(id);
|
||||
|
||||
if (target == null)
|
||||
return false;
|
||||
|
||||
if (role == Role.Teacher){
|
||||
for (Map.Entry<String, Object> entry : updates.entrySet()){
|
||||
if (entry.getKey().equals("title")){
|
||||
target.setTitle((String) entry.getValue());
|
||||
return courseRepo.save(target);
|
||||
courseRepo.save(target);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (role != Role.Secretary)
|
||||
return false;
|
||||
|
||||
for (Map.Entry<String ,Object> entry: updates.entrySet()){
|
||||
switch (entry.getKey()){
|
||||
case "title":
|
||||
@@ -46,10 +66,15 @@ public class CourseService {
|
||||
target.setCredits((Integer) entry.getValue());
|
||||
break;
|
||||
case "owner":
|
||||
target.setOwner((User) entry.getValue()); //todo check if is a teacher !
|
||||
if (((User) entry.getValue() ).getRole() != Role.Teacher)
|
||||
break;
|
||||
|
||||
target.setOwner((User) entry.getValue());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return courseRepo.save(target);
|
||||
courseRepo.save(target);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,9 +4,7 @@ import org.springframework.stereotype.Service;
|
||||
import ovh.herisson.Clyde.Repositories.CourseRepository;
|
||||
import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository;
|
||||
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
|
||||
import ovh.herisson.Clyde.Tables.Course;
|
||||
import ovh.herisson.Clyde.Tables.Curriculum;
|
||||
import ovh.herisson.Clyde.Tables.CurriculumCourse;
|
||||
import ovh.herisson.Clyde.Tables.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -31,17 +29,18 @@ public class CurriculumCourseService {
|
||||
curriculumCourseRepo.save(curriculumCourse);
|
||||
}
|
||||
|
||||
public Iterable<CurriculumCourse> findAll(){
|
||||
return curriculumCourseRepo.findAll();
|
||||
}
|
||||
|
||||
|
||||
public Map<String, Object> getDepthCurriculum(Curriculum curriculum){
|
||||
|
||||
if (curriculum == null)
|
||||
return null;
|
||||
|
||||
HashMap<String ,Object> toReturn = new HashMap<>();
|
||||
ArrayList<Course> courses = new ArrayList<>();
|
||||
for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){
|
||||
courses.add(c);
|
||||
ArrayList<Map<String ,Object>> courses = new ArrayList<>();
|
||||
Iterable<Course> foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum);
|
||||
|
||||
for (Course c: foundCourses){
|
||||
courses.add(ProtectionService.courseWithoutPassword(c));
|
||||
}
|
||||
toReturn.put("courses",courses);
|
||||
toReturn.put("curriculumId", curriculum.getCurriculumId());
|
||||
@@ -56,13 +55,39 @@ public class CurriculumCourseService {
|
||||
|
||||
ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
|
||||
|
||||
for (Curriculum curriculum : curriculumCourseRepo.findDistinctCurriculums()){
|
||||
for (Curriculum curriculum : curriculumRepo.findAll()){
|
||||
toReturn.add(getDepthCurriculum(curriculum));
|
||||
}
|
||||
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
/** tries to add all courses to the curriculum
|
||||
*
|
||||
* @param coursesIds the ids of the courses to be added
|
||||
* @param curriculum the curriculum to add the courses to
|
||||
* @return if the changes were made
|
||||
*/
|
||||
public boolean saveAll(Iterable<Long> coursesIds, Curriculum curriculum) {
|
||||
|
||||
if (curriculum == null || coursesIds == null)
|
||||
return false;
|
||||
|
||||
ArrayList<Course> toAdd = new ArrayList<>();
|
||||
for (Long courseId : coursesIds){
|
||||
|
||||
Course course = courseRepo.findById((long) courseId);
|
||||
if (course == null)
|
||||
return false;
|
||||
|
||||
if (!toAdd.contains(course))
|
||||
toAdd.add(course);
|
||||
}
|
||||
|
||||
for (Course course : toAdd){
|
||||
curriculumCourseRepo.save(new CurriculumCourse(curriculum,course));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package ovh.herisson.Clyde.Services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ovh.herisson.Clyde.Repositories.CourseRepository;
|
||||
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
|
||||
import ovh.herisson.Clyde.Tables.Curriculum;
|
||||
|
||||
@@ -10,23 +9,14 @@ public class CurriculumService {
|
||||
|
||||
private final CurriculumRepository curriculumRepo;
|
||||
|
||||
private final CourseRepository courseRepo;
|
||||
|
||||
public CurriculumService(CurriculumRepository curriculumRepo, CourseRepository courseRepo){
|
||||
public CurriculumService(CurriculumRepository curriculumRepo){
|
||||
this.curriculumRepo = curriculumRepo;
|
||||
this.courseRepo = courseRepo;
|
||||
}
|
||||
|
||||
|
||||
public void save(Curriculum curriculum){
|
||||
curriculumRepo.save(curriculum);
|
||||
public Curriculum save(Curriculum curriculum){
|
||||
return curriculumRepo.save(curriculum);
|
||||
}
|
||||
|
||||
public Curriculum findById(long id){
|
||||
return curriculumRepo.findById(id);
|
||||
}
|
||||
|
||||
public Iterable<Curriculum> findAll(){
|
||||
return curriculumRepo.findAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@ public class InscriptionService {
|
||||
this.inscriptionRepo = inscriptionRepo;
|
||||
}
|
||||
|
||||
public void save(InscriptionRequest inscriptionRequest){
|
||||
inscriptionRepo.save(inscriptionRequest);
|
||||
public InscriptionRequest save(InscriptionRequest inscriptionRequest){
|
||||
return inscriptionRepo.save(inscriptionRequest);
|
||||
}
|
||||
|
||||
public InscriptionRequest getById(long id){
|
||||
@@ -26,9 +26,14 @@ public class InscriptionService {
|
||||
return inscriptionRepo.findAll();
|
||||
}
|
||||
|
||||
public void modifyState(long id, RequestState requestState) {
|
||||
public boolean modifyState(long id, RequestState requestState) {
|
||||
InscriptionRequest inscriptionRequest = getById(id);
|
||||
|
||||
if (inscriptionRequest == null)
|
||||
return false;
|
||||
|
||||
inscriptionRequest.setState(requestState);
|
||||
save(inscriptionRequest);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package ovh.herisson.Clyde.Services;
|
||||
|
||||
import ovh.herisson.Clyde.Tables.Course;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ProtectionService {
|
||||
|
||||
/** return user's data except password
|
||||
* @param user the user to return
|
||||
* @return all the user data without the password
|
||||
*/
|
||||
public static HashMap<String,Object> userWithoutPassword(User user){
|
||||
HashMap<String,Object> toReturn = new HashMap<>();
|
||||
|
||||
toReturn.put("regNo",user.getRegNo());
|
||||
toReturn.put("lastName",user.getLastName());
|
||||
toReturn.put("firstName",user.getFirstName());
|
||||
toReturn.put("email", user.getEmail());
|
||||
toReturn.put("address",user.getAddress());
|
||||
toReturn.put("birthDate",user.getBirthDate());
|
||||
toReturn.put("country",user.getCountry());
|
||||
toReturn.put("profilePictureUrl",user.getProfilePictureUrl());
|
||||
toReturn.put("role",user.getRole());
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public static Iterable<HashMap<String ,Object>>usersWithoutPasswords(Iterable<User> users){
|
||||
ArrayList<HashMap<String,Object>> toReturn = new ArrayList<>();
|
||||
|
||||
for (User u : users){
|
||||
toReturn.add(userWithoutPassword(u));
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static HashMap<String,Object> courseWithoutPassword(Course course){
|
||||
HashMap<String ,Object> toReturn = new HashMap<>();
|
||||
|
||||
toReturn.put("courseId",course.getCourseID());
|
||||
toReturn.put("credits",course.getCredits());
|
||||
toReturn.put("title", course.getTitle());
|
||||
toReturn.put("owner", userWithoutPassword(course.getOwner()));
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public static Iterable<HashMap<String ,Object>> coursesWithoutPasswords(Iterable<Course> courses){
|
||||
ArrayList<HashMap<String,Object>> toReturn = new ArrayList<>();
|
||||
|
||||
for (Course course: courses){
|
||||
toReturn.add(ProtectionService.courseWithoutPassword(course));
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@ public class StorageService {
|
||||
|
||||
public StorageFile store(MultipartFile file, FileType fileType) {
|
||||
|
||||
if (file == null || file.getOriginalFilename() == null)
|
||||
return null;
|
||||
|
||||
if (file.getOriginalFilename().isEmpty()){return null;}
|
||||
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.springframework.stereotype.Controller;
|
||||
import ovh.herisson.Clyde.Repositories.TeacherCourseRepository;
|
||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
||||
import ovh.herisson.Clyde.Tables.Course;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.TeacherCourse;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
@@ -20,20 +21,33 @@ public class TeacherCourseService {
|
||||
this.userRepo = userRepo;
|
||||
}
|
||||
|
||||
public Iterable<User> findCourseAssistants(Course course) {
|
||||
if (course == null)
|
||||
return null;
|
||||
return teacherCourseRepo.findAllAssistantOfCourse(course);
|
||||
}
|
||||
|
||||
|
||||
public boolean saveAll(Iterable<Long> teacherIds, Course course){
|
||||
|
||||
ArrayList<Long> addedIds = new ArrayList<>();
|
||||
if (course == null || teacherIds == null)
|
||||
return false;
|
||||
|
||||
ArrayList<User> toAdd = new ArrayList<>();
|
||||
for (Long teacherId : teacherIds){
|
||||
User teacher = userRepo.findById((long) teacherId);
|
||||
if ( teacher== null){
|
||||
return false;
|
||||
}
|
||||
if (!addedIds.contains(teacherId))
|
||||
if (!toAdd.contains(teacher) && teacher.getRole() == Role.Teacher)
|
||||
{
|
||||
teacherCourseRepo.save(new TeacherCourse(teacher,course));
|
||||
addedIds.add(teacherId);
|
||||
toAdd.add(teacher);
|
||||
}
|
||||
}
|
||||
for (User teacher: toAdd){
|
||||
teacherCourseRepo.save(new TeacherCourse(teacher,course));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,16 +40,19 @@ public class TokenService {
|
||||
|
||||
public User getUserFromToken(String token) {
|
||||
Token tokenRep = tokenRepo.getByToken(token);
|
||||
if (tokenRep == null) return null;
|
||||
if (tokenRep == null)
|
||||
return null;
|
||||
|
||||
return tokenRep.getUser();
|
||||
}
|
||||
|
||||
public void saveToken(Token token){
|
||||
//Si l'utilisateur a déja 5 token delete celui qui devait expirer le plus vite
|
||||
ArrayList<Token> tokenList = tokenRepo.getByUserOrderByExpirationDate(token.getUser());
|
||||
|
||||
while(tokenList.size() >= 5){
|
||||
tokenRepo.delete(tokenList.get(0));
|
||||
tokenList.remove(tokenList.get(0));
|
||||
tokenRepo.delete(tokenList.getFirst());
|
||||
tokenList.remove(tokenList.getFirst());
|
||||
}
|
||||
tokenRepo.save(token);
|
||||
}
|
||||
@@ -67,5 +70,5 @@ public class TokenService {
|
||||
tokenRepo.delete(t);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,15 @@ public class UserService {
|
||||
}
|
||||
|
||||
|
||||
/** return the user identified by th identifier
|
||||
*
|
||||
* @param identifier can be an email or the RegNo
|
||||
* @return the identified user
|
||||
*/
|
||||
public User getUser(String identifier){
|
||||
if (identifier == null) return null;
|
||||
if (identifier == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
int id = Integer.parseInt(identifier);
|
||||
return userRepo.findById(id);
|
||||
@@ -33,16 +40,18 @@ public class UserService {
|
||||
*
|
||||
* @param poster the user wanting to modify target's data
|
||||
* @param updates the changes to be made
|
||||
* @param target the user to update
|
||||
* @param targetId the id of the user to update
|
||||
* @return if the changes were done or not
|
||||
*/
|
||||
public boolean modifyData(User poster, Map<String ,Object> updates, User target){
|
||||
public boolean modifyData(long targetId, Map<String ,Object> updates, User poster){
|
||||
|
||||
User target = userRepo.findById(targetId);
|
||||
if (target == null)
|
||||
return false;
|
||||
|
||||
if (poster.getRegNo().equals(target.getRegNo())){
|
||||
for (Map.Entry<String, Object> entry : updates.entrySet()){
|
||||
|
||||
if ( entry.getKey().equals("regNo") || entry.getKey().equals("role")) {return false;}
|
||||
|
||||
switch (entry.getKey()){
|
||||
case "firstName":
|
||||
target.setFirstName((String) entry.getValue());
|
||||
@@ -78,13 +87,14 @@ public class UserService {
|
||||
{
|
||||
for (Map.Entry<String, Object> entry : updates.entrySet()){
|
||||
|
||||
if ( !entry.getKey().equals("role")) {return false;}
|
||||
if ( entry.getKey().equals("role")) {
|
||||
|
||||
if (entry.getValue() == Role.Admin){return false;}
|
||||
if (entry.getValue() == Role.Admin) {return false;}
|
||||
|
||||
target.setRole((Role) entry.getValue());
|
||||
userRepo.save(target);
|
||||
return true;
|
||||
target.setRole((Role) entry.getValue());
|
||||
userRepo.save(target);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -95,9 +105,9 @@ public class UserService {
|
||||
return passwordEncoder.matches(tryingPassword, user.getPassword());
|
||||
}
|
||||
|
||||
public void save(User user){
|
||||
public User save(User user){
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
userRepo.save(user);
|
||||
return userRepo.save(user);
|
||||
}
|
||||
|
||||
public Iterable<User> getAll(){
|
||||
@@ -105,8 +115,7 @@ public class UserService {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Iterable<User> getAllTeachers (){return userRepo.findAllTeachers();}
|
||||
|
||||
public Iterable<User> getAllStudents(){return userRepo.findAllStudents();}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user