Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 private function add_tenant()
 {
     $tenant = $this->input->post('tenant');
     if (!empty($tenant)) {
         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();
                 $user->send_new_user_notification();
             } 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']);
             try {
                 $twilio = new TwilioRestClient($this->twilio_sid, $this->twilio_token, $this->twilio_endpoint);
                 $friendlyName = $tenant['url_prefix'] . ' - ' . $tenant['admin_email'];
                 $friendlyName = substr($friendlyName, 0, 32);
                 $response = $twilio->request("Accounts", 'POST', array('FriendlyName' => $friendlyName));
                 if ($response && $response->IsError != true) {
                     $account = $response->ResponseXml;
                     $tenant_sid = (string) $account->Account->Sid;
                     $tenant_token = (string) $account->Account->AuthToken;
                     $this->settings->add('twilio_sid', $tenant_sid, $data['id']);
                     $this->settings->add('twilio_token', $tenant_token, $data['id']);
                 } else {
                     $message = 'Failed to create new subaccount';
                     if ($response && $response->ErrorMessage) {
                         $message = $response->ErrorMessage;
                     }
                     throw new VBX_SettingsException($message);
                 }
                 $appSid = $this->create_application_for_subaccount($data['id'], $tenant['url_prefix'], $tenant_sid);
                 $this->settings->add('application_sid', $appSid, $data['id']);
             } catch (Exception $e) {
                 throw new VBX_SettingsException($e->getMessage());
             }
             $this->db->trans_complete();
             $this->session->set_flashdata('error', 'Added new tenant');
             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', $e->getMessage());
             $data['error'] = true;
             $data['message'] = $e->getMessage();
         }
     }
     if ($this->response_type == 'html') {
         redirect('settings/site');
     }
     $this->respond('', 'settings/site', $data);
 }
Ejemplo n.º 3
0
 private function save_user()
 {
     $errors = array();
     $user = false;
     $id = intval($this->input->post('id'));
     $auth_type = $this->input->post('auth_type');
     $error = false;
     $message = "Failed to save user for unknown reason.";
     $shouldGenerateNewPassword = false;
     $device_id_str = trim($this->input->post('device_id'));
     $device_number = trim($this->input->post('device_number'));
     $shouldSendWelcome = false;
     try {
         PhoneNumber::validatePhoneNumber($device_number);
     } catch (PhoneNumberException $e) {
         $data['json'] = array('error' => true, 'message' => $e->getMessage());
         return $this->respond('', 'accounts', $data);
     }
     if (!empty($auth_type)) {
         $auth_type = $this->vbx_user->get_auth_type($auth_type);
     }
     if ($id > 0) {
         $user = VBX_User::get($id);
     } else {
         $user = VBX_User::get(array('email' => $this->input->post('email')));
         if (!empty($user) && $user->is_active == 1) {
             $error = true;
             $message = 'Email address is already in use.';
         } elseif (!empty($user) && $user->is_active == 0) {
             // It's an old account that was made inactive.  By re-adding it, we're
             // assuming the user wants to re-instate the old account.
             $shouldSendWelcome = true;
         } else {
             // It's a new user
             $user = new VBX_User();
             $user->online = 9;
             $shouldSendWelcome = true;
         }
     }
     if (!$error) {
         $fields = array('first_name', 'last_name', 'email', 'is_admin');
         foreach ($fields as $field) {
             $user->{$field} = $this->input->post($field);
         }
         $user->is_active = TRUE;
         $user->auth_type = isset($auth_type->id) ? $auth_type->id : 1;
         try {
             $user->save();
             if ($shouldSendWelcome) {
                 $user->send_new_user_notification();
             }
         } catch (VBX_UserException $e) {
             $error = true;
             $message = $e->getMessage();
             log_message('error', 'Unable to send new user notification: ' . $message);
         }
         if (!$error) {
             if (strlen($device_number) > 0) {
                 // We're adding or modifying an existing device
                 if (strlen($device_id_str) > 0) {
                     // We're updating an existing record
                     $device_id = intval($device_id_str);
                     $device = VBX_Device::get($device_id);
                     $device->value = normalize_phone_to_E164($device_number);
                     try {
                         $device->save();
                     } catch (VBX_DeviceException $e) {
                         $error = true;
                         $message = 'Failed to update device: ' . $e->getMessage();
                     }
                 } else {
                     // We're creating a new device record
                     $number = array("name" => "Primary Device", "value" => normalize_phone_to_E164($device_number), "user_id" => $user->id, "sms" => 1);
                     try {
                         $new_device_id = $this->vbx_device->add($number);
                     } catch (VBX_DeviceException $e) {
                         $error = true;
                         $message = "Failed to add device: " . $e->getMessage();
                     }
                 }
             } else {
                 if (strlen($device_number) == 0 && strlen($device_id_str) > 0) {
                     // We're deleting a device
                     try {
                         $this->vbx_device->delete(intval($device_id_str), $user->id);
                     } catch (VBX_DeviceException $e) {
                         $error = true;
                         $message = "Unable to delete device entry: " . $e->getMessage();
                     }
                 }
             }
         }
     }
     if ($error) {
         $json = array('error' => $error, 'message' => $message);
     } else {
         $json = array('id' => $user->id, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'is_active' => $user->is_active, 'is_admin' => $user->is_admin, 'notification' => $user->notification, 'auth_type' => isset($auth_type->description) ? $auth_type->description : 'openvbx', 'email' => $user->email, 'error' => false, 'message' => '', 'online' => $user->online);
     }
     $data['json'] = $json;
     $this->respond('', 'accounts', $data);
 }