Initialer Laravel Commit für BetiX
This commit is contained in:
61
app/Models/UserRestriction.php
Normal file
61
app/Models/UserRestriction.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user