2014-07-29 10:07:20 +00:00
|
|
|
<?php namespace Backend\Models;
|
|
|
|
|
|
|
|
|
|
use Model;
|
|
|
|
|
use Request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Model for logging access to the back-end
|
2014-10-03 08:00:21 +00:00
|
|
|
*
|
|
|
|
|
* @package october\backend
|
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
2014-07-29 10:07:20 +00:00
|
|
|
*/
|
|
|
|
|
class AccessLog extends Model
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var string The database table used by the model.
|
|
|
|
|
*/
|
|
|
|
|
protected $table = 'backend_access_log';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Relations
|
|
|
|
|
*/
|
|
|
|
|
public $belongsTo = [
|
2017-05-20 10:01:19 +00:00
|
|
|
'user' => User::class
|
2014-07-29 10:07:20 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a log record
|
|
|
|
|
* @param Backend\Models\User $user Admin user
|
|
|
|
|
* @return self
|
|
|
|
|
*/
|
|
|
|
|
public static function add($user)
|
|
|
|
|
{
|
|
|
|
|
$record = new static;
|
|
|
|
|
$record->user = $user;
|
|
|
|
|
$record->ip_address = Request::getClientIp();
|
|
|
|
|
$record->save();
|
|
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
|
}
|
2016-05-28 01:10:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a recent entry, latest entry is not considered recent
|
|
|
|
|
* if the creation day is the same as today.
|
|
|
|
|
* @return self
|
|
|
|
|
*/
|
|
|
|
|
public static function getRecent($user)
|
|
|
|
|
{
|
|
|
|
|
$records = static::where('user_id', $user->id)
|
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
|
->limit(2)
|
2020-10-20 21:02:53 +00:00
|
|
|
->get();
|
2016-05-28 01:10:33 +00:00
|
|
|
|
|
|
|
|
if (!count($records)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$first = $records->first();
|
|
|
|
|
|
|
|
|
|
return !$first->created_at->isToday() ? $first : $records->pop();
|
|
|
|
|
}
|
2014-10-10 22:04:51 +00:00
|
|
|
}
|