예제 #1
0
파일: site.php 프로젝트: wiserweb/OpenVBX
 private function add_tenant()
 {
     $tenant = $this->input->post('tenant');
     if (empty($tenant['url_prefix'])) {
         $data['error'] = true;
         $data['message'] = 'A valid tenant name is required';
         $this->session->set_flashdata('error', 'Failed to add new tenant: ' . $data['message']);
     }
     if (empty($tenant['admin_email']) || !filter_var($tenant['admin_email'], FILTER_VALIDATE_EMAIL)) {
         $data['error'] = true;
         $data['message'] = 'A valid admin email address is required';
         $this->session->set_flashdata('error', 'Failed to add new tenant: ' . $data['message']);
     }
     if (!empty($tenant) && empty($data['error'])) {
         try {
             $data['id'] = $this->settings->tenant($tenant['url_prefix'], urlencode($tenant['url_prefix']), '');
             $this->db->trans_start();
             $user = new VBX_User();
             $user->fields[] = 'tenant_id';
             // monkey patching to override tenant_id
             $user->first_name = '';
             $user->last_name = '';
             $user->password = '';
             $user->values['tenant_id'] = $data['id'];
             // hidden field not in ORM
             $user->email = $tenant['admin_email'];
             $user->is_active = TRUE;
             $user->is_admin = TRUE;
             $user->auth_type = 1;
             try {
                 $user->save();
             } catch (VBX_UserException $e) {
                 throw new VBX_SettingsException($e->getMessage());
             }
             foreach ($this->settings->setting_options as $param) {
                 $this->settings->add($param, '', $data['id']);
             }
             $this->settings->set('from_email', $tenant['admin_email'], $data['id']);
             $friendlyName = substr($tenant['url_prefix'] . ' - ' . $tenant['admin_email'], 0, 32);
             switch ($this->input->post('auth_type')) {
                 case 'connect':
                     $auth_type = VBX_Settings::AUTH_TYPE_CONNECT;
                     break;
                 case 'subaccount':
                 default:
                     $auth_type = VBX_Settings::AUTH_TYPE_SUBACCOUNT;
                     break;
             }
             /**
              * Only do app setup for sub-accounts.
              * Connect tenants will get set up after going through the connect process.
              */
             if ($auth_type === VBX_Settings::AUTH_TYPE_SUBACCOUNT) {
                 try {
                     /** @var Services_Twilio_Rest_Accounts $accounts */
                     $accounts = OpenVBX::getAccounts();
                     // default, sub-account
                     $sub_account = $accounts->create(array('FriendlyName' => $friendlyName));
                     $tenant_sid = $sub_account->sid;
                     $tenant_token = $sub_account->auth_token;
                     $this->settings->add('twilio_sid', $tenant_sid, $data['id']);
                     $this->settings->add('twilio_token', $tenant_token, $data['id']);
                     $app_sid = $this->create_application_for_subaccount($data['id'], $tenant['url_prefix'], $tenant_sid);
                     $this->settings->add('application_sid', $app_sid, $data['id']);
                 } catch (Exception $e) {
                     throw new VBX_SettingsException($e->getMessage());
                 }
             } elseif ($auth_type === VBX_Settings::AUTH_TYPE_CONNECT) {
                 // when using connect, we won't get a sid, token, or
                 // app_sid until user first login
                 $tenant_id = $tenant_token = $app_sid = null;
                 $this->settings->add('tenant_first_run', 1, $data['id']);
             } else {
                 throw new VBX_SettingsException('Unknown auth-type encountered during ' . 'tenant creation');
             }
             $this->settings->update_tenant(array('id' => $data['id'], 'type' => $auth_type));
             $tenant_defaults = array('transcriptions' => 1, 'voice' => 'man', 'voice_language' => 'en', 'numbers_country' => 'US', 'gravatars' => 0, 'dial_timeout' => 15);
             foreach ($tenant_defaults as $key => $value) {
                 $this->settings->set($key, $value, $data['id']);
             }
             $this->db->trans_complete();
             $this->session->set_flashdata('error', 'Added new tenant');
             $user->send_new_user_notification();
             if (isset($data['id'])) {
                 return redirect('settings/site/tenant/' . $data['id']);
             }
         } catch (VBX_SettingsException $e) {
             error_log($e->getMessage());
             $this->db->trans_rollback();
             // TODO: rollback in twilio.
             $this->session->set_flashdata('error', 'Failed to add new tenant: ' . $e->getMessage());
             $data['error'] = true;
             $data['message'] = $e->getMessage();
         }
     }
     if ($this->response_type == 'html') {
         redirect('settings/site');
     }
     $this->respond('', 'settings/site', $data);
 }