ILIAS  release_8 Revision v8.24
class.ilUserPasswordManager.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 private const MIN_SALT_SIZE = 16;
29
30 private static ?self $instance = null;
31
33 protected ?ilSetting $settings = null;
34 protected ?ilDBInterface $db = null;
35 protected ?string $encoderName = null;
39 protected array $config = [];
40
48 public function __construct(array $config = [])
49 {
50 if (!empty($config)) {
51 foreach ($config as $key => $value) {
52 switch (strtolower($key)) {
53 case 'settings':
54 $this->setSettings($value);
55 break;
56 case 'db':
57 $this->setDb($value);
58 break;
59 case 'password_encoder':
60 $this->setEncoderName($value);
61 break;
62 case 'encoder_factory':
63 $this->setEncoderFactory($value);
64 break;
65 }
66 }
67 }
68
69 if (!$this->getEncoderName()) {
70 throw new ilUserException(sprintf(
71 '"password_encoder" must be set in %s.',
72 json_encode($config, JSON_THROW_ON_ERROR)
73 ));
74 }
75
76 if (!($this->getEncoderFactory() instanceof ilUserPasswordEncoderFactory)) {
77 throw new ilUserException(sprintf(
78 '"encoder_factory" must be instance of ilUserPasswordEncoderFactory and set in %s.',
79 json_encode($config, JSON_THROW_ON_ERROR)
80 ));
81 }
82 }
83
90 public static function getInstance(): self
91 {
92 global $DIC;
93
94 if (self::$instance instanceof self) {
95 return self::$instance;
96 }
97
98 $password_manager = new ilUserPasswordManager(
99 [
100 'encoder_factory' => new ilUserPasswordEncoderFactory(
101 [
102 'default_password_encoder' => 'bcryptphp',
103 'ignore_security_flaw' => true,
104 'data_directory' => ilFileUtils::getDataDir()
105 ]
106 ),
107 'password_encoder' => 'bcryptphp',
108 'settings' => $DIC->isDependencyAvailable('settings') ? $DIC->settings() : null,
109 'db' => $DIC->database(),
110 ]
111 );
112
113 self::$instance = $password_manager;
114 return self::$instance;
115 }
116
117 public function setSettings(?ilSetting $settings): void
118 {
119 $this->settings = $settings;
120 }
121
122 public function setDb(ilDBInterface $db): void
123 {
124 $this->db = $db;
125 }
126
127 public function getEncoderName(): ?string
128 {
129 return $this->encoderName;
130 }
131
132 public function setEncoderName(string $encoderName): void
133 {
134 $this->encoderName = $encoderName;
135 }
136
138 {
140 }
141
143 {
144 $this->encoderFactory = $encoderFactory;
145 }
146
147 public function encodePassword(ilObjUser $user, string $raw): void
148 {
149 $encoder = $this->getEncoderFactory()->getEncoderByName($this->getEncoderName());
150 $user->setPasswordEncodingType($encoder->getName());
151 if ($encoder->requiresSalt()) {
152 $user->setPasswordSalt(
153 substr(str_replace('+', '.', base64_encode(ilPasswordUtils::getBytes(self::MIN_SALT_SIZE))), 0, 22)
154 );
155 } else {
156 $user->setPasswordSalt(null);
157 }
158 $user->setPasswd($encoder->encodePassword($raw, (string) $user->getPasswordSalt()), ilObjUser::PASSWD_CRYPTED);
159 }
160
161 public function isEncodingTypeSupported(string $name): bool
162 {
163 return in_array($name, $this->getEncoderFactory()->getSupportedEncoderNames());
164 }
165
166 public function verifyPassword(ilObjUser $user, string $raw): bool
167 {
168 $encoder = $this->getEncoderFactory()->getEncoderByName($user->getPasswordEncodingType(), true);
169 if ($this->getEncoderName() !== $encoder->getName()) {
170 if ($encoder->isPasswordValid($user->getPasswd(), $raw, (string) $user->getPasswordSalt())) {
171 $user->resetPassword($raw, $raw);
172 return true;
173 }
174 } elseif ($encoder->isPasswordValid($user->getPasswd(), $raw, (string) $user->getPasswordSalt())) {
175 if ($encoder->requiresReencoding($user->getPasswd())) {
176 $user->resetPassword($raw, $raw);
177 }
178
179 return true;
180 }
181
182 return false;
183 }
184
186 {
187 $defaultAuthMode = $this->settings->get('auth_mode');
188 $defaultAuthModeCondition = '';
189 if ((int) $defaultAuthMode === ilAuthUtils::AUTH_LOCAL) {
190 $defaultAuthModeCondition = ' OR auth_mode = ' . $this->db->quote('default', 'text');
191 }
192
193 $this->db->manipulateF(
194 "UPDATE usr_data SET passwd_policy_reset = %s WHERE (auth_mode = %s $defaultAuthModeCondition)",
195 ['integer', 'text'],
196 [1, 'local']
197 );
198 }
199}
static getDataDir()
get data directory (outside webspace)
User class.
resetPassword(string $raw, string $raw_retype)
Resets the user password.
setPasswordSalt(?string $password_salt)
setPasswordEncodingType(?string $password_encryption_type)
const PASSWD_CRYPTED
setPasswd(string $a_str, string $a_type=ilObjUser::PASSWD_PLAIN)
static getBytes(int $length)
Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
verifyPassword(ilObjUser $user, string $raw)
__construct(array $config=[])
Please use the singleton method for instance creation The constructor is still public because of the ...
static getInstance()
Singleton method to reduce footprint (included files, created instances)
setEncoderFactory(ilUserPasswordEncoderFactory $encoderFactory)
ilUserPasswordEncoderFactory $encoderFactory
encodePassword(ilObjUser $user, string $raw)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
if($format !==null) $name
Definition: metadata.php:247
string $key
Consumer key/client ID value.
Definition: System.php:193