ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.LocalUserPasswordEncoderFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
32{
33 private ?string $default_encoder = null;
34
36 private array $supported_encoders = [];
37
42 public function __construct(array $config = [])
43 {
44 if (!empty($config)) {
45 foreach ($config as $key => $value) {
46 switch (strtolower($key)) {
47 case 'default_password_encoder':
48 $this->setDefaultEncoder($value);
49
50 break;
51 }
52 }
53 }
54
55 $this->initEncoders($config);
56 }
57
63 private function getEncoders(array $config): array
64 {
65 return [
66 new ilArgon2idPasswordEncoder($config),
67 new ilBcryptPhpPasswordEncoder($config),
68 new ilBcryptPasswordEncoder($config),
70 ];
71 }
72
77 private function initEncoders(array $config): void
78 {
79 $this->supported_encoders = [];
80
81 $encoders = $this->getEncoders($config);
82 foreach ($encoders as $encoder) {
83 if ($encoder->isSupportedByRuntime()) {
84 $this->supported_encoders[$encoder->getName()] = $encoder;
85 }
86 }
87 }
88
89 public function getDefaultEncoder(): ?string
90 {
92 }
93
94 public function setDefaultEncoder(string $default_encoder): void
95 {
96 $this->default_encoder = $default_encoder;
97 }
98
102 public function getSupportedEncoders(): array
103 {
105 }
106
111 public function setSupportedEncoders(array $supported_encoders): void
112 {
113 $this->supported_encoders = [];
114 foreach ($supported_encoders as $encoder) {
115 if (!($encoder instanceof ilPasswordEncoder) || !$encoder->isSupportedByRuntime()) {
116 throw new ilUserException(
117 \sprintf(
118 'One of the passed encoders is not valid: %s.',
119 print_r($encoder, true)
120 )
121 );
122 }
123 $this->supported_encoders[$encoder->getName()] = $encoder;
124 }
125 }
126
130 public function getSupportedEncoderNames(): array
131 {
132 return array_keys($this->getSupportedEncoders());
133 }
134
138 public function getEncoderByName(?string $name): ilPasswordEncoder
139 {
140 if ($name !== null && isset($this->supported_encoders[$name])) {
141 return $this->supported_encoders[$name];
142 }
143
144 if (!$this->getDefaultEncoder()) {
145 throw new ilUserException('No default encoder specified, fallback not possible.');
146 }
147
148 if (!isset($this->supported_encoders[$this->getDefaultEncoder()])) {
149 throw new ilUserException("No default encoder found for name: '{$this->getDefaultEncoder()}'.");
150 }
151
152 return $this->supported_encoders[$this->getDefaultEncoder()];
153 }
154}
Class for user password exception handling in ILIAS.