Merge pull request #18 from HediMjid/Blandine

sign-up + forgot-password + rester-pwd non complet
This commit is contained in:
Blandine Bajard 2022-01-21 09:34:42 +01:00 committed by GitHub
commit b6c5adb262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 30 deletions

View File

@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
@ -32,6 +33,9 @@ public class MembreController {
@Autowired @Autowired
private MembreService membreService; private MembreService membreService;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
// @Autowired // @Autowired
// private TeamRepository teamRepo; // private TeamRepository teamRepo;
@ -66,6 +70,41 @@ public class MembreController {
} }
@GetMapping("/forgot-password")
//@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity<?> findUserByEmail(@RequestBody Membre findUserByEmail)
{
try {
this.membreService.findUserByEmail(findUserByEmail);
return ResponseEntity.ok("ok");
} catch(Exception e)
{
return ResponseEntity.notFound().build();
}
}
@PutMapping("/reset-password/{email}")
//@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity<?> updatePassword(@RequestBody String password, @PathVariable String email) throws Exception {
Membre resultMembre;
try {
resultMembre = this.membreService.chercheEmail(email);
System.out.println(resultMembre);
resultMembre.setPassword(passwordEncoder.encode(password));
this.membreRepo.save(resultMembre);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
return ResponseEntity.status(HttpStatus.OK).body(resultMembre);
}
// @GetMapping(value = "/team/all") // @GetMapping(value = "/team/all")
// public ResponseEntity<?> getAllTeam(){ // public ResponseEntity<?> getAllTeam(){
// List<Team> liste = null; // List<Team> liste = null;

View File

@ -44,24 +44,16 @@ public class Membre {
public Membre() { public Membre() {
} }
public Membre(String nom, String prenom, LocalDate dateNaissance, @NotNull String email, @NotNull String password, String isAdmin, String couleur, String smiley, Team team, List<Role> roleList) { public Membre(String nom, String prenom, LocalDate dateNaissance, Team team, @NotNull String email, @NotNull String password, List<Role> roleList) {
this.nom = nom; this.nom = nom;
this.prenom = prenom; this.prenom = prenom;
this.dateNaissance = dateNaissance; this.dateNaissance = dateNaissance;
this.email = email; this.email = email;
this.password = password; this.password = password;
this.isAdmin = isAdmin;
this.couleur = couleur;
this.smiley = smiley;
this.team = team; this.team = team;
this.roleList=roleList; this.roleList=roleList;
} }
public Membre(@NotNull String email, @NotNull String password, List<Role> roleList) {
this.email = email;
this.password = password;
this.roleList=roleList;
}
public int getId() { public int getId() {
@ -114,23 +106,7 @@ public class Membre {
this.team = team; this.team = team;
} }
public String getCouleur() { public List<Role> getRoleList() {
return couleur;
}
public void setCouleur(String couleur) {
this.couleur = couleur;
}
public String getSmiley() {
return smiley;
}
public void setSmiley(String smiley) {
this.smiley = smiley;
}
public List<Role> getRoleList() {
return roleList; return roleList;
} }
public void setRoleList(List<Role> roleList) { public void setRoleList(List<Role> roleList) {

View File

@ -1,9 +1,13 @@
package fr.organizee.repository; package fr.organizee.repository;
import fr.organizee.model.Contact;
import fr.organizee.model.Membre; import fr.organizee.model.Membre;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Repository @Repository
@ -12,7 +16,12 @@ public interface MembreRepository extends JpaRepository<Membre, Integer> {
Optional<Membre> findByEmail(String email); Optional<Membre> findByEmail(String email);
@Query(value = "select * from membre where email = :email", nativeQuery = true)
Membre chercheEmail(@Param("email") String email);
boolean existsByEmail(String email); boolean existsByEmail(String email);
void deleteByEmail(String email); void deleteByEmail(String email);
} }

View File

@ -29,6 +29,7 @@ public interface MembreService {
*/ */
String signup(Membre membre) throws ExistingUsernameException; String signup(Membre membre) throws ExistingUsernameException;
/** /**
* Methode qui retourne tous les utilisateurs de la bd * Methode qui retourne tous les utilisateurs de la bd
* @return the list of all application users. * @return the list of all application users.
@ -40,6 +41,10 @@ public interface MembreService {
* @param email the username to look for. * @param email the username to look for.
* @return an Optional object containing user if found, empty otherwise. * @return an Optional object containing user if found, empty otherwise.
*/ */
Optional<Membre> findUserByEmail(String email); Optional<Membre> findUserByEmail(Membre membre);
Optional<Membre> findByEmail(String email);
Membre chercheEmail(String email);
} }

View File

@ -48,7 +48,7 @@ public class MembreServiceImpl implements MembreService {
@Override @Override
public String signup(Membre membre) throws ExistingUsernameException { public String signup(Membre membre) throws ExistingUsernameException {
if (!membreRepository.existsByEmail(membre.getEmail())) { if (!membreRepository.existsByEmail(membre.getEmail())) {
Membre membreToSave = new Membre(membre.getEmail(), passwordEncoder.encode(membre.getPassword()), membre.getRoleList()); Membre membreToSave = new Membre(membre.getNom(), membre.getPrenom(), membre.getDateNaissance(), membre.getTeam(), membre.getEmail(), passwordEncoder.encode(membre.getPassword()), membre.getRoleList());
membreRepository.save(membreToSave); membreRepository.save(membreToSave);
return jwtTokenProvider.createToken(membre.getEmail(), membre.getRoleList()); return jwtTokenProvider.createToken(membre.getEmail(), membre.getRoleList());
} else { } else {
@ -62,8 +62,20 @@ public class MembreServiceImpl implements MembreService {
} }
@Override @Override
public Optional<Membre> findUserByEmail(String email) { public Optional<Membre> findUserByEmail(Membre membre) {
return membreRepository.findByEmail(email); return this.membreRepository.findByEmail(membre.getEmail());
} }
@Override
public Optional<Membre> findByEmail(String email) {
return this.membreRepository.findByEmail(email);
}
@Override
public Membre chercheEmail(String email) {
return this.membreRepository.chercheEmail(email);
}
} }