Skip to content

Instantly share code, notes, and snippets.

@worstn8mare
Created February 12, 2018 01:56
Show Gist options
  • Select an option

  • Save worstn8mare/45ab69809e09f7b86a4fb8dc1635b4d9 to your computer and use it in GitHub Desktop.

Select an option

Save worstn8mare/45ab69809e09f7b86a4fb8dc1635b4d9 to your computer and use it in GitHub Desktop.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use DB;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
protected $varGlobal = null;
protected $permission = [];
public function boot()
{
Blade::directive('has', function($expression){
$expression = trim(str_replace('(\'', '', $expression));
$expression = trim(str_replace('\')', '', $expression));
if(in_array($this->varGlobal.'_'.$expression, $this->permission)){
return "<?php if(true) { ?>";
}
else{
return "<?php if(false) { ?>";
}
});
Blade::directive('elsehas', function(){
return "<?php }else{ ?>";
});
Blade::directive('endhas', function(){
return "<?php } ?>";
});
Blade::directive('global', function($expression){
$expression = trim(str_replace('(\'', '', $expression));
$expression = trim(str_replace('\')', '', $expression));
return $this->setvarGlobal($expression);
});
}
private function setvarGlobal($var){
$hasPermission = DB::table('user_has_permissions')
->join('permissions','permissions.id','=','user_has_permissions.permission_id')
->where('permissions.name','LIKE','accounting_%')
->where('user_has_permissions.user_id',Auth::user()->id)
->select('permissions.name as name')
->get();
$userPermission = collect($hasPermission)->pluck('name')->toArray();
$this->permission = $userPermission;
$this->varGlobal = $var;
}
private function getvarGlobal(){
return $this->varGlobal;
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment