public function getValueOptions() { if (!isset($this->valueOptions)) { $permissions = $this->permissionHandler->getPermissions(); foreach ($permissions as $perm => $perm_item) { $provider = $perm_item['provider']; $display_name = $this->moduleHandler->getName($provider); $this->valueOptions[$display_name][$perm] = SafeMarkup::checkPlain(strip_tags($perm_item['title'])); } } else { return $this->valueOptions; } }
public function getValueOptions() { if (!isset($this->valueOptions)) { $module_info = system_get_info('module'); $permissions = $this->permissionHandler->getPermissions(); foreach ($permissions as $perm => $perm_item) { $provider = $perm_item['provider']; $display_name = $module_info[$provider]['name']; $this->valueOptions[$display_name][$perm] = String::checkPlain(strip_tags($perm_item['title'])); } } else { return $this->valueOptions; } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $role_names = array(); $role_permissions = array(); $admin_roles = array(); foreach ($this->getRoles() as $role_name => $role) { // Retrieve role names for columns. $role_names[$role_name] = String::checkPlain($role->label()); // Fetch permissions for the roles. $role_permissions[$role_name] = $role->getPermissions(); $admin_roles[$role_name] = $role->isAdmin(); } // Store $role_names for use when saving the data. $form['role_names'] = array('#type' => 'value', '#value' => $role_names); // Render role/permission overview: $options = array(); $module_info = system_rebuild_module_data(); $hide_descriptions = system_admin_compact_mode(); $form['system_compact_link'] = array('#id' => FALSE, '#type' => 'system_compact_link'); $form['permissions'] = array('#type' => 'table', '#header' => array($this->t('Permission')), '#id' => 'permissions', '#attributes' => ['class' => ['permissions']], '#sticky' => TRUE); foreach ($role_names as $name) { $form['permissions']['#header'][] = array('data' => $name, 'class' => array('checkbox')); } $permissions = $this->permissionHandler->getPermissions(); $permissions_by_provider = array(); foreach ($permissions as $permission_name => $permission) { $permissions_by_provider[$permission['provider']][$permission_name] = $permission; } foreach ($permissions_by_provider as $provider => $permissions) { // Module name. $form['permissions'][$provider] = array(array('#wrapper_attributes' => array('colspan' => count($role_names) + 1, 'class' => array('module'), 'id' => 'module-' . $provider), '#markup' => $module_info[$provider]->info['name'])); foreach ($permissions as $perm => $perm_item) { // Fill in default values for the permission. $perm_item += array('description' => '', 'restrict access' => FALSE, 'warning' => !empty($perm_item['restrict access']) ? $this->t('Warning: Give to trusted roles only; this permission has security implications.') : ''); $options[$perm] = $perm_item['title']; $form['permissions'][$perm]['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' => $perm_item['title'])); // Show the permission description. if (!$hide_descriptions) { $form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description']; $form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning']; } $options[$perm] = ''; foreach ($role_names as $rid => $name) { $form['permissions'][$perm][$rid] = array('#title' => $name . ': ' . $perm_item['title'], '#title_display' => 'invisible', '#wrapper_attributes' => array('class' => array('checkbox')), '#type' => 'checkbox', '#default_value' => in_array($perm, $role_permissions[$rid]) ? 1 : 0, '#attributes' => array('class' => array('rid-' . $rid)), '#parents' => array($rid, $perm)); // Show a column of disabled but checked checkboxes. if ($admin_roles[$rid]) { $form['permissions'][$perm][$rid]['#disabled'] = TRUE; $form['permissions'][$perm][$rid]['#default_value'] = TRUE; } } } } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save permissions')); $form['#attached']['library'][] = 'user/drupal.user.permissions'; return $form; }
/** * Checks operation access on the field collection item's host's revisions. * * @param \Drupal\Core\Session\AccountInterface $account * The currently logged in account. * * TODO: Document params * * @return string * A \Drupal\Core\Access\AccessInterface constant value. */ public function access(Route $route, AccountInterface $account, $field_collection_item_revision = NULL, FieldCollectionItem $field_collection_item = NULL) { if ($field_collection_item_revision) { $field_collection_item = field_collection_item_revision_load($field_collection_item_revision); } $permissions = $this->permissionHandler->getPermissions(); $operation = $route->getRequirement('_access_field_collection_item_host_revisions'); $host = $field_collection_item->getHost(); if ($host->getEntityType()->id() == 'node') { return AccessResult::allowedIf($account->hasPermission($operation . ' ' . $host->getType() . ' revisions')); } else { if ($host->getEntityType()->id() == 'field_collection_item') { return $this->access($route, $account, $host->revision_id, $host); } else { return AccessResult::allowedIf($field_collection_item && $field_collection_item->getHost()->access($operation, $account))->cachePerPermissions(); } } }
/** * Returns module provided permissions. * * @return array * Array of permission names. */ public function getPermissions() { $permissions = []; foreach ($this->permissionHandler->getPermissions() as $name => $permission) { if ($permission['provider'] === 'masquerade') { // Filter only module's permissions. $permissions[] = $name; } } return $permissions; }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); // Build the list of all permissions grouped by module. $permissions = []; foreach ($this->permissionHandler->getPermissions() as $permission => $permission_item) { $provider = $permission_item['provider']; $display_name = $this->moduleHandler->getName($provider); $permissions[$display_name][$permission] = Html::escape($permission_item['title']); } $form['admin_permission'] = ['#type' => 'select', '#title' => $this->t('Admin permission'), '#description' => $this->t('Allows the current user to access the view even if the argument is a different user.'), '#options' => $permissions, '#empty_value' => '', '#default_value' => $this->options['admin_permission']]; }
public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); // Get list of permissions $perms = []; $permissions = $this->permissionHandler->getPermissions(); foreach ($permissions as $perm => $perm_item) { $provider = $perm_item['provider']; $display_name = $this->moduleHandler->getName($provider); $perms[$display_name][$perm] = strip_tags($perm_item['title']); } $form['perm'] = array('#type' => 'select', '#options' => $perms, '#title' => $this->t('Permission'), '#default_value' => $this->options['perm'], '#description' => $this->t('Only users with the selected permission flag will be able to access this display.')); }
public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); $module_info = system_get_info('module'); // Get list of permissions $perms = []; $permissions = $this->permissionHandler->getPermissions(); foreach ($permissions as $perm => $perm_item) { $provider = $perm_item['provider']; $display_name = $module_info[$provider]['name']; $perms[$display_name][$perm] = String::checkPlain(strip_tags($perm_item['title'])); } $form['perm'] = array('#type' => 'select', '#options' => $perms, '#title' => $this->t('Permission'), '#default_value' => $this->options['perm'], '#description' => $this->t('Only users with the selected permission flag will be able to access this display. Note that users with "access all views" can see any view, regardless of other permissions.')); }
/** * Builds a table row for the system modules page. * * @param array $modules * The list existing modules. * @param \Drupal\Core\Extension\Extension $module * The module for which to build the form row. * @param $distribution * * @return array * The form row for the given module. */ protected function buildRow(array $modules, Extension $module, $distribution) { // Set the basic properties. $row['#required'] = array(); $row['#requires'] = array(); $row['#required_by'] = array(); $row['name']['#markup'] = $module->info['name']; $row['description']['#markup'] = $this->t($module->info['description']); $row['version']['#markup'] = $module->info['version']; // Generate link for module's help page. Assume that if a hook_help() // implementation exists then the module provides an overview page, rather // than checking to see if the page exists, which is costly. $row['links']['help'] = array(); if ($this->moduleHandler->moduleExists('help') && $module->status && in_array($module->getName(), $this->moduleHandler->getImplementations('help'))) { $row['links']['help'] = array('#type' => 'link', '#title' => $this->t('Help'), '#url' => Url::fromRoute('help.page', ['name' => $module->getName()]), '#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => $this->t('Help')))); } // Generate link for module's permission, if the user has access to it. $row['links']['permissions'] = array(); if ($module->status && $this->currentUser->hasPermission('administer permissions') && $this->permissionHandler->moduleProvidesPermissions($module->getName())) { $row['links']['permissions'] = array('#type' => 'link', '#title' => $this->t('Permissions'), '#url' => Url::fromRoute('user.admin_permissions'), '#options' => array('fragment' => 'module-' . $module->getName(), 'attributes' => array('class' => array('module-link', 'module-link-permissions'), 'title' => $this->t('Configure permissions')))); } // Generate link for module's configuration page, if it has one. $row['links']['configure'] = array(); if ($module->status && isset($module->info['configure'])) { $route_parameters = isset($module->info['configure_parameters']) ? $module->info['configure_parameters'] : array(); if ($this->accessManager->checkNamedRoute($module->info['configure'], $route_parameters, $this->currentUser)) { $links = $this->menuLinkManager->loadLinksByRoute($module->info['configure']); /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ $link = reset($links); // Most configure links have a corresponding menu link, though some just // have a route. if ($link) { $description = $link->getDescription(); } else { $request = new Request(); $request->attributes->set('_route_name', $module->info['configure']); $route_object = $this->routeProvider->getRouteByName($module->info['configure']); $request->attributes->set('_route', $route_object); $request->attributes->add($route_parameters); $description = $this->titleResolver->getTitle($request, $route_object); } $row['links']['configure'] = array('#type' => 'link', '#title' => $this->t('Configure'), '#url' => Url::fromRoute($module->info['configure'], $route_parameters), '#options' => array('attributes' => array('class' => array('module-link', 'module-link-configure'), 'title' => $description))); } } // Present a checkbox for installing and indicating the status of a module. $row['enable'] = array('#type' => 'checkbox', '#title' => $this->t('Install'), '#default_value' => (bool) $module->status, '#disabled' => (bool) $module->status); // Disable the checkbox for required modules. if (!empty($module->info['required'])) { // Used when displaying modules that are required by the installation profile $row['enable']['#disabled'] = TRUE; $row['#required_by'][] = $distribution . (!empty($module->info['explanation']) ? ' (' . $module->info['explanation'] . ')' : ''); } // Check the compatibilities. $compatible = TRUE; // Initialize an empty array of reasons why the module is incompatible. Add // each reason as a separate element of the array. $reasons = array(); // Check the core compatibility. if ($module->info['core'] != \Drupal::CORE_COMPATIBILITY) { $compatible = FALSE; $reasons[] = $this->t('This version is not compatible with Drupal !core_version and should be replaced.', array('!core_version' => \Drupal::CORE_COMPATIBILITY)); } // Ensure this module is compatible with the currently installed version of PHP. if (version_compare(phpversion(), $module->info['php']) < 0) { $compatible = FALSE; $required = $module->info['php'] . (substr_count($module->info['php'], '.') < 2 ? '.*' : ''); $reasons[] = $this->t('This module requires PHP version @php_required and is incompatible with PHP version !php_version.', array('@php_required' => $required, '!php_version' => phpversion())); } // If this module is not compatible, disable the checkbox. if (!$compatible) { $status = implode(' ', $reasons); $row['enable']['#disabled'] = TRUE; $row['description']['#markup'] = $status; $row['#attributes']['class'][] = 'incompatible'; } // If this module requires other modules, add them to the array. foreach ($module->requires as $dependency => $version) { if (!isset($modules[$dependency])) { $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">missing</span>)', array('@module' => Unicode::ucfirst($dependency))); $row['enable']['#disabled'] = TRUE; } elseif (empty($modules[$dependency]->hidden)) { $name = $modules[$dependency]->info['name']; // Disable the module's checkbox if it is incompatible with the // dependency's version. if ($incompatible_version = drupal_check_incompatibility($version, str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) { $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> version @version)', array('@module' => $name . $incompatible_version, '@version' => $modules[$dependency]->info['version'])); $row['enable']['#disabled'] = TRUE; } elseif ($modules[$dependency]->info['core'] != \Drupal::CORE_COMPATIBILITY) { $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', array('@module' => $name)); $row['enable']['#disabled'] = TRUE; } elseif ($modules[$dependency]->status) { $row['#requires'][$dependency] = $this->t('@module', array('@module' => $name)); } else { $row['#requires'][$dependency] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $name)); } } } // If this module is required by other modules, list those, and then make it // impossible to disable this one. foreach ($module->required_by as $dependent => $version) { if (isset($modules[$dependent]) && empty($modules[$dependent]->info['hidden'])) { if ($modules[$dependent]->status == 1 && $module->status == 1) { $row['#required_by'][$dependent] = $this->t('@module', array('@module' => $modules[$dependent]->info['name'])); $row['enable']['#disabled'] = TRUE; } else { $row['#required_by'][$dependent] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $modules[$dependent]->info['name'])); } } } return $row; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Get anything we need form the base class. $form = parent::buildForm($form, $form_state); // Display any API key errors. MollomUtilities::getAdminAPIKeyStatus(); MollomUtilities::displayMollomTestModeWarning(); /* @var $entity \Drupal\mollom\Entity\FormInterface */ $entity = $this->getEntity(); $form_id = ''; if ($entity->isNew()) { // Determine if the form id selection just changed. $input = $form_state->getUserInput(); if (!empty($input['id'])) { $form_id = $input['id']; $mollom_form = $entity->initialize($form_id); } else { if ($query_form_id = \Drupal::request()->query->get('form_id', '')) { $form_id = $query_form_id; $mollom_form = $entity->initialize($form_id); } } } else { $form_id = $entity->id(); $mollom_form = $entity->initialize(); } $enabled_fields = []; if ($entity->isNew() && !empty($input['id'])) { foreach ($mollom_form['enabled_fields'] as $value) { $enabled_fields[] = rawurlencode($value); } // Set defaults back. // See https://www.drupal.org/node/1100170 $input['checks'] = $entity->getChecks(); $input['enabled_fields'] = $enabled_fields; $form_state->setUserInput($input); } else { foreach ($entity->getEnabledFields() as $value) { $enabled_fields[] = rawurldecode($value); } } // Build the form. if ($entity->isNew()) { $options = $this->getProtectableFormOptions(); if (empty($options)) { return $this->redirect('entity.mollom_form.list'); } $form['#attributes']['id'] = $this->getFormId(); $form['id'] = array('#type' => 'select', '#title' => $this->t('Mollom Form'), '#maxlength' => 255, '#options' => $options, '#default_value' => $form_id, '#empty_option' => t('Select a form to configure...'), '#required' => TRUE, '#ajax' => array('callback' => array($this, 'ajaxFormHandler'), 'wrapper' => $this->getFormId())); // Must select the form to protect prior to continuing. if (empty($form_id)) { return $form; } } else { $form['label'] = array('#title' => t('Protected form'), '#type' => 'textfield', '#default_value' => $entity->label(), '#disabled' => TRUE); } // Protection mode $modes = array(FormInterface::MOLLOM_MODE_ANALYSIS => $this->t('@option <em>(@recommended)</em>', array('@option' => $this->t('Text analysis'), '@recommended' => $this->t('recommended'))), FormInterface::MOLLOM_MODE_CAPTCHA => t('CAPTCHA only')); $form['mode'] = array('#type' => 'radios', '#title' => t('Protection mode'), '#options' => $modes, '#default_value' => isset($entity->mode) ? $entity->mode : key($modes)); $form['mode'][FormInterface::MOLLOM_MODE_ANALYSIS] = array('#description' => t('Mollom will analyze the post and will only show a CAPTCHA when it is unsure.')); $form['mode'][FormInterface::MOLLOM_MODE_CAPTCHA] = array('#description' => t('A CAPTCHA will be shown for every post. Only choose this if there are too few text fields to analyze.')); $form['mode'][FormInterface::MOLLOM_MODE_CAPTCHA]['#description'] .= '<br />' . t('Note: Page caching is disabled on all pages containing a CAPTCHA-only protected form.'); $all_permissions = $this->permissionHandler->getPermissions(); // Prepend Mollom's global permission to the list. if (empty($mollom_form['bypass access']) || !is_array($mollom_form['bypass access'])) { $mollom_form['bypass access'] = []; } array_unshift($mollom_form['bypass access'], 'bypass mollom protection'); $permissions = array(); if (isset($mollom_form['bypass access'])) { foreach ($mollom_form['bypass access'] as $permission) { $permissions[Html::getClass($permission)] = array('title' => $all_permissions[$permission]['title'], 'url' => Url::fromRoute('user.admin_permissions'), 'fragment' => 'module-' . $all_permissions[$permission]['provider']); } } $form['mode']['#description'] = t('The protection is omitted for users having any of the permissions: @permission-list', array('@permission-list' => \Drupal::theme()->render('links', array('links' => $permissions)))); // Textual analysis filters. $form['checks'] = array('#type' => 'checkboxes', '#title' => t('Text analysis checks'), '#options' => array('spam' => t('Spam'), 'profanity' => t('Profanity')), '#default_value' => $entity->getChecks(), '#states' => array('visible' => array('[name="mode"]' => array('value' => (string) FormInterface::MOLLOM_MODE_ANALYSIS)))); // Profanity check requires text to analyze; unlike the spam check, there // is no fallback in case there is no text. $form['checks']['profanity']['#access'] = !empty($mollom_form['elements']); // Form elements defined by hook_mollom_form_info() use the // 'parent][child' syntax, which Form API also uses internally for // form_set_error(), and which allows us to recurse into nested fields // during processing of submitted form values. However, since we are using // those keys also as internal values to configure the fields to use for // textual analysis, we need to encode them. Otherwise, a nested field key // would result in the following checkbox attribute: // '#name' => 'mollom[enabled_fields][parent][child]' // This would lead to a form validation error, because it is a valid key. // By encoding them, we prevent this from happening: // '#name' => 'mollom[enabled_fields][parent%5D%5Bchild]' $elements = array(); if (isset($mollom_form['elements']) && is_array($mollom_form['elements'])) { foreach ($mollom_form['elements'] as $key => $value) { $elements[rawurlencode($key)] = $value; } } $enabled_field_selections = []; foreach ($enabled_fields as $key => $value) { $enabled_field_selections[rawurlencode($key)] = rawurlencode($value); } $form['enabled_fields'] = array('#type' => 'checkboxes', '#title' => t('Text fields to analyze'), '#options' => $elements, '#default_value' => $enabled_field_selections, '#description' => t('Only enable fields that accept text (not numbers). Omit fields that contain sensitive data (e.g., credit card numbers) or computed/auto-generated values, as well as author information fields (e.g., name, e-mail).'), '#access' => !empty($mollom_form['elements']), '#states' => array('visible' => array('[name="mode"]' => array('value' => (string) FormInterface::MOLLOM_MODE_ANALYSIS)))); $form['mapping'] = array('#type' => 'value', '#value' => $mollom_form['mapping']); if ($entity->isNew()) { $form['module'] = array('#type' => 'value', '#value' => $mollom_form['module']); $form['label'] = array('#type' => 'value', '#value' => $mollom_form['title']); $form['entity'] = array('#type' => 'value', '#value' => $mollom_form['entity']); $form['bundle'] = array('#type' => 'value', '#value' => $mollom_form['bundle']); } $form['strictness'] = array('#type' => 'radios', '#title' => t('Text analysis strictness'), '#options' => array('normal' => t('@option <em>(@recommended)</em>', array('@option' => t('Normal'), '@recommended' => $this->t('recommended'))), 'strict' => t('Strict: Posts are more likely classified as spam'), 'relaxed' => t('Relaxed: Posts are more likely classified as ham')), '#default_value' => $entity->getStrictness(), '#states' => array('visible' => array('[name="mode"]' => array('value' => (string) FormInterface::MOLLOM_MODE_ANALYSIS)))); $form['unsure'] = array('#type' => 'radios', '#title' => t('When text analysis is unsure'), '#default_value' => $entity->getUnsure(), '#options' => array('captcha' => t('@option <em>(@recommended)</em>', array('@option' => t('Show a CAPTCHA'), '@recommended' => $this->t('recommended'))), 'moderate' => t('Retain the post for manual moderation'), 'binary' => t('Accept the post')), '#required' => $entity->getProtectionMode() == FormInterface::MOLLOM_MODE_ANALYSIS, '#states' => array('visible' => array('[name="mode"]' => array('value' => (string) FormInterface::MOLLOM_MODE_ANALYSIS), '[name="checks[spam]"]' => array('checked' => TRUE)))); // Only possible for forms supporting moderation of unpublished posts. $form['unsure']['moderate']['#access'] = !empty($mollom_form['moderation callback']); $form['discard'] = array('#type' => 'radios', '#title' => t('When text analysis identifies spam'), '#default_value' => $entity->getDiscard(), '#options' => array(1 => t('@option <em>(@recommended)</em>', array('@option' => t('Discard the post'), '@recommended' => $this->t('recommended'))), 0 => t('Retain the post for manual moderation')), '#required' => $entity->getProtectionMode() == FormInterface::MOLLOM_MODE_ANALYSIS, '#states' => array('visible' => array('[name="mode"]' => array('value' => (string) FormInterface::MOLLOM_MODE_ANALYSIS), '[name="checks[spam]"]' => array('checked' => TRUE)))); // Return the form. return $form; }