]> BookStack Code Mirror - bookstack/blobdiff - app/Api/ApiToken.php
Customization: Added parent tag classes
[bookstack] / app / Api / ApiToken.php
index cdcb33a7bf2a65646bf4a67a91d1239c6ad55063..ca89c813ed09be449376a22c8a1a739c0b94977e 100644 (file)
@@ -1,14 +1,31 @@
-<?php namespace BookStack\Api;
+<?php
 
-use BookStack\Auth\User;
+namespace BookStack\Api;
+
+use BookStack\Activity\Models\Loggable;
+use BookStack\Users\Models\User;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Support\Carbon;
 
-class ApiToken extends Model
+/**
+ * Class ApiToken.
+ *
+ * @property int    $id
+ * @property string $token_id
+ * @property string $secret
+ * @property string $name
+ * @property Carbon $expires_at
+ * @property User   $user
+ */
+class ApiToken extends Model implements Loggable
 {
+    use HasFactory;
+
     protected $fillable = ['name', 'expires_at'];
     protected $casts = [
-        'expires_at' => 'date:Y-m-d'
+        'expires_at' => 'date:Y-m-d',
     ];
 
     /**
@@ -18,4 +35,29 @@ class ApiToken extends Model
     {
         return $this->belongsTo(User::class);
     }
+
+    /**
+     * 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');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function logDescriptor(): string
+    {
+        return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
+    }
+
+    /**
+     * Get the URL for managing this token.
+     */
+    public function getUrl(string $path = ''): string
+    {
+        return url("/api-tokens/{$this->user_id}/{$this->id}/" . trim($path, '/'));
+    }
 }