ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUserPasswordEncoderFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  private ?string $default_encoder = null;
25  private array $supported_encoders = [];
26 
31  public function __construct(array $config = [])
32  {
33  if (!empty($config)) {
34  foreach ($config as $key => $value) {
35  switch (strtolower($key)) {
36  case 'default_password_encoder':
37  $this->setDefaultEncoder($value);
38  break;
39  }
40  }
41  }
42 
43  $this->initEncoders($config);
44  }
45 
51  private function getEncoders(array $config): array
52  {
53  return [
54  new ilArgon2idPasswordEncoder($config),
55  new ilBcryptPhpPasswordEncoder($config),
56  new ilBcryptPasswordEncoder($config),
58  ];
59  }
60 
65  private function initEncoders(array $config): void
66  {
67  $this->supported_encoders = [];
68 
69  $encoders = $this->getEncoders($config);
70  foreach ($encoders as $encoder) {
71  if ($encoder->isSupportedByRuntime()) {
72  $this->supported_encoders[$encoder->getName()] = $encoder;
73  }
74  }
75  }
76 
77  public function getDefaultEncoder(): ?string
78  {
80  }
81 
82  public function setDefaultEncoder(string $default_encoder): void
83  {
84  $this->default_encoder = $default_encoder;
85  }
86 
90  public function getSupportedEncoders(): array
91  {
93  }
94 
99  public function setSupportedEncoders(array $supported_encoders): void
100  {
101  $this->supported_encoders = [];
102  foreach ($supported_encoders as $encoder) {
103  if (!($encoder instanceof ilPasswordEncoder) || !$encoder->isSupportedByRuntime()) {
104  throw new ilUserException(sprintf(
105  'One of the passed encoders is not valid: %s.',
106  print_r($encoder, true)
107  ));
108  }
109  $this->supported_encoders[$encoder->getName()] = $encoder;
110  }
111  }
112 
116  public function getSupportedEncoderNames(): array
117  {
118  return array_keys($this->getSupportedEncoders());
119  }
120 
124  public function getEncoderByName(?string $name): ilPasswordEncoder
125  {
126  if ($name !== null && isset($this->supported_encoders[$name])) {
127  return $this->supported_encoders[$name];
128  }
129 
130  if (!$this->getDefaultEncoder()) {
131  throw new ilUserException('No default encoder specified, fallback not possible.');
132  } elseif (!isset($this->supported_encoders[$this->getDefaultEncoder()])) {
133  throw new ilUserException("No default encoder found for name: '{$this->getDefaultEncoder()}'.");
134  }
135 
136  return $this->supported_encoders[$this->getDefaultEncoder()];
137  }
138 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193