Search in sources :

Example 11 with OUser

use of com.orientechnologies.orient.core.metadata.security.OUser in project orientdb by orientechnologies.

the class LuceneInsertUpdateTransactionTest method testInsertUpdateTransactionWithIndex.

@Test
public void testInsertUpdateTransactionWithIndex() throws Exception {
    OSchema schema = db.getMetadata().getSchema();
    schema.reload();
    db.begin();
    ODocument doc = new ODocument("City");
    doc.field("name", "Rome");
    db.save(doc);
    OIndex idx = schema.getClass("City").getClassIndex("City.name");
    Assert.assertNotNull(idx);
    Collection<?> coll = (Collection<?>) idx.get("Rome");
    Assert.assertEquals(coll.size(), 1);
    db.rollback();
    coll = (Collection<?>) idx.get("Rome");
    Assert.assertEquals(coll.size(), 0);
    db.begin();
    doc = new ODocument("City");
    doc.field("name", "Rome");
    db.save(doc);
    OUser user = new OUser("test", "test");
    db.save(user.getDocument());
    db.commit();
    coll = (Collection<?>) idx.get("Rome");
    Assert.assertEquals(coll.size(), 1);
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OIndex(com.orientechnologies.orient.core.index.OIndex) Collection(java.util.Collection) OUser(com.orientechnologies.orient.core.metadata.security.OUser) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 12 with OUser

use of com.orientechnologies.orient.core.metadata.security.OUser in project orientdb by orientechnologies.

the class OServerCommandPostDatabase method sendDatabaseInfo.

protected void sendDatabaseInfo(final OHttpRequest iRequest, final OHttpResponse iResponse, final ODatabaseDocumentInternal db) throws IOException {
    final StringWriter buffer = new StringWriter();
    final OJSONWriter json = new OJSONWriter(buffer);
    json.beginObject();
    if (db.getMetadata().getSchema().getClasses() != null) {
        json.beginCollection(1, false, "classes");
        Set<String> exportedNames = new HashSet<String>();
        for (OClass cls : db.getMetadata().getSchema().getClasses()) {
            if (!exportedNames.contains(cls.getName()))
                try {
                    exportClass(db, json, cls);
                    exportedNames.add(cls.getName());
                } catch (Exception e) {
                    OLogManager.instance().error(this, "Error on exporting class '" + cls + "'", e);
                }
        }
        json.endCollection(1, true);
    }
    if (db.getClusterNames() != null) {
        json.beginCollection(1, false, "clusters");
        OCluster cluster;
        for (String clusterName : db.getClusterNames()) {
            cluster = db.getStorage().getClusterById(db.getClusterIdByName(clusterName));
            try {
                json.beginObject(2, true, null);
                json.writeAttribute(3, false, "id", cluster.getId());
                json.writeAttribute(3, false, "name", clusterName);
                json.writeAttribute(3, false, "records", cluster.getEntries() - cluster.getTombstonesCount());
                json.writeAttribute(3, false, "size", "-");
                json.writeAttribute(3, false, "filled", "-");
                json.writeAttribute(3, false, "maxSize", "-");
                json.writeAttribute(3, false, "files", "-");
            } catch (Exception e) {
                json.writeAttribute(3, false, "records", "? (Unauthorized)");
            }
            json.endObject(2, false);
        }
        json.endCollection(1, true);
    }
    if (db.getUser() != null)
        json.writeAttribute(1, false, "currentUser", db.getUser().getName());
    json.beginCollection(1, false, "users");
    OUser user;
    for (ODocument doc : db.getMetadata().getSecurity().getAllUsers()) {
        user = new OUser(doc);
        json.beginObject(2, true, null);
        json.writeAttribute(3, false, "name", user.getName());
        json.writeAttribute(3, false, "roles", user.getRoles() != null ? Arrays.toString(user.getRoles().toArray()) : "null");
        json.endObject(2, false);
    }
    json.endCollection(1, true);
    json.beginCollection(1, true, "roles");
    ORole role;
    for (ODocument doc : db.getMetadata().getSecurity().getAllRoles()) {
        role = new ORole(doc);
        json.beginObject(2, true, null);
        json.writeAttribute(3, false, "name", role.getName());
        json.writeAttribute(3, false, "mode", role.getMode().toString());
        json.beginCollection(3, true, "rules");
        for (Map.Entry<String, Byte> rule : role.getRules().entrySet()) {
            json.beginObject(4);
            json.writeAttribute(4, true, "name", rule.getKey());
            json.writeAttribute(4, false, "create", role.allow(rule.getKey(), ORole.PERMISSION_CREATE));
            json.writeAttribute(4, false, "read", role.allow(rule.getKey(), ORole.PERMISSION_READ));
            json.writeAttribute(4, false, "update", role.allow(rule.getKey(), ORole.PERMISSION_UPDATE));
            json.writeAttribute(4, false, "delete", role.allow(rule.getKey(), ORole.PERMISSION_DELETE));
            json.endObject(4, true);
        }
        json.endCollection(3, false);
        json.endObject(2, true);
    }
    json.endCollection(1, true);
    json.beginObject(1, true, "config");
    json.beginCollection(2, true, "values");
    json.writeObjects(3, true, null, new Object[] { "name", "dateFormat", "value", db.getStorage().getConfiguration().dateFormat }, new Object[] { "name", "dateTimeFormat", "value", db.getStorage().getConfiguration().dateTimeFormat }, new Object[] { "name", "localeCountry", "value", db.getStorage().getConfiguration().getLocaleCountry() }, new Object[] { "name", "localeLanguage", "value", db.getStorage().getConfiguration().getLocaleLanguage() }, new Object[] { "name", "definitionVersion", "value", db.getStorage().getConfiguration().version });
    json.endCollection(2, true);
    json.beginCollection(2, true, "properties");
    if (db.getStorage().getConfiguration().getProperties() != null)
        for (OStorageEntryConfiguration entry : db.getStorage().getConfiguration().getProperties()) {
            if (entry != null) {
                json.beginObject(3, true, null);
                json.writeAttribute(4, false, "name", entry.name);
                json.writeAttribute(4, false, "value", entry.value);
                json.endObject(3, true);
            }
        }
    json.endCollection(2, true);
    json.endObject(1, true);
    json.endObject();
    json.flush();
    iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
}
Also used : OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) ORole(com.orientechnologies.orient.core.metadata.security.ORole) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) StringWriter(java.io.StringWriter) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCluster(com.orientechnologies.orient.core.storage.OCluster) OUser(com.orientechnologies.orient.core.metadata.security.OUser) OStorageEntryConfiguration(com.orientechnologies.orient.core.config.OStorageEntryConfiguration) Map(java.util.Map) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 13 with OUser

