Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: from DEV-1058

Here's a Groovy script we used to reset all the passwords in our database to "". We used this when we went from Magnolia's native authentication to using LDAP.

Code Block
titleborderStylesetPasswordToNull.jspsolid
borderStylelinenumberssolidtrue
import info.magnolia.jcr.util.NodeUtil
import info.magnolia.jcr.predicate.NodeTypePredicate
import info.magnolia.jcr.util.NodeTypes
session = ctx.getJCRSession("users")
users = NodeUtil.collectAllChildren(session.rootNode, new NodeTypePredicate(NodeTypes.User.NAME))
int count = 0
users.each() {
    if (it.name != "anonymous" && it.name != "superuser" ) {
        it.setProperty("pswd", "")
        it.save()
<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
	xmlns:cms="urn:jsptld:cms-taglib"
	xmlns:cmsu="urn:jsptld:cms-util-taglib"
	xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core">

	<jsp:directive.page contentType="text/html; charset=UTF-8"
		session="false" />
	<jsp:output doctype-root-element="html"
		doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />

	<html>
	<head>
	<title>Set User Password To Null</title>
	</head>
	<body>
	<jsp:directive.page import="info.magnolia.cms.core.Content" />
	<jsp:directive.page
		import="info.magnolia.cms.beans.config.ContentRepository" />
	<jsp:directive.page import="info.magnolia.cms.core.HierarchyManager" />
	<jsp:directive.page import="info.magnolia.context.MgnlContext" />
	<jsp:directive.page import="java.util.Iterator" />
	<jsp:directive.page import="info.magnolia.cms.core.NodeData" />
	<jsp:directive.page import="info.magnolia.cms.core.ItemType" />
	<jsp:directive.page import="javax.jcr.PathNotFoundException" />
	<jsp:directive.page import="javax.jcr.RepositoryException" />

	<jsp:declaration>
		<![CDATA[	
		public void processUsers( Content c, HttpServletResponse response )  {	
		   try {
		        int counter = 0;
		        Iterator users = c.getChildren(ItemType.USER).iterator();
            	System.out.println("Setting all passwords to null except superuser and anonymous");
             	while (users.hasNext()) {
                	 Content user = (Content) users.next();
                 	 try {
                     	 // skip anonymous user
                      	 if (user.getName().equalsIgnoreCase("anonymous")) continue;
                       	 // skip superuser user
                      	 if (user.getName().equalsIgnoreCase("superuser")) continue;
                         // else set password to an empty string
                         NodeData pswdNodeData = user.getNodeData("pswd"); 
                         //Add NodeData check in case that user does not have any pswd attribute 
                         //in magnolia repository
                         if (pswdNodeData.isExist()) {
	                     pswdNodeData.setValue("");
                         }
                         response.getWriter().println("Password for " + user.getName()+ " was set to null.</br>");
                         countercount++;
                     } 
                     catch (RepositoryException re) {
                         System.out.println("Failed to set null 
}
println("Reset password for " + user.getName());
                     }
               }
               c.save();
               response.getWriter().println("<h2>Total " + counter + " users' passwords were set to null.</h2>");
               response.flushBuffer();
               System.out.println("Successfully updated users repository");
           } 
           catch (PathNotFoundException pe) {
               pe.printStackTrace();
           }  
           catch (Throwable t) {
               t.printStackTrace();
           }

		}
				
		]]>
	</jsp:declaration>

	<jsp:scriptlet>
		<![CDATA[
	
		HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.USERS);
		Content rootNode = hm.getContent("/");
		
		response.getWriter().println("<h1>Processing user profile ...</h1>");
		processUsers( rootNode, response);
		
		response.getWriter().println("count + " users.")
println("Passwords for user \"anonymous\" and \"superuser\" remain unchanged.</br>");
		response.getWriter().println("</br>**** USER PASSWORD SET TO NULL COMPLETE ****");	
		
		]]>
	</jsp:scriptlet>
	</body>
	</html>