agave-systems/systems-api/src/main/java/org/iplantc/service/systems/resources/SystemRoleResource.java
/**
	 * Implements HTTP GET method to list system roles
	 */
Implements HTTP GET method to list system roles
Implements HTTP GET method to list system roles
@Override
	public Representation represent(Variant variant) throws ResourceException
	{
		this.username = getAuthenticatedUsername();
		AgaveLogServiceClient.log(AgaveLogServiceClient.ServiceKeys.SYSTEMS02.name(), 
				AgaveLogServiceClient.ActivityKeys.SystemListRoles.name(), 
				username, "", getRequest().getClientInfo().getUpstreamAddress());
		if (!ServiceUtils.isValid(systemId))
		{
			getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
			return new IplantErrorRepresentation("Invalid system id. " +
							"Please specify an system using its system id. ");
		} 
		RemoteSystem system = null;
		try
		{
			system = dao.findActiveAndInactiveSystemBySystemId(systemId);
			if (system == null)
			{
				throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
						"No shared system found matching " + systemId);
			}
			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.");
			}
			SystemManager systemManager = new SystemManager();
			if (systemManager.isManageableByUser(system, username))
			{
				String jsonPermissions = "";
				if (StringUtils.isEmpty(sharedUsername) ) 
				{
					SystemRoleManager systemRoleManager = new SystemRoleManager(system);
					for (SystemRole role: systemRoleManager.getRoles(limit, offset))
					{
						jsonPermissions += "," + role.toJSON(system);
					}
					jsonPermissions = "[" + jsonPermissions.substring(1) + "]";
				} 
				else 
				{
					SystemRoleManager systemRoleManager = new SystemRoleManager(system);
					try 
					{
						SystemRole role = systemRoleManager.getUserRole(sharedUsername);
						AgaveProfileServiceClient authClient = new AgaveProfileServiceClient(Settings.IPLANT_PROFILE_SERVICE, Settings.IRODS_USERNAME, Settings.IRODS_PASSWORD);
						if (authClient.getUser(sharedUsername) == null) 
						{
							throw new FileNotFoundException();
						} 
						else 
						{
							if (role == null || !role.canRead()) {
								if (ServiceUtils.isAdmin(sharedUsername)) {
									role = new SystemRole(sharedUsername, RoleType.ADMIN);
								} else {
									throw new FileNotFoundException();
								}
							}
							jsonPermissions = role.toJSON(system);
						}
					} 
					catch (FileNotFoundException|ResourceException e) {
						throw e;
					}
					catch (SystemRoleException e) {
						throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
								"Unable to fetch system roles for " + sharedUsername, e);
					}
					catch (Exception e) {
						throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
								"No roles found for user " + sharedUsername, e);
					}
				}
				return new IplantSuccessRepresentation(jsonPermissions);
			}
			else if (system.isPubliclyAvailable()) 
			{
				if (StringUtils.isEmpty(sharedUsername) || StringUtils.equals(sharedUsername, username)) {
					SystemRole role = system.getUserRole(username);
					return new IplantSuccessRepresentation("[" + role.toJSON(system) + "]");
				} else {
					throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
							"User does not have the necessary role to view other user roles on this system.");
				}
			}
			else if (system.getUserRole(username).canRead()) {
			    if (StringUtils.isEmpty(sharedUsername) || StringUtils.equals(sharedUsername, username)) {
                    SystemRole role = system.getUserRole(username);
                    return new IplantSuccessRepresentation("[" + role.toJSON(system) + "]");
                } else {
                    throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
                            "User does not have the necessary role to view other user roles on this system.");
                }
			}
			else
			{
				throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
						"User does not have the necessary role to view this system.");
			}
		}
		catch (FileNotFoundException e) {
			throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
					"No roles found for user " + sharedUsername, e);
		}
		catch (ResourceException e) 
		{
			if (StringUtils.isEmpty(sharedUsername)) {
				log.error("Failed to fetch roles for system " + systemId, e);
			}
			else {
				log.error("Failed to fetch roles for user " 
						+ sharedUsername + " on system " + systemId, e);
			}
			getResponse().setStatus(e.getStatus());
			return new IplantErrorRepresentation(e.getMessage());
		}
		catch (Exception e)
		{
			if (StringUtils.isEmpty(sharedUsername)) {
				log.error("Unexpected exception attempting to fetch roles for system " + systemId, e);
			}
			else {
				log.error("Failed to fetch roles for user " 
						+ sharedUsername + " on system " + systemId, e);
			}
			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
			return new IplantErrorRepresentation("Failed to retrieve system roles: " + e.getMessage());
		}
	}
