Skip to content

Commit

Permalink
refs #1 - acrescentando novas exceções e ajustando profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ruan-pb committed Feb 10, 2021
1 parent bf0b102 commit 2cfbc84
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void configure( HttpSecurity http ) throws Exception {
http
.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST,"/hatcher/Authenticate")
.antMatchers(HttpMethod.POST,"/hatcher/auth")
.permitAll()
.antMatchers(HttpMethod.GET,"/hatcher/listUsers")
.permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;


import javax.validation.Valid;

import org.ayty.hatcher.api.v1.security.JwtService;
Expand Down Expand Up @@ -70,9 +71,9 @@ public ResponseEntity<OutRegisterDTO> registerUser(@Valid @RequestBody Register

}

@PostMapping("/Authenticate")
@PostMapping("/auth")
@ResponseStatus(HttpStatus.ACCEPTED)
public TokenDTO authenticate( @Valid @RequestBody LoginDTO credenciais){
public TokenDTO authenticate(@Valid @RequestBody LoginDTO credenciais){
try{
User user = User.builder().login(credenciais.getLogin()).password(credenciais.getPassword())
.build();
Expand All @@ -83,9 +84,7 @@ public TokenDTO authenticate( @Valid @RequestBody LoginDTO credenciais){
} catch (IncorrectUserOrPassword e){
throw new IncorrectUserOrPassword();
}
catch(UsernameNotFoundException e) {
throw new UsernameNotFoundException();
}

catch(LoginNotFound e) {
throw new LoginNotFound();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ayty.hatcher.api.v1.user.dto;

import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
Expand All @@ -26,10 +28,11 @@ public class RegisterUserDTO {

private String image;

private String profile;

private boolean admin;

@Enumerated(EnumType.STRING)
private String profile;




Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
Expand Down Expand Up @@ -51,10 +53,11 @@ public class User implements Serializable{

private String image;

private Profile profile;

private boolean admin;

@Enumerated(EnumType.STRING)
private Profile profile;




Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.ayty.hatcher.api.v1.user.exception;

public class UserDoesNotExist extends RuntimeException{


private static final long serialVersionUID = 1L;

public UserDoesNotExist(String msg) {
super(msg);
}
public UserDoesNotExist() {
super();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.ayty.hatcher.api.v1.user.exception.InvalidData;
import org.ayty.hatcher.api.v1.user.exception.LoginNotFound;
import org.ayty.hatcher.api.v1.user.exception.UserAlreadyExists;
import org.ayty.hatcher.api.v1.user.exception.UserDoesNotExist;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -16,8 +17,8 @@ public class ExceptionHandle {

@ExceptionHandler(IncorrectUserOrPassword.class)
public ResponseEntity<StandardError> IncorrectUserOrPasswordHandle(IncorrectUserOrPassword e,HttpServletRequest request){
HttpStatus status = HttpStatus.FORBIDDEN;
StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "Incorrec tUser Or Password", request.getRequestURI());
HttpStatus status = HttpStatus.UNAUTHORIZED;
StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Access denied", "Incorrec User Or Password", request.getRequestURI());
return ResponseEntity.status(status).body(err);
}

Expand All @@ -40,6 +41,12 @@ public ResponseEntity<StandardError> InvalidDataHandle(InvalidData e,HttpServlet
StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "Valid Data", "Data is invalid", request.getRequestURI());
return ResponseEntity.status(status).body(err);
}
@ExceptionHandler(UserDoesNotExist.class)
public ResponseEntity<StandardError> UserDoesNotExistHandle(UserDoesNotExist e,HttpServletRequest request){
HttpStatus status = HttpStatus.NOT_FOUND;
StandardError err = new StandardError(System.currentTimeMillis(), status.value(), "User Not found", "User Does NotE xist", request.getRequestURI());
return ResponseEntity.status(status).body(err);
}
/*
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
Optional<User> findByLogin(String login);
Optional<User> findByPassword(String password);
boolean existsByLogin(String login);
boolean existsByPassword(String password);
void deleteByLogin(String login);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import org.ayty.hatcher.api.v1.user.entity.User;
import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword;
import org.ayty.hatcher.api.v1.user.exception.UsernameNotFoundException;
import org.ayty.hatcher.api.v1.user.jpa.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -24,7 +25,7 @@ public UserDetails loadUserByUsername(String login) throws UsernameNotFoundExcep

User user = userBD.findByLogin(login)
.orElseThrow(() ->
new UsernameNotFoundException());
new IncorrectUserOrPassword());


String[] roles = user.isAdmin() ? new String[] {"ADMIN","USER"} : new String[] {"USER"};
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/ayty/hatcher/api/v1/user/service/LoginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@


import org.ayty.hatcher.api.v1.user.entity.User;

import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword;
import org.ayty.hatcher.api.v1.user.exception.LoginNotFound;
import org.ayty.hatcher.api.v1.user.exception.UserDoesNotExist;
import org.ayty.hatcher.api.v1.user.jpa.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
Expand All @@ -20,6 +23,8 @@ public class LoginImpl implements Login {

private final LoadUserByUsarname load;

private final UserRepository userBD;



public UserDetails authenticate( User user ){
Expand All @@ -28,13 +33,21 @@ public UserDetails authenticate( User user ){
if(userDetails.getUsername()== null ){
throw new LoginNotFound();
}
User usuario = userBD.findByLogin(user.getLogin())
.orElseThrow(() ->
new UserDoesNotExist());



boolean PasswordsMatch = encoder.matches(user.getPassword(),userDetails.getPassword() );
boolean PasswordsMatch = encoder.matches(user.getPassword(),userDetails.getPassword());

if(PasswordsMatch){
return userDetails;
}
throw new IncorrectUserOrPassword();
else {
throw new IncorrectUserOrPassword();

}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public Profile checkProfile(String OptionProfile) {
}else{
profile = Enum.valueOf(Profile.class, OptionProfile.toUpperCase());
}
if(OptionProfile.equalsIgnoreCase("PROFESSOR")) {
profile = Enum.valueOf(Profile.class, OptionProfile.toUpperCase());
}else{
profile = Enum.valueOf(Profile.class, OptionProfile.toUpperCase());
}

return profile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@




import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO;
import org.ayty.hatcher.api.v1.user.entity.User;
import org.ayty.hatcher.api.v1.user.exception.InvalidPasswordException;
import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPassword;
import org.ayty.hatcher.api.v1.user.jpa.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down Expand Up @@ -54,7 +55,7 @@ public UserDetails authenticate( User user ){
return userDetails;
}

throw new InvalidPasswordException();
throw new IncorrectUserOrPassword();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
insert into tb_user
values(DEFAULT,'admin','$2y$04$Hexn6JOpJn8ohHTdX0zJdODijX1ks6JmjUqlgJYJiAuV9KatL3aqS','[email protected]','admin','admin',true,'admin');
values(DEFAULT,'admin','$2y$04$Hexn6JOpJn8ohHTdX0zJdODijX1ks6JmjUqlgJYJiAuV9KatL3aqS','[email protected]','admin','admin',true,'PROFESSOR');
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Create table tb_user(
full_name varchar(255) not null,
image varchar (255),
admin BOOLEAN,
perfil varchar(255)
profile varchar(255)


);
Expand Down

0 comments on commit 2cfbc84

Please sign in to comment.