trypost/app/Models/ApiToken.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

71 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\ApiToken\Status;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids as HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ApiToken extends Model
{
use HasFactory, HasUuids;
protected $table = 'api_tokens';
protected $fillable = [
'workspace_id',
'name',
'token_lookup',
'token_hash',
'last_used_at',
'expires_at',
];
protected $hidden = [
'token_lookup',
'token_hash',
];
protected $appends = [
'status',
'key_hint',
];
protected function casts(): array
{
return [
'last_used_at' => 'datetime',
'expires_at' => 'datetime',
];
}
public function workspace(): BelongsTo
{
return $this->belongsTo(Workspace::class);
}
protected function keyHint(): Attribute
{
return Attribute::make(
get: fn () => 'tp_'.substr($this->token_lookup, 0, 8).'...',
);
}
protected function status(): Attribute
{
return Attribute::make(
get: function () {
if ($this->expires_at === null) {
return Status::Active->value;
}
return now()->greaterThan($this->expires_at) ? Status::Expired->value : Status::Active->value;
}
);
}
}