class ChecklistController in Security Review 8
The class of the 'Run & Review' page's controller.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\security_review\Controller\ChecklistController
Expanded class hierarchy of ChecklistController
File
- src/
Controller/ ChecklistController.php, line 17
Namespace
Drupal\security_review\ControllerView source
class ChecklistController extends ControllerBase {
/**
* The CSRF Token generator.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator $csrfToken
*/
protected $csrfToken;
/**
* The security_review.checklist service.
*
* @var \Drupal\security_review\Checklist
*/
protected $checklist;
/**
* The security_review service.
*
* @var \Drupal\security_review\SecurityReview
*/
protected $securityReview;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a ChecklistController.
*
* @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator
* The CSRF Token generator.
* @param \Drupal\security_review\SecurityReview $security_review
* The security_review service.
* @param \Drupal\security_review\Checklist $checklist
* The security_review.checklist service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(CsrfTokenGenerator $csrf_token_generator, SecurityReview $security_review, Checklist $checklist, MessengerInterface $messenger) {
$this->csrfToken = $csrf_token_generator;
$this->checklist = $checklist;
$this->securityReview = $security_review;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('csrf_token'), $container
->get('security_review'), $container
->get('security_review.checklist'), $container
->get('messenger'));
}
/**
* Creates the Run & Review page.
*
* @return array
* The 'Run & Review' page's render array.
*/
public function index() {
$run_form = [];
// If the user has the required permissions, show the RunForm.
if ($this
->currentUser()
->hasPermission('run security checks')) {
// Get the Run form.
$run_form = $this
->formBuilder()
->getForm('Drupal\\security_review\\Form\\RunForm');
// Close the Run form if there are results.
if ($this->securityReview
->getLastRun() > 0) {
$run_form['run_form']['#open'] = FALSE;
}
}
// Print the results if any.
if ($this->securityReview
->getLastRun() <= 0) {
// If they haven't configured the site, prompt them to do so.
if (!$this->securityReview
->isConfigured()) {
$this->messenger
->addWarning($this
->t('It appears this is your first time using the Security Review checklist. Before running the checklist please review the settings page at <a href=":url">admin/reports/security-review/settings</a> to set which roles are untrusted.', [
':url' => Url::fromRoute('security_review.settings')
->toString(),
]), 'warning');
}
}
return [
$run_form,
$this
->results(),
];
}
/**
* Creates the results' table.
*
* @return array
* The render array for the result table.
*/
public function results() {
// If there are no results return.
if ($this->securityReview
->getLastRun() <= 0) {
return [];
}
$checks = [];
foreach ($this->checklist
->getChecks() as $check) {
// Initialize with defaults.
$check_info = [
'message' => $this
->t('The check "@name" hasn\'t been run yet.', [
'@name' => $check
->getTitle(),
]),
'skipped' => $check
->isSkipped(),
];
// Get last result.
$last_result = $check
->lastResult();
if ($last_result != NULL) {
if (!$last_result
->isVisible()) {
continue;
}
$check_info['result'] = $last_result
->result();
$check_info['message'] = $last_result
->resultMessage();
}
// Determine help link.
$check_info['help_link'] = Link::createFromRoute('Details', 'security_review.help', [
'namespace' => $check
->getMachineNamespace(),
'title' => $check
->getMachineTitle(),
]);
// Add toggle button.
$toggle_text = $check
->isSkipped() ? 'Enable' : 'Skip';
$check_info['toggle_link'] = Link::createFromRoute($toggle_text, 'security_review.toggle', [
'check_id' => $check
->id(),
], [
'query' => [
'token' => $this->csrfToken
->get($check
->id()),
],
]);
// Add to array of completed checks.
$checks[] = $check_info;
}
return [
'#theme' => 'run_and_review',
'#date' => $this->securityReview
->getLastRun(),
'#checks' => $checks,
'#attached' => [
'library' => [
'security_review/run_and_review',
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChecklistController:: |
protected | property | The security_review.checklist service. | |
ChecklistController:: |
protected | property | The CSRF Token generator. | |
ChecklistController:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
ChecklistController:: |
protected | property | The security_review service. | |
ChecklistController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
ChecklistController:: |
public | function | Creates the Run & Review page. | |
ChecklistController:: |
public | function | Creates the results' table. | |
ChecklistController:: |
public | function | Constructs a ChecklistController. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |