Search in sources :

Example 1 with ModelNotReleasedException

use of org.eclipse.vorto.repository.model.ModelNotReleasedException in project vorto by eclipse.

the class ModelRepositoryController method makeModelPublic.

@PreAuthorize("isAuthenticated()")
@PostMapping(value = "/{modelId:.+}/makePublic", produces = "application/json")
public ResponseEntity<Status> makeModelPublic(@PathVariable final String modelId) {
    IUserContext user = UserContext.user(SecurityContextHolder.getContext().getAuthentication(), getWorkspaceId(ControllerUtils.sanitize(modelId)));
    ModelId modelID = ModelId.fromPrettyFormat(modelId);
    try {
        if (!userRepositoryRoleService.isSysadmin(user.getUsername()) && !userNamespaceRoleService.hasRole(user.getUsername(), modelID.getNamespace(), "model_publisher")) {
            return new ResponseEntity<>(Status.fail("Only users with Publisher roles are allowed to make models public"), HttpStatus.UNAUTHORIZED);
        }
    } catch (DoesNotExistException dnee) {
        return new ResponseEntity<>(Status.fail(dnee.getMessage()), HttpStatus.NOT_FOUND);
    }
    if (modelID.getNamespace().startsWith(Namespace.PRIVATE_NAMESPACE_PREFIX)) {
        return new ResponseEntity<>(Status.fail("Only models with official namespace can be made public"), HttpStatus.FORBIDDEN);
    }
    try {
        LOGGER.info("Making the model '" + ControllerUtils.sanitize(modelId) + "' public.");
        modelService.makeModelPublic(ModelId.fromPrettyFormat(ControllerUtils.sanitize(modelId)));
    } catch (ModelNotReleasedException | ModelNamespaceNotOfficialException e) {
        return new ResponseEntity<>(Status.fail(e.getMessage()), HttpStatus.FORBIDDEN);
    }
    return new ResponseEntity<>(Status.success(), HttpStatus.OK);
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) ModelNamespaceNotOfficialException(org.eclipse.vorto.repository.model.ModelNamespaceNotOfficialException) ModelNotReleasedException(org.eclipse.vorto.repository.model.ModelNotReleasedException) ModelId(org.eclipse.vorto.model.ModelId) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with ModelNotReleasedException

use of org.eclipse.vorto.repository.model.ModelNotReleasedException in project vorto by eclipse.

the class DefaultBulkOperationsService method makeModelPublicRecursively.

private void makeModelPublicRecursively(ModelId modelId, List<ModelId> modelsMadePublicAcc) throws ExecutionException {
    if (hasPrivateNamespace(modelId)) {
        throw new ModelNamespaceNotOfficialException(modelId);
    }
    IModelRepository repository = repositoryFactory.getRepositoryByModel(modelId);
    ModelInfo modelInfo = repository.getById(modelId);
    if (!modelInfo.isReleased()) {
        throw new ModelNotReleasedException(modelId);
    }
    if (isPrivate(modelInfo)) {
        LOGGER.info("Changing visibility of model " + modelId.getPrettyFormat() + " to public.");
        // Add public visibility property
        repository.updateVisibility(modelId, IModelRepository.VISIBILITY_PUBLIC);
        // Add corresponding policy to model
        IModelPolicyManager policyMgr = this.repositoryFactory.getPolicyManager(repository.getWorkspaceId(), getAuthenticationToken());
        policyMgr.addPolicyEntry(modelId, PolicyEntry.of(IModelPolicyManager.ANONYMOUS_ACCESS_POLICY, PrincipalType.User, Permission.READ));
        modelsMadePublicAcc.add(modelId);
        // Make references public
        for (ModelId referencedModelId : modelInfo.getReferences()) {
            makeModelPublicRecursively(referencedModelId, modelsMadePublicAcc);
        }
    }
}
Also used : ModelNamespaceNotOfficialException(org.eclipse.vorto.repository.model.ModelNamespaceNotOfficialException) ModelNotReleasedException(org.eclipse.vorto.repository.model.ModelNotReleasedException) ModelId(org.eclipse.vorto.model.ModelId)

Aggregations

ModelId (org.eclipse.vorto.model.ModelId)2 ModelNamespaceNotOfficialException (org.eclipse.vorto.repository.model.ModelNamespaceNotOfficialException)2 ModelNotReleasedException (org.eclipse.vorto.repository.model.ModelNotReleasedException)2 IUserContext (org.eclipse.vorto.repository.core.IUserContext)1 DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1