Example #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;
 }
 public function getSummary()
 {
     $resourceConfig = $this->data['resourceConfig'];
     if (isset($this->data['adminName'])) {
         $resourceConfig['username'] = $this->data['adminName'];
         if (isset($this->data['adminPassword'])) {
             $resourceConfig['password'] = $this->data['adminPassword'];
         }
     }
     $db = new DbTool($resourceConfig);
     try {
         $db->connectToDb();
         if (array_search(reset($this->data['tables']), $db->listTables(), true) === false) {
             if ($resourceConfig['username'] !== $this->data['resourceConfig']['username']) {
                 $message = sprintf(mt('setup', 'The database user "%s" will be used to setup the missing schema required by Icinga' . ' Web 2 in database "%s" and to grant access to it to a new login called "%s".'), $resourceConfig['username'], $resourceConfig['dbname'], $this->data['resourceConfig']['username']);
             } else {
                 $message = sprintf(mt('setup', 'The database user "%s" will be used to setup the missing' . ' schema required by Icinga Web 2 in database "%s".'), $resourceConfig['username'], $resourceConfig['dbname']);
             }
         } else {
             $message = sprintf(mt('setup', 'The database "%s" already seems to be fully set up. No action required.'), $resourceConfig['dbname']);
         }
     } catch (PDOException $_) {
         try {
             $db->connectToHost();
             if ($resourceConfig['username'] !== $this->data['resourceConfig']['username']) {
                 if ($db->hasLogin($this->data['resourceConfig']['username'])) {
                     $message = sprintf(mt('setup', 'The database user "%s" will be used to create the missing database' . ' "%s" with the schema required by Icinga Web 2 and to grant' . ' access to it to an existing login called "%s".'), $resourceConfig['username'], $resourceConfig['dbname'], $this->data['resourceConfig']['username']);
                 } else {
                     $message = sprintf(mt('setup', 'The database user "%s" will be used to create the missing database' . ' "%s" with the schema required by Icinga Web 2 and to grant' . ' access to it to a new login called "%s".'), $resourceConfig['username'], $resourceConfig['dbname'], $this->data['resourceConfig']['username']);
                 }
             } else {
                 $message = sprintf(mt('setup', 'The database user "%s" will be used to create the missing' . ' database "%s" with the schema required by Icinga Web 2.'), $resourceConfig['username'], $resourceConfig['dbname']);
             }
         } catch (Exception $_) {
             $message = mt('setup', 'No connection to database host possible. You\'ll need to setup the' . ' database with the schema required by Icinga Web 2 manually.');
         }
     }
     return '<h2>' . mt('setup', 'Database Setup', 'setup.page.title') . '</h2><p>' . $message . '</p>';
 }