Migrating an authenticated portlet

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.

Getting ready

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.

How to do it...

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();
}

How it works...

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.

There's more...

If you need to interrogate other fields, customization is needed. The following paragraph shows how to interrogate a different field.

Interrogating other fields of the user

Add these updates to your current project :

  1. Add a new Query class that extends the 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.

  2. Add a new User class in the configuration of the Organization Service that extends the 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;
        }
        
    }
  3. Modify the User configuration in 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>
    ...
  4. Now you need to extend the 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;
      }
    
    }
  5. Now extend the default Organization Service, the IDM Service:
    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);
    
      }
    }
  6. Update the OrganizationService configuration in the organization-configuration.xml file:
    ...  
    <component>
      <key>
    org.exoplatform.services.organization.OrganizationService
      </key>
      <type>my.extension.MyOrganizationService<type>
    ...
  7. Now in the portlet code for GateIn that you have seen before, simply change the query implementation to give the following:
    ...      
    // 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.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.107.152