cancel
Showing results for 
Search instead for 
Did you mean: 

How can i set a property for a user in webscript?

itsjjonnet
Champ in-the-making
Champ in-the-making
Hi,

I want to know how can i search for a user in alfresco repository  & set some property for him once user if found in repository? :?:
4 REPLIES 4

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

Try this :

Creation of webscript : http://wiki.alfresco.com/wiki/Web_Scripts_Examples

Samples of search, modify  : http://wiki.alfresco.com/wiki/JavaScript_API_Cookbook

To search, use :

// execute a lucene search across the repo for the text 'alfresco'
   var docs = search.luceneSearch("TEXT:alfresco");
And change the "Text:alfresco" by :
Search the creator of the document : http://forums.alfresco.com/en/viewtopic.php?f=12&t=24638#p80329 (perhaps use the modifier)

Good luck

dynamolalit
Champ on-the-rise
Champ on-the-rise
Hi itsjjonnet ,

You can try out by searching for user as per user name in a  web script as below:

var repUser =search.query({query:  "cm:name:"+usrName+""});
or
var tempUser = search.luceneSearch("{http://www.alfresco.org/model/content/1.0}name" , usrName);

To set a property, you can try out as below:


var user =  people.getPerson(usrName);
if(user == null)
{
   logger.log("User is null while setting location : "+usrName);
}else{
   var userLoc = user.properties["cm:location"];
   logger.log("Current User Location : "+userLoc);
   if(userLoc == null || userLoc == ''){
           user.properties["cm:location"] = "Some Location String";
           user.save();
    }      
}   

It will set location for a particular user.Same way you can try out.

itsjjonnet
Champ in-the-making
Champ in-the-making
Thanks,

It was of good help!

dynamolalit
Champ on-the-rise
Champ on-the-rise
Hi All,

I have implemented a java backed web script which will update the user profile properties in Alfresco as per in sync with Liferay.It invokes a Liferay Web service & get list of users from Liferay to update these users profile in Alfresco repository.It updates following properties:

ContentModel.PROP_ORGANIZATION
ContentModel.PROP_JOBTITLE
ContentModel.PROP_LOCATION



package com.xxxx.km.alfresco.user.profile;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.alfresco.service.namespace.QName;

import com.liferay.portal.service.LiferayUserBeanVO;

/**
* @author Lalit Jangra
* Web Script to set user profile details got from Liferay.
* It gets list of users from Lliferay with their profile details.
* Once got, it will check for each of such user for any updated profile details.
* If any updation is found, it will update the same in Alfresco repository.
* It will update following User Profile fields in Alfresco:
* Location, User Organization or department & User Job Title or designation.
* Folloiwng fields are updated by LDAP so can not be changed in Alfresco:
* User Login or Screen Name, Email Id, First Name, Last Name, & Company Id.
*/
public class UpdateUserProfileDetailsWebScript extends AbstractWebScript{

   Logger logger = Logger.getLogger(UpdateUserProfileDetailsWebScript.class);
   private LiferayUserBeanVO[] listOfUsers;
   private PersonService personService;
   private NodeService nodeService;
   private ArrayList<String> usersNotFound;
   
