Version 1
[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  * @package Drupal\{{module}}\Authentication\Provider
26  */
27 class {{ class }} implements AuthenticationProviderInterface {% endblock %}
28 {% block class_variables %}
29   /**
30    * The config factory.
31    *
32    * @var \Drupal\Core\Config\ConfigFactoryInterface
33    */
34   protected $configFactory;
35
36   /**
37    * The entity type manager.
38    *
39    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40    */
41   protected $entityTypeManager;
42 {% endblock %}
43
44 {% block class_construct %}
45
46   /**
47    * Constructs a HTTP basic authentication provider object.
48    *
49    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
50    *   The config factory.
51    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
52    *   The entity type manager service.
53    */
54   public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
55     $this->configFactory = $config_factory;
56     $this->entityTypeManager = $entity_type_manager;
57   }
58 {% endblock %}
59
60 {% block class_create %}
61 {% endblock %}
62
63 {% block class_methods %}
64
65   /**
66    * Checks whether suitable authentication credentials are on the request.
67    *
68    * @param \Symfony\Component\HttpFoundation\Request $request
69    *   The request object.
70    *
71    * @return bool
72    *   TRUE if authentication credentials suitable for this provider are on the
73    *   request, FALSE otherwise.
74    */
75   public function applies(Request $request) {
76     // If you return TRUE and the method Authentication logic fails,
77     // you will get out from Drupal navigation if you are logged in.
78     return FALSE;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function authenticate(Request $request) {
85     $consumer_ip = $request->getClientIp();
86     $ips = [];
87     if (in_array($consumer_ip, $ips)) {
88       // Return Anonymous user.
89       return $this->entityTypeManager->getStorage('user')->load(0);
90     }
91     else {
92       throw new AccessDeniedHttpException();
93     }
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function cleanup(Request $request) {}
100
101   /**
102    * {@inheritdoc}
103    */
104   public function handleException(GetResponseForExceptionEvent $event) {
105     $exception = $event->getException();
106     if ($exception instanceof AccessDeniedHttpException) {
107       $event->setException(
108         new UnauthorizedHttpException('Invalid consumer origin.', $exception)
109       );
110       return TRUE;
111     }
112     return FALSE;
113   }
114 {% endblock %}