Subversion Repositories oidplus

Rev

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