Search in sources :

Example 6 with Right

use of org.hsqldb_voltpatches.rights.Right in project voltdb by VoltDB.

the class DatabaseInformationMain method TABLE_PRIVILEGES.

/*
    WHERE ( GRANTEE IN ( 'PUBLIC', CURRENT_USER )
    OR GRANTEE IN ( SELECT ROLE_NAME FROM ENABLED_ROLES )
    OR GRANTOR = CURRENT_USER
    OR GRANTOR IN ( SELECT ROLE_NAME FROM ENABLED_ROLES ) )

*/
/**
     * The TABLE_PRIVILEGES view has one row for each visible access
     * right for each accessible table definied within this database. <p>
     *
     * Each row is a table privilege description with the following columns: <p>
     *
     * <pre class="SqlCodeExample">
     * GRANTOR      VARCHAR   grantor of access
     * GRANTEE      VARCHAR   grantee of access
     * TABLE_CATALOG    VARCHAR   table catalog
     * TABLE_SCHEMA  VARCHAR   table schema
     * TABLE_NAME   VARCHAR   table name
     * PRIVILEGE_TYPE    VARCHAR   { "SELECT" | "INSERT" | "UPDATE" | "DELETE" | "REFERENCES" | "TRIGGER" }
     * IS_GRANTABLE VARCHAR   { "YES" | "NO" }
     * WITH_HIERARCHY   { "YES" | "NO" }
     * </pre>
     *
     * @return a <code>Table</code> object describing the visible
     *        access rights for each accessible table
     *        defined within this database
     */
final Table TABLE_PRIVILEGES() {
    Table t = sysTables[TABLE_PRIVILEGES];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[TABLE_PRIVILEGES]);
        // not null
        addColumn(t, "GRANTOR", SQL_IDENTIFIER);
        // not null
        addColumn(t, "GRANTEE", SQL_IDENTIFIER);
        addColumn(t, "TABLE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "TABLE_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "TABLE_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "PRIVILEGE_TYPE", CHARACTER_DATA);
        // not null
        addColumn(t, "IS_GRANTABLE", YES_OR_NO);
        addColumn(t, "WITH_HIERARCHY", YES_OR_NO);
        //
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[SEQUENCES].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1, 2, 3, 4, 5, 6 }, false);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // calculated column values
    String tableCatalog;
    String tableSchema;
    String tableName;
    Grantee granteeObject;
    String privilege;
    // intermediate holders
    Iterator tables;
    Table table;
    Object[] row;
    // column number mappings
    final int grantor = 0;
    final int grantee = 1;
    final int table_catalog = 2;
    final int table_schema = 3;
    final int table_name = 4;
    final int privilege_type = 5;
    final int is_grantable = 6;
    final int with_hierarchy = 7;
    OrderedHashSet grantees = session.getGrantee().getGranteeAndAllRolesWithPublic();
    tables = allTables();
    while (tables.hasNext()) {
        table = (Table) tables.next();
        tableName = table.getName().name;
        tableCatalog = table.getCatalogName().name;
        tableSchema = table.getSchemaName().name;
        for (int i = 0; i < grantees.size(); i++) {
            granteeObject = (Grantee) grantees.get(i);
            OrderedHashSet rights = granteeObject.getAllDirectPrivileges(table);
            OrderedHashSet grants = granteeObject.getAllGrantedPrivileges(table);
            if (!grants.isEmpty()) {
                grants.addAll(rights);
                rights = grants;
            }
            for (int j = 0; j < rights.size(); j++) {
                Right right = (Right) rights.get(j);
                Right grantableRight = right.getGrantableRights();
                for (int k = 0; k < Right.privilegeTypes.length; k++) {
                    if (!right.canAccess(Right.privilegeTypes[k])) {
                        continue;
                    }
                    privilege = Right.privilegeNames[k];
                    row = t.getEmptyRowData();
                    row[grantor] = right.getGrantor().getName().name;
                    row[grantee] = right.getGrantee().getName().name;
                    row[table_catalog] = tableCatalog;
                    row[table_schema] = tableSchema;
                    row[table_name] = tableName;
                    row[privilege_type] = privilege;
                    row[is_grantable] = right.getGrantee() == table.getOwner() || grantableRight.canAccess(Right.privilegeTypes[k]) ? "YES" : "NO";
                    row[with_hierarchy] = "NO";
                    try {
                        t.insertSys(store, row);
                    } catch (HsqlException e) {
                    }
                }
            }
        }
    }
    return t;
}
Also used : Grantee(org.hsqldb_voltpatches.rights.Grantee) Table(org.hsqldb_voltpatches.Table) Iterator(org.hsqldb_voltpatches.lib.Iterator) WrapperIterator(org.hsqldb_voltpatches.lib.WrapperIterator) Right(org.hsqldb_voltpatches.rights.Right) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Constraint(org.hsqldb_voltpatches.Constraint) HsqlException(org.hsqldb_voltpatches.HsqlException)

Aggregations

Right (org.hsqldb_voltpatches.rights.Right)6 Constraint (org.hsqldb_voltpatches.Constraint)5 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)5 Table (org.hsqldb_voltpatches.Table)5 OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)5 Grantee (org.hsqldb_voltpatches.rights.Grantee)5 HsqlException (org.hsqldb_voltpatches.HsqlException)4 SchemaObject (org.hsqldb_voltpatches.SchemaObject)4 Iterator (org.hsqldb_voltpatches.lib.Iterator)4 WrapperIterator (org.hsqldb_voltpatches.lib.WrapperIterator)4 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)4 TextTable (org.hsqldb_voltpatches.TextTable)2 Routine (org.hsqldb_voltpatches.Routine)1 RoutineSchema (org.hsqldb_voltpatches.RoutineSchema)1 GranteeManager (org.hsqldb_voltpatches.rights.GranteeManager)1 User (org.hsqldb_voltpatches.rights.User)1