/**
  * Create MySQL databases, users and grant permissions.
  */
 public function createDatabases()
 {
     /** @var $dbs MysqlDatabase[] */
     $dbs = $this->_getRepository()->findAll();
     foreach ($dbs as $db) {
         $userModel = new MysqlUserModel();
         $userModel->setUsername($db->getName())->setPassword($this->_cryptservice->decrypt($db->getPassword()));
         $dbModel = new MysqlDatabaseModel();
         $dbModel->setName($db->getName())->setUsers(array($userModel));
         if ($this->_mysqladmin->checkUserExists($userModel)) {
             $this->_mysqladmin->setUserPassword($userModel);
         } else {
             $this->_mysqladmin->createUser($userModel);
         }
         if (!$this->_mysqladmin->checkDatabaseExists($dbModel)) {
             $this->_mysqladmin->createDatabase($dbModel);
             $this->_mysqladmin->grantPermissionsOnDatabase($dbModel);
         }
     }
 }
Exemple #2
0
 /**
  * Get MySQL Users with specified prefix.
  *
  * @param string $prefix
  *
  * @throws CouldNotReadDataException
  * @return MysqlUserModel[]
  */
 public function getUsers($prefix = '')
 {
     /** @var $users MysqlUserModel[] */
     $users = array();
     $q = sprintf('SELECT User, Host FROM mysql.user WHERE User LIKE "%s%%"', $prefix);
     $result = $this->_mysqli->query($q);
     if (!$result) {
         throw new CouldNotReadDataException();
     }
     if ($result->num_rows > 0) {
         while ($row = $result->fetch_assoc()) {
             $user = new MysqlUserModel();
             $user->setUsername($row['User'])->setHost($row['Host']);
             $users[] = $user;
         }
     }
     return $users;
 }