Search in sources :

Example 1 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext 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 IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class ModelRepositoryController method refactorModelId.

@PutMapping(value = "/refactorings/{oldId:.+}/{newId:.+}", produces = "application/json")
@PreAuthorize("hasAuthority('sysadmin') or " + "hasPermission(T(org.eclipse.vorto.model.ModelId).fromPrettyFormat(#oldId)," + "T(org.eclipse.vorto.repository.core.PolicyEntry.Permission).MODIFY)")
public ResponseEntity<ModelInfo> refactorModelId(@PathVariable String oldId, @PathVariable String newId) {
    final ModelId oldModelId = ModelId.fromPrettyFormat(oldId);
    final ModelId newModelId = ModelId.fromPrettyFormat(newId);
    IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication(), getWorkspaceId(oldId));
    ModelInfo result = this.modelRepositoryFactory.getRepositoryByModel(oldModelId).rename(oldModelId, newModelId, userContext);
    return new ResponseEntity<>(result, HttpStatus.OK);
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ResponseEntity(org.springframework.http.ResponseEntity) ModelId(org.eclipse.vorto.model.ModelId) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class ModelRepositoryController method createModel.

@ApiOperation(value = "Creates a model in the repository with the given model ID and model type.")
@PostMapping(value = "/{modelId:.+}/{modelType}", produces = "application/json")
public ResponseEntity<ModelInfo> createModel(@ApiParam(value = "modelId", required = true) @PathVariable String modelId, @ApiParam(value = "modelType", required = true) @PathVariable ModelType modelType, @RequestBody(required = false) List<ModelProperty> properties) throws WorkflowException {
    final ModelId modelID = ModelId.fromPrettyFormat(modelId);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    IUserContext userContext = UserContext.user(authentication, getWorkspaceId(modelId));
    IModelRepository modelRepo = getModelRepository(modelID);
    if (modelRepo.exists(modelID)) {
        throw new ModelAlreadyExistsException();
    } else {
        String modelTemplate = null;
        if (modelType == ModelType.InformationModel && properties != null) {
            modelTemplate = new InfomodelTemplate().createModelTemplate(modelID, properties);
        } else {
            modelTemplate = new ModelTemplate().createModelTemplate(modelID, modelType);
        }
        ModelInfo savedModel = modelRepo.save(modelID, modelTemplate.getBytes(), modelID.getName() + modelType.getExtension(), userContext);
        this.workflowService.start(modelID, userContext);
        return new ResponseEntity<>(savedModel, HttpStatus.CREATED);
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelAlreadyExistsException(org.eclipse.vorto.repository.core.ModelAlreadyExistsException) ModelTemplate(org.eclipse.vorto.repository.web.core.templates.ModelTemplate) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ResponseEntity(org.springframework.http.ResponseEntity) Authentication(org.springframework.security.core.Authentication) InfomodelTemplate(org.eclipse.vorto.repository.web.core.templates.InfomodelTemplate) ModelId(org.eclipse.vorto.model.ModelId) PostMapping(org.springframework.web.bind.annotation.PostMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class PayloadMappingController method saveMappingSpecification.

@PutMapping(value = "/{modelId:.+}")
@PreAuthorize("hasAuthority('model_creator')")
public void saveMappingSpecification(@RequestBody MappingSpecification mappingSpecification, @PathVariable String modelId) {
    try {
        String namespaceName = ModelId.fromPrettyFormat(modelId).getNamespace();
        Namespace namespace = namespaceService.getByName(namespaceName);
        if (null == namespace) {
            LOGGER.error(String.format("Namespace [%s] not found", namespaceName));
            return;
        }
        IUserContext userContext;
        if (namespace != null) {
            userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication(), namespace.getWorkspaceId());
        } else {
            userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
        }
        this.mappingService.saveSpecification(mappingSpecification, userContext);
    } catch (DoesNotExistException dnee) {
        LOGGER.error(dnee);
    }
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) Namespace(org.eclipse.vorto.repository.domain.Namespace) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class ModelRepositoryEventListener method createWorkspace.

private void createWorkspace(AppEvent event) {
    IUserContext userContext = event.getUserContext();
    IRepositoryManager repoMgr = repositoryFactory.getRepositoryManager(userContext.getWorkspaceId(), userContext.getAuthentication());
    repoMgr.createWorkspace(userContext.getWorkspaceId());
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) IRepositoryManager(org.eclipse.vorto.repository.core.IRepositoryManager)

Aggregations

IUserContext (org.eclipse.vorto.repository.core.IUserContext)54 Test (org.junit.Test)32 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)28 ClassPathResource (org.springframework.core.io.ClassPathResource)18 ResponseEntity (org.springframework.http.ResponseEntity)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)10 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)8 ModelId (org.eclipse.vorto.model.ModelId)7 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)6 User (org.eclipse.vorto.repository.domain.User)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 PutMapping (org.springframework.web.bind.annotation.PutMapping)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Namespace (org.eclipse.vorto.repository.domain.Namespace)4 ApiOperation (io.swagger.annotations.ApiOperation)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ModelResource (org.eclipse.vorto.repository.core.ModelResource)3 InvalidUserException (org.eclipse.vorto.repository.services.exceptions.InvalidUserException)3