PUMA API Scenarios (Portal User Management Architecture)

This article talks more about not so frequently discussed PUMA API related scenarios with samples


1.        Accessing the PUMA from webapp that is deployed on the portal (not part of themes or portlets)
        a.       If user doesn't have the portal security context in request 
        b.      Want to execute the PUMA as system user (where you want to access to all users)

2.        Searching for multiple users by passing the list of uid's without using the wildchars ( search using wildchar may returns thousands of results ) 
       a.       Searching the users based on the multiple attributes 
       b.      Retrieving multiple user objects using 'OR' condition like uid='user1' or uid='user2'
       c.       Retrieving users without using the wildcard chards

3.        Using Paged Search in PUMA
       a.       To handle large result sets returning from the LDAP.

Accessing the PUMA from webapp deployed on the Portal or Making the PUMA api calls without logging into portal.

     /*
      * <p>Execute as authenticated admin user(retrieve users without login to portal)</p>
      * <p> You can return the list of users instead of printing them by just uncommenting couple of statement in this method </p>
      */
     //public List<User> getUsersByLastName(final String lastName) throws Exception{
     public String printUsersByLastName(final String lastName) throws Exception{
                 
          final PumaLocator pumaLocator = pumaHome.getLocator();
          final PumaProfile pumaProfile = pumaHome.getProfile();
         
          final List<String> attribList = new ArrayList<String>();
          attribList.add("uid");
          attribList.add("cn");
          attribList.add("sn");
        
          //final List<User> usersList = new ArrayList<User>();
          final StringBuffer sb = new StringBuffer();
   PrivilegedExceptionAction<Void> privilegedAction = new PrivilegedExceptionAction<Void>() {
  
        @Override
        public Void run() {
          try{
            //search users who have the same last name i.e users with last name as "vaka"
            List<User> users = pumaLocator.findUsersByAttribute("sn", lastName);
            //usersList.addAll(users);
            for(User user : users){
            Map<String, Object> attributesMap = pumaProfile.getAttributes(user, attribList);
            for (Map.Entry<String, Object> entry : attributesMap.entrySet()) {
               sb.append(entry.getKey() + " : " + entry.getValue() +" , \t");
            }
            sb.append("\n").append("</br>");
          }
          }catch(Exception ex){
               System.err.println("Error while gettting the users from the PUMA");
          }
              return null;
          }
         };

         try {
              PumaEnvironment pumaEnv = pumaHome.getEnvironment();
              pumaEnv.runUnrestricted(privilegedAction);
         } catch (PrivilegedActionException ex) {
              System.err.println("error while executing the previleged action");
              ex.printStackTrace();
         }
        
         //return usersList;
         return sb.toString();

     }

Searching for multiple users by passing list of Uid’s (or any attribute) without using the wild chars

/*
 * <p> Execute the FindUsersByQuery with or condition(retrieve users without login to portal)</p>
 * <p> You can return the list of users instead of printing them by just uncommenting couple of statement in this method </p>
 */
 //public List<User> getListOfUsers(List<String> userIds) throws Exception{
 public String printListOfUsers(List<String> userIds) throws Exception{
        
          final PumaLocator pumaLocator = pumaHome.getLocator();
          final PumaProfile pumaProfile = pumaHome.getProfile();
         
          final List<String> attribList = new ArrayList<String>();
          attribList.add("uid");
          attribList.add("cn");
          attribList.add("sn");
        
          //final List<User> usersList = new ArrayList<User>();
          final StringBuffer usersDetailsStr = new StringBuffer();
         
          if(null == userIds || 0 == userIds.size()) return "Empty userIds List passed";
          final StringBuilder userIdsListQuery = new StringBuilder();
          int i = 0;
          if( 1 == userIds.size() ){
                userIdsListQuery.append("(uid='"+userIds.get(0)+"*')");
          } else {
               for (String uid : userIds) {
                    if(i == 0) {
                    userIdsListQuery.append("((uid='"+uid+"*') or ");
                    }else if(i == userIds.size()-1)
                    userIdsListQuery.append("(uid='"+uid+"*'))");
                    else
                    userIdsListQuery.append("(uid='"+uid+"*') or ");
                    ++i;
                }
          }
                
   PrivilegedExceptionAction<Void> privilegedAction = new PrivilegedExceptionAction<Void>() {
     @Override
     public Void run() {
       try{

        List<User> users = pumaLocator.findUsersByQuery(userIdsListQuery.toString());
        //usersList.addAll(users);
        for(User user : users){
           Map<String, Object> attributesMap = pumaProfile.getAttributes(user, attribList);
           for (Map.Entry<String, Object> entry : attributesMap.entrySet()) {
              usersDetailsStr.append(entry.getKey() + " : " + entry.getValue() +" , \t");
           }
           usersDetailsStr.append("\n").append("</br>");
        }
                
      }catch(Exception ex){
         System.err.println("Error while gettting the users from the PUMA");
      }
         return null;
     }
    };

         try {
              PumaEnvironment pumaEnv = pumaHome.getEnvironment();
              pumaEnv.runUnrestricted(privilegedAction);
         } catch (PrivilegedActionException ex) {
              System.err.println("error while executing the previleged action");
              ex.printStackTrace();
         }
        
         //return usersList;
         return usersDetailsStr.toString();
     }

Paged Search in PUMA

    /*
      * Print All users (using the paged search)
      * <p> You can return the list of users instead of printing them by just uncommenting couple of statement in this method </p>
      */
     //public List<User> getAllUsers() throws Exception{
     public String printAllUsers() throws Exception{
                
          final PumaLocator pumaLocator = pumaHome.getLocator();
          final PumaProfile pumaProfile = pumaHome.getProfile();
         
          //Page size is 5
          final Map<String, Object> configMap = new HashMap<String, Object>();
          configMap.put(PumaLocator.RESULTS_PER_PAGE, new Integer(5));
         
          final List<String> attribList = new ArrayList<String>();
          attribList.add("uid");
          attribList.add("cn");
          attribList.add("sn");
        
          //final List<User> usersList = new ArrayList<User>();
          final StringBuffer sb = new StringBuffer();
              
          PrivilegedExceptionAction<Void> privilegedAction = new PrivilegedExceptionAction<Void>() {

  @Override
  public Void run() {
  try{
   //you can use findUsersByQuery or findUsersByAttribute
   //PagingIterator<User> users=pumaLocator.findUsersByQuery("uid='*'", configMap);
   PagingIterator<User> users=pumaLocator.findUsersByAttribute("uid""*", configMap);
   List<User> temp = new ArrayList<User>();
   while(users.hasNextPage()){
      temp = users.getNextPage(temp);
      //usersList.addAll(temp);
      if(null != temp && 0 < temp.size()){
         for (User user : temp) {                                          
 Map<String, Object> attributesMap = pumaProfile.getAttributes(user, attribList);
      for (Map.Entry<String, Object> entry : attributesMap.entrySet()) {
        sb.append(entry.getKey() + " : " + entry.getValue() +" , \t");
      }
      sb.append("\n").append("</br>");
     }
    }
   }                      
                  
  }catch(Exception ex){
     System.err.println("Error while gettting the users from the PUMA");
  }
  return null;
 }
 };

         try {
              PumaEnvironment pumaEnv = pumaHome.getEnvironment();
              pumaEnv.runUnrestricted(privilegedAction);
         } catch (PrivilegedActionException ex) {
              System.err.println("error while executing the previleged action");
              ex.printStackTrace();
         }
        
         //return usersList;
         return sb.toString();

No comments:

Post a Comment