agave-systems/systems-api/src/main/java/org/iplantc/service/systems/resources/SystemRoleResource.java
/**
	 * Post action for adding (and overwriting) roles on a system object
	 */
Post action for adding (and overwriting) roles on a system object
Post action for adding ( and overwriting ) roles on a system object
@Override
	public void acceptRepresentation(Representation entity)
	{
		this.username = getAuthenticatedUsername();
		AgaveLogServiceClient.log(AgaveLogServiceClient.ServiceKeys.SYSTEMS02.name(), 
				AgaveLogServiceClient.ActivityKeys.SystemEditRoles.name(), 
				username, "", getRequest().getClientInfo().getUpstreamAddress());
		SystemManager systemManager = new SystemManager();
		try
		{
			if (StringUtils.isBlank(systemId))
			{
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
						"Please specify an system using its system id. ");
			}
			RemoteSystem system = dao.findActiveAndInactiveSystemBySystemId(systemId);
			if (system == null)
			{
				throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
						"No shared system found matching " + systemId,
						new SystemUnknownException());
			}
			else if (!system.isAvailable()) 
			{
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
						"System has been disabled by the administrator. "
						+ "No roles changes may be applied to a disabled system.", 
						new SystemUnavailableException());
			}
			else if (system.isPubliclyAvailable() && !systemManager.isManageableByUser(system, username)) 
			{
				throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED,
						"Share roles are not suported on public systems.",
						new NotImplementedException());
			}
			if (systemManager.isManageableByUser(system, username))
			{
				Map<String, String> postData = getPostedEntityAsMap();
				try
				{
					String name = null;
					if (StringUtils.isEmpty(sharedUsername)) 
					{
						 name = postData.get("username");
					} 
					else if (postData.containsKey("username") && 
							!StringUtils.equalsIgnoreCase(postData.get("username"), sharedUsername)) 
					{
						throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
								"The username value in the POST body, " + postData.get("username") + 
	                			", does not match the username in the URL, " + sharedUsername,
								new SystemArgumentException());         
					}
					else
					{
						name = sharedUsername;
					}
					if (StringUtils.isEmpty(name) || StringUtils.equals(name, "null")) { 
						throw new ResourceException(
							Status.CLIENT_ERROR_BAD_REQUEST, "No user found matching " + name,
							new FileNotFoundException()); 
					} 
					else 
					{
						AgaveProfileServiceClient profileServiceClient = 
								new AgaveProfileServiceClient(Settings.IPLANT_PROFILE_SERVICE, Settings.IRODS_USERNAME, Settings.IRODS_PASSWORD);
						if (profileServiceClient.getUser(name) == null) {
							throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
								"No user found matching " + name,
								new FileNotFoundException());
						}
					}
					String sRole = null;
					if (postData.containsKey("role")) 
					{
						sRole = postData.get("role");
					} 
					else {
						throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
								"Missing role field.",
								new SystemArgumentException());
					}
					try 
					{
						SystemRoleManager roleManager = new SystemRoleManager(system);
						RoleType roleType = null;
						if (StringUtils.isEmpty(sRole) || StringUtils.equals(sRole, "null")) {
							roleType = RoleType.NONE;
						} else {
							roleType = RoleType.valueOf(sRole.toUpperCase());
						}
						roleManager.setRole(name, roleType, username);
						if (roleType.equals(RoleType.NONE)) {
							getResponse().setStatus(Status.SUCCESS_OK);
						} else {
							getResponse().setStatus(Status.SUCCESS_CREATED);
						}
						SystemRole role = roleManager.getUserRole(name);
						getResponse().setEntity(new IplantSuccessRepresentation("[" + role.toJSON(system) + "]"));
					} 
					catch (IllegalArgumentException iae) {
						throw new ResourceException(
								Status.CLIENT_ERROR_BAD_REQUEST,
								"Invalid role value. Valid values are: " + RoleType.supportedValuesAsString(),
								new SystemArgumentException());
					}
				}
				catch (ResourceException e) {
					throw e;
				}
				catch (SystemException e) {
					throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
							e.getMessage(), e);
				}
				catch (Exception e)  {
					log.error("Unexpected error updating user permission", e);
					throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
							"Error occurred updating user role.", e);
				}
			} else {
				throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
						"User does not have the necessary role to modify this system", new PermissionException());
			}
		}
		catch (ResourceException e) {
			getResponse().setEntity(
					new IplantErrorRepresentation(e.getMessage()));
			getResponse().setStatus(e.getStatus());
		}
	}
