Compare commits

..

No commits in common. "19b4950be4d8ab909109ac377ce92a852df38ce4" and "d9307753c44e0085c7a02902f219fa4b2e143c41" have entirely different histories.

10 changed files with 38 additions and 93 deletions

View File

@ -54,6 +54,8 @@ public class ApplicationsController {
// if authed // if authed
authorizedApps.add(Applications.Profile); authorizedApps.add(Applications.Profile);
Role posterRole = user.getRole();
if (!authServ.isNotIn(new Role[]{Role.Teacher,Role.Student,Role.Admin},token)) { if (!authServ.isNotIn(new Role[]{Role.Teacher,Role.Student,Role.Admin},token)) {
authorizedApps.add(Applications.Msg); authorizedApps.add(Applications.Msg);
authorizedApps.add(Applications.Forum); authorizedApps.add(Applications.Forum);

View File

@ -58,6 +58,22 @@ public class InscriptionController {
return new ResponseEntity<>(requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK); return new ResponseEntity<>(requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK);
} }
/**
@GetMapping("request/user")
public ResponseEntity<InscriptionRequest> getUserInscriptionRequest(@RequestHeader("Authorization") String token){
//todo return l'inscriptionRequest ACTUELLE du user (check si le poster est bien le même que id target ou secretariat)
if (authServ.IsNotIn(new Role[]{Role.Student,Role.Admin},token))
return new UnauthorizedResponse<>(null);
User poster = authServ.getUserFromToken(token);
inscriptionServ.getById()
return null;
} **/
@PatchMapping("/request/register/{id}") @PatchMapping("/request/register/{id}")
public ResponseEntity<InscriptionRequest> changeRequestState(@PathVariable long id, public ResponseEntity<InscriptionRequest> changeRequestState(@PathVariable long id,
@RequestHeader("Authorization") String token, @RequestHeader("Authorization") String token,
@ -83,7 +99,7 @@ public class InscriptionController {
toReturn.put("email",inscriptionRequest.getEmail()); toReturn.put("email",inscriptionRequest.getEmail());
toReturn.put("birthDate", inscriptionRequest.getBirthDate()); toReturn.put("birthDate", inscriptionRequest.getBirthDate());
toReturn.put("country", inscriptionRequest.getCountry()); toReturn.put("country", inscriptionRequest.getCountry());
toReturn.put("curriculum", inscriptionRequest.getCurriculumId()); toReturn.put("curriculum", inscriptionRequest.getCurriculum());
toReturn.put("state", inscriptionRequest.getState()); toReturn.put("state", inscriptionRequest.getState());
toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture());

View File

@ -44,7 +44,7 @@ public class LoginController {
return ResponseEntity.ok().headers(responseHeaders).build(); return ResponseEntity.ok().headers(responseHeaders).build();
} }
@PostMapping("/register") @PostMapping("/request/register")
public ResponseEntity<InscriptionRequest> register(@RequestBody InscriptionRequest inscriptionRequest){ public ResponseEntity<InscriptionRequest> register(@RequestBody InscriptionRequest inscriptionRequest){
return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED); return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED);
} }

View File

@ -9,8 +9,6 @@ import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -55,15 +53,7 @@ public class UserController {
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null); return new UnauthorizedResponse<>(null);
Role posterRole = authServ.getUserFromToken(token).getRole(); Iterable<User> users = userService.getAll();
Iterable<User> users = new ArrayList<>();
if (posterRole == Role.Admin)
users = userService.getAll();
else if (posterRole == Role.Secretary)
users = userService.getAllExceptAdmins();
return new ResponseEntity<>(ProtectionService.usersWithoutPasswords(users), HttpStatus.OK); return new ResponseEntity<>(ProtectionService.usersWithoutPasswords(users), HttpStatus.OK);
} }

View File

@ -1,7 +0,0 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.UserCurriculum;
public interface UserCurriculumRepository extends CrudRepository<UserCurriculum, Long> {
}

View File

@ -10,14 +10,9 @@ public interface UserRepository extends CrudRepository<User, Long> {
User findByEmail(String email); User findByEmail(String email);
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher")
Iterable<User> findAllTeachers(); Iterable<User> findAllTeachers();
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Student") @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Student")
Iterable<User> findAllStudents(); Iterable<User> findAllStudents();
@Query("select u from User u where u.role <> ovh.herisson.Clyde.Tables.Role.Admin")
Iterable<User> findAllExceptAdmins();
} }

View File

