Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Set user admin from php or command line

  • DrunkenPoney
  • DrunkenPoney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 9 months ago #219448 by DrunkenPoney
Set user admin from php or command line was created by DrunkenPoney
Is there a way to make a user admin either by a command line or by editing a php file?
The topic has been locked.
  • DrunkenPoney
  • DrunkenPoney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 9 months ago #219528 by DrunkenPoney
Replied by DrunkenPoney on topic Set user admin from php or command line
I created a php command to give super admin rights to an existing user.
To use:
  1. Unzip the archive 

    File Attachment:

    File Name: AdminCommand.zip
    File Size:1 KB
  2. Place the AdminCommand.php file in LIMESURVEY_ROOT/application/commands/
  3. Execute the `admin` php command. (see  LimeSurvey manual  on how to execute a php command)

Here's the content of AdminCommand.php if you don't trust the archive:
Code:
<?php
// TODO documentation
class AdminCommand extends CConsoleCommand {
    /**
     * @var CDbConnection
     */
    private $conn;
 
    public function run($arguments) {
        if (isset($arguments) &amp;&amp; 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",
            ""
        )));
    }
}
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose