Rev 1116 | Rev 1199 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusPageRaChangeEMail extends OIDplusPagePluginRa { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @param string $actionID |
||
30 | * @param array $params |
||
1143 | daniel-mar | 31 | * @return array |
1116 | daniel-mar | 32 | * @throws OIDplusException |
33 | * @throws OIDplusMailException |
||
34 | */ |
||
35 | public function action(string $actionID, array $params): array { |
||
635 | daniel-mar | 36 | if ($actionID == 'change_ra_email') { |
37 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
38 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
||
39 | } |
||
40 | |||
41 | _CheckParamExists($params, 'old_email'); |
||
42 | _CheckParamExists($params, 'new_email'); |
||
43 | |||
44 | $old_email = $params['old_email']; |
||
45 | $new_email = $params['new_email']; |
||
46 | |||
47 | $ra = new OIDplusRA($old_email); |
||
48 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
49 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password')); |
||
50 | } |
||
51 | |||
52 | if (!OIDplus::authUtils()->isRaLoggedIn($old_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
53 | throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its email address.')); |
||
54 | } |
||
55 | |||
56 | if (!OIDplus::mailUtils()->validMailAddress($new_email)) { |
||
57 | throw new OIDplusException(_L('eMail address is invalid.')); |
||
58 | } |
||
59 | |||
60 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
||
790 | daniel-mar | 61 | if (!$res->any()) { |
635 | daniel-mar | 62 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
63 | } |
||
64 | |||
65 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
||
790 | daniel-mar | 66 | if ($res->any()) { |
635 | daniel-mar | 67 | throw new OIDplusException(_L('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.')); |
68 | } |
||
69 | |||
70 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
71 | $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email); |
||
72 | |||
73 | $ra = new OIDplusRA($old_email); |
||
74 | |||
75 | // Change RA email |
||
76 | $ra->change_email($new_email); |
||
77 | OIDplus::logger()->log("[WARN]RA($old_email)!+[INFO]RA($new_email)!+[OK]A!", "Admin changed email address '$old_email' to '$new_email'"); |
||
78 | |||
79 | // Change objects |
||
80 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email)); |
||
81 | while ($row = $res->fetch_array()) { |
||
82 | OIDplus::logger()->log("[INFO]OID(".$row['id'].")+SUPOID(".$row['id'].")", "Admin changed email address of RA '$old_email' (owner of ".$row['id'].") to '$new_email'"); |
||
83 | } |
||
84 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
||
978 | daniel-mar | 85 | OIDplusObject::resetObjectInformationCache(); |
635 | daniel-mar | 86 | |
87 | // Re-login |
||
88 | if ($ra_was_logged_in) { |
||
89 | OIDplus::authUtils()->raLogout($old_email); |
||
90 | OIDplus::authUtils()->raLogin($new_email); |
||
91 | } |
||
92 | |||
93 | return array("status" => 0); |
||
94 | } else { |
||
95 | OIDplus::logger()->log("[INFO]RA($old_email)!+RA($new_email)!", "Requested email address change from '$old_email' to '$new_email'"); |
||
96 | |||
97 | $timestamp = time(); |
||
801 | daniel-mar | 98 | $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:activate_new_ra_email$'.$old_email.'$'.$new_email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp)); |
635 | daniel-mar | 99 | |
100 | $message = file_get_contents(__DIR__ . '/change_request_email.tpl'); |
||
801 | daniel-mar | 101 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
635 | daniel-mar | 102 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
103 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
||
104 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
||
105 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
||
106 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
||
107 | OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->getValue('system_title').' - Change email request', $message); |
||
108 | |||
109 | return array("status" => 0); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | else if ($actionID == 'activate_new_ra_email') { |
||
114 | if (!OIDplus::config()->getValue('allow_ra_email_change')) { |
||
115 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
||
116 | } |
||
117 | |||
118 | _CheckParamExists($params, 'old_email'); |
||
119 | _CheckParamExists($params, 'new_email'); |
||
120 | _CheckParamExists($params, 'password'); |
||
121 | _CheckParamExists($params, 'auth'); |
||
122 | _CheckParamExists($params, 'timestamp'); |
||
123 | |||
124 | $old_email = $params['old_email']; |
||
125 | $new_email = $params['new_email']; |
||
126 | $password = $params['password']; |
||
127 | |||
128 | $auth = $params['auth']; |
||
129 | $timestamp = $params['timestamp']; |
||
130 | |||
131 | $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email); |
||
132 | |||
133 | $ra = new OIDplusRA($old_email); |
||
134 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
135 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password')); |
||
136 | } |
||
137 | |||
138 | if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
||
139 | throw new OIDplusException(_L('Invalid auth key')); |
||
140 | } |
||
141 | |||
1113 | daniel-mar | 142 | if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_email_change_time'))) { |
635 | daniel-mar | 143 | throw new OIDplusException(_L('Activation link expired!')); |
144 | } |
||
145 | |||
146 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
||
790 | daniel-mar | 147 | if (!$res->any()) { |
635 | daniel-mar | 148 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
149 | } |
||
150 | |||
151 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
||
790 | daniel-mar | 152 | if ($res->any()) { |
635 | daniel-mar | 153 | throw new OIDplusException(_L('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.')); |
154 | } |
||
155 | |||
156 | $ra = new OIDplusRA($old_email); |
||
157 | if (!$ra->isPasswordLess()) { |
||
158 | if (!$ra->checkPassword($password)) { |
||
159 | throw new OIDplusException(_L('Wrong password')); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | // Change address of RA |
||
164 | $ra->change_email($new_email); |
||
165 | OIDplus::logger()->log("[OK]RA($new_email)!+RA($old_email)!", "RA '$old_email' has changed their email address to '$new_email'"); |
||
166 | |||
167 | // Change objects |
||
168 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email)); |
||
169 | while ($row = $res->fetch_array()) { |
||
170 | OIDplus::logger()->log("[INFO]OID(".$row['id'].")+SUPOID(".$row['id'].")", "RA '$old_email' (owner of ".$row['id'].") has changed their email address to '$new_email'"); |
||
171 | } |
||
172 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
||
978 | daniel-mar | 173 | OIDplusObject::resetObjectInformationCache(); |
635 | daniel-mar | 174 | |
175 | // Re-login |
||
176 | if ($ra_was_logged_in) { |
||
177 | OIDplus::authUtils()->raLogout($old_email); |
||
178 | OIDplus::authUtils()->raLogin($new_email); |
||
179 | } |
||
180 | |||
181 | // Send email |
||
182 | $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl'); |
||
801 | daniel-mar | 183 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
635 | daniel-mar | 184 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
185 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
||
186 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
||
187 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
||
188 | OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->getValue('system_title').' - eMail address changed', $message); |
||
189 | |||
190 | return array("status" => 0); |
||
191 | } else { |
||
1116 | daniel-mar | 192 | return parent::action($actionID, $params); |
635 | daniel-mar | 193 | } |
194 | } |
||
195 | |||
1116 | daniel-mar | 196 | /** |
197 | * @param bool $html |
||
198 | * @return void |
||
199 | * @throws OIDplusException |
||
200 | */ |
||
201 | public function init(bool $html=true) { |
||
635 | daniel-mar | 202 | OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
203 | if (!is_numeric($value) || ($value < 0)) { |
||
204 | throw new OIDplusException(_L('Please enter a valid value.')); |
||
205 | } |
||
206 | }); |
||
207 | OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
208 | if (($value != '0') && ($value != '1')) { |
||
209 | throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).')); |
||
210 | } |
||
211 | }); |
||
212 | } |
||
213 | |||
1116 | daniel-mar | 214 | /** |
215 | * @param string $id |
||
216 | * @param array $out |
||
217 | * @param bool $handled |
||
218 | * @return void |
||
219 | * @throws OIDplusException |
||
220 | */ |
||
221 | public function gui(string $id, array &$out, bool &$handled) { |
||
635 | daniel-mar | 222 | if (explode('$',$id)[0] == 'oidplus:change_ra_email') { |
223 | $handled = true; |
||
224 | |||
225 | $ra_email = explode('$',$id)[1]; |
||
226 | |||
227 | $out['title'] = _L('Change RA email'); |
||
801 | daniel-mar | 228 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 229 | |
230 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
800 | daniel-mar | 231 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 232 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>').'</p>'; |
233 | return; |
||
234 | } |
||
235 | |||
236 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email)); |
||
790 | daniel-mar | 237 | if (!$res->any()) { |
800 | daniel-mar | 238 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 239 | $out['text'] = _L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'); |
240 | return; |
||
241 | } |
||
242 | |||
243 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
800 | daniel-mar | 244 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 245 | $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>'; |
246 | return; |
||
247 | } |
||
248 | |||
249 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
250 | $ra = new OIDplusRA($ra_email); |
||
251 | if ($ra->isPasswordLess()) { |
||
252 | $out['text'] .= '<p>'._L('Attention: This user does not have a password because they log in using LDAP or Google OAuth etc.').'</p>'; |
||
253 | $out['text'] .= '<p>'._L('If you change the email address, the user cannot log in anymore, because the LDAP/OAuth plugin identifies the user via email address, not OpenID.').'</p>'; |
||
254 | $out['text'] .= '<p>'._L('If you want to change the email address of the user, please <a %1>define a password</a> for them, so that they can use the regular login method using their new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>'; |
||
255 | } |
||
256 | |||
257 | $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(true);">'; |
||
258 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
||
259 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
||
260 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
||
261 | $out['text'] .= '<br><input type="submit" value="'._L('Change password').'"> '._L('(admin does not require email verification)').'</form>'; |
||
262 | } else { |
||
263 | $ra = new OIDplusRA($ra_email); |
||
264 | if ($ra->isPasswordLess()) { |
||
800 | daniel-mar | 265 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 266 | $out['text'] .= '<p>'._L('Attention: You are logged in without password (via LDAP or Google OAuth etc.).').'</p>'; |
267 | $out['text'] .= '<p>'._L('Therefore, you cannot change your email address, otherwise you would love access to your account!').'</p>'; |
||
268 | $out['text'] .= '<p>'._L('If you want to change your email address, then please <a %1>setup a password</a> first, and then use the regular login method to log in using your new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>'; |
||
269 | return; |
||
270 | } |
||
271 | |||
272 | $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(false);">'; |
||
273 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
||
274 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
||
275 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
||
276 | $out['text'] .= '<br><input type="submit" value="'._L('Send new activation email').'"></form>'; |
||
277 | } |
||
278 | } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') { |
||
279 | $handled = true; |
||
280 | |||
281 | $old_email = explode('$',$id)[1]; |
||
282 | $new_email = explode('$',$id)[2]; |
||
283 | $timestamp = explode('$',$id)[3]; |
||
284 | $auth = explode('$',$id)[4]; |
||
285 | |||
286 | $out['title'] = _L('Perform email address change'); |
||
801 | daniel-mar | 287 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 288 | |
289 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
800 | daniel-mar | 290 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 291 | $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>'; |
292 | return; |
||
293 | } |
||
294 | |||
295 | $ra = new OIDplusRA($old_email); |
||
296 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
||
800 | daniel-mar | 297 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 298 | $out['text'] = '<p>'._L('E-Mail-Address cannot be changed because this user does not have a password').'</p>'; |
299 | return; |
||
300 | } |
||
301 | |||
302 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
||
790 | daniel-mar | 303 | if (!$res->any()) { |
800 | daniel-mar | 304 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 305 | $out['text'] = _L('eMail address does not exist anymore. It was probably already changed.'); |
306 | } else { |
||
307 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
||
790 | daniel-mar | 308 | if ($res->any()) { |
800 | daniel-mar | 309 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 310 | $out['text'] = _L('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.'); |
311 | } else { |
||
312 | if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
||
800 | daniel-mar | 313 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 314 | $out['text'] = _L('Invalid authorization. Is the URL OK?'); |
315 | } else { |
||
316 | $out['text'] = '<p>'._L('Old eMail-Address').': <b>'.$old_email.'</b></p> |
||
317 | <p>'._L('New eMail-Address').': <b>'.$new_email.'</b></p> |
||
318 | |||
319 | <form id="activateNewRaEmailForm" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.activateNewRaEmailFormOnSubmit();"> |
||
320 | <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/> |
||
321 | <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/> |
||
322 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
||
323 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
||
324 | |||
325 | <div><label class="padding_label">'._L('Please verify your password').':</label><input type="password" id="password" value=""/></div> |
||
326 | <br><input type="submit" value="'._L('Change email address').'"> |
||
327 | </form>'; |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | |||
1116 | daniel-mar | 334 | /** |
335 | * @param array $json |
||
336 | * @param string|null $ra_email |
||
337 | * @param bool $nonjs |
||
338 | * @param string $req_goto |
||
339 | * @return bool |
||
340 | * @throws OIDplusException |
||
341 | */ |
||
342 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
||
635 | daniel-mar | 343 | if (!$ra_email) return false; |
344 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false; |
||
345 | |||
800 | daniel-mar | 346 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 347 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 348 | } else { |
349 | $tree_icon = null; // default icon (folder) |
||
350 | } |
||
351 | |||
352 | $json[] = array( |
||
353 | 'id' => 'oidplus:change_ra_email$'.$ra_email, |
||
354 | 'icon' => $tree_icon, |
||
355 | 'text' => _L('Change email address') |
||
356 | ); |
||
357 | |||
358 | return true; |
||
359 | } |
||
360 | |||
1116 | daniel-mar | 361 | /** |
362 | * @param string $request |
||
363 | * @return array|false |
||
364 | */ |
||
365 | public function tree_search(string $request) { |
||
635 | daniel-mar | 366 | return false; |
367 | } |
||
368 | } |