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(); $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->setting_set('online', 9); $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->setting('online')); } $data['json'] = $json; $this->respond('', 'accounts', $data); }