<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use App\Modules\User\Models\Modules_user;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\User;
use Blade;
class BladeServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::if('check', function($url){
            $mm = new Modules_user();
            $check = $mm->permission_by_roles(Auth::id(),strtolower($url));
            if ($check) {
                return true;
            }else{
                return false;
            }
        });
        Blade::if('permission', function($permiso){
            $user = User::find(Auth::id());
            $permissions = $user->getPermissionsViaRoles();
            $all_roles = User::with('roles')->find(Auth::id());
            foreach ($permissions as $a) {
                if ($a->name == $permiso) {
                    return true;
                }
            }
            return false;
        });
    }
}
