Authentication is a standard in the portlet specification. Accordingly, migrating is very simple if the standard APIs are used. In other cases, use the extended features provided by the products. Each Portal Product provides its own extensions. In this recipe, you will see an example of the migration of a portlet written with Liferay.
This is the Liferay portlet code to to be migrated:
try { User currentUser = PortalUtil.getUser(request); } catch (PortalException e) { // something went wrong, we should handle it } catch (SystemException e) { // something went terribly wrong, we should handle it } // the roles List<Role> roles = currentUser.getRoles(); // other users List<User> liferayUsers = UserLocalServiceUtil.getUserByEmailAddress(currentUser.getCompanyId(), "[email protected]"); // ... rest of the code
This portlet takes the information of the current user through a utility class, PortalUtil
.
It gets a User object and with it takes the information of the roles for this user.
It then requests information from the other available users of the same group. Let's see how to migrate this code.
This is the new code in GateIn:
... import org.exoplatform.services.organization.UserHandler; import org.exoplatform.services.organization.GroupHandler; import org.exoplatform.services.organization.OrganizationService; import org.exoplatform.services.organization.Query; import org.exoplatform.services.organization.User; ... // Get the Organization Service PortalRequestContext context = (PortalRequestContext) WebuiRequestContextget.CurrentInstance(); ExoContainer pcontainer = context.getApplication().getApplicationServiceContainer(); OrganizationService securityService = (OrganizationService) pcontainer.getComponentInstanceOfType( OrganizationService.class); try { // Get the handlers for users and groups by the Organization Service UserHandler userHandler = securityService.getUserHandler(); GroupHandler groupHandler = securityService.getGroupHandler(); // Get the current user String currentUser = request.getRemoteUser(); User user = userHandler.findUserByName(currentUser); // Get the roles for the current user Collection roles = groupHandler.findGroupsOfUser(currentUser); System.out.println(roles); // Get all users with the same email Query query = new Query(); query.setEmail(user.getEmail()); ListAccess<User> users = userHandler.findUsersByQuery(query); System.out.println(users); } catch (Exception e) { e.printStackTrace(); }
We have used the Organization Service, shown in Chapter 7, Developing Using Components API. The JSR 289 specifications define two methods to retrieve information about the current user through the javax.portlet.PortletRequest
interface. Of course, they offer limited functionality compared to the features of the Organization Service, but it's good idea to keep them in mind.
request.getRemoteUser()
The preceding line of code returns the ID of the current user as a String. It represents the
userName
attribute of the org.exoplatform.services.organization.User
object.
request.isUserInRole(String group)
The preceding line of code returns a Boolean verifying if the current user is in the requested group. The parameter is the group ID and it represents the
groupName
attribute of the org.exoplatform.services.organization.Group
object.
The Query object is an implementation by eXo. By default, it can interrogate only the five main fields of the User: user name, first name, last name, e-mail, and the last login date.
If you need to interrogate other fields, customization is needed. The following paragraph shows how to interrogate a different field.
Add these updates to your current project :
org.exoplatform.services.organization.Query
class:package my.extension;
import org.exoplatform.services.organization.Query;
public class MyQuery extends Query {
public MyQuery(String organization_) {
this.organization_ = organization_;
}
private String organization_;
public String getOrganization() {
return organization_;
}
public void setOrganization(String s) {
this.organization_ = s;
}
}
This class will interrogate the organizationId
of the user.
org.exoplatform.services.organization.OrganizationConfig.User
class adding the organizationId
field:package my.extension;
import org.exoplatform.services.organization.OrganizationConfig.User;
public class MyUser extends User {
private String organizationId;
public MyUser()
{
}
public String getOrganizationId()
{
return organizationId;
}
public void setOrganizationId(String organizationId)
{
this.organizationId = organizationId;
}
}
organization-configuration.xml
in the portal/WEB-INF/conf/organization
folder by adding the new implementation and the new organizationId
field:... <field name="user"> <collection type="java.util.ArrayList"> <value> <object type="my.extension.MyUser"> <field name="userName"> <string>root</string></field> <field name="password"> <string>gtn</string></field> <field name="firstName"> <string>Root</string></field> <field name="lastName"> <string>Root</string></field> <field name="email"> <string>root@localhost</string></field> <field name="organizationId"> <string>my organization</string></field> ...
UserHandler
. Here is an example:package my.extension; import org.exoplatform.commons.utils.ListAccess; import org.exoplatform.services.organization.Query; import org.exoplatform.services.organization.User; import org.exoplatform.services.organization.idm.IDMUserListAccess; import org.exoplatform.services.organization.idm.PicketLinkIDMOrganizationServiceImpl; import org.exoplatform.services.organization.idm.PicketLinkIDMService; import org.exoplatform.services.organization.idm.Tools; import org.exoplatform.services.organization.idm.UserDAOImpl; import org.gatein.common.logging.LogLevel; import org.gatein.common.logging.Logger; import org.gatein.common.logging.LoggerFactory; import org.picketlink.idm.api.query.UserQueryBuilder; public class MyUserDAOImpl extends UserDAOImpl { private static Logger log = LoggerFactory.getLogger(MyUserDAOImpl.class); private final PicketLinkIDMService service_; public MyUserDAOImpl( PicketLinkIDMOrganizationServiceImpl orgService, PicketLinkIDMService idmService) throws Exception { super(orgService, idmService); this.service_ = idmService; } @Override public ListAccess<User> findUsersByQuery(Query q) throws Exception { if (log.isTraceEnabled()) { Tools.logMethodIn(log, LogLevel.TRACE, "findUsersByQuery", new Object[] { "q", q }); } IDMUserListAccess list; getOrgService().flush(); UserQueryBuilder qb = service_.getIdentitySession() .createUserQueryBuilder(); MyQuery myq = (MyQuery)q; if (myq.getOrganization() != null) { qb.attributeValuesFilter( MyUserDAOImpl.USER_ORGANIZATION_ID, new String[] { myq.getOrganization() } ); } list = new IDMUserListAccess(qb, 20, false); return list; } }
package my.extension;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.services.organization.idm.PicketLinkIDMOrganizationServiceImpl;
import org.exoplatform.services.organization.idm.PicketLinkIDMService;
public class MyOrganizationService extends PicketLinkIDMOrganizationServiceImpl {
public MyOrganizationService(InitParams params,
PicketLinkIDMService idmService) throws Exception {
super(params, idmService);
userDAO_ = new MyUserDAOImpl(this, idmService);
}
}
OrganizationService
configuration in the organization-configuration.xml
file:...
<component>
<key>
org.exoplatform.services.organization.OrganizationService
</key>
<type>my.extension.MyOrganizationService<type>
...
...
// Get all users with the same email
MyQuery query = new MyQuery("my organization id");
ListAccess<User> users =
userHandler.findUsersByQuery(query);
System.out.println(users);
This will return the root user.
13.59.107.152