Update Role fasilitator, kordinator
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Concerns;
|
||||
|
||||
// Mengisi kolom deleted_by otomatis saat data di-soft delete (AISTANDAR 5.3).
|
||||
trait HasDeletedBy
|
||||
{
|
||||
protected static function bootHasDeletedBy(): void
|
||||
{
|
||||
static::deleting(function ($model) {
|
||||
if (! $model->isForceDeleting()) {
|
||||
$model->deleted_by = auth()->id();
|
||||
$model->saveQuietly();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Konfigurasi;
|
||||
|
||||
use App\Models\Concerns\HasDeletedBy;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class KoordinatorKomoditas extends Model
|
||||
{
|
||||
use SoftDeletes, HasDeletedBy;
|
||||
|
||||
protected $table = 'koordinator_komoditas';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'komoditas_id',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function komoditas(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Komoditas::class, 'komoditas_id');
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
use App\Models\Konfigurasi\KoordinatorKomoditas;
|
||||
use App\Models\Pertanian\Petani;
|
||||
|
||||
class User extends Authenticatable
|
||||
@@ -52,4 +53,42 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasMany(Petani::class, 'created_by');
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::deleting(function (User $user) {
|
||||
if ($user->isForceDeleting()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user->koordinatorKomoditas()->get()->each->delete();
|
||||
});
|
||||
}
|
||||
|
||||
public function koordinatorKomoditas(): HasMany
|
||||
{
|
||||
return $this->hasMany(KoordinatorKomoditas::class, 'user_id');
|
||||
}
|
||||
|
||||
public function isKoordinator(): bool
|
||||
{
|
||||
return $this->role === Role::KOORDINATOR;
|
||||
}
|
||||
|
||||
// Daftar id komoditas yang ditugaskan ke koordinator ini.
|
||||
public function komoditasKoordinatorIds(): array
|
||||
{
|
||||
return $this->koordinatorKomoditas()->pluck('komoditas_id')->all();
|
||||
}
|
||||
|
||||
// Koordinator hanya boleh mengakses petani dengan komoditas utama yang ditugaskan.
|
||||
// Role lain tidak dibatasi di sini (aturannya tetap di masing-masing controller).
|
||||
public function bolehAksesPetani(Petani $petani): bool
|
||||
{
|
||||
if (! $this->isKoordinator()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array($petani->komoditas_id, $this->komoditasKoordinatorIds());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user