forked from drugo32/field_permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_permissions.module
207 lines (196 loc) · 6.75 KB
/
field_permissions.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* @file
* Contains field_permissions.module.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field_permissions\FieldPermissionsService;
/**
* Indicates that a field does not have any access control.
*/
define('FIELD_PERMISSIONS_PUBLIC', 0);
/**
* Indicates that a field is private.
*
* Private fields are never displayed, and are only editable by the author (and
* by site administrators with the 'access private fields' permission).
*/
define('FIELD_PERMISSIONS_PRIVATE', 1);
/**
* Indicates that a field has custom permissions.
*/
define('FIELD_PERMISSIONS_CUSTOM', 2);
/**
* Implements hook_entity_field_access().
*
* @inheritdoc
*/
function field_permissions_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ((!$field_definition->isDisplayConfigurable('view')
&& !$field_definition->isDisplayConfigurable('form'))
|| empty($items)) {
return AccessResult::neutral();
}
$access_field = FieldPermissionsService::getFieldAccess($operation, $items, $account, $field_definition);
if (!$access_field) {
return AccessResult::forbidden();
}
return AccessResult::neutral();
}
/**
* Implements hook_form_alter().
*
* @inheritdoc
*/
function field_permissions_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
/** @var Drupal\field\Entity\FieldConfig $field */
$field = $form_state->getBuildInfo()['callback_object']->getEntity();
$field_storage = $field->getFieldStorageDefinition();
$default_type = FieldPermissionsService::fieldGetPermissionType($field_storage);
$form['fid'] = array('#type' => 'hidden', '#value' => $field->id());
$form['field']['field_permissions'] = array(
'#weight' => -10,
);
$form['field']['field_permissions']['type'] = array(
'#title' => t('Field visibility and permissions'),
'#type' => 'radios',
'#options' => array(
FIELD_PERMISSIONS_PUBLIC => t('Public (author and administrators can edit, everyone can view)'),
FIELD_PERMISSIONS_PRIVATE => t('Private (only author and administrators can edit and view)'),
FIELD_PERMISSIONS_CUSTOM => t('Custom permissions'),
),
'#default_value' => isset($form['#field']['field_permissions']['type']) ? $form['#field']['field_permissions']['type'] : $default_type,
);
$form['field_permissions']['permissions'] = field_permissions_permissions_matrix($field_storage);
$form['actions']['submit']['#submit'][] = 'field_permission_field_config_edit_form_submit';
}
/**
* Implements hook_form_alter().
*
* @inheritdoc
*/
function field_permissions_permissions_matrix(\Drupal\field\Entity\FieldStorageConfig $field) {
$roles = user_roles();
$role_names = array_keys($roles);
$form['role_names'] = array(
'#type' => 'value',
'#value' => $role_names,
);
$label = $field->getName();
$permissions = \Drupal::service('field_permissions.permissions_service')
->listFieldPermissionSupport($field, $label);
$options = array_keys($permissions);
/*header theme table */
$form['permissions'] = array(
'#type' => 'table',
'#header' => array(t('Permission')),
'#id' => 'permissions',
'#attributes' => ['class' => ['permissions', 'js-permissions']],
'#sticky' => TRUE,
);
foreach ($role_names as $name) {
$form['permissions']['#header'][] = array(
'data' => $name,
'class' => array('checkbox'),
);
}
$test = FieldPermissionsService::getPermissionValue($field);
foreach ($permissions as $provider => $permission) {
$form['permissions'][$provider]['description'] = array(
'#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' => array(
'title' => $permission["title"],
),
);
$options[$provider] = '';
foreach ($role_names as $rid => $name) {
$form['permissions'][$provider][$rid] = array(
'#title' => $name . ': ' . $permission["title"],
'#title_display' => 'invisible',
'#wrapper_attributes' => array(
'class' => array('checkbox'),
),
'#type' => 'checkbox',
'#default_value' => in_array($provider, $test[$name]) ? 1 : 0,
'#attributes' => array(
'class' => array(
'rid-' . $rid,
'js-rid-' . $rid
)
),
);
}
}
return $form;
}
/**
* Submit callback for form.
*
* @inheritdoc
*/
function field_permission_field_config_edit_form_submit(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Config\Config $config */
$config = \Drupal::service('config.factory')
->getEditable('field_permissions.field.settings');
/** @var Drupal\field\Entity\FieldConfig $field */
$field = $form_state->getBuildInfo()['callback_object']->getEntity();
$custom_permissions = $form_state->getValue("permissions");
$roles = $form_state->getValue("role_names");
$type_permission = $form_state->getValue("type");
$field_name = $field->getName();
$config->set('permission_type_' . $field_name, $type_permission);
$config->save();
$perm_settings = [];
// Set all perm.
if ($type_permission == FIELD_PERMISSIONS_CUSTOM) {
foreach ($custom_permissions as $perm_name => $field_perm) {
foreach ($field_perm as $rid => $roule_perm) {
$role_name = $roles[$rid];
$perm_settings[$role_name][$perm_name] = $roule_perm;
}
}
}
elseif ($type_permission == FIELD_PERMISSIONS_PUBLIC) {
foreach ($custom_permissions as $perm_name => $field_perm) {
foreach ($field_perm as $rid => $roule_perm) {
if ($perm_name != 'view_' . $field_name) {
$roule_perm = 0;
}
else {
$roule_perm = 1;
}
$role_name = $roles[$rid];
$perm_settings[$role_name][$perm_name] = $roule_perm;
}
}
}
elseif ($type_permission == FIELD_PERMISSIONS_PRIVATE) {
foreach ($custom_permissions as $perm_name => $field_perm) {
foreach ($field_perm as $rid => $roule_perm) {
if ($perm_name != 'view_own_' . $field_name && $perm_name != 'edit_own_' . $field_name) {
$roule_perm = 0;
}
else {
$roule_perm = 1;
}
$role_name = $roles[$rid];
$perm_settings[$role_name][$perm_name] = $roule_perm;
}
}
}
foreach ($perm_settings as $role_name => $perm) {
user_role_change_permissions($role_name, $perm);
}
}