agave-metadata/metadata-api/src/main/java/org/iplantc/service/metadata/resources/MetadataSchemaShareResource.java
/**
	 * Post action for adding (and overwriting) permissions on a metadata iod
	 * 
	 */
Post action for adding (and overwriting) permissions on a metadata iod
Post action for adding ( and overwriting ) permissions on a metadata iod
@Override
	public void acceptRepresentation(Representation entity)
	{
		try
		{
			if (StringUtils.isEmpty(schemaId))
			{
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
						"No schema id provided.");
			}
			String name;
            String sPermission;
            JSONObject postPermissionContent = super.getPostedEntityAsJsonObject(true);
            if (StringUtils.isEmpty(sharedUsername))
            {
            	AgaveLogServiceClient.log(METADATA02.name(), SchemaPemsUpdate.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
                if (postPermissionContent.has("username")) 
                {
                    name = postPermissionContent.getString("username");
            	} 
                else
                {	
                	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
                			"No username specified. Please specify a valid user to whom the permission will apply."); 
                }
            }
            else
            {
            	AgaveLogServiceClient.log(METADATA02.name(), SchemaPemsCreate.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
            	if (postPermissionContent.has("username") && 
            			!StringUtils.equalsIgnoreCase(postPermissionContent.getString("username"), sharedUsername)) 
                {
            		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
                			"The username value in the POST body, " + postPermissionContent.getString("username") + 
                			", does not match the username in the URL, " + sharedUsername);            		
            	} 
                else
                {
                	name = sharedUsername;
                }
            }
            if (postPermissionContent.has("permission")) 
            {
                sPermission = postPermissionContent.getString("permission");
                if (StringUtils.equalsIgnoreCase(sPermission, "none") ||
                		StringUtils.equalsIgnoreCase(sPermission, "null")) {
                	sPermission = null;
                }
            } 
            else 
            {
            	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                        "Missing permission field. Please specify a valid permission of READ, WRITE, or READ_WRITE.");
            }
			if (!ServiceUtils.isValid(name)) { 
				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);
				}
			}
            MetadataSchemaPermissionManager pm = new MetadataSchemaPermissionManager(schemaId, owner);
			if (pm.canWrite(username))
			{
				try 
				{
					pm.setPermission(name, sPermission);
					if (StringUtils.isEmpty(sPermission)) {
						getResponse().setStatus(Status.SUCCESS_OK);
					} else {
						getResponse().setStatus(Status.SUCCESS_CREATED);
					}
					MetadataSchemaPermission pem = MetadataSchemaPermissionDao.getByUsernameAndSchemaId(name, schemaId);
					if (pem == null) {
						pem = new MetadataSchemaPermission(schemaId, name, PermissionType.NONE);
					}
					getResponse().setEntity(new IplantSuccessRepresentation(pem.toJSON()));
				} 
				catch (PermissionException e) {
					throw new ResourceException(
							Status.CLIENT_ERROR_FORBIDDEN,
							e.getMessage(), e);
				}
				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 (Throwable e) {
			getResponse().setEntity(
					new IplantErrorRepresentation("Failed to update metadata schema permissions: " + e.getMessage()));
			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
		}
		finally {
			try { HibernateUtil.closeSession(); } catch (Throwable ignored) {}
		}
	}
@ Override public void acceptRepresentation ( Representation entity ) { try { if ( StringUtils . isEmpty ( schemaId ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No schema id provided." ) ; } String name ; String sPermission ; JSONObject postPermissionContent = super . getPostedEntityAsJsonObject ( true ) ; if ( StringUtils . isEmpty ( sharedUsername ) ) { AgaveLogServiceClient . log ( METADATA02 . name ( ) , SchemaPemsUpdate . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; if ( postPermissionContent . has ( "username" ) ) { name = postPermissionContent . getString ( "username" ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No username specified. Please specify a valid user to whom the permission will apply." ) ; } } else { AgaveLogServiceClient . log ( METADATA02 . name ( ) , SchemaPemsCreate . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; if ( postPermissionContent . has ( "username" ) && ! StringUtils . equalsIgnoreCase ( postPermissionContent . getString ( "username" ) , sharedUsername ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The username value in the POST body, " + postPermissionContent . getString ( "username" ) + ", does not match the username in the URL, " + sharedUsername ) ; } else { name = sharedUsername ; } } if ( postPermissionContent . has ( "permission" ) ) { sPermission = postPermissionContent . getString ( "permission" ) ; if ( StringUtils . equalsIgnoreCase ( sPermission , "none" ) || StringUtils . equalsIgnoreCase ( sPermission , "null" ) ) { sPermission = null ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Missing permission field. Please specify a valid permission of READ, WRITE, or READ_WRITE." ) ; } if ( ! ServiceUtils . isValid ( name ) ) { 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 ) ; } } MetadataSchemaPermissionManager pm = new MetadataSchemaPermissionManager ( schemaId , owner ) ; if ( pm . canWrite ( username ) ) { try { pm . setPermission ( name , sPermission ) ; if ( StringUtils . isEmpty ( sPermission ) ) { getResponse ( ) . setStatus ( Status . SUCCESS_OK ) ; } else { getResponse ( ) . setStatus ( Status . SUCCESS_CREATED ) ; } MetadataSchemaPermission pem = MetadataSchemaPermissionDao . getByUsernameAndSchemaId ( name , schemaId ) ; if ( pem == null ) { pem = new MetadataSchemaPermission ( schemaId , name , PermissionType . NONE ) ; } getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( pem . toJSON ( ) ) ) ; } catch ( PermissionException e ) { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , e . getMessage ( ) , e ) ; } 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 ( Throwable e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Failed to update metadata schema permissions: " + e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; } finally { try { HibernateUtil . closeSession ( ) ; } catch ( Throwable ignored ) { } } }
Post action for adding (and overwriting) permissions on a metadata iod
Post action for adding ( and overwriting ) permissions on a metadata iod
// a username must be provided either in the form or the body // name in url and json, if provided, should match // validate the user they are giving permissions to exists // if the permission is null or empty, the permission // will be removed