Example #1
0
 /**
  * create the empty database
  *
  * @return boolean
  */
 function _createDatabase($aDsn = '')
 {
     if ($aDsn) {
         $this->aDsn = $aDsn;
     }
     $GLOBALS['_MAX']['CONF']['database'] = $this->aDsn['database'];
     $GLOBALS['_MAX']['CONF']['table']['prefix'] = $this->aDsn['table']['prefix'];
     $GLOBALS['_MAX']['CONF']['table']['type'] = $this->aDsn['table']['type'];
     // Try connecting to the database
     $this->oDbh =& OA_DB::singleton(OA_DB::getDsn($this->aDsn));
     if (PEAR::isError($this->oDbh)) {
         $GLOBALS['_OA']['CONNECTIONS'] = array();
         $GLOBALS['_MDB2_databases'] = array();
         if (PEAR::isError($result)) {
             $this->oLogger->logError($result->getMessage());
             $this->oLogger->logErrorUnlessEmpty($result->getUserInfo());
             return false;
         }
         //attempt to create DB
         $result = OA_DB::createDatabase($this->aDsn['database']['name']);
         if (PEAR::isError($result)) {
             $this->oLogger->logError($result->getMessage());
             $this->oLogger->logErrorUnlessEmpty($result->getUserInfo());
             return false;
         }
         $this->oDbh = OA_DB::changeDatabase($this->aDsn['database']['name']);
         if (PEAR::isError($this->oDbh)) {
             $this->oLogger->logError($this->oDbh->getMessage());
             $this->oLogger->logErrorUnlessEmpty($this->getUserInfo());
             $this->oDbh = null;
             return false;
         }
         $this->oLogger->log('Database created ' . $this->aDsn['database']['name']);
         $this->can_drop_database = true;
     }
     $result = OA_DB::createFunctions();
     if (PEAR::isError($result)) {
         $this->oLogger->logError($result->getMessage());
         return false;
     }
     return true;
 }
Example #2
0
 /**
  * Create the admin user and account, plus a default manager, also
  * inserts admin default timezone preferences
  *
  * @param array $aAdmin
  * @param array $aPrefs
  * @return boolean
  */
 public function putAdmin($aAdmin, $aPrefs)
 {
     try {
         // init transaction
         $oDbh = OA_DB::singleton();
         $useTransaction = $oDbh->supports('transactions');
         if ($useTransaction) {
             $oDbh->beginTransaction();
         }
         // Create Admin account
         $doAccount = OA_Dal::factoryDO('accounts');
         $doAccount->account_name = 'Administrator account';
         $doAccount->account_type = OA_ACCOUNT_ADMIN;
         $adminAccountId = $doAccount->insert();
         if (!$adminAccountId) {
             throw new Exception('error creating the admin account');
         }
         // Create Manager entity
         $doAgency = OA_Dal::factoryDO('agency');
         $doAgency->name = 'Default manager';
         $doAgency->email = $aAdmin['email'];
         $doAgency->active = 1;
         $agencyId = $doAgency->insert();
         if (!$agencyId) {
             throw new Exception('error creating the manager entity');
         }
         $doAgency = OA_Dal::factoryDO('agency');
         if (!$doAgency->get($agencyId)) {
             throw new Exception('error retrieving the manager account ID');
         }
         $agencyAccountId = $doAgency->account_id;
         // Create Admin user
         $doUser = OA_Dal::factoryDO('users');
         $doUser->contact_name = 'Administrator';
         $doUser->email_address = $aAdmin['email'];
         $doUser->username = $aAdmin['name'];
         $doUser->password = md5($aAdmin['pword']);
         $doUser->default_account_id = $agencyAccountId;
         $doUser->language = $aAdmin['language'];
         $userId = $doUser->insert();
         if (!$userId) {
             throw new Exception('error creating the admin user');
         }
         $result = OA_Permission::setAccountAccess($adminAccountId, $userId);
         if (!$result) {
             throw new Exception("error creating access to admin account, account id: {$adminAccountId}, user ID: {$userId}");
         }
         $result = OA_Permission::setAccountAccess($agencyAccountId, $userId);
         if (!$result) {
             throw new Exception("error creating access to default agency account, account id: {$agencyAccountId}, user ID: {$userId}");
         }
         $this->putDefaultPreferences($adminAccountId);
         if (!$this->putTimezoneAccountPreference($aPrefs)) {
             // rollback if fails
             throw new Exception();
         }
         if ($useTransaction) {
             $oDbh->commit();
         }
     } catch (Exception $e) {
         $this->oLogger->logErrorUnlessEmpty($e->getMessage());
         if ($useTransaction) {
             $oDbh->rollback();
         } else {
             $this->_rollbackPutAdmin();
         }
         return false;
     }
     return true;
 }