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

   

}

Monday, May 23, 2016

Searching user from userstore

//execute query to find the user node
var luceneQuery = "@usr\\:username:\"johndoe\"";
var userStoreReference = "user://alfrescoUserStore";
var usersResultList = 
search.luceneSearch(userStoreReference, luceneQuery);
//get the user node and change enable property
var userInTheUserStore = usersResultList[0];
userInTheUserStore.properties["usr:enabled"] = false;
userInTheUserStore.save();

Alfresco Password

Password can be retrieved with the below query

SELECT anp1.node_id,
       anp1.qname_id,       
       anp1.string_value       
FROM alf_node_properties anp1  
   INNER JOIN alf_qname aq1 ON aq1.id = anp1.qname_id   
   INNER JOIN alf_node_properties anp2 ON anp2.node_id = anp1.node_id    
   INNER JOIN alf_qname aq2 ON aq2.id = anp2.qname_id                    
WHERE aq1.local_name = 'password'
AND aq2.local_name = 'username'
AND anp2.string_value = 'admin'

Tuesday, May 17, 2016

The data types ntext and varchar are incompatible in the equal to operator

You can't use text columns in an equal operation of a WHERE clause.  If you are using SQL Express or SQL Server 2005, change your column type from text to either varchar(max) or nvarchar(max)