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