|
| 1 | +package io.sentrius.sso.core.services.documents; |
| 2 | + |
| 3 | +import io.sentrius.sso.core.model.documents.Document; |
| 4 | +import io.sentrius.sso.core.model.users.UserAttribute; |
| 5 | +import io.sentrius.sso.core.repository.UserAttributeRepository; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.apache.accumulo.access.AccessEvaluator; |
| 8 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | + |
| 11 | +import java.util.*; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +/** |
| 15 | + * Service for managing document access control based on user attributes and markings. |
| 16 | + * Uses ABAC (Attribute-Based Access Control) similar to the memory access control system. |
| 17 | + */ |
| 18 | +@Slf4j |
| 19 | +@Service |
| 20 | +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) |
| 21 | +public class DocumentAccessControlService { |
| 22 | + |
| 23 | + private final UserAttributeRepository userAttributeRepository; |
| 24 | + |
| 25 | + public DocumentAccessControlService(UserAttributeRepository userAttributeRepository) { |
| 26 | + this.userAttributeRepository = userAttributeRepository; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Check if a user can access a specific document based on markings and attributes. |
| 31 | + * |
| 32 | + * @param document The document to check access for |
| 33 | + * @param evaluator AccessEvaluator built from user's attributes (can be null) |
| 34 | + * @param userId The user ID attempting to access the document |
| 35 | + * @return true if user can access the document, false otherwise |
| 36 | + */ |
| 37 | + public boolean canAccessDocument(Document document, AccessEvaluator evaluator, String userId) { |
| 38 | + log.debug("Evaluating document access for user: {}, document: {}", userId, document.getDocumentName()); |
| 39 | + |
| 40 | + // If document has no markings, allow access (unclassified/public) |
| 41 | + if (document.getMarkings() == null || document.getMarkings().trim().isEmpty()) { |
| 42 | + if ("UNCLASSIFIED".equalsIgnoreCase(document.getClassification()) || |
| 43 | + "PUBLIC".equalsIgnoreCase(document.getClassification())) { |
| 44 | + log.debug("Document is unclassified/public with no markings - access granted"); |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // If user is the creator, allow access |
| 50 | + if (userId != null && userId.equals(document.getCreatedBy())) { |
| 51 | + log.debug("User is the document creator - access granted"); |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + // Check USER: markings (private user-specific documents) |
| 56 | + if (document.getMarkings() != null && document.getMarkings().contains("USER:")) { |
| 57 | + String[] markingsArray = document.getMarkings().split(","); |
| 58 | + for (String marking : markingsArray) { |
| 59 | + if (marking.trim().startsWith("USER:")) { |
| 60 | + String markedUserId = marking.trim().substring(5); |
| 61 | + if (userId != null && userId.equals(markedUserId)) { |
| 62 | + log.debug("USER marking matched - access granted to owning user: {}", userId); |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + log.debug("USER marking(s) present but user {} does not match - access denied", userId); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // Check if document is PUBLIC or UNCLASSIFIED - these should be accessible to all |
| 72 | + if ("UNCLASSIFIED".equalsIgnoreCase(document.getClassification()) || |
| 73 | + "PUBLIC".equalsIgnoreCase(document.getClassification())) { |
| 74 | + log.debug("Document is PUBLIC/UNCLASSIFIED - access granted"); |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + // Use AccessEvaluator to check if user has required markings |
| 79 | + if (evaluator != null && document.getMarkings() != null && !document.getMarkings().trim().isEmpty()) { |
| 80 | + boolean canAccess = evaluator.canAccess(document.getMarkings()); |
| 81 | + log.debug("AccessEvaluator result for markings '{}': {}", document.getMarkings(), canAccess); |
| 82 | + return canAccess; |
| 83 | + } |
| 84 | + |
| 85 | + // If no evaluator and document has markings, deny access |
| 86 | + if (document.getMarkings() != null && !document.getMarkings().trim().isEmpty()) { |
| 87 | + log.debug("Document has markings but user has no authorizations - access denied"); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Default: deny access for classified documents |
| 92 | + log.debug("Document is classified but access could not be determined - access denied"); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Filter a list of documents based on user access. |
| 98 | + * |
| 99 | + * @param documents List of documents to filter |
| 100 | + * @param evaluator AccessEvaluator built from user's attributes (can be null) |
| 101 | + * @param userId The user ID attempting to access the documents |
| 102 | + * @return Filtered list of documents the user can access |
| 103 | + */ |
| 104 | + public List<Document> filterAccessibleDocuments(List<Document> documents, AccessEvaluator evaluator, String userId) { |
| 105 | + if (documents == null || documents.isEmpty()) { |
| 106 | + return Collections.emptyList(); |
| 107 | + } |
| 108 | + |
| 109 | + return documents.stream() |
| 110 | + .filter(doc -> canAccessDocument(doc, evaluator, userId)) |
| 111 | + .collect(Collectors.toList()); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get user attributes as a map for logging/debugging |
| 116 | + */ |
| 117 | + public Map<String, Object> getUserAttributesMap(String userId) { |
| 118 | + if (userId == null) { |
| 119 | + return new HashMap<>(); |
| 120 | + } |
| 121 | + |
| 122 | + List<UserAttribute> attributes = userAttributeRepository.findByUserIdAndIsActiveTrue(userId); |
| 123 | + Map<String, Object> attributeMap = new HashMap<>(); |
| 124 | + |
| 125 | + for (UserAttribute attr : attributes) { |
| 126 | + attributeMap.put(attr.getAttributeName(), attr.getAttributeValue()); |
| 127 | + } |
| 128 | + |
| 129 | + attributeMap.put("user_id", userId); |
| 130 | + |
| 131 | + log.debug("Loaded {} attributes for user: {}", attributeMap.size(), userId); |
| 132 | + return attributeMap; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Check if user has specific attribute value |
| 137 | + */ |
| 138 | + public boolean userHasAttributeValue(String userId, String attributeName, String attributeValue) { |
| 139 | + return userAttributeRepository.userHasAttributeValue(userId, attributeName, attributeValue); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get all active attributes for a user (for building AccessEvaluator) |
| 144 | + */ |
| 145 | + public List<UserAttribute> getUserAttributes(String userId) { |
| 146 | + if (userId == null) { |
| 147 | + return Collections.emptyList(); |
| 148 | + } |
| 149 | + return userAttributeRepository.findByUserIdAndIsActiveTrue(userId); |
| 150 | + } |
| 151 | +} |
0 commit comments