@ -2,7 +2,9 @@ package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Tables.*; import ovh.herisson.Clyde.Tables.*;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
@Service @Service
public class AuthenticatorService { public class AuthenticatorService {

View File

@ -1,39 +1,20 @@
package ovh.herisson.Clyde.Services; package ovh.herisson.Clyde.Services;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
import ovh.herisson.Clyde.Repositories.InscriptionRepository; import ovh.herisson.Clyde.Repositories.InscriptionRepository;
import ovh.herisson.Clyde.Repositories.UserCurriculumRepository;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.RequestState; import ovh.herisson.Clyde.Tables.RequestState;
import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.Tables.UserCurriculum;
@Service @Service
public class InscriptionService { public class InscriptionService {
private final InscriptionRepository inscriptionRepo; InscriptionRepository inscriptionRepo;
private final UserRepository userRepo; public InscriptionService(InscriptionRepository inscriptionRepo){
private final UserCurriculumRepository userCurriculumRepo;
private final CurriculumRepository curriculumRepo;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public InscriptionService(InscriptionRepository inscriptionRepo, UserRepository userRepo, UserCurriculumRepository userCurriculumRepo, CurriculumRepository curriculumRepo){
this.inscriptionRepo = inscriptionRepo; this.inscriptionRepo = inscriptionRepo;
this.userRepo = userRepo;
this.userCurriculumRepo = userCurriculumRepo;
this.curriculumRepo = curriculumRepo;
} }
public InscriptionRequest save(InscriptionRequest inscriptionRequest){ public InscriptionRequest save(InscriptionRequest inscriptionRequest){
inscriptionRequest.setPassword(passwordEncoder.encode(inscriptionRequest.getPassword()));
return inscriptionRepo.save(inscriptionRequest); return inscriptionRepo.save(inscriptionRequest);
} }
@ -46,45 +27,13 @@ public class InscriptionService {
} }
public boolean modifyState(long id, RequestState requestState) { public boolean modifyState(long id, RequestState requestState) {
InscriptionRequest inscrRequest = getById(id); InscriptionRequest inscriptionRequest = getById(id);
if (inscrRequest == null) if (inscriptionRequest == null)
return false; return false;
// if th state is the same we don't send an email inscriptionRequest.setState(requestState);
if (requestState == inscrRequest.getState()) save(inscriptionRequest);
return false;
/** todo send an email to tell the poster of the inscrRequest (inscrRequest.getEmail())
* to notify them that the state of their request changed
* FooEmailFormat toSend = (String.format("Your request state changed from %s to %s"),
* inscrRequest.getState(), requestState)
* FooEmailSender.send(toSend, inscrRequest.getEmail())
*/
//saves the user from the request if accepted
if (requestState == RequestState.Accepted)
{
if (curriculumRepo.findById(inscrRequest.getCurriculumId()) == null)
return false;
User userFromRequest = new User(
inscrRequest.getLastName(),
inscrRequest.getFirstName(),
inscrRequest.getEmail(),
inscrRequest.getAddress(),
inscrRequest.getCountry(),
inscrRequest.getBirthDate(),
inscrRequest.getProfilePicture(),
inscrRequest.getPassword()
);
userRepo.save(userFromRequest);
userCurriculumRepo.save(new UserCurriculum(userFromRequest, curriculumRepo.findById(inscrRequest.getCurriculumId())));
}
inscrRequest.setState(requestState);
save(inscrRequest);
return true; return true;
} }
} }

View File

@ -114,10 +114,6 @@ public class UserService {
return userRepo.findAll(); return userRepo.findAll();
} }
public Iterable<User> getAllExceptAdmins(){
return userRepo.findAllExceptAdmins();
}
public Iterable<User> getAllTeachers (){return userRepo.findAllTeachers();} public Iterable<User> getAllTeachers (){return userRepo.findAllTeachers();}

View File

@ -16,20 +16,22 @@ public class InscriptionRequest {
private String country; private String country;
private Date birthDate; private Date birthDate;
private Long curriculumId; @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="Curriculum")
private Curriculum curriculum;
private RequestState state; private RequestState state;
private String profilePicture; private String profilePicture;
private String password; private String password;
public InscriptionRequest(){} public InscriptionRequest(){}
public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Long curriculumId, RequestState state, String profilePicture, String password){ public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Curriculum curriculum, RequestState state, String profilePicture, String password){
this.lastName = lastName; this.lastName = lastName;
this.firstName = firstName; this.firstName = firstName;
this.address = address; this.address = address;
this.email = email; this.email = email;
this.country = country; this.country = country;
this.birthDate = birthDate; this.birthDate = birthDate;
this.curriculumId = curriculumId; this.curriculum = curriculum;
this.state = state; this.state = state;
this.profilePicture = profilePicture; this.profilePicture = profilePicture;
this.password = password; this.password = password;
@ -87,12 +89,12 @@ public class InscriptionRequest {
this.birthDate = birthDate; this.birthDate = birthDate;
} }
public long getCurriculumId() { public Curriculum getCurriculum() {
return curriculumId; return curriculum;
} }
public void setCurriculumId(long curriculum) { public void setCurriculum(Curriculum curriculum) {
this.curriculumId = curriculum; this.curriculum = curriculum;
} }
public RequestState getState() { public RequestState getState() {