Java OrientDB-如何使用ODocument记录级别安全性
我正在尝试使用OrientDB的文档数据库(Java)API。我喜欢将数据库中某些记录的访问限制为特定角色的想法;但是,我在文档中找不到如何做到这一点。我是OrientDB的新手,所以我肯定我错过了一些东西 这是我到目前为止所拥有的Java OrientDB-如何使用ODocument记录级别安全性,java,orientdb,Java,Orientdb,我正在尝试使用OrientDB的文档数据库(Java)API。我喜欢将数据库中某些记录的访问限制为特定角色的想法;但是,我在文档中找不到如何做到这一点。我是OrientDB的新手,所以我肯定我错过了一些东西 这是我到目前为止所拥有的 // Create database. ODatabaseDocumentTx db = new ODatabaseDocumentTx(path); db.create(); // Create role with no permissions. db.comm
// Create database.
ODatabaseDocumentTx db = new ODatabaseDocumentTx(path);
db.create();
// Create role with no permissions.
db.command(new OCommandSQL("INSERT INTO orole SET name = 'foobar', mode = 0;")).execute();
// Create a user with the new role.
OSecurity sm = db.getMetadata().getSecurity();
OUser user = sm.createUser("user", "user", "foobar");
ORole foobarRole = sm.getRole("foobar");
// Insert 2 records, one restricted, one is not.
OClass restricted = db.getMetadata().getSchema().getClass("ORestricted");
OClass docClass = db.getMetadata().getSchema().getOrCreateClass(TABLE_NAME, restricted);
ODocument doc1 = new ODocument(docClass);
ODocument doc2 = new ODocument(docClass);
// The restricted record...
doc1.field("name", TABLE_NAME);
doc1.field("Id", 1, OType.INTEGER);
doc1.field("Message", "Hello 1", OType.STRING);
doc1.save();
// The unrestricted record...
doc2.field("name", TABLE_NAME);
doc2.field("Id", 2, OType.INTEGER);
doc2.field("Message", "Hello 2", OType.STRING);
doc2.save();
// Allow "user" with "foobar" role to read record doc2.
String sql = String.format(
"UPDATE %s ADD _allowRead = %s",
doc2.getIdentity().toString(),
foobarRole.getDocument().getIdentity().toString());
db.command(new OCommandSQL(sql)).execute();
// Give foobar role permission to read from table.
db.command(new OCommandSQL(String.format("GRANT READ ON database.class.%s TO foobar", TABLE_NAME))).execute();
db.close();
// Open connection for "user".
ODatabaseDocumentTx userDb = new ODatabaseDocumentTx(path);
userDb.open("user", "user");
// Here I would expect to see the message from doc2 but not doc1.
// Nothing gets printed...
for (ODocument odoc : userDb.browseClass(TABLE_NAME))
{
System.out.println(odoc.field("Message"));
}
文档中说,角色无法访问的记录在读取过程中被跳过。在我的例子中,用户无法读取任何内容
有什么办法可以让这种行为起作用吗?经过几天的摆弄,我就是这样让它起作用的
// Create database.
ODatabaseDocumentTx db = new ODatabaseDocumentTx(path);
db.create();
// Create users/roles.
OSecurity sm = db.getMetadata().getSecurity();
restrictedRole = sm.createRole("foobar", OSecurityRole.ALLOW_MODES.DENY_ALL_BUT);
restrictedRole.addRule(ORule.ResourceGeneric.CLASS, TABLE_NAME, ORole.PERMISSION_READ);
restrictedRole.addRule(ORule.ResourceGeneric.DATABASE, TABLE_NAME, ORole.PERMISSION_READ);
restrictedRole.addRule(ORule.ResourceGeneric.CLUSTER, TABLE_NAME, ORole.PERMISSION_READ);
restrictedRole.save();
restrictedRole.reload();
admin = sm.getUser("admin");
user2 = sm.createUser("user2", "user2", "foobar");
// Insert 2 records, one restricted, one is not.
OClass restricted = db.getMetadata().getSchema().getClass("ORestricted");
OClass docClass = db.getMetadata().getSchema().getOrCreateClass(TABLE_NAME, restricted);
ODocument doc1 = new ODocument(docClass);
ODocument doc2 = new ODocument(docClass);
// The restricted record...
doc1.field("name", TABLE_NAME);
doc1.field("Id", 1, OType.INTEGER);
doc1.field("Message", "Hello 1", OType.STRING);
doc1.save();
// The unrestricted record...
doc2.field("name", TABLE_NAME);
doc2.field("Id", 2, OType.INTEGER);
doc2.field("Message", "Hello 2", OType.STRING);
db.getMetadata().getSecurity().allowRole(doc2, OSecurityShared.ALLOW_READ_FIELD, "foobar");
doc2.save();
//
// PRINT RESTRICTED
//
db.close();
db = new ODatabaseDocumentTx(path);
db.open("user2", "user2");
// Prints:
// Hello 2
for (ODocument doc : db.browseClass(TABLE_NAME))
{
System.out.println(doc.field("Message"));
}
//
// PRINT ADMIN
//
db.close();
db = new ODatabaseDocumentTx(path);
db.open("admin", "admin");
// Prints:
// Hello 1
// Hello 2
for (ODocument doc : db.browseClass(TABLE_NAME))
{
System.out.println(doc.field("Message"));
}
角色上的addRule
方法允许具有该角色的用户从类中读取。第二个文档有一行额外的allowRole
,它允许具有该角色的用户阅读该特定文档,这是第一个文档故意遗漏的。结果是,具有“foobar”角色的用户在阅读时只会获得第二个文档。管理员角色可以读取这两个文档
另外,请注意来自“ORestricted”的文档类继承
文档需要继承自ORestricted
,以便记录级安全性工作。就我个人而言,我在任何地方的代码中都找不到关于如何做到这一点的解释(可能我没有找到正确的地方),Java中也没有直接从ORestricted
类继承的文档类。因此,我们必须使用模式类元数据来告诉驱动程序,我们正在创建的文档需要从ORestricted
继承
OClass restricted = db.getMetadata().getSchema().getClass("ORestricted");
OClass docClass = db.getMetadata().getSchema().getOrCreateClass(TABLE_NAME, restricted);