class Security in Bamboo Twig 8.2
Same name and namespace in other branches
- 8.5 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
- 8 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
- 8.3 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
- 8.4 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
Provides a 'Security' Twig Extensions.
Hierarchy
- class \Drupal\bamboo_twig\TwigExtension\TwigExtensionBase extends \Drupal\bamboo_twig\TwigExtension\Twig_Extension uses \Symfony\Component\DependencyInjection\ContainerAwareTrait
- class \Drupal\bamboo_twig_security\TwigExtension\Security
Expanded class hierarchy of Security
1 string reference to 'Security'
- bamboo_twig_security.services.yml in bamboo_twig_security/
bamboo_twig_security.services.yml - bamboo_twig_security/bamboo_twig_security.services.yml
1 service uses Security
File
- bamboo_twig_security/
src/ TwigExtension/ Security.php, line 10
Namespace
Drupal\bamboo_twig_security\TwigExtensionView source
class Security extends TwigExtensionBase {
/**
* List of all Twig functions.
*/
public function getFunctions() {
return [
new \Twig_SimpleFunction('bamboo_has_permission', [
$this,
'hasPermission',
]),
new \Twig_SimpleFunction('bamboo_has_role', [
$this,
'hasRole',
]),
];
}
/**
* Unique identifier for this Twig extension.
*/
public function getName() {
return 'bamboo_twig.twig.security';
}
/**
* Does the current|given user has the given permission ?
*
* @param string $permission
* Drupal permission string.
* @param int $user
* (Optional) user id to check permission. Otherwise current user is used.
*
* @return bool
* True if the current|given user has the given permission. Otherwise FALSE.
*/
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);
}
/**
* Does the current|given user has the given permission ?
*
* @param string $role
* Drupal role name.
* @param int $user
* (Optional) user id to check permission. Otherwise current user is used.
*
* @return bool
* True if the current|given user has the given permission. Otherwise FALSE.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Security:: |
public | function | List of all Twig functions. | |
Security:: |
public | function |
Unique identifier for this Twig extension. Overrides TwigExtensionBase:: |
|
Security:: |
public | function | Does the current|given user has the given permission ? | |
Security:: |
public | function | Does the current|given user has the given permission ? | |
TwigExtensionBase:: |
protected | function | Return the block storage. | |
TwigExtensionBase:: |
protected | function | Provides an interface for a configuration object factory. | |
TwigExtensionBase:: |
protected | function | Return the current route match. | |
TwigExtensionBase:: |
protected | function | Lazy loading for the Drupal current user account proxy. | |
TwigExtensionBase:: |
protected | function | Provides a service to handle various date related functionality. | |
TwigExtensionBase:: |
protected | function | Lazy loading for the Drupal entity type manager. | |
TwigExtensionBase:: |
protected | function | Return a singleton mime type to file extension guesser. | |
TwigExtensionBase:: |
protected | function | Return the factory for image objects. | |
TwigExtensionBase:: |
protected | function | Return the file storage. | |
TwigExtensionBase:: |
protected | function | Provides helpers to operate on files and stream wrappers. | |
TwigExtensionBase:: |
protected | function | Provides an interface for form building and processing. | |
TwigExtensionBase:: |
protected | function | Return the factory for image objects. | |
TwigExtensionBase:: |
protected | function | Provides an interface defining an image style. | |
TwigExtensionBase:: |
protected | function | Returns the language manager service. | |
TwigExtensionBase:: |
protected | function | Interface for loading, transforming and rendering menu link trees. | |
TwigExtensionBase:: |
protected | function | Manages discovery and instantiation of block plugins. | |
TwigExtensionBase:: |
protected | function | Read only settings singleton. | |
TwigExtensionBase:: |
protected | function | The state storage service. | |
TwigExtensionBase:: |
protected | function | Return the token service. | |
TwigExtensionBase:: |
protected | function | Return the user storage. |