Initial commit: Struktur aplikasi ilycats
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Food;
|
||||
use App\Models\Medicine;
|
||||
use App\Models\Setting;
|
||||
use App\Observers\FoodObserver;
|
||||
use App\Observers\MedicineObserver;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Gate; // Import Gate untuk sistem permission
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void { /* ... */ }
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
// 1. DAFTARKAN OBSERVERS (Tetap Utuh & Aman)
|
||||
if (Schema::hasTable('medicines')) {
|
||||
Medicine::observe(MedicineObserver::class);
|
||||
}
|
||||
|
||||
if (class_exists(FoodObserver::class) && Schema::hasTable('foods')) {
|
||||
Food::observe(FoodObserver::class);
|
||||
}
|
||||
|
||||
// 2. GLOBAL SETTINGS (Dinamis)
|
||||
if (!app()->runningInConsole() && Schema::hasTable('settings')) {
|
||||
$rawSettings = Setting::pluck('value', 'key')->toArray();
|
||||
View::share('setting', (object) $rawSettings);
|
||||
}
|
||||
|
||||
// 3. VIEW COMPOSER (Fungsi Notifikasi Stok Tetap Berjalan)
|
||||
$alertViews = ['layouts.app', 'layouts.navigation-sidebar', 'dashboard', 'pages.*'];
|
||||
|
||||
View::composer($alertViews, function ($view) {
|
||||
$stockData = Cache::remember('low_stock_alerts', 600, function () {
|
||||
$lowMedicines = Medicine::where('qty', '<', 5)->get(['nama_obat', 'qty', 'unit']);
|
||||
$lowFoods = Food::where('qty', '<', 5)->get(['nama_makanan', 'qty', 'unit']);
|
||||
|
||||
return [
|
||||
'lowStockMedicines' => $lowMedicines,
|
||||
'lowStockFoods' => $lowFoods,
|
||||
'totalAlerts' => $lowMedicines->count() + $lowFoods->count(),
|
||||
];
|
||||
});
|
||||
$view->with($stockData);
|
||||
});
|
||||
|
||||
// 4. SISTEM OTORISASI GLOBAL (Gate Permission)
|
||||
Gate::define('check-access', function ($user, $module, $permission) {
|
||||
// Hindari pengecekan jika user tidak memiliki role_id atau data role
|
||||
if (!$user->role_id && !$user->role) return false;
|
||||
|
||||
// Deteksi Role: Apakah string (lama) atau object (baru)
|
||||
// Jika $user->role adalah string, ambil langsung. Jika object, ambil slug-nya.
|
||||
$roleIdentifier = is_object($user->role) ? $user->role->slug : $user->role;
|
||||
|
||||
// Admin Bypass: Role admin memiliki akses ke seluruh fitur
|
||||
if ($roleIdentifier === 'admin') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Menyelaraskan nama kolom (can_read, can_create, can_update, can_delete)
|
||||
$column = str_starts_with($permission, 'can_') ? $permission : 'can_' . $permission;
|
||||
|
||||
// Cek matriks di tabel role_permissions berdasarkan role_id
|
||||
return \App\Models\RolePermission::where('role_id', $user->role_id)
|
||||
->where('module_slug', $module)
|
||||
->where($column, 1)
|
||||
->exists();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user