Powered By Blogger

Tuesday, May 24, 2016

API to retrieve alfresco user password in multi tenant environment


import java.util.HashMap;
import java.util.Map;

import net.sf.acegisecurity.UserDetails;

import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;

/**
 * This class will be used for getting the alfresco password
 * @author bapatil
 *
 */
public class GetAlfrescoPasswordWebscript extends DeclarativeWebScript {
    private static Log LOGGER = LogFactory.getLog(GetAlfrescoPasswordWebscript.class); 
    MutableAuthenticationDao authenticationDao;   
    
    protected Map executeImpl(final WebScriptRequest req, Status status, Cache cache){
    Map model = new HashMap();
   
    try{
        
       final String userName = req.getParameter("name");
            LOGGER.debug("Get the alfresco password for the user :"+ userName); 
             String currentDomain = userName.substring(userName.lastIndexOf("@")+1,userName.length());
             LOGGER.debug("Domain name of the user is :"+ currentDomain); 
                 
                  String password = TenantUtil.runAsUserTenant(new TenantRunAsWork(){
                       @Override
                       public String doWork() throws Exception{
                           String password;
                           try{ 
                               LOGGER.debug(" userName used to get password is :" + userName);        
                               UserDetails userdetails  = authenticationDao.loadUserByUsername(userName);
                               //LOGGER.debug("userNodeRef of the user" + userdetails.getUsername());
                               password = userdetails.getPassword();
                               LOGGER.debug("password retrieved : " + password);
                              
                           }finally{
                            
                           }
                           return password;
                       }
                   }, "admin", currentDomain);
               
                  LOGGER.debug("password : " + password);
                  model.put("password", password);
                  model.put("message", "success");
                  
       
       // Below are the different attempts, keeping it commented just in case if we need for reference
    //AuthenticationUtil.setRunAsUser("admin");
    //authenticationComponent.setSystemUserAsCurrentUser("clienta.com");
    //authenticationComponent.setCurrentUser("admin@clienta.com");
   
   
    /*LOGGER.debug("Get the alfresco password"); 
            String userName = req.getParameter("name");
            LOGGER.debug("Password for the userName is :" + userName);        
            UserDetails userdetails  = authenticationDao.loadUserByUsername(userName);
            LOGGER.debug("userNodeRef of the user" + userdetails.getUsername());
            String password = userdetails.getPassword();*/
            
   
    /*String updatedpassword =  (String)AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork() {

@Override
public Object doWork() throws Exception {
// TODO Auto-generated method stub  
LOGGER.debug("Get the alfresco password"); 
           String userName = req.getParameter("name");
           LOGGER.debug("Password for the userName is :" + userName);        
           UserDetails userdetails  = authenticationDao.loadUserByUsername(userName);
           LOGGER.debug("userNodeRef of the user" + userdetails.getUsername());
           return userdetails.getPassword();            
}
},"admin@clienta.com");*/
               
           // String password = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(userNodeRef, ContentModel.PROP_PASSWORD));
   
   
    }catch(Exception e){
    LOGGER.error("Error in getting the alfresco password for the user" , e);
    model.put("message", e.getMessage());
    model.put("password", "");
    }
        
        return model;
    }

   


public MutableAuthenticationDao getAuthenticationDao() {
return authenticationDao;
}

public void setAuthenticationDao(MutableAuthenticationDao authenticationDao) {
this.authenticationDao = authenticationDao;
}

   

}
 
   Get alfresco password
   Get alfresco password for the user
   /com/patil/getalfpassword  
   user
   any

No comments:

Post a Comment