Subversion Repositories oidplus

Rev

Rev 433 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
61 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
 
511 daniel-mar 20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
256 daniel-mar 22
class OIDplusPageRaChangePassword extends OIDplusPagePluginRa {
61 daniel-mar 23
 
321 daniel-mar 24
        public function action($actionID, $params) {
25
                if ($actionID == 'change_ra_password') {
26
                        $email = $params['email'];
61 daniel-mar 27
 
261 daniel-mar 28
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
236 daniel-mar 29
                        if ($res->num_rows() == 0) {
360 daniel-mar 30
                                throw new OIDplusException(_L('RA does not exist'));
61 daniel-mar 31
                        }
32
 
33
                        if (!OIDplus::authUtils()::isRaLoggedIn($email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
360 daniel-mar 34
                                throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its data.'));
61 daniel-mar 35
                        }
36
 
152 daniel-mar 37
                        if (!OIDplus::authUtils()::isAdminLoggedIn()) {
321 daniel-mar 38
                                $old_password = $params['old_password'];
152 daniel-mar 39
                        }
321 daniel-mar 40
                        $password1 = $params['new_password1'];
41
                        $password2 = $params['new_password2'];
61 daniel-mar 42
 
43
                        if ($password1 !== $password2) {
360 daniel-mar 44
                                throw new OIDplusException(_L('Passwords do not match'));
61 daniel-mar 45
                        }
46
 
257 daniel-mar 47
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
360 daniel-mar 48
                                $minlen = OIDplus::config()->getValue('ra_min_password_length');
49
                                throw new OIDplusException(_L('New password is too short. Minimum password length: %1',$minlen));
61 daniel-mar 50
                        }
51
 
52
                        $ra = new OIDplusRA($email);
433 daniel-mar 53
                        if (!$ra->isPasswordLess()) {
54
                                if (!OIDplus::authUtils()::isAdminLoggedIn()) {
55
                                        if (!$ra->checkPassword($old_password)) {
56
                                                throw new OIDplusException(_L('Old password incorrect'));
57
                                        }
152 daniel-mar 58
                                }
433 daniel-mar 59
                                OIDplus::logger()->log("[?WARN/!OK]RA($email)?/[?INFO/!OK]A?", "Password of RA '$email' changed");
60
                        } else {
61
                                OIDplus::logger()->log("[?WARN/!OK]RA($email)?/[?INFO/!OK]A?", "Password of RA '$email' created");
61 daniel-mar 62
                        }
63
                        $ra->change_password($password1);
64
 
328 daniel-mar 65
                        return array("status" => 0);
321 daniel-mar 66
                } else {
360 daniel-mar 67
                        throw new OIDplusException(_L('Unknown action ID'));
61 daniel-mar 68
                }
69
        }
70
 
75 daniel-mar 71
        public function init($html=true) {
61 daniel-mar 72
                // Nothing
73
        }
74
 
75
        public function gui($id, &$out, &$handled) {
76
                if (explode('$',$id)[0] == 'oidplus:change_ra_password') {
77
                        $handled = true;
360 daniel-mar 78
 
281 daniel-mar 79
                        $ra_email = explode('$',$id)[1];
433 daniel-mar 80
                        $ra = new OIDplusRA($ra_email);
281 daniel-mar 81
 
433 daniel-mar 82
                        $out['title'] = $ra->isPasswordLess() ? _L('Create password') : _L('Change RA password');
241 daniel-mar 83
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
61 daniel-mar 84
 
281 daniel-mar 85
                        if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
86
                                $out['icon'] = 'img/error_big.png';
360 daniel-mar 87
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2.',OIDplus::gui()->link('oidplus:login'),'<b>'.htmlentities($ra_email).'</b>').'</p>';
281 daniel-mar 88
                                return;
89
                        }
61 daniel-mar 90
 
261 daniel-mar 91
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
236 daniel-mar 92
                        if ($res->num_rows() == 0) {
61 daniel-mar 93
                                $out['icon'] = 'img/error_big.png';
360 daniel-mar 94
                                $out['text'] = _L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>');
281 daniel-mar 95
                                return;
61 daniel-mar 96
                        }
97
 
428 daniel-mar 98
                        $out['text'] .= '<form id="raChangePasswordForm" action="javascript:void(0);" onsubmit="return raChangePasswordFormOnSubmit();">';
281 daniel-mar 99
                        $out['text'] .= '<input type="hidden" id="email" value="'.htmlentities($ra_email).'"/><br>';
360 daniel-mar 100
                        $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><b>'.htmlentities($ra_email).'</b></div>';
433 daniel-mar 101
                        if (!$ra->isPasswordLess()) {
102
                                if (OIDplus::authUtils()::isAdminLoggedIn()) {
103
                                        $out['text'] .= '<div><label class="padding_label">'._L('Old password').':</label><i>'._L('Admin can change the password without verification of the old password.').'</i></div>';
104
                                } else {
105
                                        $out['text'] .= '<div><label class="padding_label">'._L('Old password').':</label><input type="password" id="old_password" value=""/></div>';
106
                                }
61 daniel-mar 107
                        }
360 daniel-mar 108
                        $out['text'] .= '<div><label class="padding_label">'._L('New password').':</label><input type="password" id="new_password1" value=""/></div>';
109
                        $out['text'] .= '<div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="new_password2" value=""/></div>';
433 daniel-mar 110
                        $out['text'] .= '<br><input type="submit" value="'.($ra->isPasswordLess() ? _L('Create password') : _L('Change password')).'"></form>';
61 daniel-mar 111
                }
112
        }
113
 
106 daniel-mar 114
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
281 daniel-mar 115
                if (!$ra_email) return false;
116
                if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false;
360 daniel-mar 117
 
61 daniel-mar 118
                if (file_exists(__DIR__.'/treeicon.png')) {
241 daniel-mar 119
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
61 daniel-mar 120
                } else {
121
                        $tree_icon = null; // default icon (folder)
122
                }
123
 
433 daniel-mar 124
                $ra = new OIDplusRA($ra_email);
125
 
61 daniel-mar 126
                $json[] = array(
127
                        'id' => 'oidplus:change_ra_password$'.$ra_email,
128
                        'icon' => $tree_icon,
433 daniel-mar 129
                        'text' => $ra->isPasswordLess() ? _L('Create password') : _L('Change password')
61 daniel-mar 130
                );
104 daniel-mar 131
 
132
                return true;
61 daniel-mar 133
        }
108 daniel-mar 134
 
135
        public function tree_search($request) {
136
                return false;
137
        }
433 daniel-mar 138
}