ILIAS  release_8 Revision v8.24
class.ilUserPasswordEncoderFactory.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 protected ?string $defaultEncoder = null;
30 protected array $encoders = [];
31
36 public function __construct(array $config = []) // Missing array type.
37 {
38 if (!empty($config)) {
39 foreach ($config as $key => $value) {
40 switch (strtolower($key)) {
41 case 'default_password_encoder':
42 $this->setDefaultEncoder($value);
43 break;
44 }
45 }
46 }
47
48 $this->initEncoders($config);
49 }
50
56 protected function getValidEncoders(array $config): array // Missing array type.
57 {
58 return [
62 ];
63 }
64
69 protected function initEncoders(array $config): void // Missing array type.
70 {
71 $this->encoders = [];
72
73 $encoders = $this->getValidEncoders($config);
74 foreach ($encoders as $encoder) {
75 if ($encoder->isSupportedByRuntime()) {
76 $this->encoders[$encoder->getName()] = $encoder;
77 }
78 }
79 }
80
81 public function getDefaultEncoder(): ?string
82 {
84 }
85
86 public function setDefaultEncoder(string $defaultEncoder): void
87 {
88 $this->defaultEncoder = $defaultEncoder;
89 }
90
94 public function getEncoders(): array
95 {
96 return $this->encoders;
97 }
98
103 public function setEncoders(array $encoders): void
104 {
105 $this->encoders = [];
106 foreach ($encoders as $encoder) {
107 if (!($encoder instanceof ilPasswordEncoder)) {
108 throw new ilUserException(sprintf(
109 'One of the passed encoders is not valid: %s.',
110 json_encode($encoder, JSON_THROW_ON_ERROR)
111 ));
112 }
113 $this->encoders[$encoder->getName()] = $encoder;
114 }
115 }
116
120 public function getSupportedEncoderNames(): array
121 {
122 return array_keys($this->getEncoders());
123 }
124
131 public function getEncoderByName(?string $name, bool $get_default_on_mismatch = false): ilPasswordEncoder
132 {
133 if ($name !== null && isset($this->encoders[$name])) {
134 return $this->encoders[$name];
135 }
136
137 if (!$get_default_on_mismatch) {
138 throw new ilUserException(sprintf('The encoder "%s" was not configured.', $name));
139 } elseif (!$this->getDefaultEncoder()) {
140 throw new ilUserException('No default encoder specified, fallback not possible.');
141 } elseif (!isset($this->encoders[$this->getDefaultEncoder()])) {
142 throw new ilUserException("No default encoder found for name: '{$this->getDefaultEncoder()}'.");
143 }
144
145 return $this->encoders[$this->getDefaultEncoder()];
146 }
147
154 public function getFirstEncoderForEncodedPasswordAndMatchers(string $encoded, array $matchers): ilPasswordEncoder
155 {
156 foreach ($this->getEncoders() as $encoder) {
157 foreach ($matchers as $encoderName => $callback) {
158 if (
159 is_callable($callback) &&
160 $encoder->getName() === $encoderName &&
161 $callback($encoded) === true
162 ) {
163 return $encoder;
164 }
165 }
166 }
167
168 return $this->getEncoderByName($this->getDefaultEncoder());
169 }
170}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getEncoderByName(?string $name, bool $get_default_on_mismatch=false)
getFirstEncoderForEncodedPasswordAndMatchers(string $encoded, array $matchers)
if($format !==null) $name
Definition: metadata.php:247
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
string $key
Consumer key/client ID value.
Definition: System.php:193