]> BookStack Code Mirror - bookstack/blob - app/Api/ApiToken.php
Moved models to folder, renamed managers to tools
[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  */
18 class ApiToken extends Model implements Loggable
19 {
20     protected $fillable = ['name', 'expires_at'];
21     protected $casts = [
22         'expires_at' => 'date:Y-m-d'
23     ];
24
25     /**
26      * Get the user that this token belongs to.
27      */
28     public function user(): BelongsTo
29     {
30         return $this->belongsTo(User::class);
31     }
32
33     /**
34      * Get the default expiry value for an API token.
35      * Set to 100 years from now.
36      */
37     public static function defaultExpiry(): string
38     {
39         return Carbon::now()->addYears(100)->format('Y-m-d');
40     }
41
42     /**
43      * @inheritdoc
44      */
45     public function logDescriptor(): string
46     {
47         return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
48     }
49 }