Conversion of my alfresco javascript file into jav

2019-09-16 11:50发布

I need to convert this javascript file into java code.Please help

if (document.isContainer && document.displayPath == "/Company Home/User Homes") {
    var owner = document.properties["cm:owner"];
    var pNode = people.getPerson(owner);
    if (pNode!=null && pNode.exists()){

        var userName = pNode.properties.userName;
        var email = pNode.properties.email;
        var randPassword = Math.random().toString(36).substr(2, 30)+"-"+(Date.now());

        people.setPassword(userName, randPassword);
        logger.debug("Invitation mail: User "+userName+" password has been changed.");

        var mail = actions.create("mail");
        //mail.parameters.from = "noreply@customdomain";
        mail.parameters.to = email; 
        mail.parameters.subject = "Welcome to the Site, login: "+userName+", password: "+randPassword;
        mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Invite Email Templates/invite_user_email.ftl");
        var templateModel = new Array();
        templateModel['newPassword'] = randPassword; // use ${newPassword} expression inside template
        mail.parameters.template_model = templateModel;
        mail.executeAsynchronously(document);
        logger.debug("Invitation mail has been sent to "+email);
    } else {
        logger.warn("Invitation mail: User not found: "+owner);
    }
} else {
    logger.warn("Invitation mail: Document "+document.name+" / "+document.nodeRef+" is not a user home folder.");
} 

1条回答
姐就是有狂的资本
2楼-- · 2019-09-16 12:13

Hope this should help you.

public void createUser()
{
    final String randPassword = getRandonPassword();
    final String userName= "someuser";
    final String email = "someuser@example.com";
    authenticationService.setAuthentication(userName, randPassword.toCharArray());
    System.out.println(randPassword);

    AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>()
    {
        public Void doWork() throws Exception
        {
            Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
            properties.put(ContentModel.PROP_USERNAME,userName);
            properties.put(ContentModel.PROP_PASSWORD,randPassword);
            properties.put(ContentModel.PROP_EMAIL,email);
            NodeRef personNodeRef = personService.createPerson(properties);
            personService.notifyPerson(userName, randPassword);
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());        

}

private String getRandonPassword()
{
    Calendar calendar = Calendar.getInstance();
    SecureRandom random = new SecureRandom();
    String randomPassword = new BigInteger(130, random).toString(32);
    randomPassword  = randomPassword +"-" + calendar.getTimeInMillis();
    return randomPassword ;        
}
查看更多
登录 后发表回答