mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Merge pull request #1344 from flarum/1236-database-changes
Database changes
This commit is contained in:
@@ -11,22 +11,19 @@
|
||||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\User\User;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
* @property string $token
|
||||
* @property int $user_id
|
||||
* @property int $last_activity
|
||||
* @property int $lifetime
|
||||
* @property int $last_activity_at
|
||||
* @property int $lifetime_seconds
|
||||
* @property \Flarum\User\User|null $user
|
||||
*/
|
||||
class AccessToken extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $table = 'access_tokens';
|
||||
|
||||
/**
|
||||
* Use a custom primary key for this model.
|
||||
*
|
||||
@@ -34,6 +31,10 @@ class AccessToken extends AbstractModel
|
||||
*/
|
||||
public $incrementing = false;
|
||||
|
||||
protected $primaryKey = 'token';
|
||||
|
||||
protected $dates = ['last_activity_at'];
|
||||
|
||||
/**
|
||||
* Generate an access token for the specified user.
|
||||
*
|
||||
@@ -45,17 +46,17 @@ class AccessToken extends AbstractModel
|
||||
{
|
||||
$token = new static;
|
||||
|
||||
$token->id = str_random(40);
|
||||
$token->token = str_random(40);
|
||||
$token->user_id = $userId;
|
||||
$token->last_activity = time();
|
||||
$token->lifetime = $lifetime;
|
||||
$token->last_activity_at = Carbon::now();
|
||||
$token->lifetime_seconds = $lifetime;
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
public function touch()
|
||||
{
|
||||
$this->last_activity = time();
|
||||
$this->last_activity_at = Carbon::now();
|
||||
|
||||
return $this->save();
|
||||
}
|
||||
@@ -67,6 +68,6 @@ class AccessToken extends AbstractModel
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Flarum\User\User');
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Http\AccessToken;
|
||||
use Flarum\User\AuthToken;
|
||||
use Flarum\User\EmailToken;
|
||||
@@ -55,9 +56,11 @@ class CollectGarbage implements Middleware
|
||||
return;
|
||||
}
|
||||
|
||||
AccessToken::whereRaw('last_activity <= ? - lifetime', [time()])->delete();
|
||||
$time = Carbon::now()->timestamp;
|
||||
|
||||
$earliestToKeep = date('Y-m-d H:i:s', time() - 24 * 60 * 60);
|
||||
AccessToken::whereRaw('last_activity_at <= ? - lifetime_seconds', [$time])->delete();
|
||||
|
||||
$earliestToKeep = date('Y-m-d H:i:s', $time - 24 * 60 * 60);
|
||||
|
||||
EmailToken::where('created_at', '<=', $earliestToKeep)->delete();
|
||||
PasswordToken::where('created_at', '<=', $earliestToKeep)->delete();
|
||||
|
@@ -33,12 +33,12 @@ class Rememberer
|
||||
|
||||
public function remember(ResponseInterface $response, AccessToken $token)
|
||||
{
|
||||
$token->lifetime = 5 * 365 * 24 * 60 * 60; // 5 years
|
||||
$token->lifetime_seconds = 5 * 365 * 24 * 60 * 60; // 5 years
|
||||
$token->save();
|
||||
|
||||
return FigResponseCookies::set(
|
||||
$response,
|
||||
$this->cookie->make(self::COOKIE_NAME, $token->id, $token->lifetime)
|
||||
$this->cookie->make(self::COOKIE_NAME, $token->token, $token->lifetime_seconds)
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user