Security.php in Bamboo Twig 8.2
File
bamboo_twig_security/src/TwigExtension/Security.php
View source
<?php
namespace Drupal\bamboo_twig_security\TwigExtension;
use Drupal\bamboo_twig\TwigExtension\TwigExtensionBase;
class Security extends TwigExtensionBase {
public function getFunctions() {
return [
new \Twig_SimpleFunction('bamboo_has_permission', [
$this,
'hasPermission',
]),
new \Twig_SimpleFunction('bamboo_has_role', [
$this,
'hasRole',
]),
];
}
public function getName() {
return 'bamboo_twig.twig.security';
}
public function hasPermission($permission, $user = NULL) {
$currentUser = $this
->getCurrentUser();
if (is_null($user) && $currentUser
->isAnonymous()) {
return NULL;
}
$user_id = $currentUser
->id();
if (!is_null($user) && is_int($user)) {
$user_id = $user;
}
$account = $this
->getUserStorage()
->load($user_id);
if (!$account) {
return NULL;
}
return $account
->hasPermission($permission);
}
public function hasRole($role, $user = NULL) {
$currentUser = $this
->getCurrentUser();
if (is_null($user) && $currentUser
->isAnonymous()) {
return NULL;
}
$user_id = $currentUser
->id();
if (!is_null($user) && is_int($user)) {
$user_id = $user;
}
$account = $this
->getUserStorage()
->load($user_id);
if (!$account) {
return NULL;
}
return $account
->hasRole($role);
}
}
Classes
Name |
Description |
Security |
Provides a 'Security' Twig Extensions. |