Added hasRole twig extension and Auditer role & permission set
This commit is contained in:
65
src/Twig/HasRoleExtension.php
Normal file
65
src/Twig/HasRoleExtension.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* AVSDev UF Tweaks (https://avsdev.uk)
|
||||
*
|
||||
* @link https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks
|
||||
* @license https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||
*/
|
||||
|
||||
namespace UserFrosting\Sprinkle\UFTweaks\Twig;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\Extension\GlobalsInterface;
|
||||
use Twig\TwigFunction;
|
||||
use UserFrosting\Support\Repository\Repository as Config;
|
||||
|
||||
/**
|
||||
* Extends Twig functionality to add hasRole.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class HasRoleExtension extends AbstractExtension implements GlobalsInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $services;
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $services
|
||||
*/
|
||||
public function __construct(ContainerInterface $services)
|
||||
{
|
||||
$this->services = $services;
|
||||
$this->config = $services->config;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'avsdev/uf-tweaks-hasRole';
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
// Add Twig function for checking permissions during dynamic menu rendering
|
||||
new TwigFunction('hasRole', function ($roleSlug) {
|
||||
$currentUser = $this->services->currentUser;
|
||||
return $currentUser->roles()->where('slug', $roleSlug)->count() > 0;
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
public function getGlobals()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user