Skip to content

Commit

Permalink
Drop support for soft deletes in order to reduce the code complexity …
Browse files Browse the repository at this point in the history
…and data usage
  • Loading branch information
alextselegidis committed Oct 19, 2023
1 parent 1710d46 commit cf19a90
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 474 deletions.
4 changes: 2 additions & 2 deletions application/libraries/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function regenerate_password(string $username, string $email): string

$user = $query->row_array();

// Generate a new password for the user.
// Generate a new password for the user.
$new_password = random_string('alnum', 12);

$salt = $this->get_salt_by_username($username);
Expand All @@ -157,6 +157,6 @@ public function regenerate_password(string $username, string $email): string
*/
public function does_account_exist(int $user_id): bool
{
return $this->CI->users_model->query()->where(['id' => $user_id, 'delete_datetime' => NULL])->get()->num_rows() > 0;
return $this->CI->users_model->query()->where(['id' => $user_id])->get()->num_rows() > 0;
}
}
4 changes: 4 additions & 0 deletions application/migrations/037_add_timestamp_columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* ---------------------------------------------------------------------------- */

class Migration_Add_timestamp_columns extends EA_Migration {
/**
* @var string[]
*/
protected $tables = [
'appointments',
'categories',
Expand All @@ -21,6 +24,7 @@ class Migration_Add_timestamp_columns extends EA_Migration {
'settings',
'users'
];

/**
* @var string[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <[email protected]>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.4.0
* ---------------------------------------------------------------------------- */

class Migration_Drop_delete_datetime_column_from_all_tables extends EA_Migration {
/**
* @var string[]
*/
protected $tables = [
'appointments',
'categories',
'consents',
'roles',
'services',
'settings',
'users',
'webhooks'
];

/**
* Upgrade method.
*/
public function up()
{
foreach ($this->tables as $table)
{
if ($this->db->field_exists('delete_datetime', $table))
{
$this->dbforge->drop_column($table, 'delete_datetime');
}
}
}

/**
* Downgrade method.
*/
public function down()
{
foreach ($this->tables as $table)
{
if ( ! $this->db->field_exists('delete_datetime', $table))
{
$fields = [
'delete_datetime' => [
'type' => 'DATETIME',
'null' => TRUE,
'after' => 'update_datetime',
]
];

$this->dbforge->add_column($table, $fields);
}
}
}
}
53 changes: 13 additions & 40 deletions application/models/Admins_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function validate(array $admin)
}
}

// Make sure all required fields are provided.
// Make sure all required fields are provided.
if (
empty($admin['first_name'])
|| empty($admin['last_name'])
Expand All @@ -108,7 +108,7 @@ public function validate(array $admin)
throw new InvalidArgumentException('Invalid email address provided: ' . $admin['email']);
}

// Make sure the username is unique.
// Make sure the username is unique.
if ( ! empty($admin['settings']['username']))
{
$admin_id = $admin['id'] ?? NULL;
Expand All @@ -119,7 +119,7 @@ public function validate(array $admin)
}
}

// Validate the password.
// Validate the password.
if ( ! empty($admin['settings']['password']))
{
if (strlen($admin['settings']['password']) < MIN_PASSWORD_LENGTH)
Expand All @@ -128,7 +128,7 @@ public function validate(array $admin)
}
}

// New users must always have a password value set.
// New users must always have a password value set.
if (empty($admin['id']) && empty($admin['settings']['password']))
{
throw new InvalidArgumentException('The admin password cannot be empty when inserting a new record.');
Expand All @@ -154,7 +154,6 @@ public function validate(array $admin)
->where('roles.slug', DB_SLUG_ADMIN)
->where('users.email', $admin['email'])
->where('users.id !=', $admin_id)
->where('users.delete_datetime')
->get()
->num_rows();

Expand Down Expand Up @@ -183,7 +182,7 @@ public function validate_username(string $username, int $admin_id = NULL): bool
->db
->from('users')
->join('user_settings', 'user_settings.id_users = users.id', 'inner')
->where(['username' => $username, 'delete_datetime' => NULL])
->where(['username' => $username])
->get()
->num_rows() === 0;
}
Expand Down Expand Up @@ -272,48 +271,34 @@ protected function update(array $admin): int
* Remove an existing admin from the database.
*
* @param int $admin_id Admin ID.
* @param bool $force_delete Override soft delete.
*
* @throws RuntimeException
*/
public function delete(int $admin_id, bool $force_delete = FALSE)
public function delete(int $admin_id): void
{
$role_id = $this->get_admin_role_id();

$count = $this->db->get_where('users', ['id_roles' => $role_id, 'delete_datetime' => NULL])->num_rows();
$count = $this->db->get_where('users', ['id_roles' => $role_id])->num_rows();

if ($count <= 1)
{
throw new RuntimeException('Record could not be deleted as the app requires at least one admin user.');
}

if ($force_delete)
{
$this->db->delete('users', ['id' => $admin_id]);
}
else
{
$this->db->update('users', ['delete_datetime' => date('Y-m-d H:i:s')], ['id' => $admin_id]);
}
$this->db->delete('users', ['id' => $admin_id]);
}

/**
* Get a specific admin from the database.
*
* @param int $admin_id The ID of the record to be returned.
* @param bool $with_trashed
*
* @return array Returns an array with the admin data.
*
* @throws InvalidArgumentException
*/
public function find(int $admin_id, bool $with_trashed = FALSE): array
public function find(int $admin_id): array
{
if ( ! $with_trashed)
{
$this->db->where('delete_datetime IS NULL');
}

$admin = $this->db->get_where('users', ['id' => $admin_id])->row_array();

if ( ! $admin)
Expand Down Expand Up @@ -384,11 +369,10 @@ public function value(int $admin_id, string $field): mixed
* @param int|null $limit Record limit.
* @param int|null $offset Record offset.
* @param string|null $order_by Order by.
* @param bool $with_trashed
*
* @return array Returns an array of admins.
*/
public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL, bool $with_trashed = FALSE): array
public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
$role_id = $this->get_admin_role_id();

Expand All @@ -402,11 +386,6 @@ public function get(array|string $where = NULL, int $limit = NULL, int $offset =
$this->db->order_by($order_by);
}

if ( ! $with_trashed)
{
$this->db->where('delete_datetime IS NULL');
}

$admins = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();

foreach ($admins as &$admin)
Expand Down Expand Up @@ -457,7 +436,7 @@ protected function save_settings(int $admin_id, array $settings)
throw new InvalidArgumentException('The settings argument cannot be empty.');
}

// Make sure the settings record exists in the database.
// Make sure the settings record exists in the database.
$count = $this->db->get_where('user_settings', ['id_users' => $admin_id])->num_rows();

if ( ! $count)
Expand Down Expand Up @@ -525,19 +504,13 @@ public function query(): CI_DB_query_builder
* @param int|null $limit Record limit.
* @param int|null $offset Record offset.
* @param string|null $order_by Order by.
* @param bool $with_trashed
*
* @return array Returns an array of admins.
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL, bool $with_trashed = FALSE): array
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
$role_id = $this->get_admin_role_id();

if ( ! $with_trashed)
{
$this->db->where('delete_datetime IS NULL');
}

$admins = $this
->db
->select()
Expand Down Expand Up @@ -588,7 +561,7 @@ public function search(string $keyword, int $limit = NULL, int $offset = NULL, s
*/
public function load(array &$admin, array $resources)
{
// Admins do not currently have any related resources (settings are already part of the admins).
// Admins do not currently have any related resources (settings are already part of the admins).
}

/**
Expand Down
Loading

0 comments on commit cf19a90

Please sign in to comment.