ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilUserPasswordEncoderFactory.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/User/exceptions/class.ilUserException.php';
5
12{
14 protected $defaultEncoder;
15
19 protected $encoders = array();
20
25 public function __construct(array $config = [])
26 {
27 if (!empty($config)) {
28 foreach ($config as $key => $value) {
29 switch (strtolower($key)) {
30 case 'default_password_encoder':
31 $this->setDefaultEncoder($value);
32 break;
33 }
34 }
35 }
36
37 $this->initEncoders($config);
38 }
39
45 protected function getValidEncoders($config) : array
46 {
47 return [
51 ];
52 }
53
58 protected function initEncoders(array $config)
59 {
60 $this->encoders = [];
61
62 $encoders = $this->getValidEncoders($config);
63
64 foreach ($encoders as $encoder) {
65 if ($encoder->isSupportedByRuntime()) {
66 $this->encoders[$encoder->getName()] = $encoder;
67 }
68 }
69 }
70
74 public function getDefaultEncoder() : ?string
75 {
77 }
78
82 public function setDefaultEncoder(string $defaultEncoder)
83 {
84 $this->defaultEncoder = $defaultEncoder;
85 }
86
90 public function getEncoders() : array
91 {
92 return $this->encoders;
93 }
94
99 public function setEncoders(array $encoders) : void
100 {
101 $this->encoders = array();
102 foreach ($encoders as $encoder) {
103 if (!($encoder instanceof ilPasswordEncoder)) {
104 throw new ilUserException(sprintf(
105 'One of the passed encoders is not valid: %s.',
106 json_encode($encoder)
107 ));
108 }
109 $this->encoders[$encoder->getName()] = $encoder;
110 }
111 }
112
116 public function getSupportedEncoderNames() : array
117 {
118 return array_keys($this->getEncoders());
119 }
120
127 public function getEncoderByName($name, $get_default_on_mismatch = false) : ilPasswordEncoder
128 {
129 if (!isset($this->encoders[$name])) {
130 if (!$get_default_on_mismatch) {
131 throw new ilUserException(sprintf('The encoder "%s" was not configured.', $name));
132 } elseif (!$this->getDefaultEncoder()) {
133 throw new ilUserException('No default encoder specified, fallback not possible.');
134 } elseif (!isset($this->encoders[$this->getDefaultEncoder()])) {
135 throw new ilUserException("No default encoder found for name: '{$this->getDefaultEncoder()}'.");
136 }
137
138 return $this->encoders[$this->getDefaultEncoder()];
139 }
140
141 return $this->encoders[$name];
142 }
143
150 public function getFirstEncoderForEncodedPasswordAndMatchers(string $encoded, array $matchers) : ilPasswordEncoder
151 {
152 foreach ($this->getEncoders() as $encoder) {
153 foreach ($matchers as $encoderName => $callback) {
154 if (
155 $encoder->getName() === $encoderName &&
156 is_callable($callback) && $callback($encoded) === true
157 ) {
158 return $encoder;
159 }
160 }
161 }
162
163 return $this->getEncoderByName($this->getDefaultEncoder());
164 }
165}
An exception for terminatinating execution or to throw for unit testing.
Class for user related exception handling in ILIAS.
getEncoderByName($name, $get_default_on_mismatch=false)
getFirstEncoderForEncodedPasswordAndMatchers(string $encoded, array $matchers)
if($format !==null) $name
Definition: metadata.php:230
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68