use of com.orientechnologies.orient.core.metadata.security.OUser in project orientdb by orientechnologies.

the class OTokenHandlerImplTest method testWebTokenCreationValidation.

@Test
@Ignore
public void testWebTokenCreationValidation() throws InvalidKeyException, NoSuchAlgorithmException, IOException {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:" + OTokenHandlerImplTest.class.getSimpleName());
    db.create();
    try {
        OSecurityUser original = db.getUser();
        OTokenHandlerImpl handler = new OTokenHandlerImpl("any key".getBytes(), 60, "HmacSHA256");
        byte[] token = handler.getSignedWebToken(db, original);
        try {
            // Make this thread wait at least 10 milliseconds before check the validity
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        OToken tok = handler.parseWebToken(token);
        assertNotNull(tok);
        assertTrue(tok.getIsVerified());
        OUser user = tok.getUser(db);
        assertEquals(user.getName(), original.getName());
        boolean boole = handler.validateToken(tok, "open", db.getName());
        assertTrue(boole);
        assertTrue(tok.getIsValid());
    } finally {
        db.drop();
    }
}
Also used : OToken(com.orientechnologies.orient.core.metadata.security.OToken) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OUser(com.orientechnologies.orient.core.metadata.security.OUser) OSecurityUser(com.orientechnologies.orient.core.metadata.security.OSecurityUser) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with OUser

use of com.orientechnologies.orient.core.metadata.security.OUser in project orientdb by orientechnologies.

the class OSystemUserAuthenticator method isAuthorized.

// OSecurityAuthenticator
// If not supported by the authenticator, return false.
// Checks to see if a
public boolean isAuthorized(final String username, final String resource) {
    if (username == null || resource == null)
        return false;
    try {
        if (getServer() != null) {
            OUser user = getServer().getSecurity().getSystemUser(username, null);
            if (user != null && user.getAccountStatus() == OSecurityUser.STATUSES.ACTIVE) {
                ORole role = null;
                ORule.ResourceGeneric rg = ORule.mapLegacyResourceToGenericResource(resource);
                if (rg != null) {
                    String specificResource = ORule.mapLegacyResourceToSpecificResource(resource);
                    if (specificResource == null || specificResource.equals("*")) {
                        specificResource = null;
                    }
                    role = user.checkIfAllowed(rg, specificResource, ORole.PERMISSION_EXECUTE);
                }
                return role != null;
            }
        }
    } catch (Exception ex) {
        OLogManager.instance().error(this, "isAuthorized() Exception: %s", ex.getMessage());
    }
    return false;
}
Also used : ORole(com.orientechnologies.orient.core.metadata.security.ORole) OUser(com.orientechnologies.orient.core.metadata.security.OUser) ORule(com.orientechnologies.orient.core.metadata.security.ORule)

Example 15 with OUser

use of com.orientechnologies.orient.core.metadata.security.OUser in project orientdb by orientechnologies.

the class JsonWebToken method getUser.

@Override
public OUser getUser(ODatabaseDocumentInternal db) {
    ORID userRid = ((OrientJwtPayload) payload).getUserRid();
    ODocument result;
    result = db.load(userRid, "roles:1");
    if (!result.getSchemaClass().isSubClassOf(OUser.CLASS_NAME)) {
        result = null;
    }
    return new OUser(result);
}
Also used : ORID(com.orientechnologies.orient.core.id.ORID) OUser(com.orientechnologies.orient.core.metadata.security.OUser) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OUser (com.orientechnologies.orient.core.metadata.security.OUser)16 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)10 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)6 Test (org.junit.Test)5 ORole (com.orientechnologies.orient.core.metadata.security.ORole)4 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)3 ORID (com.orientechnologies.orient.core.id.ORID)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OSecurityUser (com.orientechnologies.orient.core.metadata.security.OSecurityUser)2 OToken (com.orientechnologies.orient.core.metadata.security.OToken)2 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 Collection (java.util.Collection)2 OStorageEntryConfiguration (com.orientechnologies.orient.core.config.OStorageEntryConfiguration)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OIndex (com.orientechnologies.orient.core.index.OIndex)1