mailwolt/app/Models/SandboxRoute.php

64 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
class SandboxRoute extends Model
{
protected $fillable = ['type', 'target', 'is_active'];
protected $casts = [
'is_active' => 'boolean',
];
/**
* All active routes ordered by type and target.
*/
public static function activeRoutes(): Collection
{
return static::where('is_active', true)
->orderBy('type')
->orderBy('target')
->get();
}
/**
* Check whether sandbox is active for a given domain.
* True if global sandbox is active OR domain-specific rule is active.
*/
public static function isActiveForDomain(string $domain): bool
{
return static::where('is_active', true)
->where(function ($q) use ($domain) {
$q->where('type', 'global')
->orWhere(function ($q2) use ($domain) {
$q2->where('type', 'domain')->where('target', $domain);
});
})
->exists();
}
/**
* Check whether sandbox is active for a given address.
* True if global is active OR domain matches OR address-specific rule exists.
*/
public static function isActiveForAddress(string $address): bool
{
$domain = substr(strrchr($address, '@'), 1) ?: '';
return static::where('is_active', true)
->where(function ($q) use ($address, $domain) {
$q->where('type', 'global')
->orWhere(function ($q2) use ($domain) {
$q2->where('type', 'domain')->where('target', $domain);
})
->orWhere(function ($q3) use ($address) {
$q3->where('type', 'address')->where('target', $address);
});
})
->exists();
}
}