62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class UserRestriction extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'type',
|
|
'reason',
|
|
'notes',
|
|
'imposed_by',
|
|
'starts_at',
|
|
'ends_at',
|
|
'active',
|
|
'source',
|
|
'metadata',
|
|
];
|
|
|
|
protected $casts = [
|
|
'starts_at' => 'datetime',
|
|
'ends_at' => 'datetime',
|
|
'active' => 'boolean',
|
|
'metadata' => 'array',
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function imposer()
|
|
{
|
|
return $this->belongsTo(User::class, 'imposed_by');
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include currently-active restrictions.
|
|
* Active means:
|
|
* - active flag is true AND
|
|
* - (starts_at is null OR starts_at <= now) AND
|
|
* - (ends_at is null OR ends_at > now)
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
$now = now();
|
|
return $query->where('active', true)
|
|
->where(function ($q) use ($now) {
|
|
$q->whereNull('starts_at')->orWhere('starts_at', '<=', $now);
|
|
})
|
|
->where(function ($q) use ($now) {
|
|
$q->whereNull('ends_at')->orWhere('ends_at', '>', $now);
|
|
});
|
|
}
|
|
}
|