|
|
@@ -3,6 +3,8 @@ package com.yimeng.okr.security;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
|
+import com.yimeng.okr.enums.UserRole;
|
|
|
+
|
|
|
public class SecurityUtils {
|
|
|
|
|
|
private SecurityUtils() {}
|
|
|
@@ -23,4 +25,28 @@ public class SecurityUtils {
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ public static boolean hasRole(UserRole requiredRole) {
|
|
|
+ Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ if (auth == null || auth.getAuthorities() == null) return false;
|
|
|
+ String roleName = auth.getAuthorities().stream()
|
|
|
+ .map(a -> a.getAuthority().replace("ROLE_", ""))
|
|
|
+ .findFirst().orElse("EMPLOYEE");
|
|
|
+ try {
|
|
|
+ UserRole current = UserRole.valueOf(roleName);
|
|
|
+ return current.ordinal() >= requiredRole.ordinal();
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrentUserRole() {
|
|
|
+ Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ if (auth != null && auth.getAuthorities() != null) {
|
|
|
+ return auth.getAuthorities().stream()
|
|
|
+ .map(a -> a.getAuthority().replace("ROLE_", ""))
|
|
|
+ .findFirst().orElse("EMPLOYEE");
|
|
|
+ }
|
|
|
+ return "EMPLOYEE";
|
|
|
+ }
|
|
|
}
|