Search in sources :

Example 11 with RESTApiJsonResponse

use of io.hops.hopsworks.api.util.RESTApiJsonResponse in project hopsworks by logicalclocks.

the class ProjectService method removeProjectAndFiles.

@POST
@Path("{projectId}/delete")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER })
public Response removeProjectAndFiles(@PathParam("projectId") Integer id, @Context HttpServletRequest req, @Context SecurityContext sc) throws ProjectException, GenericException {
    Users user = jWTHelper.getUserPrincipal(sc);
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    projectController.removeProject(user.getEmail(), id, req.getSession().getId());
    json.setSuccessMessage(ResponseMessages.PROJECT_REMOVED);
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
}
Also used : RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Users(io.hops.hopsworks.persistence.entity.user.Users) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 12 with RESTApiJsonResponse

use of io.hops.hopsworks.api.util.RESTApiJsonResponse in project hopsworks by logicalclocks.

the class RequestService method requestAccess.

@POST
@Path("/access")
@Produces(MediaType.APPLICATION_JSON)
public Response requestAccess(RequestDTO requestDTO, @Context SecurityContext sc) throws DatasetException, ProjectException {
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    if (requestDTO == null || requestDTO.getInodeId() == null || requestDTO.getProjectId() == null) {
        throw new IllegalArgumentException("requestDTO was not provided or was incomplete!");
    }
    Users user = jWTHelper.getUserPrincipal(sc);
    Inode inode = inodes.findById(requestDTO.getInodeId());
    // requested project
    Project proj = datasetCtrl.getOwningProject(inode);
    Dataset ds = datasetFacade.findByProjectAndInode(proj, inode);
    // requesting project
    Project project = projectFacade.find(requestDTO.getProjectId());
    Dataset dsInRequesting = datasetFacade.findByProjectAndInode(project, inode);
    if (dsInRequesting != null) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DESTINATION_EXISTS, Level.INFO);
    }
    ProjectTeam projectTeam = projectTeamFacade.findByPrimaryKey(project, user);
    ProjectTeam projTeam = projectTeamFacade.findByPrimaryKey(proj, user);
    if (projTeam != null && proj.equals(project)) {
        throw new ProjectException(RESTCodes.ProjectErrorCode.TEAM_MEMBER_ALREADY_EXISTS, Level.FINE);
    }
    DatasetRequest dsRequest = datasetRequest.findByProjectAndDataset(project, ds);
    // email body
    String msg = "Hi " + proj.getOwner().getFname() + " " + proj.getOwner().getLname() + ", \n\n" + user.getFname() + " " + user.getLname() + " wants access to a dataset in a project you own. \n\n" + "Dataset name: " + ds.getInode().getInodePK().getName() + "\n" + "Project name: " + proj.getName() + "\n";
    if (!Strings.isNullOrEmpty(requestDTO.getMessageContent())) {
        msg += "Attached message: " + requestDTO.getMessageContent() + "\n";
    }
    msg += "After logging in to Hopsworks go to : /project/" + proj.getId() + "/datasets " + " if you want to share this dataset. \n";
    // or the prior request is from a data owner do nothing.
    if (dsRequest != null && (dsRequest.getProjectTeam().getTeamRole().equals(projectTeam.getTeamRole()) || dsRequest.getProjectTeam().getTeamRole().equals(AllowedProjectRoles.DATA_OWNER))) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DATASET_REQUEST_EXISTS, Level.FINE);
    } else if (dsRequest != null && projectTeam.getTeamRole().equals(AllowedProjectRoles.DATA_OWNER)) {
        dsRequest.setProjectTeam(projectTeam);
        dsRequest.setMessageContent(requestDTO.getMessageContent());
        datasetRequest.merge(dsRequest);
    } else {
        Users to = userFacade.findByEmail(proj.getOwner().getEmail());
        String message = "Hi " + to.getFname() + "<br>" + "I would like to request access to a dataset in a project you own. <br>" + "Project name: " + proj.getName() + "<br>" + "Dataset name: " + ds.getInode().getInodePK().getName() + "<br>" + "To be shared with my project: " + project.getName() + ".<br>" + "Thank you in advance.";
        String preview = user.getFname() + " would like to have access to a dataset in a project you own.";
        String subject = Settings.MESSAGE_DS_REQ_SUBJECT;
        String path = "project/" + proj.getId() + "/datasets";
        // to, from, msg, requested path
        Message newMsg = new Message(user, to, null, message, true, false);
        newMsg.setPath(path);
        newMsg.setSubject(subject);
        newMsg.setPreview(preview);
        messageBean.send(newMsg);
        dsRequest = new DatasetRequest(ds, projectTeam, requestDTO.getMessageContent(), newMsg);
        try {
            datasetRequest.persistDataset(dsRequest);
        } catch (Exception ex) {
            messageBean.remove(newMsg);
            throw new DatasetException(RESTCodes.DatasetErrorCode.DATASET_REQUEST_ERROR, Level.WARNING, ex.getMessage(), null, ex);
        }
    }
    try {
        emailBean.sendEmail(proj.getOwner().getEmail(), RecipientType.TO, "Access request for dataset " + ds.getInode().getInodePK().getName(), msg);
    } catch (MessagingException ex) {
        json.setErrorMsg("Could not send e-mail to " + project.getOwner().getEmail());
        datasetRequest.remove(dsRequest);
        return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
    }
    json.setSuccessMessage("Request sent successfully.");
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
}
Also used : Message(io.hops.hopsworks.persistence.entity.message.Message) MessagingException(javax.mail.MessagingException) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) Users(io.hops.hopsworks.persistence.entity.user.Users) MessagingException(javax.mail.MessagingException) ProjectException(io.hops.hopsworks.exceptions.ProjectException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) ProjectException(io.hops.hopsworks.exceptions.ProjectException) Project(io.hops.hopsworks.persistence.entity.project.Project) ProjectTeam(io.hops.hopsworks.persistence.entity.project.team.ProjectTeam) Inode(io.hops.hopsworks.persistence.entity.hdfs.inode.Inode) DatasetRequest(io.hops.hopsworks.persistence.entity.dataset.DatasetRequest) RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 13 with RESTApiJsonResponse

