Accessing user attributes using UserAccessor

2019-08-26 17:58发布

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?

标签: s4sdk
1条回答
Emotional °昔
2楼-- · 2019-08-26 18:38

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.

查看更多
登录 后发表回答