HelpController.php in Security Review 8
File
src/Controller/HelpController.php
View source
<?php
namespace Drupal\security_review\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Link;
use Drupal\security_review\Checklist;
use Drupal\security_review\CheckResult;
use Drupal\security_review\SecurityReview;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class HelpController extends ControllerBase {
protected $checklist;
protected $securityReview;
private $dateFormatter;
public function __construct(SecurityReview $security_review, Checklist $checklist, DateFormatterInterface $dateFormatter) {
$this->checklist = $checklist;
$this->securityReview = $security_review;
$this->dateFormatter = $dateFormatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('security_review'), $container
->get('security_review.checklist'), $container
->get('date.formatter'));
}
public function index($namespace, $title) {
if ($namespace === NULL) {
return $this
->generalHelp();
}
return $this
->checkHelp($namespace, $title);
}
private function generalHelp() {
$paragraphs = [];
$paragraphs[] = $this
->t('You should take the security of your site very seriously. Fortunately, Drupal is fairly secure by default. The Security Review module automates many of the easy-to-make mistakes that render your site insecure, however it does not automatically make your site impenetrable. You should give care to what modules you install and how you configure your site and server. Be mindful of who visits your site and what features you expose for their use.');
$paragraphs[] = $this
->t('You can read more about securing your site in the <a href="http://drupal.org/security/secure-configuration">drupal.org handbooks</a> and on <a href="http://crackingdrupal.com">CrackingDrupal.com</a>. There are also additional modules you can install to secure or protect your site. Be aware though that the more modules you have running on your site the greater (usually) attack area you expose.');
$paragraphs[] = $this
->t('<a href="http://drupal.org/node/382752">Drupal.org Handbook: Introduction to security-related contrib modules</a>');
$checks = [];
foreach ($this->checklist
->getChecks() as $check) {
$check_namespace =& $checks[$check
->getMachineNamespace()];
if (!isset($check_namespace)) {
$check_namespace['namespace'] = $check
->getNamespace();
$check_namespace['check_links'] = [];
}
$check_namespace['check_links'][] = Link::createFromRoute($this
->t('@title', [
'@title' => $check
->getTitle(),
]), 'security_review.help', [
'namespace' => $check
->getMachineNamespace(),
'title' => $check
->getMachineTitle(),
]);
}
return [
'#theme' => 'general_help',
'#paragraphs' => $paragraphs,
'#checks' => $checks,
];
}
private function checkHelp($namespace, $title) {
$check = $this->checklist
->getCheck($namespace, $title);
if ($check == NULL) {
throw new NotFoundHttpException();
}
$output = [];
$output[] = $check
->help();
if ($check
->isSkipped()) {
if ($check
->skippedBy() != NULL) {
$user_object = $check
->skippedBy();
$user = $user_object
->toLink()
->toString();
}
else {
$user = 'Anonymous';
}
$skip_message = $this
->t('Check marked for skipping on @date by @user', [
'@date' => $this->dateFormatter
->format($check
->skippedOn()),
'@user' => $user,
]);
$output[] = [
'#type' => 'markup',
'#markup' => "<p>{$skip_message}</p>",
];
}
else {
$last_result = $check
->lastResult(TRUE);
if ($last_result instanceof CheckResult) {
$output[] = [
'#type' => 'markup',
'#markup' => '<div />',
];
$output[] = $check
->evaluate($last_result);
}
}
return $output;
}
}