use of org.eclipse.vorto.repository.core.PolicyEntry.Permission in project vorto by eclipse.
the class HasPermissionEvaluator method hasPermission.
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object targetPermission) {
final String username = authentication.getName();
if (targetDomainObject instanceof ModelId) {
if (targetPermission instanceof String) {
try {
ModelId modelId = (ModelId) targetDomainObject;
String workspaceId = namespaceService.resolveWorkspaceIdForNamespace(modelId.getNamespace()).orElseThrow(() -> new ModelNotFoundException("Model '" + modelId.getPrettyFormat() + "' can't be found in any workspace."));
String permission = (String) targetPermission;
ModelInfo modelInfo = repositoryFactory.getRepository(workspaceId, authentication).getById(modelId);
if (modelInfo != null) {
if ("model:delete".equalsIgnoreCase(permission)) {
return modelInfo.getAuthor().equalsIgnoreCase(username);
} else if ("model:get".equalsIgnoreCase(permission)) {
return modelInfo.getState().equals(SimpleWorkflowModel.STATE_RELEASED.getName()) || modelInfo.getState().equals(SimpleWorkflowModel.STATE_DEPRECATED.getName()) || modelInfo.getAuthor().equals(username);
} else if ("model:owner".equalsIgnoreCase(permission)) {
return modelInfo.getAuthor().equals(username);
}
}
} catch (NotAuthorizedException ex) {
return false;
}
} else if (targetPermission instanceof Permission) {
ModelId modelId = (ModelId) targetDomainObject;
Permission permission = (Permission) targetPermission;
String workspaceId = namespaceService.resolveWorkspaceIdForNamespace(modelId.getNamespace()).orElseThrow(() -> new ModelNotFoundException("The workspace for '" + modelId.getPrettyFormat() + "' could not be found."));
return repositoryFactory.getPolicyManager(workspaceId, authentication).hasPermission(modelId, permission);
}
} else if (targetDomainObject instanceof String) {
return username.equalsIgnoreCase((String) targetDomainObject);
}
return false;
}
Aggregations