I created this task for the job. 

[code]
package com.researchresearch.news.webapp;

import info.magnolia.cms.security.Digester;
import info.magnolia.module.InstallContext;
import info.magnolia.module.delta.AbstractTask;
import info.magnolia.module.delta.TaskExecutionException;
import info.magnolia.repository.RepositoryConstants;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

public class ChangePasswordTask extends AbstractTask {

        private String newPassword, expectedPassword, username;
        
        public ChangePasswordTask(String userPath, String newPassword, String 
expectedPassword) {
                super("Change password", "Change password a user's password if 
their current value is the expected one");
                this.username=userPath;
                this.newPassword=newPassword;
                this.expectedPassword=expectedPassword;
        }

        public ChangePasswordTask(String userPath, String newPassword) {
                super("Change password", "Change password a user's password, 
regardless of the current value.");
                this.username=userPath;
                this.newPassword=newPassword;
        }

        @Override
        public void execute(InstallContext installContext)
                        throws TaskExecutionException {
                try {
                        Session session = 
installContext.getJCRSession(RepositoryConstants.USERS);
                        Node node = session.getNode(username);
                Property prop = node.getProperty("pswd");
                final String current = prop.getString();
                
if(expectedPassword==null||Digester.matchBCrypted(expectedPassword, current)) {
                        prop.setValue(Digester.getBCrypt(newPassword));
                        session.save();
                }
                } catch (RepositoryException e) {
                        throw new TaskExecutionException("Exception setting 
password", e);
                }
        }

        
}
[/code]

I use it in a VersionHandler to change the superuser password if it is 
"superuser" thus:

[code]
startupTasks.add(new ChangePasswordTask("/system/superuser", "<new password>", 
"superuser"));
[/code]

but the other constructor allows you to change the password regardless.

HTH

Regards,

Dominic

-- 
Context is everything: 
http://forum.magnolia-cms.com/forum/thread.html?threadId=fa98a037-8034-4b10-bffc-aea1957b1987


----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------

Reply via email to