Postgres::setPrivileges PHP Method

setPrivileges() public method

Grants a privilege to a user, group or public
public setPrivileges ( $mode, $type, $object, $public, $usernames, $groupnames, $privileges, $grantoption, $cascade, $table ) : -4
$mode 'GRANT' or 'REVOKE';
$type The type of object
$object The name of the object
$public True to grant to public, false otherwise
$usernames The array of usernames to grant privs to.
$groupnames The array of group names to grant privs to.
$privileges The array of privileges to grant (eg. ('SELECT', 'ALL PRIVILEGES', etc.) )
$grantoption True if has grant option, false otherwise
$cascade True for cascade revoke, false otherwise
$table the column's table if type=column
return -4
    function setPrivileges($mode, $type, $object, $public, $usernames, $groupnames, $privileges, $grantoption, $cascade, $table)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldArrayClean($usernames);
        $this->fieldArrayClean($groupnames);
        // Input checking
        if (!is_array($privileges) || sizeof($privileges) == 0) {
            return -3;
        }
        if (!is_array($usernames) || !is_array($groupnames) || !$public && sizeof($usernames) == 0 && sizeof($groupnames) == 0) {
            return -4;
        }
        if ($mode != 'GRANT' && $mode != 'REVOKE') {
            return -5;
        }
        $sql = $mode;
        // Grant option
        if ($this->hasGrantOption() && $mode == 'REVOKE' && $grantoption) {
            $sql .= ' GRANT OPTION FOR';
        }
        if (in_array('ALL PRIVILEGES', $privileges)) {
            $sql .= ' ALL PRIVILEGES';
        } else {
            if ($type == 'column') {
                $this->fieldClean($object);
                $sql .= ' ' . join(" (\"{$object}\"), ", $privileges);
            } else {
                $sql .= ' ' . join(', ', $privileges);
            }
        }
        switch ($type) {
            case 'column':
                $sql .= " (\"{$object}\")";
                $object = $table;
            case 'table':
            case 'view':
            case 'sequence':
                $this->fieldClean($object);
                $sql .= " ON \"{$f_schema}\".\"{$object}\"";
                break;
            case 'database':
                $this->fieldClean($object);
                $sql .= " ON DATABASE \"{$object}\"";
                break;
            case 'function':
                // Function comes in with $object as function OID
                $fn = $this->getFunction($object);
                $this->fieldClean($fn->fields['proname']);
                $sql .= " ON FUNCTION \"{$f_schema}\".\"{$fn->fields['proname']}\"({$fn->fields['proarguments']})";
                break;
            case 'language':
                $this->fieldClean($object);
                $sql .= " ON LANGUAGE \"{$object}\"";
                break;
            case 'schema':
                $this->fieldClean($object);
                $sql .= " ON SCHEMA \"{$object}\"";
                break;
            case 'tablespace':
                $this->fieldClean($object);
                $sql .= " ON TABLESPACE \"{$object}\"";
                break;
            default:
                return -1;
        }
        // Dump PUBLIC
        $first = true;
        $sql .= $mode == 'GRANT' ? ' TO ' : ' FROM ';
        if ($public) {
            $sql .= 'PUBLIC';
            $first = false;
        }
        // Dump users
        foreach ($usernames as $v) {
            if ($first) {
                $sql .= "\"{$v}\"";
                $first = false;
            } else {
                $sql .= ", \"{$v}\"";
            }
        }
        // Dump groups
        foreach ($groupnames as $v) {
            if ($first) {
                $sql .= "GROUP \"{$v}\"";
                $first = false;
            } else {
                $sql .= ", GROUP \"{$v}\"";
            }
        }
        // Grant option
        if ($this->hasGrantOption() && $mode == 'GRANT' && $grantoption) {
            $sql .= ' WITH GRANT OPTION';
        }
        // Cascade revoke
        if ($this->hasGrantOption() && $mode == 'REVOKE' && $cascade) {
            $sql .= ' CASCADE';
        }
        return $this->execute($sql);
    }
Postgres