Subversion Repositories oidplus

Rev

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