-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeAccessResolutionService.java
More file actions
34 lines (30 loc) · 1.48 KB
/
Copy pathCompositeAccessResolutionService.java
File metadata and controls
34 lines (30 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.jug.joker.javadopexample.service;
import com.jug.joker.javadopexample.api.dto.AccessCheckRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.function.Predicate;
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class CompositeAccessResolutionService {
private final EntityWalkerService<Boolean> entityWalkerService;
private final SecuredEntityByDefinitionRegistryFetcher entityFetcher;
private final MapperResolverService mapperResolver;
public boolean checkAccess(AccessCheckRequest accessCheckRequest) {
// Build mapper
var accessType = accessCheckRequest.actionType();
var authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
var mapper = mapperResolver.getMapperByAccessTypeAndRoleSet(accessType, authorities);
// Fetch entity
var entityDefinition = accessCheckRequest.entityDefinition();
var rootEntity = entityFetcher.findEntityByDefinition(entityDefinition).orElseThrow();
// Process the entity tree
return entityWalkerService
.processEntityTree(rootEntity, mapper)
.stream()
// If all entities down the tree are accessible -> root accessible
.allMatch(Predicate.isEqual(true));
}
}