2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Api;
|
2019-12-29 21:02:26 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
|
|
|
use BookStack\Users\Models\User;
|
2023-07-12 23:16:12 +08:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2019-12-29 21:02:26 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-12-30 10:16:07 +08:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2019-12-30 22:51:28 +08:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-12-29 21:02:26 +08:00
|
|
|
|
2020-11-21 02:53:01 +08:00
|
|
|
/**
|
2021-06-26 23:23:15 +08:00
|
|
|
* Class ApiToken.
|
|
|
|
*
|
|
|
|
* @property int $id
|
2020-11-21 02:53:01 +08:00
|
|
|
* @property string $token_id
|
|
|
|
* @property string $secret
|
|
|
|
* @property string $name
|
|
|
|
* @property Carbon $expires_at
|
2021-06-26 23:23:15 +08:00
|
|
|
* @property User $user
|
2020-11-21 02:53:01 +08:00
|
|
|
*/
|
|
|
|
class ApiToken extends Model implements Loggable
|
2019-12-29 21:02:26 +08:00
|
|
|
{
|
2023-07-12 23:16:12 +08:00
|
|
|
use HasFactory;
|
|
|
|
|
2019-12-29 21:02:26 +08:00
|
|
|
protected $fillable = ['name', 'expires_at'];
|
2019-12-30 01:03:52 +08:00
|
|
|
protected $casts = [
|
2021-06-26 23:23:15 +08:00
|
|
|
'expires_at' => 'date:Y-m-d',
|
2019-12-30 01:03:52 +08:00
|
|
|
];
|
2019-12-30 10:16:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user that this token belongs to.
|
|
|
|
*/
|
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
2019-12-30 22:51:28 +08: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-21 02:53:01 +08:00
|
|
|
|
|
|
|
/**
|
2021-10-27 05:04:18 +08:00
|
|
|
* {@inheritdoc}
|
2020-11-21 02:53:01 +08:00
|
|
|
*/
|
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
|
|
|
|
}
|
2023-10-19 18:31:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL for managing this token.
|
|
|
|
*/
|
|
|
|
public function getUrl(string $path = ''): string
|
|
|
|
{
|
|
|
|
return url("/api-tokens/{$this->user_id}/{$this->id}/" . trim($path, '/'));
|
|
|
|
}
|
2019-12-29 21:02:26 +08:00
|
|
|
}
|