AuthenticationCollector.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Drupal\Core\Authentication;
  3. /**
  4. * A collector class for authentication providers.
  5. */
  6. class AuthenticationCollector implements AuthenticationCollectorInterface {
  7. /**
  8. * Array of all registered authentication providers, keyed by ID.
  9. *
  10. * @var \Drupal\Core\Authentication\AuthenticationProviderInterface[]
  11. */
  12. protected $providers;
  13. /**
  14. * Array of all providers and their priority.
  15. *
  16. * @var array
  17. */
  18. protected $providerOrders = [];
  19. /**
  20. * Sorted list of registered providers.
  21. *
  22. * @var \Drupal\Core\Authentication\AuthenticationProviderInterface[]
  23. */
  24. protected $sortedProviders;
  25. /**
  26. * List of providers which are allowed on routes with no _auth option.
  27. *
  28. * @var string[]
  29. */
  30. protected $globalProviders;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function addProvider(AuthenticationProviderInterface $provider, $provider_id, $priority = 0, $global = FALSE) {
  35. $this->providers[$provider_id] = $provider;
  36. $this->providerOrders[$priority][$provider_id] = $provider;
  37. // Force the providers to be re-sorted.
  38. $this->sortedProviders = NULL;
  39. if ($global) {
  40. $this->globalProviders[$provider_id] = TRUE;
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function isGlobal($provider_id) {
  47. return isset($this->globalProviders[$provider_id]);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getProvider($provider_id) {
  53. return isset($this->providers[$provider_id]) ? $this->providers[$provider_id] : NULL;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getSortedProviders() {
  59. if (!isset($this->sortedProviders)) {
  60. // Sort the providers according to priority.
  61. krsort($this->providerOrders);
  62. // Merge nested providers from $this->providers into $this->sortedProviders.
  63. $this->sortedProviders = [];
  64. foreach ($this->providerOrders as $providers) {
  65. $this->sortedProviders = array_merge($this->sortedProviders, $providers);
  66. }
  67. }
  68. return $this->sortedProviders;
  69. }
  70. }