updated code for PreAuthorize role
This commit is contained in:
@@ -31,7 +31,7 @@ public interface CallApi {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) }))
|
||||
})
|
||||
@PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
@PreAuthorize("hasRole('SUPER_ADMIN')")
|
||||
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
|
||||
public ResponseEntity<Response<CreateCallResponseBean>> createCall(HttpServletRequest request,
|
||||
@Parameter(description = "Call request object", required = true)
|
||||
@Valid @RequestBody CreateCallRequest createCallRequest);
|
||||
|
||||
@@ -16,6 +16,7 @@ import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -37,6 +38,7 @@ public interface UserApi {
|
||||
@RequestMapping(value = "",
|
||||
produces = {"application/json"},
|
||||
method = RequestMethod.POST)
|
||||
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
|
||||
default ResponseEntity<Response<UserResponseBean>> createUser(
|
||||
@Parameter(description = "User request object", required = true) @Validated @RequestBody UserReq userReq) {
|
||||
return new ResponseEntity<Response<UserResponseBean>>(HttpStatus.NOT_IMPLEMENTED);
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package net.gepafin.tendermanagement.web.rest.api.impl;
|
||||
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.RoleEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.repositories.RoleRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserRepository;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
@@ -22,11 +26,9 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
private final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final RoleRepository roleRepository;
|
||||
|
||||
public CustomUserDetailsService(UserRepository userRepository, RoleRepository roleRepository) {
|
||||
public CustomUserDetailsService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
this.roleRepository = roleRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -34,17 +36,15 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
public UserDetails loadUserByUsername(final String email) throws UsernameNotFoundException {
|
||||
log.debug("Authenticating {}", email);
|
||||
|
||||
UserEntity user = userRepository.findByEmail(email);
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("User " + email + " was not found in the database");
|
||||
}
|
||||
|
||||
UserEntity user = userRepository.findByEmailIgnoreCase(email)
|
||||
.orElseThrow(
|
||||
() -> new UsernameNotFoundException("User " + email + " was not found in the database"));
|
||||
return createSpringSecurityUser(user);
|
||||
}
|
||||
|
||||
private org.springframework.security.core.userdetails.User createSpringSecurityUser(UserEntity user) {
|
||||
RoleEntity role = user.getRoleEntity();
|
||||
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role.getRoleName());
|
||||
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role.getRoleType());
|
||||
|
||||
return new org.springframework.security.core.userdetails.User(
|
||||
user.getEmail(),
|
||||
|
||||
Reference in New Issue
Block a user