Pimcore\Model\Tool\Setup::createOrUpdateUser PHP Method

createOrUpdateUser() public method

public createOrUpdateUser ( array $config = [] )
$config array
    public function createOrUpdateUser($config = [])
    {
        $defaultConfig = ["username" => "admin", "password" => md5(microtime())];
        $settings = array_replace_recursive($defaultConfig, $config);
        if ($user = Model\User::getByName($settings["username"])) {
            $user->delete();
        }
        $user = Model\User::create(["parentId" => 0, "username" => $settings["username"], "password" => \Pimcore\Tool\Authentication::getPasswordHash($settings["username"], $settings["password"]), "active" => true]);
        $user->setAdmin(true);
        $user->save();
    }

Usage Example

示例#1
0
 public function installAction()
 {
     // database configuration host/unix socket
     $dbConfig = ['username' => $this->getParam("mysql_username"), 'password' => $this->getParam("mysql_password"), 'dbname' => $this->getParam("mysql_database")];
     $hostSocketValue = $this->getParam("mysql_host_socket");
     if (file_exists($hostSocketValue)) {
         $dbConfig["unix_socket"] = $hostSocketValue;
     } else {
         $dbConfig["host"] = $hostSocketValue;
         $dbConfig["port"] = $this->getParam("mysql_port");
     }
     // try to establish a mysql connection
     try {
         $db = \Zend_Db::factory($this->getParam("mysql_adapter"), $dbConfig);
         $db->getConnection();
         // check utf-8 encoding
         $result = $db->fetchRow('SHOW VARIABLES LIKE "character\\_set\\_database"');
         if ($result['Value'] != "utf8") {
             $errors[] = "Database charset is not utf-8";
         }
     } catch (\Exception $e) {
         $errors[] = "Couldn't establish connection to mysql: " . $e->getMessage();
     }
     // check username & password
     if (strlen($this->getParam("admin_password")) < 4 || strlen($this->getParam("admin_username")) < 4) {
         $errors[] = "Username and password should have at least 4 characters";
     }
     if (empty($errors)) {
         $setup = new Tool\Setup();
         // check if /website folder already exists, if not, look for /website_demo & /website_example
         // /website_install is just for testing in dev environment
         if (!is_dir(PIMCORE_WEBSITE_PATH)) {
             foreach (["website_install", "website_demo", "website_example"] as $websiteDir) {
                 $dir = PIMCORE_DOCUMENT_ROOT . "/" . $websiteDir;
                 if (is_dir($dir)) {
                     rename($dir, PIMCORE_WEBSITE_PATH);
                     break;
                 }
             }
         }
         $setup->config(array("database" => array("adapter" => $this->getParam("mysql_adapter"), "params" => $dbConfig)));
         // look for a template dump
         // eg. for use with demo installer
         $dbDataFile = PIMCORE_WEBSITE_PATH . "/dump/data.sql";
         $contentConfig = array("username" => $this->getParam("admin_username"), "password" => $this->getParam("admin_password"));
         if (!file_exists($dbDataFile)) {
             $setup->database();
             \Pimcore::initConfiguration();
             $setup->contents($contentConfig);
         } else {
             $setup->database();
             $setup->insertDump($dbDataFile);
             \Pimcore::initConfiguration();
             $setup->createOrUpdateUser($contentConfig);
         }
         $this->_helper->json(array("success" => true));
     } else {
         echo implode("<br />", $errors);
         die;
     }
 }