The class com.sap.cloud.sdk.cloudplatform.security.user.UserAccessor allows to me to retrieve the current user and it's attributes.
For example:
Optional<UserAttribute> optionalfirstName = user.getAttribute("firstname");
UserAttribute ua = optionalfirstName.get();
Once I have retrieved the UserAttribute, it has two properites "Name" and "Value". However there is no method available to get the Value. How can I access the value?
Depending on the SAP CP environment you are using, the UserAttribute is an instance of:
SimpleUserAttribute<String>
on Neo
CollectionUserAttribute<String>
on Cloud Foundry
You can access the respective values by type-casting to the required instance:
if( ua instanceof SimpleUserAttribute ) {
String value = (String) ((SimpleUserAttribute<?>)ua).getValue();
}
else if ( ua instanceof CollectionUserAttribute ) {
Collection<?> values = ((CollectionUserAttribute<?>)ua).getValues();
}
Note: We plan to simplify this in future releases of the SDK so that StringUserAttribute
and StringCollectionUserAttribute
instances are returned for more convenient consumption.