Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / templates / module / src / Authentication / Provider / authentication-provider.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{module}}\Authentication\Provider\{{class}}.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{module}}\Authentication\Provider;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Core\Authentication\AuthenticationProviderInterface;
13 use Drupal\Core\Config\ConfigFactoryInterface;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
17 use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
18 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
19 {% endblock %}
20
21 {% block class_declaration %}
22 /**
23  * Class {{ class }}.
24  */
25 class {{ class }} implements AuthenticationProviderInterface {% endblock %}
26 {% block class_variables %}
27   /**
28    * The config factory.
29    *
30    * @var \Drupal\Core\Config\ConfigFactoryInterface
31    */
32   protected $configFactory;
33
34   /**
35    * The entity type manager.
36    *
37    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
38    */
39   protected $entityTypeManager;
40 {% endblock %}
41
42 {% block class_construct %}
43
44   /**
45    * Constructs a HTTP basic authentication provider object.
46    *
47    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
48    *   The config factory.
49    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
50    *   The entity type manager service.
51    */
52   public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
53     $this->configFactory = $config_factory;
54     $this->entityTypeManager = $entity_type_manager;
55   }
56 {% endblock %}
57
58 {% block class_create %}
59 {% endblock %}
60
61 {% block class_methods %}
62
63   /**
64    * Checks whether suitable authentication credentials are on the request.
65    *
66    * @param \Symfony\Component\HttpFoundation\Request $request
67    *   The request object.
68    *
69    * @return bool
70    *   TRUE if authentication credentials suitable for this provider are on the
71    *   request, FALSE otherwise.
72    */
73   public function applies(Request $request) {
74     // If you return TRUE and the method Authentication logic fails,
75     // you will get out from Drupal navigation if you are logged in.
76     return FALSE;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function authenticate(Request $request) {
83     $consumer_ip = $request->getClientIp();
84     $ips = [];
85     if (in_array($consumer_ip, $ips)) {
86       // Return Anonymous user.
87       return $this->entityTypeManager->getStorage('user')->load(0);
88     }
89     else {
90       throw new AccessDeniedHttpException();
91     }
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function cleanup(Request $request) {}
98
99   /**
100    * {@inheritdoc}
101    */
102   public function handleException(GetResponseForExceptionEvent $event) {
103     $exception = $event->getException();
104     if ($exception instanceof AccessDeniedHttpException) {
105       $event->setException(
106         new UnauthorizedHttpException('Invalid consumer origin.', $exception)
107       );
108       return TRUE;
109     }
110     return FALSE;
111   }
112 {% endblock %}