Search in sources :

Example 1 with ProjectPermission

use of de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission in project webanno by webanno.

the class ProjectServiceImpl method setProjectPermissionLevels.

@Override
@Transactional(noRollbackFor = NoResultException.class)
public void setProjectPermissionLevels(User aUser, Project aProject, Collection<PermissionLevel> aLevels) {
    Set<PermissionLevel> levelsToBeGranted = new HashSet<>(aLevels);
    List<ProjectPermission> permissions = new ArrayList<>();
    try {
        permissions.addAll(listProjectPermissionLevel(aUser, aProject));
    } catch (NoResultException e) {
    // Nothing to do
    }
    // Remove permissions that no longer exist
    for (ProjectPermission permission : permissions) {
        if (!aLevels.contains(permission.getLevel())) {
            removeProjectPermission(permission);
        } else {
            levelsToBeGranted.remove(permission.getLevel());
        }
    }
    // Grant new permissions
    for (PermissionLevel level : levelsToBeGranted) {
        createProjectPermission(new ProjectPermission(aProject, aUser.getUsername(), level));
    }
}
Also used : ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) ArrayList(java.util.ArrayList) NoResultException(javax.persistence.NoResultException) PermissionLevel(de.tudarmstadt.ukp.clarin.webanno.model.PermissionLevel) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ProjectPermission

use of de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission in project webanno by webanno.

the class ProjectServiceImpl method removeProject.

@Override
@Transactional
public void removeProject(Project aProject) throws IOException {
    // remove metadata from DB
    Project project = aProject;
    if (!entityManager.contains(project)) {
        project = entityManager.merge(project);
    }
    applicationEventPublisher.publishEvent(new BeforeProjectRemovedEvent(this, aProject));
    for (ProjectPermission permissions : getProjectPermissions(aProject)) {
        entityManager.remove(permissions);
    }
    entityManager.remove(project);
    // remove the project directory from the file system
    String path = dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + aProject.getId();
    try {
        FileUtils.deleteDirectory(new File(path));
    } catch (FileNotFoundException e) {
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aProject.getId()))) {
            log.info("Project directory to be deleted was not found: [{}]. Ignoring.", path);
        }
    }
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aProject.getId()))) {
        log.info("Removed project [{}]({})", aProject.getName(), aProject.getId());
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) FileNotFoundException(java.io.FileNotFoundException) BeforeProjectRemovedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.BeforeProjectRemovedEvent) ZipFile(java.util.zip.ZipFile) File(java.io.File) MDC(org.slf4j.MDC) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ProjectPermission

use of de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission in project webanno by webanno.

the class RemoteApiController2 method projectCreate.

@ApiOperation(value = "Create a new project")
@RequestMapping(value = ("/" + PROJECTS), method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RProject>> projectCreate(@RequestParam(PARAM_NAME) String aName, @RequestParam(PARAM_CREATOR) Optional<String> aCreator, UriComponentsBuilder aUcb) throws Exception {
    // Get current user - this will throw an exception if the current user does not exit
    User user = getCurrentUser();
    // Check for the access
    assertPermission("User [" + user.getUsername() + "] is not allowed to create projects", isProjectCreator(projectService, user) || isSuperAdmin(projectService, user));
    // Check if the user can create projects for another user
    assertPermission("User [" + user.getUsername() + "] is not allowed to create projects for user [" + aCreator.orElse("<unspecified>") + "]", isSuperAdmin(projectService, user) || (aCreator.isPresent() && aCreator.get().equals(user.getUsername())));
    // Existing project
    if (projectService.existsProject(aName)) {
        throw new ObjectExistsException("A project with name [" + aName + "] already exists");
    }
    // Create the project and initialize tags
    LOG.info("Creating project [" + aName + "]");
    Project project = new Project();
    project.setName(aName);
    projectService.createProject(project);
    annotationService.initializeProject(project);
    // Create permission for the project creator
    String owner = aCreator.isPresent() ? aCreator.get() : user.getUsername();
    projectService.createProjectPermission(new ProjectPermission(project, owner, PermissionLevel.ADMIN));
    projectService.createProjectPermission(new ProjectPermission(project, owner, PermissionLevel.CURATOR));
    projectService.createProjectPermission(new ProjectPermission(project, owner, PermissionLevel.USER));
    RResponse<RProject> response = new RResponse<>(new RProject(project));
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{id}").buildAndExpand(project.getId()).toUri()).body(response);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ObjectExistsException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.ObjectExistsException) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ProjectPermission

use of de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission in project webanno by webanno.

the class UserSelectionPanel method actionAdd.

private void actionAdd(AjaxRequestTarget aTarget, Form<List<User>> aForm) {
    for (User user : aForm.getModelObject()) {
        projectRepository.createProjectPermission(new ProjectPermission(projectModel.getObject(), user.getUsername(), PermissionLevel.USER));
    }
    aForm.getModelObject().clear();
    usersToAdd.getChoicesModel().detach();
    aTarget.add(this);
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission)

Example 5 with ProjectPermission

use of de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission in project webanno by webanno.

the class ProjectDetailPanel method actionSave.

private void actionSave(AjaxRequestTarget aTarget, Form<Project> aForm) {
    aTarget.add(getPage());
    // aTarget.add(((ApplicationPageBase) getPage()).getPageContent());
    // aTarget.addChildren(getPage(), IFeedback.class);
    Project project = aForm.getModelObject();
    if (isNull(project.getId())) {
        try {
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            projectService.createProject(project);
            projectService.createProjectPermission(new ProjectPermission(project, username, PermissionLevel.ADMIN));
            projectService.createProjectPermission(new ProjectPermission(project, username, PermissionLevel.CURATOR));
            projectService.createProjectPermission(new ProjectPermission(project, username, PermissionLevel.USER));
            annotationService.initializeProject(project);
        } catch (IOException e) {
            error("Project repository path not found " + ":" + ExceptionUtils.getRootCauseMessage(e));
            LOG.error("Project repository path not found " + ":" + ExceptionUtils.getRootCauseMessage(e));
        }
    } else {
        projectService.updateProject(project);
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) IOException(java.io.IOException)

Aggregations

ProjectPermission (de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission)9 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)5 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ZipFile (java.util.zip.ZipFile)2 JSONObject (org.apache.wicket.ajax.json.JSONObject)2 Transactional (org.springframework.transaction.annotation.Transactional)2 BeforeProjectRemovedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.BeforeProjectRemovedEvent)1 Mode (de.tudarmstadt.ukp.clarin.webanno.model.Mode)1 PermissionLevel (de.tudarmstadt.ukp.clarin.webanno.model.PermissionLevel)1 ObjectExistsException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.ObjectExistsException)1 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)1 RResponse (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse)1 ApiOperation (io.swagger.annotations.ApiOperation)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1