Groovy 将所有用户更改为大写的脚本

Groovy script to change all users to uppercase

我正在尝试将 magnolia 应用程序中的所有用户名更改为大写,因为我们的登录存在区分大小写的问题。

我写了这个 groovy 脚本,遵循一个用于将密码重置为“”的示例,以捕获用户并将其更改为大写,但似乎未设置名称 属性。

https://documentation.magnolia-cms.com/display/WIKI/Reset+all+passwords

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.getNode("/public"), new NodeTypePredicate(NodeTypes.User.NAME))

users.each() {
    changedName = it.name.toUpperCase();
    it.setProperty("name", changedName)
    it.save();
    println "1 " + changedName;
    println "2 " + it.name;
}
session.save();

当我检查 it.name 时,return 它们是如何存储在 mangolia 中的,而不是全部大写,并且在查看用户名。

IIRC 有 3 处需要更改。
name 是其中之一,然后是 jcrName 属性 然后您需要更改节点本身的名称。至少如果您想以这种方式在安全应用程序中看到它。
对于登录本身,你所做的应该已经足够了。

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.getNode("/admin"), new NodeTypePredicate(NodeTypes.User.NAME))

users.each() {
    name = it.name
    changedName = it.name.toUpperCase();
    it.setProperty("name", changedName)
    it.setProperty("jcrName", changedName)
    it.save()

    NodeUtil.renameNode(it, changedName)
    it.getNode("acl_users").getNodes().each { node ->
        newPath = node.getProperty("path").getString().replace(name, changedName)
        node.setProperty("path", newPath)
        node.save()
    }
}
session.save()

嘿,也许这就是你要找的。您需要在我的版本中更改节点名称和 jcrName 我遍历 acl_users 节点并更改每个节点的路径。希望这对你有用。

尝试使用PropertyUtil的setProperty方法。 您必须提取所需的所有节点,然后循环遍历它们。假设变量 node 是您要更改名称的节点,请执行以下操作:

String newName = StringUtils.upperCase(PropertyUtil.getString(node, "jcrName"));
PropertyUtil.setProperty(node, "jcrName", newName);

jcrName 是您需要覆盖的 属性。将代码包装到 try/catch 块中,我们开始吧。

希望对您有所帮助。