How can I get user information like user name, group, role ..,  from
portlet code level ?
Do I have to add JetSpeed2 proprietary libraries ? Which ones ? Can I
do it without them ?

The username is available via getUserPrincipal() call Randy described.

String username = request.getUserPrincipal().getName();

If you are interested in getting the user's attributes such as first or last name, email address, etc, then there is a standard way for portlets to access a map with user information attributes via the request attribute:

static final java.lang.String USER_INFO ("javax.portlet.userinfo")

The user information is returned as a Map object.
The portlet must define the user information attribute it is interested in inside the user-attribute section of the deployment descriptor. If an attribute is not supported by the current runtime system it will not show up in the user attribute map. If the user-attribute is supported by the runtime system, but not defined for a particular user, then for that user the attribute exists in the returned map and the attribute has a null value.

If the user-attribute is not defined for the current user it will not show up in the Map.

Example:
Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
String givenName = (userInfo!=null) ? (String) userInfo.get(PortletRequest.P3PUserInfos.USER_NAME_GIVEN) : “”; String lastName = (userInfo!=null) ? (String) userInfo.get(PortletRequest.P3PUserInfos.USER_NAME_FAMILY) : “”;

Here is how to define the user attributes that your application will require in your portlet.xml:
<portlet-app>
  …
  <user-attribute>
    <description>User Given Name</description>
    <name>user.name.given</name>
  </user-attribute>
  <user-attribute>
    <description>User Last Name</description>
    <name>user.name.family</name>
  </user-attribute>
  <user-attribute>
    <description>User eMail</description>
    <name>user.home-info.online.email</name>
  </user-attribute>
  <user-attribute>
    <description>Company Organization</description>
    <name>user.business-info.postal.organization</name>
  </user-attribute>
  …
<portlet-app>

There is no standard way to get all roles or groups for a user, however you can check for a user in a role with:

if (request.isUserInRole("someRole"))

> Do I have to add JetSpeed2 proprietary libraries

Well, i wouldn't exactly call the libraries proprietary, at least the Jetspeed APIs are 'open' as in open source anyway im not going to mince works, the answer is yes, you can retrieve everything you require via the Jetspeed API. Best to look at the tutorial for examples:

http://portals.apache.org/jetspeed-2/tutorial/04/jetspeed-service.html






---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscr...@portals.apache.org
For additional commands, e-mail: jetspeed-user-h...@portals.apache.org

Reply via email to