示例#1
0
 /**
  * Return the new page to set as current page
  *
  * {@inheritdoc} Runs additional checks related to some registered pages.
  *
  * @param   string  $requestedPage      The name of the requested page
  * @param   Form    $originPage         The origin page
  *
  * @return  Form                        The new page
  *
  * @throws  InvalidArgumentException    In case the requested page does not exist or is not permitted yet
  */
 protected function getNewPage($requestedPage, Form $originPage)
 {
     $skip = false;
     $newPage = parent::getNewPage($requestedPage, $originPage);
     if ($newPage->getName() === 'setup_auth_db_resource') {
         $authData = $this->getPageData('setup_authentication_type');
         $skip = $authData['type'] !== 'db';
     } elseif ($newPage->getname() === 'setup_ldap_discovery') {
         $authData = $this->getPageData('setup_authentication_type');
         $skip = $authData['type'] !== 'ldap';
         /*} elseif ($newPage->getName() === 'setup_ldap_discovery_confirm') {
           $skip = false === $this->hasPageData('setup_ldap_discovery');*/
     } elseif ($newPage->getName() === 'setup_ldap_resource') {
         $authData = $this->getPageData('setup_authentication_type');
         $skip = $authData['type'] !== 'ldap';
     } elseif ($newPage->getName() === 'setup_config_db_resource') {
         $authData = $this->getPageData('setup_authentication_type');
         $configData = $this->getPageData('setup_general_config');
         $skip = $authData['type'] === 'db' || $configData['global_config_backend'] !== 'db';
     } elseif (in_array($newPage->getName(), array('setup_auth_db_creation', 'setup_config_db_creation'))) {
         if (($newPage->getName() === 'setup_auth_db_creation' || $this->hasPageData('setup_config_db_resource')) && (($config = $this->getPageData('setup_auth_db_resource')) !== null || ($config = $this->getPageData('setup_config_db_resource')) !== null) && !$config['skip_validation']) {
             $db = new DbTool($config);
             try {
                 $db->connectToDb();
                 // Are we able to login on the database?
                 if (array_search(reset($this->databaseTables), $db->listTables()) === false) {
                     // In case the database schema does not yet exist the
                     // user needs the privileges to setup the database
                     $skip = $db->checkPrivileges($this->databaseSetupPrivileges, $this->databaseTables);
                 } else {
                     // In case the database schema exists the user needs the required privileges
                     // to operate the database, if those are missing we ask for another user
                     $skip = $db->checkPrivileges($this->databaseUsagePrivileges, $this->databaseTables);
                 }
             } catch (PDOException $_) {
                 try {
                     $db->connectToHost();
                     // Are we able to login on the server?
                     // It is not possible to reliably determine whether a database exists or not if a user can't
                     // log in to the database, so we just require the user to be able to create the database
                     $skip = $db->checkPrivileges(array_unique(array_merge($this->databaseCreationPrivileges, $this->databaseSetupPrivileges)), $this->databaseTables);
                 } catch (PDOException $_) {
                     // We are NOT able to login on the server..
                 }
             }
         } else {
             $skip = true;
         }
     }
     return $skip ? $this->skipPage($newPage) : $newPage;
 }
 /**
  * Validate the given form data and check whether the defined user has sufficient access rights
  *
  * @param   array   $data   The data to validate
  *
  * @return  bool
  */
 public function isValid($data)
 {
     if (false === parent::isValid($data)) {
         return false;
     }
     if (isset($data['skip_validation']) && $data['skip_validation']) {
         return true;
     }
     $config = $this->config;
     $config['username'] = $this->getValue('username');
     $config['password'] = $this->getValue('password');
     $db = new DbTool($config);
     try {
         $db->connectToDb();
         // Are we able to login on the database?
     } catch (PDOException $_) {
         try {
             $db->connectToHost();
             // Are we able to login on the server?
         } catch (PDOException $e) {
             // We are NOT able to login on the server..
             $this->error($e->getMessage());
             $this->addSkipValidationCheckbox();
             return false;
         }
     }
     // In case we are connected the credentials filled into this
     // form need to be granted to create databases, users...
     if (false === $db->checkPrivileges($this->databaseSetupPrivileges)) {
         $this->error($this->translate('The provided credentials cannot be used to create the database and/or the user.'));
         $this->addSkipValidationCheckbox();
         return false;
     }
     // ...and to grant all required usage privileges to others
     if (false === $db->isGrantable($this->databaseUsagePrivileges)) {
         $this->error(sprintf($this->translate('The provided credentials cannot be used to grant all required privileges to the login "%s".'), $this->config['username']));
         $this->addSkipValidationCheckbox();
         return false;
     }
     return true;
 }
 protected function setupPgsqlDatabase(DbTool $db)
 {
     try {
         $db->connectToDb();
         $this->log(mt('setup', 'Successfully connected to existing database "%s"...'), $this->data['resourceConfig']['dbname']);
     } catch (PDOException $_) {
         $db->connectToHost();
         $this->log(mt('setup', 'Creating new database "%s"...'), $this->data['resourceConfig']['dbname']);
         $db->exec(sprintf("CREATE DATABASE %s WITH ENCODING 'UTF-8'", $db->quoteIdentifier($this->data['resourceConfig']['dbname'])));
         $db->reconnect($this->data['resourceConfig']['dbname']);
     }
     if (array_search(reset($this->data['tables']), $db->listTables(), true) !== false) {
         $this->log(mt('setup', 'Database schema already exists...'));
     } else {
         $this->log(mt('setup', 'Creating database schema...'));
         $db->import($this->data['schemaPath'] . '/pgsql.schema.sql');
     }
     if ($db->hasLogin($this->data['resourceConfig']['username'])) {
         $this->log(mt('setup', 'Login "%s" already exists...'), $this->data['resourceConfig']['username']);
     } else {
         $this->log(mt('setup', 'Creating login "%s"...'), $this->data['resourceConfig']['username']);
         $db->addLogin($this->data['resourceConfig']['username'], $this->data['resourceConfig']['password']);
     }
     $username = $this->data['resourceConfig']['username'];
     if ($db->checkPrivileges($this->data['privileges'], $this->data['tables'], $username)) {
         $this->log(mt('setup', 'Required privileges were already granted to login "%s".'), $this->data['resourceConfig']['username']);
     } else {
         $this->log(mt('setup', 'Granting required privileges to login "%s"...'), $this->data['resourceConfig']['username']);
         $db->grantPrivileges($this->data['privileges'], $this->data['tables'], $this->data['resourceConfig']['username']);
     }
 }