@ Override public Representation represent ( Variant variant ) throws ResourceException { this . username = getAuthenticatedUsername ( ) ; AgaveLogServiceClient . log ( AgaveLogServiceClient . ServiceKeys . SYSTEMS02 . name ( ) , AgaveLogServiceClient . ActivityKeys . SystemListRoles . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; if ( ! ServiceUtils . isValid ( systemId ) ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; return new IplantErrorRepresentation ( "Invalid system id. " + "Please specify an system using its system id. " ) ; } RemoteSystem system = null ; try { system = dao . findActiveAndInactiveSystemBySystemId ( systemId ) ; if ( system == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No shared system found matching " + systemId ) ; } 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." ) ; } SystemManager systemManager = new SystemManager ( ) ; if ( systemManager . isManageableByUser ( system , username ) ) { String jsonPermissions = "" ; if ( StringUtils . isEmpty ( sharedUsername ) ) { SystemRoleManager systemRoleManager = new SystemRoleManager ( system ) ; for ( SystemRole role : systemRoleManager . getRoles ( limit , offset ) ) { jsonPermissions += "," + role . toJSON ( system ) ; } jsonPermissions = "[" + jsonPermissions . substring ( 1 ) + "]" ; } else { SystemRoleManager systemRoleManager = new SystemRoleManager ( system ) ; try { SystemRole role = systemRoleManager . getUserRole ( sharedUsername ) ; AgaveProfileServiceClient authClient = new AgaveProfileServiceClient ( Settings . IPLANT_PROFILE_SERVICE , Settings . IRODS_USERNAME , Settings . IRODS_PASSWORD ) ; if ( authClient . getUser ( sharedUsername ) == null ) { throw new FileNotFoundException ( ) ; } else { if ( role == null || ! role . canRead ( ) ) { if ( ServiceUtils . isAdmin ( sharedUsername ) ) { role = new SystemRole ( sharedUsername , RoleType . ADMIN ) ; } else { throw new FileNotFoundException ( ) ; } } jsonPermissions = role . toJSON ( system ) ; } } catch ( FileNotFoundException | ResourceException e ) { throw e ; } catch ( SystemRoleException e ) { throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "Unable to fetch system roles for " + sharedUsername , e ) ; } catch ( Exception e ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No roles found for user " + sharedUsername , e ) ; } } return new IplantSuccessRepresentation ( jsonPermissions ) ; } else if ( system . isPubliclyAvailable ( ) ) { if ( StringUtils . isEmpty ( sharedUsername ) || StringUtils . equals ( sharedUsername , username ) ) { SystemRole role = system . getUserRole ( username ) ; return new IplantSuccessRepresentation ( "[" + role . toJSON ( system ) + "]" ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have the necessary role to view other user roles on this system." ) ; } } else if ( system . getUserRole ( username ) . canRead ( ) ) { if ( StringUtils . isEmpty ( sharedUsername ) || StringUtils . equals ( sharedUsername , username ) ) { SystemRole role = system . getUserRole ( username ) ; return new IplantSuccessRepresentation ( "[" + role . toJSON ( system ) + "]" ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have the necessary role to view other user roles on this system." ) ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have the necessary role to view this system." ) ; } } catch ( FileNotFoundException e ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No roles found for user " + sharedUsername , e ) ; } catch ( ResourceException e ) { if ( StringUtils . isEmpty ( sharedUsername ) ) { log . error ( "Failed to fetch roles for system " + systemId , e ) ; } else { log . error ( "Failed to fetch roles for user " + sharedUsername + " on system " + systemId , e ) ; } getResponse ( ) . setStatus ( e . getStatus ( ) ) ; return new IplantErrorRepresentation ( e . getMessage ( ) ) ; } catch ( Exception e ) { if ( StringUtils . isEmpty ( sharedUsername ) ) { log . error ( "Unexpected exception attempting to fetch roles for system " + systemId , e ) ; } else { log . error ( "Failed to fetch roles for user " + sharedUsername + " on system " + systemId , e ) ; } getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; return new IplantErrorRepresentation ( "Failed to retrieve system roles: " + e . getMessage ( ) ) ; } }
Implements HTTP GET method to list system roles
Implements HTTP GET method to list system roles
// check validate username // we back off this for now to prevent people using this as a lookup service