Done ticket GEPAFINBE-32
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
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.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
@@ -86,4 +82,14 @@ public class DateTimeUtil {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,4 +227,10 @@ public class Utils {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,8 +37,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
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() {
|
||||
@@ -86,5 +85,18 @@ public class Validator {
|
||||
}
|
||||
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 UnauthorizedAccessException(Status.UNAUTHORIZED, Translator.toLocale(GepafinConstant.INVALID_REQUEST));
|
||||
}
|
||||
return userService.validateUser(userId);
|
||||
}
|
||||
|
||||
private Long getUserIdFromToken(HttpServletRequest request) {
|
||||
Map<String, Object> userInfo= tokenProvider.getUserInfoAndUserIdFromToken(request);
|
||||
return Long.parseLong(userInfo.get("userId").toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user