Subversion Repositories oidplus

Rev

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