class CustomAccess in Field Permissions 8
Same name and namespace in other branches
- 8.2 src/Plugin/FieldPermissionType/CustomAccess.php \Drupal\field_permissions\Plugin\FieldPermissionType\CustomAccess
Defines custom access for fields.
Plugin annotation
@FieldPermissionType(
id = "custom",
title = @Translation("Custom permissions"),
description = @Translation("Define custom permissions for this field."),
weight = 50
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\field_permissions\Plugin\FieldPermissionType\Base implements ContainerFactoryPluginInterface, FieldPermissionTypeInterface
- class \Drupal\field_permissions\Plugin\FieldPermissionType\CustomAccess implements AdminFormSettingsInterface, CustomPermissionsInterface
- class \Drupal\field_permissions\Plugin\FieldPermissionType\Base implements ContainerFactoryPluginInterface, FieldPermissionTypeInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CustomAccess
1 file declares its use of CustomAccess
- CustomAccessTest.php in tests/
src/ Unit/ Plugin/ FieldPermissionType/ CustomAccessTest.php
File
- src/
Plugin/ FieldPermissionType/ CustomAccess.php, line 25
Namespace
Drupal\field_permissions\Plugin\FieldPermissionTypeView source
class CustomAccess extends Base implements CustomPermissionsInterface, AdminFormSettingsInterface {
/**
* {@inheritdoc}
*/
public function hasFieldAccess($operation, EntityInterface $entity, AccountInterface $account) {
assert(in_array($operation, [
"edit",
"view",
]), 'The operation is either "edit" or "view", "' . $operation . '" given instead.');
$field_name = $this->fieldStorage
->getName();
if ($operation === 'edit' && $entity
->isNew()) {
return $account
->hasPermission('create ' . $field_name);
}
if ($account
->hasPermission($operation . ' ' . $field_name)) {
return TRUE;
}
else {
// User entities don't implement `EntityOwnerInterface`.
if ($entity instanceof UserInterface) {
return $entity
->id() == $account
->id() && $account
->hasPermission($operation . ' own ' . $field_name);
}
elseif ($entity instanceof EntityOwnerInterface) {
return $entity
->getOwnerId() == $account
->id() && $account
->hasPermission($operation . ' own ' . $field_name);
}
}
// Default to deny since access can be explicitly granted (edit field_name),
// even if this entity type doesn't implement the EntityOwnerInterface.
return FALSE;
}
/**
* {@inheritdoc}
*/
public function hasFieldViewAccessForEveryEntity(AccountInterface $account) {
$field_name = $this->fieldStorage
->getName();
return $account
->hasPermission('view ' . $field_name);
}
/**
* {@inheritdoc}
*/
public function buildAdminForm(array &$form, FormStateInterface $form_state, RoleStorageInterface $role_storage) {
$this
->addPermissionsGrid($form, $form_state, $role_storage);
// Only display the permissions matrix if this type is selected.
$form['#attached']['library'][] = 'field_permissions/field_permissions';
}
/**
* {@inheritdoc}
*/
public function submitAdminForm(array &$form, FormStateInterface $form_state, RoleStorageInterface $role_storage) {
$custom_permissions = $form_state
->getValue('permissions');
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = [];
foreach ($custom_permissions as $permission_name => $field_perm) {
foreach ($field_perm as $role_name => $role_permission) {
$roles[$role_name] = $role_storage
->load($role_name);
// If using this plugin, set permissions to the value submitted in the
// form. Otherwise remove all permissions as they will no longer exist.
$role_permission = $form_state
->getValue('type') === $this
->getPluginId() ? $role_permission : FALSE;
if ($role_permission) {
$roles[$role_name]
->grantPermission($permission_name);
}
else {
$roles[$role_name]
->revokePermission($permission_name);
}
}
}
// Save all roles.
foreach ($roles as $role) {
$role
->trustData()
->save();
}
}
/**
* {@inheritdoc}
*/
public function getPermissions() {
$permissions = [];
$field_name = $this->fieldStorage
->getName();
$permission_list = FieldPermissionsService::getList($field_name);
$perms_name = array_keys($permission_list);
foreach ($perms_name as $perm_name) {
$name = $perm_name . ' ' . $field_name;
$permissions[$name] = $permission_list[$perm_name];
}
return $permissions;
}
/**
* Attach a permissions grid to the field edit form.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param \Drupal\user\RoleStorageInterface $role_storage
* The user role storage.
*/
protected function addPermissionsGrid(array &$form, FormStateInterface $form_state, RoleStorageInterface $role_storage) {
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = $role_storage
->loadMultiple();
$permissions = $this
->getPermissions();
$options = array_keys($permissions);
// The permissions table.
$form['permissions'] = [
'#type' => 'table',
'#header' => [
$this
->t('Permission'),
],
'#id' => 'permissions',
'#attributes' => [
'class' => [
'permissions',
'js-permissions',
],
],
'#sticky' => TRUE,
];
foreach ($roles as $role) {
$form['permissions']['#header'][] = [
'data' => $role
->label(),
'class' => [
'checkbox',
],
];
}
// @todo Remove call to global service.
$test = \Drupal::service('field_permissions.permissions_service')
->getPermissionsByRole();
foreach ($permissions as $provider => $permission) {
$form['permissions'][$provider]['description'] = [
'#type' => 'inline_template',
'#template' => '<div class="permission"><span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{% if warning %}<em class="permission-warning">{{ warning }}</em> {% endif %}{{ description }}</div>{% endif %}</div>',
'#context' => [
'title' => $permission["title"],
],
];
$options[$provider] = '';
foreach ($roles as $name => $role) {
$form['permissions'][$provider][$name] = [
'#title' => $name . ': ' . $permission["title"],
'#title_display' => 'invisible',
'#type' => 'checkbox',
'#attributes' => [
'class' => [
'rid-' . $name,
'js-rid-' . $name,
],
],
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
];
if (!empty($test[$name]) && in_array($provider, $test[$name])) {
$form['permissions'][$provider][$name]['#default_value'] = in_array($provider, $test[$name]);
}
if ($role
->isAdmin()) {
$form['permissions'][$provider][$name]['#disabled'] = TRUE;
$form['permissions'][$provider][$name]['#default_value'] = TRUE;
}
}
}
// Attach the Drupal user permissions library.
$form['#attached']['library'][] = 'user/drupal.user.permissions';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Base:: |
protected | property | The field storage. | |
Base:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
Base:: |
public | function |
The permission type description. Overrides FieldPermissionTypeInterface:: |
|
Base:: |
public | function |
The permission type label. Overrides FieldPermissionTypeInterface:: |
|
Base:: |
public | function |
Constructs the plugin. Overrides PluginBase:: |
|
CustomAccess:: |
protected | function | Attach a permissions grid to the field edit form. | |
CustomAccess:: |
public | function |
Build or alter the field admin form. Overrides AdminFormSettingsInterface:: |
|
CustomAccess:: |
public | function |
Returns an array of permissions suitable for use in a permission callback. Overrides CustomPermissionsInterface:: |
|
CustomAccess:: |
public | function |
Determine if access to the field is granted for a given account. Overrides FieldPermissionTypeInterface:: |
|
CustomAccess:: |
public | function |
Determines if the given account may view the field, regardless of entity. Overrides Base:: |
|
CustomAccess:: |
public | function |
Allows the plugin to react to the field settings form submission. Overrides AdminFormSettingsInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldPermissionTypeInterface:: |
constant | Indicates that a field is using the custom permission type. | ||
FieldPermissionTypeInterface:: |
constant | Indicates that a field is using the private access permission type. | ||
FieldPermissionTypeInterface:: |
constant | Indicates that a field does not have field-specific access control. | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |