]> BookStack Code Mirror - bookstack/blob - app/Api/ApiToken.php
Merge pull request #2360 from BookStackApp/activity_revamp
[bookstack] / app / Api / ApiToken.php
1 <?php namespace BookStack\Api;
2
3 use BookStack\Auth\User;
4 use BookStack\Interfaces\Loggable;
5 use Illuminate\Database\Eloquent\Model;
6 use Illuminate\Database\Eloquent\Relations\BelongsTo;
7 use Illuminate\Support\Carbon;
8
9 /**
10  * Class ApiToken
11  * @property int $id
12  * @property string $token_id
13  * @property string $secret
14  * @property string $name
15  * @property Carbon $expires_at
16  * @property User $user
17  * @package BookStack\Api
18  */
19 class ApiToken extends Model implements Loggable
20 {
21     protected $fillable = ['name', 'expires_at'];
22     protected $casts = [
23         'expires_at' => 'date:Y-m-d'
24     ];
25
26     /**
27      * Get the user that this token belongs to.
28      */
29     public function user(): BelongsTo
30     {
31         return $this->belongsTo(User::class);
32     }
33
34     /**
35      * Get the default expiry value for an API token.
36      * Set to 100 years from now.
37      */
38     public static function defaultExpiry(): string
39     {
40         return Carbon::now()->addYears(100)->format('Y-m-d');
41     }
42
43     /**
44      * @inheritdoc
45      */
46     public function logDescriptor(): string
47     {
48         return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
49     }
50 }