Search in sources :

Example 6 with ProjectPermission

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

the class UserSelectionPanel method makeUserChoiceRenderer.

private IChoiceRenderer<User> makeUserChoiceRenderer() {
    return new ChoiceRenderer<User>() {

        private static final long serialVersionUID = 4607720784161484145L;

        @Override
        public Object getDisplayValue(User aObject) {
            List<ProjectPermission> projectPermissions = projectRepository.listProjectPermissionLevel(aObject, projectModel.getObject());
            List<String> permissionLevels = new ArrayList<>();
            for (ProjectPermission projectPermission : projectPermissions) {
                permissionLevels.add(projectPermission.getLevel().getName());
            }
            return aObject.getUsername() + " " + permissionLevels + (aObject.isEnabled() ? "" : " (login disabled)");
        }
    };
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) IChoiceRenderer(org.apache.wicket.markup.html.form.IChoiceRenderer) ChoiceRenderer(org.apache.wicket.markup.html.form.ChoiceRenderer) ArrayList(java.util.ArrayList)

Example 7 with ProjectPermission

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

the class ProjectServiceImpl method createProjectPermission.

/**
 * Create {@link ProjectPermission} from the exported
 * {@link de.tudarmstadt.ukp.clarin.webanno.export.model.ProjectPermission}
 *
 * @param aImportedProjectSetting
 *            the imported project.
 * @param aImportedProject
 *            the project.
 * @throws IOException
 *             if an I/O error occurs.
 */
private void createProjectPermission(de.tudarmstadt.ukp.clarin.webanno.export.model.Project aImportedProjectSetting, Project aImportedProject) throws IOException {
    for (de.tudarmstadt.ukp.clarin.webanno.export.model.ProjectPermission importedPermission : aImportedProjectSetting.getProjectPermissions()) {
        ProjectPermission permission = new ProjectPermission();
        permission.setLevel(importedPermission.getLevel());
        permission.setProject(aImportedProject);
        permission.setUser(importedPermission.getUser());
        createProjectPermission(permission);
    }
}
Also used : Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission)

Example 8 with ProjectPermission

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

the class RemoteApiController method projectCreate.

/**
 * Create a new project.
 *
 * To test when running in Eclipse, use the Linux "curl" command.
 *
 * curl -v -F 'file=@test.zip' -F 'name=Test' -F 'filetype=tcf'
 * 'http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects'
 *
 * @param aName
 *            the name of the project to create.
 * @param aFileType
 *            the type of the files contained in the ZIP. The possible file types are configured
 *            in the formats.properties configuration file of WebAnno.
 * @param aFile
 *            a ZIP file containing the project data.
 * @throws Exception if there was an error.
 */
@RequestMapping(value = ("/" + PROJECTS), method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> projectCreate(@RequestParam(PARAM_FILE) MultipartFile aFile, @RequestParam(PARAM_NAME) String aName, @RequestParam(PARAM_FILETYPE) String aFileType) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectCreator(projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to create projects");
    }
    // Existing project
    if (projectRepository.existsProject(aName)) {
        return ResponseEntity.status(HttpStatus.CONFLICT).body("A project with name [" + aName + "] already exists");
    }
    // Check archive
    try (InputStream is = new BufferedInputStream(aFile.getInputStream())) {
        if (!ZipUtils.isZipStream(is)) {
            return ResponseEntity.badRequest().body("Invalid ZIP file");
        }
    }
    // Create the project and initialize tags
    LOG.info("Creating project [" + aName + "]");
    Project project = new Project();
    project.setName(aName);
    projectRepository.createProject(project);
    annotationService.initializeProject(project);
    // Create permission for the project creator
    projectRepository.createProjectPermission(new ProjectPermission(project, username, PermissionLevel.ADMIN));
    projectRepository.createProjectPermission(new ProjectPermission(project, username, PermissionLevel.CURATOR));
    projectRepository.createProjectPermission(new ProjectPermission(project, username, PermissionLevel.USER));
    // Iterate through all the files in the ZIP
    // If the current filename does not start with "." and is in the root folder of the ZIP,
    // import it as a source document
    File zipFile = File.createTempFile(aFile.getOriginalFilename(), ".zip");
    aFile.transferTo(zipFile);
    ZipFile zip = new ZipFile(zipFile);
    for (Enumeration<?> zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements(); ) {
        // Get ZipEntry which is a file or a directory
        ZipEntry entry = (ZipEntry) zipEnumerate.nextElement();
        // If it is the zip name, ignore it
        if ((FilenameUtils.removeExtension(aFile.getOriginalFilename()) + "/").equals(entry.toString())) {
            continue;
        } else // project meta data
        if (entry.toString().replace("/", "").equals((META_INF + "webanno/source-meta-data.properties").replace("/", ""))) {
            InputStream zipStream = zip.getInputStream(entry);
            projectRepository.savePropertiesFile(project, zipStream, entry.toString());
        } else // META-INF/webanno/source-meta-data.properties
        if (StringUtils.countMatches(entry.toString(), "/") > 1) {
            continue;
        } else // ZIP, import it as a source document
        if (!FilenameUtils.getExtension(entry.toString()).equals("") && !FilenameUtils.getName(entry.toString()).equals(".")) {
            uploadSourceDocument(zip, entry, project, aFileType);
        }
    }
    LOG.info("Successfully created project [" + aName + "] for user [" + username + "]");
    JSONObject projectJSON = new JSONObject();
    long pId = projectRepository.getProject(aName).getId();
    projectJSON.append(aName, pId);
    return ResponseEntity.ok(projectJSON.toString());
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ZipFile(java.util.zip.ZipFile) JSONObject(org.apache.wicket.ajax.json.JSONObject) BufferedInputStream(java.io.BufferedInputStream) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) ZipFile(java.util.zip.ZipFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with ProjectPermission

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

the class RemoteApiController method projectList.

/**
 * List all the projects for a given user with their roles
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values
 * for username and password:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects
 *
 * @return JSON string of project where user has access to and respective roles in the project.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = ("/" + PROJECTS), method = RequestMethod.GET)
public ResponseEntity<String> projectList() throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get projects with permission
    List<Project> accessibleProjects = projectRepository.listAccessibleProjects(user);
    // Add permissions for each project into JSON array and store in JSON object
    JSONObject returnJSONObj = new JSONObject();
    for (Project project : accessibleProjects) {
        String projectId = Long.toString(project.getId());
        List<ProjectPermission> projectPermissions = projectRepository.listProjectPermissionLevel(user, project);
        JSONArray permissionArr = new JSONArray();
        JSONObject projectJSON = new JSONObject();
        for (ProjectPermission p : projectPermissions) {
            permissionArr.put(p.getLevel().getName());
        }
        projectJSON.put(project.getName(), permissionArr);
        returnJSONObj.put(projectId, projectJSON);
    }
    return ResponseEntity.ok(returnJSONObj.toString());
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) JSONObject(org.apache.wicket.ajax.json.JSONObject) ProjectPermission(de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission) JSONArray(org.apache.wicket.ajax.json.JSONArray) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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