100 lines
2.7 KiB
PHP
Executable File
100 lines
2.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\UserModel;
|
|
|
|
class UserService
|
|
{
|
|
protected $userModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->userModel = new UserModel();
|
|
}
|
|
|
|
/**
|
|
* Get all users with their roles.
|
|
*
|
|
* @param string $sort Column to sort by
|
|
* @param string $order Sorting order (asc/desc)
|
|
* @return array List of users with their roles
|
|
*/
|
|
public function getUsersWithRoles($sort = 'id', $order = 'asc')
|
|
{
|
|
return $this->userModel->getUsersWithRoles($sort, $order);
|
|
}
|
|
|
|
/**
|
|
* Get all unverified users.
|
|
*
|
|
* @return array List of unverified users
|
|
*/
|
|
public function getUnverifiedUsers()
|
|
{
|
|
return $this->userModel->getUnverifiedUsers();
|
|
}
|
|
|
|
/**
|
|
* Validate registration input.
|
|
*
|
|
* @param array $data Input data to validate
|
|
* @return array Validation errors
|
|
*/
|
|
public function validateRegistrationInput(array $data)
|
|
{
|
|
return $this->userModel->validate_registration_input($data);
|
|
}
|
|
|
|
/**
|
|
* Get users by role.
|
|
*
|
|
* @param string $role Role name
|
|
* @return array List of users with the specified role
|
|
*/
|
|
public function getUsersByRole($role)
|
|
{
|
|
return $this->userModel->getUsersByRole($role);
|
|
}
|
|
|
|
/**
|
|
* Assign a role to a user.
|
|
*
|
|
* @param int $userId The ID of the user
|
|
* @param int $roleId The ID of the role to assign
|
|
* @return bool True if the role was successfully assigned
|
|
*/
|
|
public function assignRole($userId, $roleId)
|
|
{
|
|
return $this->userModel->assignRole($userId, $roleId);
|
|
}
|
|
|
|
// Additional business logic related to users can be added here
|
|
public function cleanupUnverifiedUsers()
|
|
{
|
|
// Load the UserModel
|
|
$userModel = new UserModel();
|
|
|
|
// Get the current timestamp and subtract 1 hour
|
|
//$oneHourAgo = date('Y-m-d H:i:s', strtotime('-1 hour'));
|
|
$fifteenMinutesAgo = date('Y-m-d H:i:s', strtotime('-15 minutes'));
|
|
|
|
|
|
// Find and delete users where:
|
|
// 1. 'is_verified' is false (unverified users)
|
|
// 2. 'created_at' is more than 1 hour ago
|
|
$builder = $userModel->builder();
|
|
$builder->where('is_verified', false)
|
|
->where('created_at <=', $fifteenMinutesAgo)
|
|
->delete();
|
|
|
|
// Get the database connection to retrieve affected rows
|
|
$db = \Config\Database::connect();
|
|
$affectedRows = $db->affectedRows();
|
|
|
|
// Log the number of affected rows (users deleted)
|
|
log_message('info', "Deleted {$affectedRows} unverified users who were created more than 1 hour ago.");
|
|
|
|
return true;
|
|
}
|
|
} |