Skip to content

Commit

Permalink
Merge pull request #556 from bounswe/fix/be-540-remove-selected-role
Browse files Browse the repository at this point in the history
Well done
  • Loading branch information
alitpc25 authored Dec 12, 2023
2 parents a59030e + 01d6275 commit 16f8389
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.groupa1.resq.entity.User;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand Down Expand Up @@ -41,21 +40,7 @@ public UserDetailsImpl(Long id, String name, String surname, String email, Strin

public static UserDetailsImpl build(User user) {
List<GrantedAuthority> authorities = user.getRoles().stream()
.map(role -> new SimpleGrantedAuthority(role.name()))
.collect(Collectors.toList());

return new UserDetailsImpl(user.getId(),
user.getName(),
user.getSurname(),
user.getEmail(),
user.getPassword(),
authorities);
}

public static UserDetailsImpl build(User user, String userRole) {
List<GrantedAuthority> authorities = user.getRoles().stream()
.filter(role -> role.name().toUpperCase().equals(userRole.toUpperCase()))
.map(role -> new SimpleGrantedAuthority("ROLE_"+role.name()))
.map(role ->new SimpleGrantedAuthority("ROLE_"+role.name()))
.collect(Collectors.toList());

return new UserDetailsImpl(user.getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class UserDetailsServiceImpl implements UserDetailsService {
UserService userService;


// Since the methods shoul override the UserDetailsService interface, method name is loadUserByUsername, but it uses email
@Override
@Transactional
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Expand All @@ -26,10 +25,4 @@ public UserDetails loadUserByUsername(String email) throws UsernameNotFoundExcep
return UserDetailsImpl.build(user);
}

public UserDetails loadUserByUsername(String email, String selectedRole) {
User user = userService.findByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("User Not Found with email: " + email));

return UserDetailsImpl.build(user, selectedRole);
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
package com.groupa1.resq.config;

import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenApiConfig {
@Bean
public OperationCustomizer customGlobalHeaders() {
return (operation, handlerMethod) -> {

// Add role to all endpoints
Parameter missingParam2 = new Parameter().in(ParameterIn.HEADER.toString())
.name("X-Selected-Role")
.schema(new StringSchema())
.description("ROLE");

operation.addParametersItem(missingParam2);

return operation;
};
}

@Bean
public OpenAPI customizeOpenAPI() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.groupa1.resq.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.groupa1.resq.entity.enums.EUserRole;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
Expand All @@ -8,7 +9,6 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.HashSet;
import java.util.Set;

Expand All @@ -20,6 +20,7 @@
@Data
@EqualsAndHashCode(callSuper = true, exclude = {"userProfile", "requests", "needs", "resourcesReceived","resourcesSent", "tasksAssigned", "tasksAssignedTo", "feedbacks", "actions", "infos", "notifications", "reportedEvents"})
@ToString(callSuper = true, exclude = {"userProfile", "requests", "needs", "resourcesReceived","resourcesSent", "tasksAssigned", "tasksAssignedTo", "feedbacks", "actions", "infos", "notifications", "reportedEvents"})

public class User extends BaseEntity {

@NotBlank
Expand All @@ -44,8 +45,9 @@ public class User extends BaseEntity {
@Enumerated(EnumType.STRING)
private Set<EUserRole> roles = new HashSet<>();

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_profile_id", referencedColumnName = "id")
@OneToOne(fetch= FetchType.LAZY, mappedBy = "user")
@JsonManagedReference

private UserProfile userProfile;

@OneToMany(fetch = FetchType.LAZY, mappedBy="requester")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.groupa1.resq.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.groupa1.resq.entity.enums.EGender;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.time.LocalDate;


Expand All @@ -26,7 +26,9 @@ public class UserProfile extends BaseEntity{
@Enumerated(EnumType.STRING)
private EGender gender;

@OneToOne(mappedBy = "userProfile")
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
@JsonBackReference
private User user;

private boolean isEmailConfirmed;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.groupa1.resq.security;

import com.groupa1.resq.auth.UserDetailsServiceImpl;
import com.groupa1.resq.entity.enums.EUserRole;
import com.groupa1.resq.security.jwt.JwtUtils;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -34,10 +33,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
if (jwt != null && jwtUtils.validateJwtToken(jwt)) {
String username = jwtUtils.getUserNameFromJwtToken(jwt);

String selectedRole = request.getHeader("X-Selected-Role");
EUserRole userRole = EUserRole.getEnumByStr(selectedRole.toUpperCase()); // If not found, throws error.

UserDetails userDetails = userDetailsService.loadUserByUsername(username, selectedRole);
UserDetails userDetails = userDetailsService.loadUserByUsername(username);

UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null,
userDetails.getAuthorities());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public String updateProfile(Long userId, ProfileDto profileDto)
UserProfile updatedProfile = profileConverter.convertToEntity(updatedProfileDto);
beanUtils.copyProperties(userProfile, updatedProfile);
user.setUserProfile(userProfile);
user.setName(userProfile.getName());
user.setSurname(userProfile.getSurname());
userRepository.save(user);

return "Profile successfully updated.";

}
Expand Down

0 comments on commit 16f8389

Please sign in to comment.