2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Api;
|
2019-12-29 14:02:26 +01:00
|
|
|
|
2019-12-30 03:16:07 +01:00
|
|
|
use BookStack\Auth\User;
|
2020-11-20 19:53:01 +01:00
|
|
|
use BookStack\Interfaces\Loggable;
|
2019-12-29 14:02:26 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-12-30 03:16:07 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2019-12-30 15:51:28 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-12-29 14:02:26 +01:00
|
|
|
|
2020-11-20 19:53:01 +01:00
|
|
|
/**
|
2021-06-26 17:23:15 +02:00
|
|
|
* Class ApiToken.
|
|
|
|
*
|
|
|
|
* @property int $id
|
2020-11-20 19:53:01 +01:00
|
|
|
* @property string $token_id
|
|
|
|
* @property string $secret
|
|
|
|
* @property string $name
|
|
|
|
* @property Carbon $expires_at
|
2021-06-26 17:23:15 +02:00
|
|
|
* @property User $user
|
2020-11-20 19:53:01 +01:00
|
|
|
*/
|
|
|
|
class ApiToken extends Model implements Loggable
|
2019-12-29 14:02:26 +01:00
|
|
|
{
|
|
|
|
protected $fillable = ['name', 'expires_at'];
|
2019-12-29 18:03:52 +01:00
|
|
|
protected $casts = [
|
2021-06-26 17:23:15 +02:00
|
|
|
'expires_at' => 'date:Y-m-d',
|
2019-12-29 18:03:52 +01:00
|
|
|
];
|
2019-12-30 03:16:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user that this token belongs to.
|
|
|
|
*/
|
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
2019-12-30 15:51:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default expiry value for an API token.
|
|
|
|
* Set to 100 years from now.
|
|
|
|
*/
|
|
|
|
public static function defaultExpiry(): string
|
|
|
|
{
|
|
|
|
return Carbon::now()->addYears(100)->format('Y-m-d');
|
|
|
|
}
|
2020-11-20 19:53:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
|
|
|
|
}
|
2019-12-29 14:02:26 +01:00
|
|
|
}
|