ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilUserPasswordEncoderFactory.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/User/exceptions/class.ilUserException.php';
5
12{
17
21 protected $encoders = array();
22
26 public function __construct(array $config = array())
27 {
28 if (!empty($config)) {
29 foreach ($config as $key => $value) {
30 switch (strtolower($key)) {
31 case 'default_password_encoder':
32 $this->setDefaultEncoder($value);
33 break;
34 }
35 }
36 }
37
38 $this->initEncoders($config);
39 }
40
45 protected function getValidEncoders($config) : array
46 {
47 return [
48 new \ilBcryptPhpPasswordEncoder($config),
49 new \ilBcryptPasswordEncoder($config),
50 new \ilMd5PasswordEncoder($config),
51 ];
52 }
53
57 protected function initEncoders(array $config)
58 {
59 $this->encoders = [];
60
61 $encoders = $this->getValidEncoders($config);
62
63 foreach ($encoders as $encoder) {
64 if ($encoder->isSupportedByRuntime()) {
65 $this->encoders[$encoder->getName()] = $encoder;
66 }
67 }
68 }
69
73 public function getDefaultEncoder()
74 {
76 }
77
82 {
83 $this->default_encoder = $default_encoder;
84 }
85
89 public function getEncoders()
90 {
91 return $this->encoders;
92 }
93
98 public function setEncoders(array $encoders)
99 {
100 $this->encoders = array();
101 foreach ($encoders as $encoder) {
102 if (!($encoder instanceof \ilPasswordEncoder)) {
103 throw new \ilUserException(sprintf('One of the passed encoders is not valid: %s.', json_encode($encoder)));
104 }
105 $this->encoders[$encoder->getName()] = $encoder;
106 }
107 }
108
112 public function getSupportedEncoderNames()
113 {
114 return array_keys($this->getEncoders());
115 }
116
123 public function getEncoderByName($name, $get_default_on_mismatch = false)
124 {
125 if (!isset($this->encoders[$name])) {
126 if (!$get_default_on_mismatch) {
127 throw new \ilUserException(sprintf('The encoder "%s" was not configured.', $name));
128 } elseif (!$this->getDefaultEncoder()) {
129 throw new \ilUserException('No default encoder specified, fallback not possible.');
130 } elseif (!isset($this->encoders[$this->getDefaultEncoder()])) {
131 throw new \ilUserException("No default encoder found for name: '{$this->getDefaultEncoder()}'.");
132 }
133
134 return $this->encoders[$this->getDefaultEncoder()];
135 }
136
137 return $this->encoders[$name];
138 }
139
146 public function getFirstEncoderForEncodedPasswordAndMatchers(string $encoded, array $matchers) : \ilPasswordEncoder
147 {
148 foreach ($this->getEncoders() as $encoder) {
149 foreach ($matchers as $encoderName => $callback) {
150 if (
151 $encoder->getName() === $encoderName &&
152 is_callable($callback) && $callback($encoded) === true
153 ) {
154 return $encoder;
155 }
156 }
157 }
158
159 return $this->getEncoderByName($this->getDefaultEncoder());
160 }
161}
An exception for terminatinating execution or to throw for unit testing.
getEncoderByName($name, $get_default_on_mismatch=false)
getFirstEncoderForEncodedPasswordAndMatchers(string $encoded, array $matchers)
$key
Definition: croninfo.php:18
$config
Definition: bootstrap.php:15