3 namespace Drupal\security_review\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Datetime\DateFormatterInterface;
8 use Drupal\security_review\Checklist;
9 use Drupal\security_review\CheckResult;
10 use Drupal\security_review\SecurityReview;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 * The class of the Help pages' controller.
17 class HelpController extends ControllerBase {
20 * The security_review.checklist service.
22 * @var \Drupal\security_review\Checklist
27 * The security_review service.
29 * @var \Drupal\security_review\SecurityReview
31 protected $securityReview;
34 * The date.formatter service.
36 * @var \Drupal\Core\Datetime\DateFormatterInterface
38 private $dateFormatter;
41 * Constructs a HelpController.
43 * @param \Drupal\security_review\SecurityReview $security_review
44 * The security_review service.
45 * @param \Drupal\security_review\Checklist $checklist
46 * The security_review.checklist service.
47 * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
48 * The date.formatter service.
50 public function __construct(SecurityReview $security_review, Checklist $checklist, DateFormatterInterface $dateFormatter) {
51 // Store the dependencies.
52 $this->checklist = $checklist;
53 $this->securityReview = $security_review;
54 $this->dateFormatter = $dateFormatter;
60 public static function create(ContainerInterface $container) {
62 $container->get('security_review'),
63 $container->get('security_review.checklist'),
64 $container->get('date.formatter')
69 * Serves as an entry point for the help pages.
71 * @param string|NULL $namespace
72 * The namespace of the check (null if general page).
73 * @param string $title
74 * The name of the check.
77 * The requested help page.
79 public function index($namespace, $title) {
80 // If no namespace is set, print the general help page.
81 if ($namespace === NULL) {
82 return $this->generalHelp();
85 // Print check-specific help.
86 return $this->checkHelp($namespace, $title);
90 * Returns the general help page.
93 * The general help page.
95 private function generalHelp() {
98 // Print the general help.
99 $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.');
100 $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.');
101 $paragraphs[] = $this->t('<a href="http://drupal.org/node/382752">Drupal.org Handbook: Introduction to security-related contrib modules</a>');
103 // Print the list of security checks with links to their help pages.
105 foreach ($this->checklist->getChecks() as $check) {
106 // Get the namespace array's reference.
107 $check_namespace = &$checks[$check->getMachineNamespace()];
109 // Set up the namespace array if not set.
110 if (!isset($check_namespace)) {
111 $check_namespace['namespace'] = $check->getNamespace();
112 $check_namespace['check_links'] = [];
115 // Add the link pointing to the check-specific help.
116 $check_namespace['check_links'][] = Link::createFromRoute(
117 $this->t('@title', ['@title' => $check->getTitle()]),
118 'security_review.help',
120 'namespace' => $check->getMachineNamespace(),
121 'title' => $check->getMachineTitle(),
127 '#theme' => 'general_help',
128 '#paragraphs' => $paragraphs,
129 '#checks' => $checks,
134 * Returns a check-specific help page.
136 * @param string $namespace
137 * The namespace of the check.
138 * @param string $title
139 * The name of the check.
142 * The check's help page.
144 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
145 * If the check is not found.
147 private function checkHelp($namespace, $title) {
148 // Get the requested check.
149 $check = $this->checklist->getCheck($namespace, $title);
151 // If the check doesn't exist, throw 404.
152 if ($check == NULL) {
153 throw new NotFoundHttpException();
156 // Print the help page.
158 $output[] = $check->help();
160 // If the check is skipped print the skip message, else print the
162 if ($check->isSkipped()) {
164 if ($check->skippedBy() != NULL) {
165 $user = $check->skippedBy()->link();
171 $skip_message = $this->t(
172 'Check marked for skipping on @date by @user',
174 '@date' => $this->dateFormatter->format($check->skippedOn()),
181 '#markup' => "<p>$skip_message</p>",
185 // Evaluate last result, if any.
186 $last_result = $check->lastResult(TRUE);
187 if ($last_result instanceof CheckResult) {
191 '#markup' => '<div />',
195 $output[] = $check->evaluate($last_result);
199 // Return the completed page.