View source
<?php
namespace Drupal\content_access\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\PermissionHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentAccessAdminSettingsForm extends FormBase {
use ContentAccessRoleBasedFormTrait;
protected $permissionHandler;
protected $moduleHandler;
public function __construct(PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
$this->permissionHandler = $permission_handler;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user.permissions'), $container
->get('module_handler'));
}
public function getFormId() {
return 'content_access_admin_settings';
}
public function buildForm(array $form, FormStateInterface $form_state, $node_type = NULL) {
$storage = [
'node_type' => $node_type,
];
$form_state
->setStorage($storage);
$defaults = [];
foreach (_content_access_get_operations() as $op => $label) {
$defaults[$op] = content_access_get_settings($op, $node_type);
}
$this
->roleBasedForm($form, $defaults, $node_type);
$form['node'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Per content node access control settings'),
'#collapsible' => TRUE,
'#description' => $this
->t('Optionally you can enable per content node access control settings. If enabled, a new tab for the content access settings appears when viewing content. You have to configure permission to access these settings at the <a href=":url">permissions</a> page.', [
':url' => Url::fromRoute('user.admin_permissions')
->toString(),
]),
];
$form['node']['per_node'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable per content node access control settings'),
'#default_value' => content_access_get_settings('per_node', $node_type),
];
$form['advanced'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['advanced']['priority'] = [
'#type' => 'weight',
'#title' => $this
->t('Give content node grants priority'),
'#default_value' => content_access_get_settings('priority', $node_type),
'#description' => $this
->t('If you are only using this access control module, you can safely ignore this. If you are using multiple access control modules you can adjust the priority of this module.'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#weight' => 10,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$storage = $form_state
->getStorage();
$roles = array_keys(user_roles());
$roles_permissions = user_role_permissions($roles);
$permissions = $this->permissionHandler
->getPermissions();
$node_type = $storage['node_type'];
foreach ($roles_permissions as $rid => $role_permissions) {
foreach ($role_permissions as $permission => $value) {
if (!array_key_exists($permission, $permissions)) {
unset($roles_permissions[$rid][$permission]);
}
}
}
foreach ([
'update',
'update_own',
'delete',
'delete_own',
] as $op) {
foreach ($values[$op] as $rid => $value) {
$permission = content_access_get_permission_by_op($op, $node_type);
if ($value) {
$roles_permissions[$rid][$permission] = TRUE;
}
else {
$roles_permissions[$rid][$permission] = FALSE;
}
}
unset($values[$op]);
}
$this
->savePermissions($roles_permissions);
$settings = content_access_get_settings('all', $node_type);
foreach (content_access_available_settings() as $setting) {
if (isset($values[$setting])) {
$settings[$setting] = is_array($values[$setting]) ? array_keys(array_filter($values[$setting])) : $values[$setting];
}
}
content_access_set_settings($settings, $node_type);
if (content_access_get_settings('per_node', $node_type) || content_access_get_settings('view', $node_type) != $form['per_role']['view']['#default_value'] || content_access_get_settings('view_own', $node_type) != $form['per_role']['view_own']['#default_value'] || content_access_get_settings('priority', $node_type) != $form['advanced']['priority']['#default_value'] || content_access_get_settings('per_node', $node_type) != $form['node']['per_node']['#default_value']) {
if (!content_access_get_settings('per_node', $node_type) && $form['node']['per_node']['#default_value'] && $this->moduleHandler
->moduleExists('acl')) {
_content_access_remove_acls($node_type);
}
if (content_access_mass_update([
$node_type,
])) {
$node_types = node_type_get_names();
$this
->messenger()
->addMessage($this
->t('Permissions have been changed for the content type @types.<br />You may have to <a href=":rebuild">rebuild permissions</a> for your changes to take effect.', [
'@types' => $node_types[$node_type],
':rebuild' => Url::fromRoute('node.configure_rebuild_confirm')
->toString(),
]));
}
}
else {
$this
->messenger()
->addMessage($this
->t('No change.'));
}
}
protected function savePermissions($roles_permissions) {
foreach ($roles_permissions as $rid => $permissions) {
user_role_change_permissions($rid, $permissions);
}
}
}