updated code
This commit is contained in:
@@ -1,21 +1,30 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Component
|
||||
public class DateTimeUtil {
|
||||
|
||||
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile(
|
||||
"^((([01]?\\d|2[0-3]):([0-5]\\d)(:[0-5]\\d)?(\\s?[AP]M)?)|((0?[1-9]|1[0-2]):([0-5]\\d)(:[0-5]\\d)?\\s?[AP]M))$");
|
||||
|
||||
|
||||
public static LocalDateTime DateServerToUTC(LocalDateTime systemDate) {
|
||||
|
||||
ZonedDateTime ldtZoned = systemDate.atZone(ZoneId.systemDefault());
|
||||
LocalDateTime localDatetime = ldtZoned.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
|
||||
LocalDateTime localDatetime = ldtZoned.withZoneSameInstant(ZoneId.of("Europe/Rome")).toLocalDateTime();
|
||||
return localDatetime;
|
||||
}
|
||||
|
||||
@@ -50,4 +59,53 @@ public class DateTimeUtil {
|
||||
.from(localDateTime.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
}
|
||||
|
||||
public static LocalTime parseTime(String timeString) throws DateTimeParseException {
|
||||
DateTimeFormatter formatter;
|
||||
if(timeString==null) {
|
||||
return null;
|
||||
}
|
||||
if (!TIME_PATTERN.matcher(timeString).matches()) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,"Invalid time format: " + timeString);
|
||||
}
|
||||
// Try to parse using default formats if no format is provided
|
||||
String[] defaultFormats = {"HH:mm:ss", "HH:mm", "HH:mm:ss a", "hh:mm a"};
|
||||
for (String defaultFormat : defaultFormats) {
|
||||
formatter = DateTimeFormatter.ofPattern(defaultFormat);
|
||||
try {
|
||||
return LocalTime.parse(timeString, formatter);
|
||||
} catch (DateTimeParseException e) {
|
||||
// Continue to the next format
|
||||
}
|
||||
}
|
||||
|
||||
// If all parsing attempts fail, throw an exception
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,"Failed to parse time: " + timeString);
|
||||
}
|
||||
|
||||
public static String formatLocalDateTime(LocalDateTime dateTime, String pattern) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
||||
return dateTime.format(formatter);
|
||||
}
|
||||
|
||||
public static LocalDateTime parseStringToLocalDateTime(String dateTimeStr, String pattern) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
||||
return LocalDateTime.parse(dateTimeStr, formatter);
|
||||
}
|
||||
|
||||
public static String parseLocalTimeToString(LocalTime time, String format) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
return time.format(formatter);
|
||||
}
|
||||
|
||||
// Method 2: Convert String and format to LocalTime
|
||||
public static LocalTime parseStringToLocalTime(String timeString, String format) {
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
return LocalTime.parse(timeString, formatter);
|
||||
} catch (DateTimeParseException e) {
|
||||
System.out.println("Invalid time format: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,16 +27,16 @@ public class FieldValidator {
|
||||
private VatCheckDao vatCheckDao;
|
||||
|
||||
|
||||
public FieldValidator notNull(Object object, String fieldName) {
|
||||
public FieldValidator notNull(Object object, String fieldLabel) {
|
||||
if (Objects.isNull(object)) {
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_NULL), fieldName));
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_NULL), fieldLabel));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public FieldValidator notEmpty(List<?> list, String fieldName) {
|
||||
public FieldValidator notEmpty(List<?> list, String fieldLabel) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_EMPTY), fieldName));
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_EMPTY), fieldLabel));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -46,23 +46,23 @@ public class FieldValidator {
|
||||
throw new ValidationException(Status.VALIDATION_ERROR, errors, Translator.toLocale(GepafinConstant.VALIDATION_MESSAGE));
|
||||
}
|
||||
}
|
||||
public FieldValidator minLength(String value, Long minLength, String fieldName) {
|
||||
public FieldValidator minLength(String value, Long minLength, String fieldLabel) {
|
||||
if (minLength != null && value != null && value.length() < minLength) {
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_MIN_LENGTH), fieldName, minLength));
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_MIN_LENGTH), fieldLabel, minLength));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public FieldValidator maxLength(String value, Long maxLength, String fieldName) {
|
||||
public FieldValidator maxLength(String value, Long maxLength, String fieldLabel) {
|
||||
if (maxLength != null && value != null && value.length() > maxLength) {
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_MAX_LENGTH), fieldName, maxLength));
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_MAX_LENGTH), fieldLabel, maxLength));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public FieldValidator matchesPattern(String value, String pattern, String fieldName) {
|
||||
public FieldValidator matchesPattern(String value, String pattern, String fieldLabel) {
|
||||
if (value != null && pattern != null && !value.matches(pattern)) {
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_PATTERN), fieldName));
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_PATTERN), fieldLabel));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -147,4 +147,7 @@ public class FieldValidator {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public boolean hasErrors() {
|
||||
return !errors.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.mailgun.api.v3.MailgunMessagesApi;
|
||||
import com.mailgun.client.MailgunClient;
|
||||
|
||||
@Component
|
||||
public class MailUtil {
|
||||
|
||||
@Value("${apiKey}")
|
||||
private String apiKeyValue;
|
||||
|
||||
@Value("${mailGun_user}")
|
||||
private String mailGunUser;
|
||||
|
||||
@Value("${mailGun_apiKey}")
|
||||
private String mailGunApiKey;
|
||||
|
||||
@Value("${mailGun_domainName}")
|
||||
private String mailGunDomainName;
|
||||
|
||||
@Value("${mailGun_base_url}")
|
||||
private String mailGunBaseUrl;
|
||||
|
||||
@Value("${isMailSendingEnabled}")
|
||||
private String isEmailSendingEnabled;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public Boolean isTestProfileActivated() {
|
||||
String[] activeProfiles = environment.getActiveProfiles();
|
||||
return Arrays.stream(activeProfiles).anyMatch("test"::equals);
|
||||
}
|
||||
|
||||
public void sendMailByMailGunAPI(List<String> recipents, List<String> CC, List<String> BCC, String subject,
|
||||
String body, String replyTo) {
|
||||
if (Boolean.FALSE.equals(Boolean.parseBoolean(isEmailSendingEnabled))) {
|
||||
return;
|
||||
}
|
||||
|
||||
MailgunMessagesApi mailgunMessagesApi = MailgunClient.config(mailGunBaseUrl, mailGunApiKey)
|
||||
.createApi(MailgunMessagesApi.class);
|
||||
|
||||
String mailFrom = mailGunUser;
|
||||
|
||||
com.mailgun.model.message.Message.MessageBuilder temp = com.mailgun.model.message.Message.builder()
|
||||
.replyTo(replyTo).from(mailFrom).to(recipents).subject(subject).html(body);
|
||||
|
||||
if (Boolean.FALSE.equals(CollectionUtils.isEmpty(CC))) {
|
||||
temp.cc(CC);
|
||||
}
|
||||
|
||||
if (Boolean.FALSE.equals(CollectionUtils.isEmpty(BCC))) {
|
||||
temp.bcc(BCC);
|
||||
}
|
||||
|
||||
if (Boolean.FALSE.equals(isTestProfileActivated())) {
|
||||
com.mailgun.model.message.Message message = temp.build();
|
||||
mailgunMessagesApi.sendMessage(mailGunDomainName, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void sendByMailGun(String subject, String body, List<String> receiverEmails, String replyTo) {
|
||||
sendMailByMailGunAPI(receiverEmails, null, null, subject, body, replyTo);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +1,36 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.json.JsonReadFeature;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import io.micrometer.common.util.StringUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.json.JsonReadFeature;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
import feign.FeignException;
|
||||
import io.micrometer.common.util.StringUtils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientForbiddenException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientUnauthorizedException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.FeignClientValidationException;
|
||||
|
||||
|
||||
public class Utils {
|
||||
|
||||
@@ -161,4 +170,142 @@ public class Utils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String encodeData(String data) {
|
||||
return Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static String decodeData(String token) {
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(token);
|
||||
return new String(decodedBytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String generateSecureToken() {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
byte[] tokenBytes = new byte[24];
|
||||
secureRandom.nextBytes(tokenBytes);
|
||||
String token = Base64.getUrlEncoder().withoutPadding().encodeToString(tokenBytes);
|
||||
log.debug("Generated secure token: {}", token);
|
||||
return token;
|
||||
}
|
||||
|
||||
public static Map<String, List<Object>> convertStringIntoMap(String jsonString) {
|
||||
try {
|
||||
return mapper.readValue(jsonString, new TypeReference<Map<String, List<Object>>>() {
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting object: " + e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void callException(Integer staus, FeignException ex) {
|
||||
switch (staus) {
|
||||
case 400:
|
||||
throw new FeignClientValidationException(HttpStatus.valueOf(staus), ex.getMessage());
|
||||
|
||||
case 401:
|
||||
throw new FeignClientUnauthorizedException(HttpStatus.valueOf(staus), ex.getMessage());
|
||||
|
||||
case 403:
|
||||
throw new FeignClientForbiddenException(HttpStatus.valueOf(staus), ex.getMessage());
|
||||
|
||||
case 404:
|
||||
throw new FeignClientNotFoundException(HttpStatus.valueOf(staus), ex.getMessage());
|
||||
default:
|
||||
log.error("Exception occured :- {0}", ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean isValidEmail(String email) {
|
||||
String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
|
||||
if (email == null || email.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Pattern pattern = Pattern.compile(EMAIL_REGEX);
|
||||
return pattern.matcher(email).matches();
|
||||
}
|
||||
|
||||
public static String randomKey(Integer range) {
|
||||
String data = String.valueOf(System.currentTimeMillis());
|
||||
return data.substring(data.length() - range);
|
||||
}
|
||||
|
||||
public static String convertObjectToJsonString(Object object) {
|
||||
try {
|
||||
// Check if the object is a string
|
||||
if (object instanceof String) {
|
||||
String str = (String) object;
|
||||
// Return null if the string is null or empty
|
||||
if (str != null && !str.trim().isEmpty()) {
|
||||
return str; // Return the non-empty string
|
||||
} else {
|
||||
return null; // Return null for null or empty string
|
||||
}
|
||||
} else if (object != null) {
|
||||
// Convert non-string objects (arrays, objects) to JSON strings
|
||||
return mapper.writeValueAsString(object);
|
||||
}
|
||||
return null; // Return null if the object is null
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error while converting object to string: {}", e.getMessage(), e);
|
||||
return null; // Return null in case of exception
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Object getFieldValueAsObject(String fieldValue) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
// Check if the string is a valid JSON object, array, or simple string
|
||||
if (fieldValue.startsWith("{")) {
|
||||
// Convert to a Map (representing an object)
|
||||
return mapper.readValue(fieldValue, Map.class);
|
||||
} else if (fieldValue.startsWith("[")) {
|
||||
// Convert to a List (representing an array)
|
||||
return mapper.readValue(fieldValue, List.class);
|
||||
} else {
|
||||
// Return the raw string (it's a simple value)
|
||||
return fieldValue;
|
||||
}
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error while converting string to object: {}", e.getMessage(), e);
|
||||
return fieldValue; // If there's an error, return the raw string
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Map<String, String>> parseJsonContent(String jsonContent) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
return mapper.readValue(jsonContent, HashMap.class);
|
||||
} catch (Exception exception) {
|
||||
log.error(exception.getMessage());
|
||||
}
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
// Utility method to replace placeholders with their values, handling nulls
|
||||
public static String replacePlaceholders(String text, Map<String, String> placeholders) {
|
||||
if (text == null) {
|
||||
return "";
|
||||
}
|
||||
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
|
||||
text = replaceNull(text, entry.getKey(), entry.getValue());
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
// Method to safely replace nulls with an empty string or a default value
|
||||
private static String replaceNull(String text, String target, String replacement) {
|
||||
return text.replace(target, replacement != null ? replacement : "");
|
||||
}
|
||||
public static String getClientIpAddress(HttpServletRequest request) {
|
||||
String header = request.getHeader("X-Forwarded-For");
|
||||
if (org.apache.commons.lang3.StringUtils.isBlank(header)) {
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
|
||||
return new StringTokenizer(header, ",").nextToken().trim();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,25 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
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.dao.CallDao;
|
||||
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;
|
||||
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.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -18,14 +32,88 @@ public class Validator {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
|
||||
@Autowired
|
||||
private CallService callService;
|
||||
|
||||
public Map<String, Object> getUserInfoFromToken(HttpServletRequest request) {
|
||||
return tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
}
|
||||
|
||||
public UserEntity validateUser(HttpServletRequest request) {
|
||||
Map<String, Object> userInfo= tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
return userService.validateUser(Long.parseLong(userInfo.get("userId").toString()));
|
||||
return userService.validateUser(getUserIdFromToken(request));
|
||||
}
|
||||
|
||||
public Boolean checkIsSuperAdmin() {
|
||||
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_SUPER_ADMIN.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())) {
|
||||
throw new UnauthorizedAccessException(Status.UNAUTHORIZED, Translator.toLocale(GepafinConstant.INVALID_REQUEST));
|
||||
}
|
||||
}
|
||||
|
||||
public CompanyEntity validateUserWithCompany(HttpServletRequest request, Long companyId) {
|
||||
if (checkIsSuperAdmin()) {
|
||||
return companyService.validateCompany(companyId);
|
||||
}
|
||||
Map<String, Object> userInfo = tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
companyService.validateUserWithCompny(getUserId(userInfo), companyId);
|
||||
return companyService.validateCompany(companyId);
|
||||
}
|
||||
|
||||
private Long getUserId(Map<String, Object> userInfo) {
|
||||
return Long.parseLong(userInfo.get("userId").toString());
|
||||
}
|
||||
|
||||
public Boolean checkIsBeneficiary() {
|
||||
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_BENEFICIARY.getValue().equals(authority.getAuthority())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
return userService.validateUser(userId);
|
||||
}
|
||||
|
||||
private Long getUserIdFromToken(HttpServletRequest request) {
|
||||
Map<String, Object> userInfo= tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
return Long.parseLong(userInfo.get("userId").toString());
|
||||
}
|
||||
|
||||
public CallEntity validateUserWithCall(UserEntity user, Long callId) {
|
||||
CallEntity callEntity = callService.validateCall(callId);
|
||||
if(user.getHub().getId().equals(callEntity.getHub().getId())) {
|
||||
throw new ForbiddenAccessException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
||||
}
|
||||
return callEntity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user