@ Override public void acceptRepresentation ( Representation entity ) { this . username = getAuthenticatedUsername ( ) ; AgaveLogServiceClient . log ( AgaveLogServiceClient . ServiceKeys . SYSTEMS02 . name ( ) , AgaveLogServiceClient . ActivityKeys . SystemEditRoles . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; SystemManager systemManager = new SystemManager ( ) ; try { if ( StringUtils . isBlank ( systemId ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Please specify an system using its system id. " ) ; } RemoteSystem system = dao . findActiveAndInactiveSystemBySystemId ( systemId ) ; if ( system == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No shared system found matching " + systemId , new SystemUnknownException ( ) ) ; } else if ( ! system . isAvailable ( ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "System has been disabled by the administrator. " + "No roles changes may be applied to a disabled system." , new SystemUnavailableException ( ) ) ; } else if ( system . isPubliclyAvailable ( ) && ! systemManager . isManageableByUser ( system , username ) ) { throw new ResourceException ( Status . SERVER_ERROR_NOT_IMPLEMENTED , "Share roles are not suported on public systems." , new NotImplementedException ( ) ) ; } if ( systemManager . isManageableByUser ( system , username ) ) { Map < String , String > postData = getPostedEntityAsMap ( ) ; try { String name = null ; if ( StringUtils . isEmpty ( sharedUsername ) ) { name = postData . get ( "username" ) ; } else if ( postData . containsKey ( "username" ) && ! StringUtils . equalsIgnoreCase ( postData . get ( "username" ) , sharedUsername ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The username value in the POST body, " + postData . get ( "username" ) + ", does not match the username in the URL, " + sharedUsername , new SystemArgumentException ( ) ) ; } else { name = sharedUsername ; } if ( StringUtils . isEmpty ( name ) || StringUtils . equals ( name , "null" ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No user found matching " + name , new FileNotFoundException ( ) ) ; } else { AgaveProfileServiceClient profileServiceClient = new AgaveProfileServiceClient ( Settings . IPLANT_PROFILE_SERVICE , Settings . IRODS_USERNAME , Settings . IRODS_PASSWORD ) ; if ( profileServiceClient . getUser ( name ) == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No user found matching " + name , new FileNotFoundException ( ) ) ; } } String sRole = null ; if ( postData . containsKey ( "role" ) ) { sRole = postData . get ( "role" ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Missing role field." , new SystemArgumentException ( ) ) ; } try { SystemRoleManager roleManager = new SystemRoleManager ( system ) ; RoleType roleType = null ; if ( StringUtils . isEmpty ( sRole ) || StringUtils . equals ( sRole , "null" ) ) { roleType = RoleType . NONE ; } else { roleType = RoleType . valueOf ( sRole . toUpperCase ( ) ) ; } roleManager . setRole ( name , roleType , username ) ; if ( roleType . equals ( RoleType . NONE ) ) { getResponse ( ) . setStatus ( Status . SUCCESS_OK ) ; } else { getResponse ( ) . setStatus ( Status . SUCCESS_CREATED ) ; } SystemRole role = roleManager . getUserRole ( name ) ; getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( "[" + role . toJSON ( system ) + "]" ) ) ; } catch ( IllegalArgumentException iae ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid role value. Valid values are: " + RoleType . supportedValuesAsString ( ) , new SystemArgumentException ( ) ) ; } } catch ( ResourceException e ) { throw e ; } catch ( SystemException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , e . getMessage ( ) , e ) ; } catch ( Exception e ) { log . error ( "Unexpected error updating user permission" , e ) ; throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "Error occurred updating user role." , e ) ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have the necessary role to modify this system" , new PermissionException ( ) ) ; } } catch ( ResourceException e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; } }
Post action for adding (and overwriting) roles on a system object
Post action for adding ( and overwriting ) roles on a system object
// parse the form to get the job specs // validate the user they are giving roles to exists // if the role is null or empty, the role // will be removed //		catch (Exception e) //		{ //			getResponse().setEntity( //					new IplantErrorRepresentation("Failed to update system roles: " + e.getMessage())); //			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); //		}