Conflict Resolved
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class DynamicLocalTimeSerializer extends JsonSerializer<LocalTime> {
|
||||
private static final DateTimeFormatter HH_MM_FORMAT = DateTimeFormatter.ofPattern("HH:mm");
|
||||
private static final DateTimeFormatter HH_MM_SS_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public void serialize(LocalTime time, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
// Use HH:mm if seconds are 00, otherwise use HH:mm:ss
|
||||
String formattedTime = (time.getSecond() == 0)
|
||||
? time.format(HH_MM_FORMAT)
|
||||
: time.format(HH_MM_SS_FORMAT);
|
||||
|
||||
gen.writeString(formattedTime);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -32,6 +32,8 @@ import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientNotFoundExcep
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientUnauthorizedException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientValidationException;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
||||
|
||||
|
||||
public class Utils {
|
||||
|
||||
@@ -324,7 +326,64 @@ public class Utils {
|
||||
return new ObjectMapper().writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Failed to convert object to JSON: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("Failed to convert object to JSON", e);
|
||||
throw new RuntimeException("Failed to convert object to JSON", e);}}
|
||||
|
||||
public static String replaceSpacesWithUnderscores(String content) {
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
return content.trim().replace(" ", "_");
|
||||
}
|
||||
public static List<Map<String, Object>> convertJsonStringIntoJsonList(String jsonString) {
|
||||
try {
|
||||
if(isEmpty(jsonString))
|
||||
{
|
||||
return new ArrayList<>();
|
||||
}
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
|
||||
return mapper.readValue(jsonString, List.class);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String convertToString(Object input) {
|
||||
if (input == null) {
|
||||
return "null"; // Return string "null" for null input
|
||||
}
|
||||
|
||||
if (input instanceof String) {
|
||||
return (String) input; // Return the string directly if input is a string
|
||||
}
|
||||
|
||||
if (input instanceof Collection<?>) {
|
||||
// Handle collections (List, Set, etc.)
|
||||
return convertCollectionToString((Collection<?>) input);
|
||||
}
|
||||
|
||||
if (input instanceof Map<?, ?>) {
|
||||
// Handle maps
|
||||
return convertMapToString((Map<?, ?>) input);
|
||||
}
|
||||
|
||||
// For other types (like Integer, Boolean, etc.), use toString()
|
||||
return input.toString();
|
||||
}
|
||||
|
||||
private static String convertCollectionToString(Collection<?> collection) {
|
||||
try {
|
||||
return mapper.writeValueAsString(collection); // Convert the collection to a JSON string
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException("Error converting collection to string", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String convertMapToString(Map<?, ?> map) {
|
||||
try {
|
||||
return mapper.writeValueAsString(map); // Convert the map to a JSON string
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException("Error converting map to string", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.config.jwt.TokenProvider;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.CallEntity;
|
||||
import net.gepafin.tendermanagement.entities.CompanyEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.service.CompanyService;
|
||||
import net.gepafin.tendermanagement.service.UserService;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ForbiddenAccessException;
|
||||
@@ -14,11 +16,13 @@ import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.UnauthorizedAccessException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@@ -32,6 +36,12 @@ public class Validator {
|
||||
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
|
||||
@Autowired
|
||||
private CallService callService;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public Map<String, Object> getUserInfoFromToken(HttpServletRequest request) {
|
||||
return tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
@@ -54,6 +64,20 @@ public class Validator {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Boolean checkIsPreInstructor() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication != null && authentication.isAuthenticated()) {
|
||||
// Check if the user has the ROLE_SUPER_ADMIN authority
|
||||
for (GrantedAuthority authority : authentication.getAuthorities()) {
|
||||
if (RoleStatusEnum.ROLE_PRE_INSTRUCTOR.getValue().equals(authority.getAuthority())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void validateRequest(HttpServletRequest request,RoleStatusEnum role) {
|
||||
if (RoleStatusEnum.ROLE_SUPER_ADMIN.equals(role) && Boolean.FALSE.equals(checkIsSuperAdmin())) {
|
||||
@@ -62,13 +86,24 @@ public class Validator {
|
||||
}
|
||||
|
||||
public CompanyEntity validateUserWithCompany(HttpServletRequest request, Long companyId) {
|
||||
CompanyEntity companyEntity = companyService.validateCompany(companyId);
|
||||
validateHubId(request, companyEntity.getHub().getId());
|
||||
if (checkIsSuperAdmin()) {
|
||||
return companyService.validateCompany(companyId);
|
||||
return companyEntity;
|
||||
}
|
||||
Map<String, Object> userInfo = tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
companyService.validateUserWithCompny(getUserId(userInfo), companyId);
|
||||
return companyService.validateCompany(companyId);
|
||||
}
|
||||
|
||||
public void validateHubId(HttpServletRequest request, Long hubId) {
|
||||
UserEntity user = validateUser(request);
|
||||
Long hubIdFromHttpRequest = user.getHub().getId();
|
||||
if (Boolean.FALSE.equals(hubIdFromHttpRequest.equals(hubId))) {
|
||||
throw new ForbiddenAccessException(Status.FORBIDDEN,
|
||||
Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
||||
}
|
||||
}
|
||||
|
||||
private Long getUserId(Map<String, Object> userInfo) {
|
||||
return Long.parseLong(userInfo.get("userId").toString());
|
||||
@@ -89,10 +124,15 @@ public class Validator {
|
||||
|
||||
public UserEntity validateUserId(HttpServletRequest request, Long userId) {
|
||||
UserEntity user = validateUser(request);
|
||||
if(user.getRoleEntity().getRoleType().equals(RoleStatusEnum.ROLE_BENEFICIARY.getValue()) && Boolean.FALSE.equals(user.getId().equals(userId))) {
|
||||
throw new ForbiddenAccessException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
||||
UserEntity requestedUser = userService.validateUser(userId);
|
||||
|
||||
validateHubId(request, requestedUser.getHub().getId());
|
||||
if (Boolean.FALSE.equals(user.getRoleEntity().getRoleType().equals(RoleStatusEnum.ROLE_SUPER_ADMIN.getValue()))
|
||||
&& Boolean.FALSE.equals(user.getId().equals(userId))) {
|
||||
throw new ForbiddenAccessException(Status.FORBIDDEN,
|
||||
Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
||||
}
|
||||
return userService.validateUser(userId);
|
||||
return requestedUser;
|
||||
}
|
||||
|
||||
public Long getUserIdFromToken(HttpServletRequest request) {
|
||||
@@ -106,4 +146,33 @@ public class Validator {
|
||||
}
|
||||
return userService.validateUser(userId);
|
||||
}
|
||||
|
||||
public CallEntity validateUserWithCall(UserEntity user, Long callId) {
|
||||
CallEntity callEntity = callService.validateCall(callId);
|
||||
if(Boolean.FALSE.equals(user.getHub().getId().equals(callEntity.getHub().getId()))) {
|
||||
throw new ForbiddenAccessException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
||||
}
|
||||
return callEntity;
|
||||
}
|
||||
|
||||
public Boolean isProductionProfileActivated() {
|
||||
String[] activeProfiles = environment.getActiveProfiles();
|
||||
return Arrays.stream(activeProfiles).anyMatch("production"::equals);
|
||||
}
|
||||
|
||||
public UserEntity validatePreInstructor(HttpServletRequest request, Long preInstructorUserId) {
|
||||
UserEntity preInstructorUser = userService.validateUser(preInstructorUserId);
|
||||
if (checkIsSuperAdmin()) {
|
||||
if (preInstructorUserId != null) {
|
||||
validateHubId(request, preInstructorUser.getHub().getId());
|
||||
}
|
||||
return preInstructorUser;
|
||||
} else if (checkIsPreInstructor()) {
|
||||
return validateUserId(request, preInstructorUserId);
|
||||
} else {
|
||||
throw new ForbiddenAccessException(Status.FORBIDDEN,
|
||||
Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user