如何在 OrientDB 2.0-rc2 中可编程更改密码?

How the change programmably the password in OrientDB 2.0-rc2?

这适用于以前的(测试版)OrientDB 版本,但不再适用。那么正确的做法是什么?

用例:创建新的(文档)数据库时,我想更改 admin 用户的密码。代码:

ODatabaseDocumentTx oDb = ...
if (!oDb.exists()) {
        // this will create AND open the database with credentials "admin"/"admin"
        oDb.create();
        // the "admin" user MUST exists, otherwise it is recreated / "repaired" during login, see OSecurityShared. 
        // So we always use 'admin' as name and only change the password
        if ("admin".equals(userName)) {
            OSecurityUser admin = oDb.getUser();
            admin.setPassword(password);
....

这将抛出一个 java.lang.UnsupportedOperationException

java.lang.UnsupportedOperationException: null
at
com.orientechnologies.orient.core.metadata.security.OImmutableUser.setPassword(OImmutableUser.java:132)

引入了不可变用户以改进多线程。您随时可以获取底层文档。

试试这个应该有用

ODocument admin = db.getUser().getDocument();
admin.field("password","newpassword");
admin.save();

这似乎是另一种解决方案:

// change username and password
ODatabaseDocumentTx database = orientGraphFactory.getDatabase();
OCommandSQL cmd = new OCommandSQL("UPDATE ouser "
  + "SET name = '" + "newName" + "', "
  + "password = '" + "newPassword" + "'"
  + " WHERE name = 'admin'");
database.command(cmd).execute();
database.close();

它触发编码密码的正确数据库挂钩。