View source
<?php
namespace Drupal\security_review\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\security_review\Checklist;
use Drupal\security_review\Security;
use Drupal\security_review\SecurityReview;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
protected $checklist;
protected $security;
protected $securityReview;
private $dateFormatter;
public function __construct(ConfigFactoryInterface $config_factory, Checklist $checklist, Security $security, SecurityReview $security_review, DateFormatterInterface $dateFormatter) {
parent::__construct($config_factory);
$this->checklist = $checklist;
$this->security = $security;
$this->securityReview = $security_review;
$this->dateFormatter = $dateFormatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('security_review.checklist'), $container
->get('security_review.security'), $container
->get('security_review'), $container
->get('date.formatter'));
}
public function getFormId() {
return 'security-review-settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$checks = $this->checklist
->getChecks();
$roles = user_roles();
$options = [];
foreach ($roles as $rid => $role) {
$options[$rid] = $role
->label();
}
$message = '';
if (in_array(AccountInterface::AUTHENTICATED_ROLE, $this->security
->defaultUntrustedRoles())) {
$message = $this
->t('You have allowed anonymous users to create accounts without approval so the authenticated role defaults to untrusted.');
}
$form['untrusted_roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Untrusted roles'),
'#description' => $this
->t('Define which roles are for less trusted users. The anonymous role defaults to untrusted. @message Most Security Review checks look for resources usable by untrusted roles.', [
'@message' => $message,
]),
'#options' => $options,
'#default_value' => $this->security
->untrustedRoles(),
];
$form['advanced'] = [
'#type' => 'details',
'#title' => $this
->t('Advanced'),
'#open' => TRUE,
];
$form['advanced']['logging'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Log checklist results and skips'),
'#description' => $this
->t('The result of each check and skip can be logged to watchdog for tracking.'),
'#default_value' => $this->securityReview
->isLogging(),
];
$values = [];
$options = [];
foreach ($checks as $check) {
if ($check
->isSkipped()) {
$values[] = $check
->id();
$label = $this
->t('@name <em>skipped by UID @uid on @date</em>', [
'@name' => $check
->getTitle(),
'@uid' => $check
->skippedBy()
->id(),
'@date' => $this->dateFormatter
->format($check
->skippedOn()),
]);
}
else {
$label = $check
->getTitle();
}
$options[$check
->id()] = $label;
}
$form['advanced']['skip'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Checks to skip'),
'#description' => $this
->t('Skip running certain checks. This can also be set on the <em>Run & review</em> page. It is recommended that you do not skip any checks unless you know the result is wrong or the process times out while running.'),
'#options' => $options,
'#default_value' => $values,
];
foreach ($checks as $check) {
$check_form = $check
->settings()
->buildForm();
if (!empty($check_form)) {
if (!isset($form['advanced']['check_specific'])) {
$form['advanced']['check_specific'] = [
'#type' => 'details',
'#title' => $this
->t('Check-specific settings'),
'#open' => FALSE,
'#tree' => TRUE,
];
}
$sub_form =& $form['advanced']['check_specific'][$check
->id()];
$title = $check
->getTitle();
if ($check
->getMachineNamespace() != 'security_review') {
$title .= $this
->t('%namespace', [
'%namespace' => $check
->getNamespace(),
]);
}
$sub_form = [
'#type' => 'details',
'#title' => $title,
'#open' => TRUE,
'#tree' => TRUE,
'form' => $check_form,
];
}
}
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (isset($form['advanced']['check_specific'])) {
$check_specific_values = $form_state
->getValue('check_specific');
foreach ($this->checklist
->getChecks() as $check) {
$check_form =& $form['advanced']['check_specific'][$check
->id()];
if (isset($check_form)) {
$check
->settings()
->validateForm($check_form, $check_specific_values[$check
->id()]);
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$check_settings = $this
->config('security_review.checks');
$this->securityReview
->setConfigured(TRUE);
$untrusted_roles = array_keys(array_filter($form_state
->getValue('untrusted_roles')));
$this->securityReview
->setUntrustedRoles($untrusted_roles);
$logging = $form_state
->getValue('logging') == 1;
$this->securityReview
->setLogging($logging);
$skipped = array_keys(array_filter($form_state
->getValue('skip')));
foreach ($this->checklist
->getChecks() as $check) {
if (in_array($check
->id(), $skipped)) {
$check
->skip();
}
else {
$check
->enable();
}
}
if (isset($form['advanced']['check_specific'])) {
$check_specific_values = $form_state
->getValue('check_specific');
foreach ($check_specific_values as $id => $values) {
$check = $this->checklist
->getCheckById($id);
$check_form =& $form['advanced']['check_specific'][$id]['form'];
$check_form_values = $check_specific_values[$id]['form'];
$check
->settings()
->submitForm($check_form, $check_form_values);
}
}
$check_settings
->save();
parent::submitForm($form, $form_state);
}
protected function getEditableConfigNames() {
return [
'security_review.checks',
];
}
}