use of io.jmix.security.model.ResourcePolicy in project jmix by jmix-framework.
the class ExplicitResourcePolicyExtractor method extractResourcePolicies.
@Override
public Collection<ResourcePolicy> extractResourcePolicies(Method method) {
if (Modifier.isStatic(method.getModifiers())) {
ExplicitResourcePolicies annotation = method.getAnnotation(ExplicitResourcePolicies.class);
if (annotation != null) {
if (isMethodReturnCollectionOfResourcePolicies(method)) {
try {
Set<ResourcePolicy> resourcePolicies = new HashSet<>();
Collection<ResourcePolicy> definedPolicies = (Collection<ResourcePolicy>) method.invoke(null);
for (ResourcePolicy definedPolicy : definedPolicies) {
ResourcePolicy resourcePolicy = ResourcePolicy.builder(definedPolicy.getType(), definedPolicy.getResource()).withAction(definedPolicy.getAction()).withEffect(definedPolicy.getEffect()).withPolicyGroup(definedPolicy.getPolicyGroup()).withCustomProperties(Collections.singletonMap("uniqueKey", UUID.randomUUID().toString())).build();
resourcePolicies.add(resourcePolicy);
}
return resourcePolicies;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Cannot evaluate resource policies", e);
}
} else {
String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName() + "()";
throw new RuntimeException("Method " + methodName + " with ExplicitResourcePolicies annotation should return a " + "collection of ResourcePolicy");
}
}
}
return Collections.emptyList();
}
use of io.jmix.security.model.ResourcePolicy in project jmix by jmix-framework.
the class TestExplicitResourcePoliciesRole method explicitOrderPolicies.
@ExplicitResourcePolicies
static Collection<ResourcePolicy> explicitOrderPolicies() {
List<ResourcePolicy> resourcePolicies = new ArrayList<>();
ResourcePolicy policy1 = ResourcePolicy.builder(ResourcePolicyType.SCREEN, "sample_Order.browsr").build();
resourcePolicies.add(policy1);
ResourcePolicy policy2 = ResourcePolicy.builder(ResourcePolicyType.ENTITY, "sample_Order").withAction(EntityPolicyAction.READ.getId()).build();
resourcePolicies.add(policy2);
return resourcePolicies;
}
use of io.jmix.security.model.ResourcePolicy in project jmix by jmix-framework.
the class MenuPolicyExtractor method extractResourcePolicies.
@Override
public Collection<ResourcePolicy> extractResourcePolicies(Method method) {
Set<ResourcePolicy> resourcePolicies = new HashSet<>();
Set<MenuPolicy> annotations = AnnotatedElementUtils.findMergedRepeatableAnnotations(method, MenuPolicy.class, MenuPolicyContainer.class);
for (MenuPolicy annotation : annotations) {
for (String menuId : annotation.menuIds()) {
ResourcePolicy resourcePolicy = ResourcePolicy.builder(ResourcePolicyType.MENU, menuId).withPolicyGroup(method.getName()).build();
resourcePolicies.add(resourcePolicy);
}
}
return resourcePolicies;
}
use of io.jmix.security.model.ResourcePolicy in project jmix by jmix-framework.
the class ScreenPolicyExtractor method extractResourcePolicies.
@Override
public Collection<ResourcePolicy> extractResourcePolicies(Method method) {
Set<ResourcePolicy> resourcePolicies = new HashSet<>();
Set<ScreenPolicy> annotations = AnnotatedElementUtils.findMergedRepeatableAnnotations(method, ScreenPolicy.class, ScreenPolicyContainer.class);
for (ScreenPolicy screenPolicyAnnotation : annotations) {
for (String screenId : screenPolicyAnnotation.screenIds()) {
ResourcePolicy resourcePolicy = ResourcePolicy.builder(ResourcePolicyType.SCREEN, screenId).withPolicyGroup(method.getName()).build();
resourcePolicies.add(resourcePolicy);
}
for (Class screenClass : screenPolicyAnnotation.screenClasses()) {
UiController uiControllerAnnotation = (UiController) screenClass.getAnnotation(UiController.class);
String screenId = uiControllerAnnotation.value();
ResourcePolicy resourcePolicy = ResourcePolicy.builder(ResourcePolicyType.SCREEN, screenId).withPolicyGroup(method.getName()).build();
resourcePolicies.add(resourcePolicy);
}
}
return resourcePolicies;
}
use of io.jmix.security.model.ResourcePolicy in project jmix by jmix-framework.
the class DatabaseResourceRoleProvider method buildRole.
protected ResourceRole buildRole(Object entity) {
ResourceRoleEntity roleEntity = (ResourceRoleEntity) entity;
ResourceRole role = new ResourceRole();
role.setName(roleEntity.getName());
role.setCode(roleEntity.getCode());
role.setDescription(roleEntity.getDescription());
role.setSource(RoleSource.DATABASE);
role.setChildRoles(roleEntity.getChildRoles());
role.getCustomProperties().put("databaseId", roleEntity.getId().toString());
role.setScopes(roleEntity.getScopes() == null ? Collections.emptySet() : roleEntity.getScopes());
role.setTenantId(roleEntity.getSysTenantId());
List<ResourcePolicyEntity> resourcePolicyEntities = roleEntity.getResourcePolicies();
if (resourcePolicyEntities != null) {
List<ResourcePolicy> resourcePolicies = resourcePolicyEntities.stream().map(e -> {
Map<String, String> customProperties = new HashMap<>();
customProperties.put("databaseId", e.getId().toString());
return ResourcePolicy.builder(e.getType(), e.getResource()).withAction(e.getAction()).withEffect(e.getEffect()).withPolicyGroup(e.getPolicyGroup()).withCustomProperties(customProperties).build();
}).collect(Collectors.toList());
role.setResourcePolicies(resourcePolicies);
}
return role;
}
Aggregations