Search in sources :

Example 1 with Permission

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;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) Permission(org.eclipse.vorto.repository.core.PolicyEntry.Permission) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) ModelId(org.eclipse.vorto.model.ModelId)

Aggregations

ModelId (org.eclipse.vorto.model.ModelId)1 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)1 ModelNotFoundException (org.eclipse.vorto.repository.core.ModelNotFoundException)1 Permission (org.eclipse.vorto.repository.core.PolicyEntry.Permission)1 NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)1