54 lines
1.2 KiB
PHP
54 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\IntegrationHub\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class IhOdataToken extends Model
|
|
{
|
|
protected $table = 'ih_odata_tokens';
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $casts = [
|
|
'scopes' => 'array',
|
|
'expires_at' => 'datetime',
|
|
];
|
|
|
|
public function scopeForBusiness($query, int $businessId)
|
|
{
|
|
return $query->where('business_id', $businessId);
|
|
}
|
|
|
|
public function scopeValid($query)
|
|
{
|
|
return $query->where(function ($q) {
|
|
$q->whereNull('expires_at')
|
|
->orWhere('expires_at', '>', now());
|
|
});
|
|
}
|
|
|
|
public function isExpired(): bool
|
|
{
|
|
return $this->expires_at !== null && $this->expires_at->isPast();
|
|
}
|
|
|
|
public function hasScope(string $dataset): bool
|
|
{
|
|
$scopes = $this->scopes ?? [];
|
|
|
|
return in_array('*', $scopes, true) || in_array($dataset, $scopes, true);
|
|
}
|
|
|
|
public static function hashToken(string $plainToken): string
|
|
{
|
|
return hash('sha256', $plainToken);
|
|
}
|
|
|
|
public static function generatePlainToken(): string
|
|
{
|
|
return 'ih_'.Str::random(48);
|
|
}
|
|
}
|