mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 20:40:49 +08:00
refactor isSeedBox judgement
This commit is contained in:
@@ -2,7 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\SeedBoxRecord\IpAsnEnum;
|
||||
use App\Enums\SeedBoxRecord\IsAllowedEnum;
|
||||
use App\Enums\SeedBoxRecord\TypeEnum;
|
||||
use App\Repositories\SeedBoxRepository;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class SeedBoxRecord extends NexusModel
|
||||
{
|
||||
@@ -30,6 +35,43 @@ class SeedBoxRecord extends NexusModel
|
||||
self::STATUS_DENIED => ['text' => 'Denied'],
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(function (SeedBoxRecord $model) {
|
||||
self::updateCache($model);
|
||||
});
|
||||
static::deleted(function (SeedBoxRecord $model) {
|
||||
self::updateCache($model);
|
||||
});
|
||||
}
|
||||
|
||||
private static function updateCache(SeedBoxRecord $model): void
|
||||
{
|
||||
SeedBoxRepository::updateCache(
|
||||
$model->type == TypeEnum::ADMIN->value ? 0 : $model->uid,
|
||||
TypeEnum::from($model->type),
|
||||
IsAllowedEnum::from($model->is_allowed),
|
||||
!empty($model->ip) ? IpAsnEnum::IP : IpAsnEnum::ASN,
|
||||
);
|
||||
}
|
||||
|
||||
public static function getValidQuery(TypeEnum $type, IsAllowedEnum $isAllowed, IpAsnEnum $field)
|
||||
{
|
||||
$query = self::query()
|
||||
->where('status', self::STATUS_ALLOWED)
|
||||
->where('type', $type->value)
|
||||
->where('is_allowed', $isAllowed->value)
|
||||
;
|
||||
if ($field == IpAsnEnum::IP) {
|
||||
$query->whereNotNull("ip");
|
||||
} elseif ($field == IpAsnEnum::ASN) {
|
||||
$query->where("asn", ">", 0);
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Invalid ipOrAsn");
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function typeText(): Attribute
|
||||
{
|
||||
return new Attribute(
|
||||
|
||||
Reference in New Issue
Block a user