I created a php command to give super admin rights to an existing user.
Code:
<?php
// TODO documentation
class AdminCommand extends CConsoleCommand {
/**
* @var CDbConnection
*/
private $conn;
public function run($arguments) {
if (isset($arguments) && isset($arguments[0])) {
$this->conn = App()->getDb();
// Ensures the specified users exist and retrieve their infos
$users = array();
foreach($arguments as $arg) {
if (empty($arg)) {
continue;
}
$user = $this->findUser($arg);
if ($user == null) {
$this->err("ERROR: User not found: $arg\n");
return 1;
}
array_push($users, $user);
}
// Gives admin rights to
$this->out("Administrative rights given to:");
foreach($users as $user) {
$this->makeAdmin($user['uid']);
$this->out(' > '.$user['uid'].': '.$user['users_name'].' ('.$user['full_name'].')');
}
} else {
$this->err("ERREUR: No user specified!");
$this->printHelp();
$this->out();
return 1;
}
$this->out();
return 0;
}
private function findUser($username) {
$query = $this->conn->createCommand()
->select('*')
->from($this->conn->tablePrefix.'users u');
if (is_int($username) || ctype_digit($username)) {
$query->where('u.uid = :1', array(':1' => intval($username)));
} else {
$query->where('LOWER(u.users_name) = :1', array(':1' => $username));
}
return $query->queryRow();
}
private function permExists($uid) {
$res = $this->conn->createCommand()
->select('count(1)')
->from($this->conn->tablePrefix.'permissions')
->where(
'entity = :ent AND uid = :uid AND permission = :perm',
array(':ent' => 'global', ':uid' => $uid, ':perm' => 'superadmin'),
)
->queryColumn();
return !!$res;
}
private function makeAdmin($uid) {
$cmd = $this->conn->createCommand();
if ($this->permExists($uid)) {
$cmd->update(
$this->conn->tablePrefix.'permissions',
array('read_p' => 1),
'entity = :ent AND uid = :uid AND permission = :perm',
array(':ent' => 'global', ':uid' => $uid, ':perm' => 'superadmin'),
);
} else {
$cmd->insert(
$this->conn->tablePrefix.'permissions',
array(
'entity'=>'global',
'entity_id'=>0,
'uid'=>$uid,
'permission'=>'superadmin',
'create_p'=>0,
'read_p'=>1,
'update_p'=>0,
'delete_p'=>0,
'import_p'=>0,
'export_p'=>0
)
);
}
$cmd->execute();
}
private function err($msg="") {
$this->out("\e[31m".$msg."\e[m");
}
private function out($msg="") {
echo $msg.PHP_EOL;
}
private function printHelp() {
$this->out(implode("\n", array(
"Give administrative rights to existing users.",
"",
"Usage:",
" php console.php admin <USER...>",
"",
"Examples:",
" php console.php admin 20 # By user ID",
" php console.php admin user01 # By username",
" php console.php admin user02 21 22 user03",
""
)));
}
}