<?php

namespace Modules\User\Providers;

use Illuminate\Support\Facades\Blade;
use Livewire\Livewire;
use Illuminate\Support\Facades\Gate;

use Illuminate\Support\Facades\Auth;
use Modules\User\Models\UserRole;
use Modules\User\Models\Role;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Gate::define('admin', function ($user)
        {
            if (!Auth::check())
            {
                abort(403, 'Unauthorized action');
            }
            $user = Auth::user();
            if (
                !UserRole::where('user_id', $user->id)
                    ->where('role_id', Role::SUPER_ADMIN)->count()
            )
            {
                abort(403, 'Unauthorized action');
            }
            return true;
        });
    }
}