use of io.hops.hopsworks.api.util.RESTApiJsonResponse in project hopsworks by logicalclocks.

the class ProjectMembersService method removeMembersByID.

@DELETE
@Path("/{email}")
@Produces(MediaType.APPLICATION_JSON)
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER", "HOPS_SERVICE_USER" })
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
public Response removeMembersByID(@PathParam("email") String email, @Context HttpServletRequest req, @Context SecurityContext sc) throws ProjectException, ServiceException, HopsSecurityException, UserException, GenericException, IOException, JobException, TensorBoardException, FeaturestoreException {
    Project project = projectController.findProjectById(this.projectId);
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    Users reqUser = jWTHelper.getUserPrincipal(sc);
    if (email == null) {
        throw new IllegalArgumentException("Email was not provided");
    }
    // Not able to remove members that do not exist
    String userProjectRole = projectTeamFacade.findCurrentRole(project, email);
    if (userProjectRole == null || userProjectRole.isEmpty()) {
        throw new ProjectException(RESTCodes.ProjectErrorCode.TEAM_MEMBER_NOT_FOUND, Level.FINE);
    }
    // Data scientists can only remove themselves
    String reqUserProjectRole = projectTeamFacade.findCurrentRole(project, reqUser.getEmail());
    if (reqUserProjectRole.equals(AllowedProjectRoles.DATA_SCIENTIST) && !reqUser.getEmail().equals(email)) {
        throw new ProjectException(RESTCodes.ProjectErrorCode.MEMBER_REMOVAL_NOT_ALLOWED, Level.FINE);
    }
    projectController.removeMemberFromTeam(project, reqUser, email);
    json.setSuccessMessage(ResponseMessages.MEMBER_REMOVED_FROM_TEAM);
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
}
Also used : ProjectException(io.hops.hopsworks.exceptions.ProjectException) Project(io.hops.hopsworks.persistence.entity.project.Project) RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Users(io.hops.hopsworks.persistence.entity.user.Users) Path(javax.ws.rs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 14 with RESTApiJsonResponse

use of io.hops.hopsworks.api.util.RESTApiJsonResponse in project hopsworks by logicalclocks.

the class ProjectMembersService method updateRoleByEmail.

@POST
@Path("/{email}")
@Produces(MediaType.APPLICATION_JSON)
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER", "HOPS_SERVICE_USER" })
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER })
public Response updateRoleByEmail(@PathParam("email") String email, @FormParam("role") String role, @Context HttpServletRequest req, @Context SecurityContext sc) throws ProjectException, UserException, FeaturestoreException, IOException {
    Project project = projectController.findProjectById(this.projectId);
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    Users user = jWTHelper.getUserPrincipal(sc);
    if (email == null) {
        throw new IllegalArgumentException("Email was not provided.");
    }
    if (role == null || !ProjectRoleTypes.isAllowedRole(role)) {
        throw new IllegalArgumentException("Role was not provided.");
    }
    if (project.getOwner().getEmail().equals(email)) {
        throw new ProjectException(RESTCodes.ProjectErrorCode.PROJECT_OWNER_ROLE_NOT_ALLOWED, Level.FINE);
    }
    projectController.updateMemberRole(project, user, email, ProjectRoleTypes.fromString(role).getRole());
    json.setSuccessMessage(ResponseMessages.MEMBER_ROLE_UPDATED);
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
}
Also used : ProjectException(io.hops.hopsworks.exceptions.ProjectException) Project(io.hops.hopsworks.persistence.entity.project.Project) RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Users(io.hops.hopsworks.persistence.entity.user.Users) Path(javax.ws.rs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 15 with RESTApiJsonResponse

use of io.hops.hopsworks.api.util.RESTApiJsonResponse in project hopsworks by logicalclocks.

the class CertificateMaterializerAdmin method removeRemoteMaterializedCrypto.

/**
 * Removes crypto material from remote filesystem.
 *
 * CAUTION: This is a *dangerous* operation as other instances of Hopsworks might be
 * running and using the remote crypto material
 *
 * @param materialName Name of the materialized crypto
 * @param directory Remote directory of the crypto material
 * @return
 */
@DELETE
@Path("/remote/{name}/{directory}")
public Response removeRemoteMaterializedCrypto(@PathParam("name") String materialName, @PathParam("directory") String directory, @Context SecurityContext sc) {
    if (Strings.isNullOrEmpty(materialName)) {
        throw new IllegalArgumentException("materialName was not provided or was empty");
    }
    RESTApiJsonResponse response;
    Matcher psuMatcher = projectSpecificPattern.matcher(materialName);
    if (psuMatcher.matches()) {
        String projectName = psuMatcher.group(1);
        String userName = psuMatcher.group(2);
        if (certificateMaterializer.existsInRemoteStore(userName, projectName, directory)) {
            certificateMaterializer.forceRemoveRemoteMaterial(userName, projectName, directory, false);
        } else {
            response = noCacheResponse.buildJsonResponse(Response.Status.NOT_FOUND, "Material for user " + materialName + " does not exist in the store");
            return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).entity(response).build();
        }
    } else {
        if (certificateMaterializer.existsInRemoteStore(null, materialName, directory)) {
            certificateMaterializer.forceRemoveRemoteMaterial(null, materialName, directory, false);
        } else {
            response = noCacheResponse.buildJsonResponse(Response.Status.NOT_FOUND, "Material for project " + materialName + " does not exist in the store");
            return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).entity(response).build();
        }
    }
    response = noCacheResponse.buildJsonResponse(Response.Status.OK, "Deleted material");
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(response).build();
}
Also used : Matcher(java.util.regex.Matcher) RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

RESTApiJsonResponse (io.hops.hopsworks.api.util.RESTApiJsonResponse)43 Path (javax.ws.rs.Path)35 Produces (javax.ws.rs.Produces)32 Users (io.hops.hopsworks.persistence.entity.user.Users)24 POST (javax.ws.rs.POST)23 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)14 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)11 Project (io.hops.hopsworks.persistence.entity.project.Project)11 ProjectException (io.hops.hopsworks.exceptions.ProjectException)9 Consumes (javax.ws.rs.Consumes)8 GET (javax.ws.rs.GET)7 DELETE (javax.ws.rs.DELETE)6 JWTNotRequired (io.hops.hopsworks.api.filter.JWTNotRequired)4 DatasetPath (io.hops.hopsworks.common.dataset.util.DatasetPath)4 UserException (io.hops.hopsworks.exceptions.UserException)3 Dataset (io.hops.hopsworks.persistence.entity.dataset.Dataset)3 JsonResponse (io.hops.hopsworks.restutils.JsonResponse)3 ApiOperation (io.swagger.annotations.ApiOperation)3 RolesAllowed (javax.annotation.security.RolesAllowed)3 QrCode (io.hops.hopsworks.common.user.QrCode)2