|
| 1 | +package io.sentrius.sso.controllers.api; |
| 2 | + |
| 3 | +import io.sentrius.sso.core.annotations.LimitAccess; |
| 4 | +import io.sentrius.sso.core.config.SystemOptions; |
| 5 | +import io.sentrius.sso.core.data.attributes.UIResourceConfig; |
| 6 | +import io.sentrius.sso.core.data.attributes.UIResourceMappings; |
| 7 | +import io.sentrius.sso.core.model.security.enums.ApplicationAccessEnum; |
| 8 | +import io.sentrius.sso.core.model.users.User; |
| 9 | +import io.sentrius.sso.core.services.UserService; |
| 10 | +import io.sentrius.sso.core.services.abac.EvaluationContext; |
| 11 | +import io.sentrius.sso.core.services.abac.PolicyDecision; |
| 12 | +import io.sentrius.sso.core.services.abac.PolicyEvaluator; |
| 13 | +import jakarta.servlet.http.HttpServletRequest; |
| 14 | +import jakarta.servlet.http.HttpServletResponse; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.context.annotation.Lazy; |
| 18 | +import org.springframework.http.ResponseEntity; |
| 19 | +import org.springframework.web.bind.annotation.*; |
| 20 | + |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +/** |
| 24 | + * REST API Controller for UI Permission Checks |
| 25 | + * Provides endpoints to check user's UI access permissions based on ABAC policies |
| 26 | + */ |
| 27 | +@Slf4j |
| 28 | +@RestController |
| 29 | +@RequestMapping("/api/v1/ui/permissions") |
| 30 | +public class UIPermissionsApiController { |
| 31 | + |
| 32 | + private final UserService userService; |
| 33 | + private final SystemOptions systemOptions; |
| 34 | + |
| 35 | + @Lazy |
| 36 | + @Autowired(required = false) |
| 37 | + private PolicyEvaluator policyEvaluator; |
| 38 | + |
| 39 | + public UIPermissionsApiController( |
| 40 | + UserService userService, |
| 41 | + SystemOptions systemOptions) { |
| 42 | + this.userService = userService; |
| 43 | + this.systemOptions = systemOptions; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Get UI permissions for the current user. |
| 48 | + * Returns a map of UI resource identifiers to permission status. |
| 49 | + */ |
| 50 | + @GetMapping |
| 51 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_LOG_IN}) |
| 52 | + public ResponseEntity<UIPermissionsResponse> getUserUIPermissions( |
| 53 | + HttpServletRequest request, |
| 54 | + HttpServletResponse response) { |
| 55 | + |
| 56 | + try { |
| 57 | + User operatingUser = userService.getOperatingUser(request, response, null); |
| 58 | + if (operatingUser == null) { |
| 59 | + return ResponseEntity.status(401).build(); |
| 60 | + } |
| 61 | + |
| 62 | + log.debug("Fetching UI permissions for user: {}", operatingUser.getUsername()); |
| 63 | + |
| 64 | + // Get standard access set permissions |
| 65 | + Set<String> accessSet = operatingUser.getAuthorizationType().getAccessSet(); |
| 66 | + |
| 67 | + // Initialize permissions map with standard menu items |
| 68 | + Map<String, Boolean> permissions = new HashMap<>(); |
| 69 | + |
| 70 | + // Check if ABAC UI control is enabled |
| 71 | + boolean abacEnabled = systemOptions.getEnableAbacUiControl() != null && |
| 72 | + systemOptions.getEnableAbacUiControl(); |
| 73 | + |
| 74 | + // Define UI resource mappings (menu items to their access requirements) |
| 75 | + Map<String, UIResourceConfig> uiResources = UIResourceMappings.getUIResourceMappings(); |
| 76 | + |
| 77 | + for (Map.Entry<String, UIResourceConfig> entry : uiResources.entrySet()) { |
| 78 | + String resourceKey = entry.getKey(); |
| 79 | + UIResourceConfig config = entry.getValue(); |
| 80 | + |
| 81 | + boolean hasAccess = false; |
| 82 | + |
| 83 | + // First check standard access set permissions |
| 84 | + if (config.getRequiredAccess() != null) { |
| 85 | + hasAccess = accessSet.contains(config.getRequiredAccess()); |
| 86 | + } |
| 87 | + |
| 88 | + // If ABAC is enabled and user doesn't have standard access, check ABAC policies |
| 89 | + if (abacEnabled && !hasAccess && policyEvaluator != null && config.getAbacResource() != null) { |
| 90 | + try { |
| 91 | + EvaluationContext context = policyEvaluator.buildContext( |
| 92 | + operatingUser.getUsername(), |
| 93 | + config.getAbacResource() |
| 94 | + ); |
| 95 | + |
| 96 | + PolicyDecision decision = policyEvaluator.evaluate( |
| 97 | + context, |
| 98 | + config.getAbacResource(), |
| 99 | + "VIEW", |
| 100 | + false |
| 101 | + ); |
| 102 | + |
| 103 | + if (decision.getEffect() == PolicyDecision.Effect.ALLOW) { |
| 104 | + hasAccess = true; |
| 105 | + log.debug("ABAC policy granted access to {} for user {}", |
| 106 | + resourceKey, operatingUser.getUsername()); |
| 107 | + } |
| 108 | + } catch (Exception e) { |
| 109 | + log.warn("Error evaluating ABAC policy for resource {}: {}", |
| 110 | + resourceKey, e.getMessage()); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + permissions.put(resourceKey, hasAccess); |
| 115 | + } |
| 116 | + |
| 117 | + UIPermissionsResponse permissionsResponse = new UIPermissionsResponse( |
| 118 | + operatingUser.getUsername(), |
| 119 | + permissions, |
| 120 | + abacEnabled |
| 121 | + ); |
| 122 | + |
| 123 | + return ResponseEntity.ok(permissionsResponse); |
| 124 | + |
| 125 | + } catch (Exception e) { |
| 126 | + log.error("Error fetching UI permissions", e); |
| 127 | + return ResponseEntity.status(500).build(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Check if user has permission for a specific UI resource |
| 133 | + */ |
| 134 | + @GetMapping("/check/{resourceKey}") |
| 135 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_LOG_IN}) |
| 136 | + public ResponseEntity<ResourcePermissionResponse> checkResourcePermission( |
| 137 | + @PathVariable String resourceKey, |
| 138 | + HttpServletRequest request, |
| 139 | + HttpServletResponse response) { |
| 140 | + |
| 141 | + try { |
| 142 | + User operatingUser = userService.getOperatingUser(request, response, null); |
| 143 | + if (operatingUser == null) { |
| 144 | + return ResponseEntity.status(401).build(); |
| 145 | + } |
| 146 | + |
| 147 | + Set<String> accessSet = operatingUser.getAuthorizationType().getAccessSet(); |
| 148 | + boolean abacEnabled = systemOptions.getEnableAbacUiControl() != null && |
| 149 | + systemOptions.getEnableAbacUiControl(); |
| 150 | + |
| 151 | + UIResourceConfig config = UIResourceMappings.getUIResourceMappings().get(resourceKey); |
| 152 | + if (config == null) { |
| 153 | + return ResponseEntity.notFound().build(); |
| 154 | + } |
| 155 | + |
| 156 | + boolean hasAccess = false; |
| 157 | + String grantedBy = "none"; |
| 158 | + |
| 159 | + // Check standard access set |
| 160 | + if (config.getRequiredAccess() != null && accessSet.contains(config.getRequiredAccess())) { |
| 161 | + hasAccess = true; |
| 162 | + grantedBy = "access_set"; |
| 163 | + } |
| 164 | + |
| 165 | + // Check ABAC if enabled and no standard access |
| 166 | + if (abacEnabled && !hasAccess && policyEvaluator != null && config.getAbacResource() != null) { |
| 167 | + try { |
| 168 | + EvaluationContext context = policyEvaluator.buildContext( |
| 169 | + operatingUser.getUsername(), |
| 170 | + config.getAbacResource() |
| 171 | + ); |
| 172 | + |
| 173 | + PolicyDecision decision = policyEvaluator.evaluate( |
| 174 | + context, |
| 175 | + config.getAbacResource(), |
| 176 | + "VIEW" |
| 177 | + ); |
| 178 | + |
| 179 | + if (decision.getEffect() == PolicyDecision.Effect.ALLOW) { |
| 180 | + hasAccess = true; |
| 181 | + grantedBy = "abac_policy"; |
| 182 | + } |
| 183 | + } catch (Exception e) { |
| 184 | + log.warn("Error evaluating ABAC policy for {}: {}", resourceKey, e.getMessage()); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + ResourcePermissionResponse permissionResponse = new ResourcePermissionResponse( |
| 189 | + resourceKey, |
| 190 | + hasAccess, |
| 191 | + grantedBy |
| 192 | + ); |
| 193 | + |
| 194 | + return ResponseEntity.ok(permissionResponse); |
| 195 | + |
| 196 | + } catch (Exception e) { |
| 197 | + log.error("Error checking resource permission for {}", resourceKey, e); |
| 198 | + return ResponseEntity.status(500).build(); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + /** |
| 204 | + * Response DTO for UI permissions |
| 205 | + */ |
| 206 | + public record UIPermissionsResponse( |
| 207 | + String username, |
| 208 | + Map<String, Boolean> permissions, |
| 209 | + boolean abacEnabled |
| 210 | + ) {} |
| 211 | + |
| 212 | + /** |
| 213 | + * Response DTO for individual resource permission check |
| 214 | + */ |
| 215 | + public record ResourcePermissionResponse( |
| 216 | + String resourceKey, |
| 217 | + boolean hasAccess, |
| 218 | + String grantedBy |
| 219 | + ) {} |
| 220 | +} |
0 commit comments