File: /var/www/heifetz/heifetz-app/models/LogAuth.php
<?php
declare(strict_types=1);
namespace Models;
use Core\DbLib\DbModel;
use Core\Models\CoreHelper;
use Core\Models\OldUser;
use Helpers\Data;
use Traits\ModelGetQuery;
use Traits\ModelSaveWithLogTrait;
/**
* Class LogAuth
*
* @property int $user_id
* @property string $created_at
* @property string $ip
* @property string $location
* @property string $user_agent
* @property string $agent
* @property string $cookie
* @property int $id
* @property int $company_id
*/
class LogAuth extends DbModel
{
use ModelGetQuery;
use ModelSaveWithLogTrait;
static string $tableName = 'logs_auth';
protected static bool $hasCompanyRelation = true;
public static function log($userId)
{
$ip = $_SERVER['REMOTE_ADDR'];
$agentInfo = parse_user_agent();
$cookie = $_COOKIE['rtvahtaAuth'] ?? '';
if (is_null($cookie)) {
$cookie = Data::strRand();
setcookie('rtvahtaAuth', $cookie);
}
self::create(
[
'user_id' => $userId,
'created_at' => date('Y-m-d H:i:s'),
'ip' => $ip,
'agent' => $agentInfo['platform'] . ': ' . $agentInfo['browser'] . ' ' . $agentInfo['version'],
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'cookie' => $cookie,
'company_id' => CoreHelper::$companyId,
], false
);
}
public static function getUsersIds(): array
{
return self::getQuery(['la.user_id'], 'la')
->join(OldUser::$tableName . ' u', 'la.user_id = u.id')
->group('la.user_id, u.username')
->order('u.username')
->fetchCol();
}
public static function getIps(): array
{
return self::getQuery(['ip'])->group('ip')->fetchCol();
}
public static function getWithEmptyLocation()
{
return self::getQuery()
->where('location IS NULL OR location = ?', '')
->fetchAll();
}
public static function getWithEmptyLocationCount()
{
return self::getQuery()
->where('location IS NULL OR location = ?', '')
->totalCount();
}
}