   /**
    * Method to update user profile details as with that of Liferay.
    * It invokes LiferayWebServiceInvoker class to get List of users who have logged in since last 7 days.
    * Once got, it checks if the users in this list exists in ALfresco Repository.If yes, it gets each such user properties
    * in userPropertyMap & iterate over this map to get userName for that particular user.After that , it matches user in list with
    * that of Alfresco repository.If matched, it will get User's Organization, Designation & Location and compare it with LiferayUserBeanVO.
    * If it is same, it will escape that property else it will set the updated property value in userPropertyMap and update
    * the userPropertyMap for that user in Alfresco repository usinig NodeService.At the end, it will show if there are any users
    * which do not exist in repository among list of users got from Liferay.
    */
   @Override
   public void execute(WebScriptRequest request, WebScriptResponse response) throws IOException {
      logger.debug("Inside execute() of UpdateUserProfileDetailsWebScript ");      
      //String noOfDaysPassed = request.getParameter("noOfDays");
      //int noOfDays = new Integer(noOfDaysPassed).intValue();
      int noOfDays = 7;
      //Getting list of users from Liferay.
      listOfUsers = LiferayWebServiceInvoker.getListOfUsers(noOfDays);
      if(listOfUsers != null && listOfUsers.length > 0){
         for(LiferayUserBeanVO userBeanVO : listOfUsers){
            //Getting User Login Name.
            String userLoginName = userBeanVO.getScreenName();
            logger.debug("User userLoginName : "+userLoginName);
            //Checking if user exists.
            boolean validUserFlag = getPersonService().personExists(userLoginName);
            //User exists so getting all properties of user.
            if(validUserFlag ==  true){
               logger.debug("User exists : "+userLoginName);   
               //Getting User NodeRef.
               NodeRef userNodeRef = getPersonService().getPerson(userLoginName);
               //logger.debug("userNodeRef : "+userNodeRef);
               //Getting User Properties in a Map.
               Map<QName, Serializable> userPropertyMap = getNodeService().getProperties(userNodeRef);
               //Iterating over userPropertyMap.
               Iterator<Entry<QName, Serializable>> iterator = userPropertyMap.entrySet().iterator();
               while(iterator.hasNext()){
                  Map.Entry<QName, Serializable> entry = (Entry<QName, Serializable>) iterator.next();
                  //For {http://www.alfresco.org/model/content/1.0}userName.
                  if(entry.getKey().toString().endsWith("userName")){
                     String userName = (String) entry.getValue();
                     logger.debug("userName : "+userName);
                     //Checking if the user from Liferay is same as in Alfresco.
                     //logger.debug("userBeanVO.getScreenName() : "+userBeanVO.getScreenName());
                     if(userName.equalsIgnoreCase(userBeanVO.getScreenName())){
                        logger.debug("User Name matched,Updating User Properties.");
                        //Checking for user organization or department.
                        //{http://www.alfresco.org/model/content/1.0}organization
                        String usrOrgn = (String) userPropertyMap.get(ContentModel.PROP_ORGANIZATION);
                        logger.debug("Current User Organization : "+usrOrgn);
                        String lrUsrOrgn = userBeanVO.getDepartment();
                        logger.debug("From Liferay : "+lrUsrOrgn);
                        if(StringUtils.isBlank(usrOrgn)){
                           if(StringUtils.isNotBlank(lrUsrOrgn) && !(StringUtils.equalsIgnoreCase(usrOrgn, lrUsrOrgn))){
                              userPropertyMap.put(ContentModel.PROP_ORGANIZATION, lrUsrOrgn);
                           }                           
                        }
                        String userDesg = (String) userPropertyMap.get(ContentModel.PROP_JOBTITLE);
                        logger.debug("Current User Designation : "+userDesg);
                        String lrUserDesg = userBeanVO.getDesignation();
                        logger.debug("From Liferay : "+lrUserDesg);
                        if(StringUtils.isBlank(userDesg) ){
                           if(StringUtils.isNotBlank(lrUserDesg) && !(StringUtils.equalsIgnoreCase(userDesg, lrUserDesg))){
                              userPropertyMap.put(ContentModel.PROP_JOBTITLE, lrUserDesg);
                           }
                        }
                        String userLoc = (String) userPropertyMap.get(ContentModel.PROP_LOCATION);
                        logger.debug("Current User Location : "+userLoc);
                        String lrUserLoc =  userBeanVO.getLocation();
                        logger.debug("From Liferay : "+lrUserLoc);
                        if(StringUtils.isBlank(userLoc)){
                           if((StringUtils.isNotBlank(lrUserLoc) && !(StringUtils.equalsIgnoreCase(userLoc, lrUserLoc)))){
                              userPropertyMap.put(ContentModel.PROP_LOCATION, lrUserLoc);
                           }                           
                        }
                     }
                     //Setting updated properties in repository.
                     getPersonService().setPersonProperties(userLoginName, userPropertyMap);
                  }
               }
            }else{
               logger.debug("User does not exist : "+userLoginName);
               usersNotFound.add(userLoginName);
            }                     
         }         
      }   
      //List all those users who are not in repository.
      if(usersNotFound != null && usersNotFound.size() > 0){
         for(String userNotFound : usersNotFound){
            logger.debug("This user is not found in repository : "+userNotFound);
         }
      }   
   }

   public void setPersonService(PersonService personService) {
      this.personService = personService;
   }

   public PersonService getPersonService() {
      return personService;
   }

   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }

   public NodeService getNodeService() {
      return nodeService;
   }
}

Hope this helps!