2021-10-27 11:05:43 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-10-27 11:05:43 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2021-10-27 11:05:43 +03:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Users model.
|
2021-10-27 11:05:43 +03:00
|
|
|
*
|
|
|
|
* Handles all the database operations of the user resource.
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Users_model extends EA_Model
|
|
|
|
{
|
2021-10-29 13:39:38 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $casts = [
|
2021-10-29 13:39:38 +03:00
|
|
|
'id' => 'integer',
|
2023-12-22 13:35:41 +03:00
|
|
|
'id_roles' => 'integer',
|
2021-10-29 13:39:38 +03:00
|
|
|
];
|
2021-11-06 17:02:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $api_resource = [
|
2021-11-06 17:02:40 +03:00
|
|
|
'id' => 'id',
|
|
|
|
'firstName' => 'first_name',
|
|
|
|
'lastName' => 'last_name',
|
|
|
|
'email' => 'email',
|
|
|
|
'mobile' => 'mobile_number',
|
|
|
|
'phone' => 'phone_number',
|
|
|
|
'address' => 'address',
|
|
|
|
'city' => 'city',
|
|
|
|
'state' => 'state',
|
|
|
|
'zip' => 'zip_code',
|
|
|
|
'timezone' => 'timezone',
|
|
|
|
'language' => 'language',
|
2024-05-14 00:36:54 +03:00
|
|
|
'ldapDn' => 'ldap_dn',
|
2021-11-06 17:02:40 +03:00
|
|
|
'notes' => 'notes',
|
2023-12-22 13:35:41 +03:00
|
|
|
'roleId' => 'id_roles',
|
2021-11-06 17:02:40 +03:00
|
|
|
];
|
2021-12-18 20:55:42 +03:00
|
|
|
|
2021-10-27 11:05:43 +03:00
|
|
|
/**
|
|
|
|
* Save (insert or update) a user.
|
|
|
|
*
|
|
|
|
* @param array $user Associative array with the user data.
|
|
|
|
*
|
|
|
|
* @return int Returns the user ID.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws Exception
|
2021-10-27 11:05:43 +03:00
|
|
|
*/
|
|
|
|
public function save(array $user): int
|
|
|
|
{
|
|
|
|
$this->validate($user);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($user['id'])) {
|
2021-10-27 11:05:43 +03:00
|
|
|
return $this->insert($user);
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2021-10-27 11:05:43 +03:00
|
|
|
return $this->update($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the user data.
|
|
|
|
*
|
|
|
|
* @param array $user Associative array with the user data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function validate(array $user): void
|
2021-10-27 11:05:43 +03:00
|
|
|
{
|
|
|
|
// If a user ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($user['id'])) {
|
2021-10-27 11:05:43 +03:00
|
|
|
$count = $this->db->get_where('users', ['id' => $user['id']])->num_rows();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$count) {
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
'The provided user ID does not exist in the database: ' . $user['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-10-27 11:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all required fields are provided.
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
empty($user['first_name']) ||
|
|
|
|
empty($user['last_name']) ||
|
|
|
|
empty($user['email']) ||
|
|
|
|
empty($user['phone_number'])
|
|
|
|
) {
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($user, true));
|
2021-10-27 11:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new user into the database.
|
|
|
|
*
|
|
|
|
* @param array $user Associative array with the user data.
|
|
|
|
*
|
|
|
|
* @return int Returns the user ID.
|
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws RuntimeException|Exception
|
2021-10-27 11:05:43 +03:00
|
|
|
*/
|
|
|
|
protected function insert(array $user): int
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
$user['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$user['update_datetime'] = date('Y-m-d H:i:s');
|
2023-10-19 17:25:45 +03:00
|
|
|
|
2021-10-27 11:05:43 +03:00
|
|
|
$settings = $user['settings'];
|
|
|
|
unset($user['settings']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->insert('users', $user)) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new RuntimeException('Could not insert user.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$user['id'] = $this->db->insert_id();
|
|
|
|
$settings['salt'] = generate_salt();
|
|
|
|
$settings['password'] = hash_password($settings['salt'], $settings['password']);
|
|
|
|
|
2024-05-14 00:36:54 +03:00
|
|
|
$this->set_settings($user['id'], $settings);
|
2021-10-27 11:05:43 +03:00
|
|
|
|
|
|
|
return $user['id'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Save the user settings.
|
|
|
|
*
|
|
|
|
* @param int $user_id User ID.
|
|
|
|
* @param array $settings Associative array with the settings data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2024-05-14 00:36:54 +03:00
|
|
|
protected function set_settings(int $user_id, array $settings): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
if (empty($settings)) {
|
|
|
|
throw new InvalidArgumentException('The settings argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the settings record exists in the database.
|
|
|
|
$count = $this->db->get_where('user_settings', ['id_users' => $user_id])->num_rows();
|
|
|
|
|
|
|
|
if (!$count) {
|
|
|
|
$this->db->insert('user_settings', ['id_users' => $user_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($settings as $name => $value) {
|
|
|
|
$this->set_setting($user_id, $name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 00:36:54 +03:00
|
|
|
/**
|
|
|
|
* Get the user settings.
|
|
|
|
*
|
|
|
|
* @param int $user_id User ID.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function get_settings(int $user_id): array
|
|
|
|
{
|
|
|
|
$settings = $this->db->get_where('user_settings', ['id_users' => $user_id])->row_array();
|
|
|
|
|
|
|
|
unset($settings['id_users'], $settings['password'], $settings['salt']);
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Set the value of a user setting.
|
|
|
|
*
|
|
|
|
* @param int $user_id User ID.
|
|
|
|
* @param string $name Setting name.
|
|
|
|
* @param string $value Setting value.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function set_setting(int $user_id, string $name, string $value): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
if (!$this->db->update('user_settings', [$name => $value], ['id_users' => $user_id])) {
|
|
|
|
throw new RuntimeException('Could not set the new user setting value: ' . $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 11:05:43 +03:00
|
|
|
/**
|
|
|
|
* Update an existing user.
|
|
|
|
*
|
|
|
|
* @param array $user Associative array with the user data.
|
|
|
|
*
|
|
|
|
* @return int Returns the user ID.
|
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws RuntimeException|Exception
|
2021-10-27 11:05:43 +03:00
|
|
|
*/
|
|
|
|
protected function update(array $user): int
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
$user['update_datetime'] = date('Y-m-d H:i:s');
|
2023-10-19 17:25:45 +03:00
|
|
|
|
2021-10-27 11:05:43 +03:00
|
|
|
$settings = $user['settings'];
|
|
|
|
unset($user['settings']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (isset($settings['password'])) {
|
2021-10-27 11:05:43 +03:00
|
|
|
$existing_settings = $this->db->get_where('user_settings', ['id_users' => $user['id']])->row_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($existing_settings)) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new RuntimeException('No settings record found for user with ID: ' . $user['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->update('users', $user, ['id' => $user['id']])) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new RuntimeException('Could not update user.');
|
|
|
|
}
|
|
|
|
|
2024-05-14 00:36:54 +03:00
|
|
|
$this->set_settings($user['id'], $settings);
|
2021-10-27 11:05:43 +03:00
|
|
|
|
|
|
|
return $user['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing user from the database.
|
|
|
|
*
|
|
|
|
* @param int $user_id User ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function delete(int $user_id): void
|
2021-10-27 11:05:43 +03:00
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
$this->db->delete('users', ['id' => $user_id]);
|
2021-10-27 11:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific user from the database.
|
|
|
|
*
|
|
|
|
* @param int $user_id The ID of the record to be returned.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the user data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function find(int $user_id): array
|
2021-10-27 11:05:43 +03:00
|
|
|
{
|
|
|
|
$user = $this->db->get_where('users', ['id' => $user_id])->row_array();
|
2021-12-18 20:55:42 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$user) {
|
2022-01-25 01:32:42 +03:00
|
|
|
throw new InvalidArgumentException('The provided user ID was not found in the database: ' . $user_id);
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:39:38 +03:00
|
|
|
$this->cast($user);
|
2021-10-27 11:05:43 +03:00
|
|
|
|
2024-05-14 00:36:54 +03:00
|
|
|
$user['settings'] = $this->get_settings($user['id']);
|
2021-10-27 11:05:43 +03:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
|
|
|
* @param int $user_id User ID.
|
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
* @return mixed Returns the selected user value from the database.
|
2021-10-27 11:05:43 +03:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
public function value(int $user_id, string $field): mixed
|
2021-10-27 11:05:43 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($field)) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($user_id)) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new InvalidArgumentException('The user ID argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the user exists.
|
|
|
|
$query = $this->db->get_where('users', ['id' => $user_id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$query->num_rows()) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new InvalidArgumentException('The provided user ID was not found in the database: ' . $user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the required field is part of the user data.
|
|
|
|
$user = $query->row_array();
|
2021-12-18 20:55:42 +03:00
|
|
|
|
2021-10-29 13:39:38 +03:00
|
|
|
$this->cast($user);
|
2021-10-27 11:05:43 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($field, $user)) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new InvalidArgumentException('The requested field was not found in the user data: ' . $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of a user setting.
|
|
|
|
*
|
|
|
|
* @param int $user_id User ID.
|
|
|
|
* @param string $name Setting name.
|
|
|
|
*
|
|
|
|
* @return string Returns the value of the requested user setting.
|
|
|
|
*/
|
|
|
|
public function get_setting(int $user_id, string $name): string
|
|
|
|
{
|
|
|
|
$settings = $this->db->get_where('user_settings', ['id_users' => $user_id])->row_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($settings[$name])) {
|
2021-10-27 11:05:43 +03:00
|
|
|
throw new RuntimeException('The requested setting value was not found: ' . $user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query builder interface, configured for use with the users table.
|
|
|
|
*
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
*/
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
{
|
|
|
|
return $this->db->from('users');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search users by the provided keyword.
|
|
|
|
*
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
2023-10-19 17:25:45 +03:00
|
|
|
*
|
2021-10-27 11:05:43 +03:00
|
|
|
* @return array Returns an array of settings.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2021-10-27 11:05:43 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$users = $this->db
|
2021-10-27 11:05:43 +03:00
|
|
|
->select()
|
|
|
|
->from('users')
|
2022-07-07 16:27:10 +03:00
|
|
|
->group_start()
|
2021-10-27 11:05:43 +03:00
|
|
|
->like('first_name', $keyword)
|
|
|
|
->or_like('last_name', $keyword)
|
|
|
|
->or_like('email', $keyword)
|
|
|
|
->or_like('phone_number', $keyword)
|
|
|
|
->or_like('mobile_number', $keyword)
|
|
|
|
->or_like('address', $keyword)
|
|
|
|
->or_like('city', $keyword)
|
|
|
|
->or_like('state', $keyword)
|
|
|
|
->or_like('zip_code', $keyword)
|
|
|
|
->or_like('notes', $keyword)
|
2022-07-07 16:27:10 +03:00
|
|
|
->group_end()
|
2021-10-27 11:05:43 +03:00
|
|
|
->limit($limit)
|
|
|
|
->offset($offset)
|
|
|
|
->order_by($order_by)
|
|
|
|
->get()
|
|
|
|
->result_array();
|
2021-10-29 13:39:38 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($users as &$user) {
|
|
|
|
$this->cast($user);
|
2024-05-14 00:36:54 +03:00
|
|
|
$user['settings'] = $this->get_settings($user['id']);
|
2023-11-29 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all users that match the provided criteria.
|
|
|
|
*
|
|
|
|
* @param array|string|null $where Where conditions
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of users.
|
|
|
|
*/
|
|
|
|
public function get(
|
|
|
|
array|string $where = null,
|
|
|
|
int $limit = null,
|
|
|
|
int $offset = null,
|
2023-12-22 13:35:41 +03:00
|
|
|
string $order_by = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
): array {
|
|
|
|
if ($where !== null) {
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = $this->db->get('users', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
foreach ($users as &$user) {
|
2021-10-29 13:39:38 +03:00
|
|
|
$this->cast($user);
|
2024-05-14 00:36:54 +03:00
|
|
|
$user['settings'] = $this->get_settings($user['id']);
|
2021-10-29 13:39:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
2021-10-27 11:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
* Load related resources to a user.
|
2021-10-27 11:05:43 +03:00
|
|
|
*
|
|
|
|
* @param array $user Associative array with the user data.
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
public function load(array &$user, array $resources)
|
2021-10-27 11:05:43 +03:00
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
// Users do not currently have any related resources.
|
2021-10-27 11:05:43 +03:00
|
|
|
}
|
2021-12-18 20:55:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the username.
|
|
|
|
*
|
|
|
|
* @param string $username Username.
|
|
|
|
* @param int|null $user_id Exclude user ID.
|
|
|
|
*
|
|
|
|
* @return bool Returns the validation result.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function validate_username(string $username, int $user_id = null): bool
|
2021-12-18 20:55:42 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($user_id)) {
|
2021-12-18 20:55:42 +03:00
|
|
|
$this->db->where('id_users !=', $user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db->get_where('user_settings', ['username' => $username])->num_rows() === 0;
|
|
|
|
}
|
2021-10-27 11:05:43 +03:00
|
|
|
}
|