<?php
class REConfigUser Extends REConfig
{
function BuildQuery() {
$privs='';
$json_data=&$_POST['json_data']['building'];
if(!strcasecmp('yes',@$json_data[4]['value'][0])) {
$privs='ALL PRIVILEGES';
} else {
if(!strcasecmp('y',@$json_data[5]['value'][0])) $privs.='select';
if(!strcasecmp('y',@$json_data[6]['value'][0])) $privs.=',insert';
if(!strcasecmp('y',@$json_data[7]['value'][0])) $privs.=',update';
if(!strcasecmp('y',@$json_data[8]['value'][0])) $privs.=',delete';
$privs=ltrim($privs,',');
}
if(@in_array('insert',$this->action)) {
$this->sources['building']['out']['query']=$this->SqlGrantPrivs(
$json_data[0]['value'][0],$json_data[2]['value'][0],$json_data[3]['value'][0],$privs,@$json_data[9]['value'][0]);
} else if(@in_array('update',$this->action)) {
$this->sources['building']['out']['query']=$this->SqlUpdatePrivs(
$json_data[0]['value'][0],$json_data[2]['value'][0],$json_data[3]['value'][0],$privs,@$json_data[9]['value'][0]);
} else if(@in_array('delete',$this->action)) {
$this->sources['building']['out']['query']=$this->SqlRevokePrivs(
$json_data[0]['value'][0],$json_data[1]['value'][0],@$json_data[9]['value'][0]);
}
$this->sources['building']['out']['dbname']=$json_data[0]['value'][0];
}
function SqlListPrivs($host,$user) {
return "SELECT usename as user,'' as password,'n' as Select_priv,'n' as Insert_priv,'n' as Update_priv,'n' as Delete_priv,'n' as Grant_priv FROM pg_user WHERE usename='".$user.'\';';
}
function SqlGrantPrivs($db,$user,$password,$privs,$grant) {
$grantoption=($grant=='Y' ? ' WITH GRANT OPTION' : '');
$sql = array(
'CREATE USER '.$user." WITH PASSWORD '".$password."';",
"GRANT ".$privs.' ON ALL TABLES IN SCHEMA public TO '.$user.$grantoption.';'
);
return $sql;
}
function SqlUpdatePrivs($db,$user,$password,$privs,$grant) {
$grantoption=($grant=='Y' ? ' WITH GRANT OPTION' : '');
$sql = array(
'REVOKE ALL PRIVILEGES ON DATABASE '.$db.' FROM '.$user.';',
"GRANT ".$privs.' ON ALL TABLES IN SCHEMA public TO '.$user.$grantoption.';');
if(strlen($password) > 2) {
$sql[] = "ALTER USER $user WITH PASSWORD '$password';";
}
return $sql;
}
function SqlRevokePrivs($db,$user,$grant) {
global $GLOBALCFG;
$sql = array(
'REVOKE ALL PRIVILEGES ON DATABASE '.$db.' FROM '.$user); //,'DROP USER '.$user);
return $sql;
}
};
|