agave-jobs/jobs-api/src/main/java/org/iplantc/service/jobs/resources/JobPermissionsResource.java
/**
	 * Post action for adding (and overwriting) permissions on a job object
	 * 
	 */
Post action for adding (and overwriting) permissions on a job object
Post action for adding ( and overwriting ) permissions on a job object
@Override
	public void acceptRepresentation(Representation entity)
	{
		try {
			if (!ServiceUtils.isValid(sJobId)) {
				getResponse().setEntity(
						new IplantErrorRepresentation("Job id cannot be empty"));
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
				return;
			}
			Job job = null;
			try {
				job = JobDao.getByUuid(sJobId);
				if (job == null) {
					String msg = "No job found with job id " + sJobId + ".";
					log.error(msg);
					getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
					getResponse().setEntity(new IplantErrorRepresentation(msg));
					throw new ResourceException(
							Status.CLIENT_ERROR_BAD_REQUEST, "Invalid job id");
				}
			} catch (Exception e) {
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
				getResponse().setEntity(
						new IplantErrorRepresentation(e.getMessage()));
				return;
			}
			if (entity != null) {
				try {
					Map<String, String> postContentData = super.getPostedEntityAsMap();
					String name = null;
					if (StringUtils.isEmpty(sharedUsername)) {
						name = postContentData.get("username");
					} else if (postContentData.containsKey("username") &&
							!StringUtils.equalsIgnoreCase(postContentData.get("username"), sharedUsername)) {
						throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
								"The username value in the POST body, " + postContentData.get("username") +
										", does not match the username in the URL, " + sharedUsername);
					} else {
						name = sharedUsername;
					}
					if (StringUtils.isEmpty(name) || StringUtils.equals(name, "null")) {
						throw new ResourceException(
								Status.CLIENT_ERROR_BAD_REQUEST, "No user found matching " + name);
					} else {
						AgaveProfileServiceClient authClient = new AgaveProfileServiceClient(
								Settings.IPLANT_PROFILE_SERVICE,
								Settings.IRODS_USERNAME,
								Settings.IRODS_PASSWORD);
						if (authClient.getUser(name) == null) {
							throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
									"No user found matching " + name);
						}
					}
					String sPermission = null;
					if (postContentData.containsKey("permission")) {
						sPermission = postContentData.get("permission");
					} else {
						throw new ResourceException(
								Status.CLIENT_ERROR_BAD_REQUEST,
								"Missing permission field.");
					}
					JobPermissionManager pm = new JobPermissionManager(job, username);
					if (pm.canWrite(username)) {
						try {
							pm.setPermission(name, sPermission);
							if (StringUtils.isEmpty(sPermission)) {
								getResponse().setStatus(Status.SUCCESS_OK);
							} else {
								getResponse().setStatus(Status.SUCCESS_CREATED);
							}
							JobPermission pem = JobPermissionDao.getByUsernameAndJobId(name, job.getId());
							if (pem == null) {
								pem = new JobPermission(job, name, PermissionType.NONE);
							}
							getResponse().setEntity(
									new IplantSuccessRepresentation(pem.toJSON(job)));
						} catch (IllegalArgumentException iae) {
							throw new ResourceException(
									Status.CLIENT_ERROR_BAD_REQUEST,
									"Invalid permission value. Valid values are: " + PermissionType.supportedValuesAsString());
						}
					} else {
						throw new ResourceException(
								Status.CLIENT_ERROR_FORBIDDEN,
								"User does not have permission to modify this resource.");
					}
				} catch (ResourceException e) {
					getResponse().setEntity(
							new IplantErrorRepresentation(e.getMessage()));
					getResponse().setStatus(e.getStatus());
				} catch (Exception e) {
					getResponse().setEntity(
							new IplantErrorRepresentation("Failed to update job permissions: " + e.getMessage()));
					getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
				}
			} else {
				getResponse().setEntity(
						new IplantErrorRepresentation(
								"Post request with no entity"));
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
			}
		}
		finally {
			try { HibernateUtil.closeSession(); } catch (Throwable ignored) {}
		}
	}
@ Override public void acceptRepresentation ( Representation entity ) { try { if ( ! ServiceUtils . isValid ( sJobId ) ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Job id cannot be empty" ) ) ; getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; return ; } Job job = null ; try { job = JobDao . getByUuid ( sJobId ) ; if ( job == null ) { String msg = "No job found with job id " + sJobId + "." ; log . error ( msg ) ; getResponse ( ) . setStatus ( Status . CLIENT_ERROR_NOT_FOUND ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( msg ) ) ; throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid job id" ) ; } } catch ( Exception e ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; return ; } if ( entity != null ) { try { Map < String , String > postContentData = super . getPostedEntityAsMap ( ) ; String name = null ; if ( StringUtils . isEmpty ( sharedUsername ) ) { name = postContentData . get ( "username" ) ; } else if ( postContentData . containsKey ( "username" ) && ! StringUtils . equalsIgnoreCase ( postContentData . get ( "username" ) , sharedUsername ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The username value in the POST body, " + postContentData . get ( "username" ) + ", does not match the username in the URL, " + sharedUsername ) ; } else { name = sharedUsername ; } if ( StringUtils . isEmpty ( name ) || StringUtils . equals ( name , "null" ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No user found matching " + name ) ; } else { AgaveProfileServiceClient authClient = new AgaveProfileServiceClient ( Settings . IPLANT_PROFILE_SERVICE , Settings . IRODS_USERNAME , Settings . IRODS_PASSWORD ) ; if ( authClient . getUser ( name ) == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No user found matching " + name ) ; } } String sPermission = null ; if ( postContentData . containsKey ( "permission" ) ) { sPermission = postContentData . get ( "permission" ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Missing permission field." ) ; } JobPermissionManager pm = new JobPermissionManager ( job , username ) ; if ( pm . canWrite ( username ) ) { try { pm . setPermission ( name , sPermission ) ; if ( StringUtils . isEmpty ( sPermission ) ) { getResponse ( ) . setStatus ( Status . SUCCESS_OK ) ; } else { getResponse ( ) . setStatus ( Status . SUCCESS_CREATED ) ; } JobPermission pem = JobPermissionDao . getByUsernameAndJobId ( name , job . getId ( ) ) ; if ( pem == null ) { pem = new JobPermission ( job , name , PermissionType . NONE ) ; } getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( pem . toJSON ( job ) ) ) ; } catch ( IllegalArgumentException iae ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid permission value. Valid values are: " + PermissionType . supportedValuesAsString ( ) ) ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have permission to modify this resource." ) ; } } catch ( ResourceException e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; } catch ( Exception e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Failed to update job permissions: " + e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; } } else { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Post request with no entity" ) ) ; getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; } } finally { try { HibernateUtil . closeSession ( ) ; } catch ( Throwable ignored ) { } } }
Post action for adding (and overwriting) permissions on a job object
Post action for adding ( and overwriting ) permissions on a job object
// validate the user they are giving permissions to exists // if the permission is null or empty, the permission // will be removed