forked from splitbrain/dokuwiki-plugin-passpolicy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
212 lines (187 loc) · 6.64 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
use dokuwiki\Action\Exception\ActionException;
use dokuwiki\Action\Resendpwd;
/**
* DokuWiki Plugin passpolicy (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
class action_plugin_passpolicy extends DokuWiki_Action_Plugin
{
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller)
{
$controller->register_hook('FORM_REGISTER_OUTPUT', 'BEFORE', $this, 'handleForms');
$controller->register_hook('FORM_UPDATEPROFILE_OUTPUT', 'BEFORE', $this, 'handleForms');
$controller->register_hook('FORM_RESENDPWD_OUTPUT', 'BEFORE', $this, 'handleForms');
$controller->register_hook('HTML_REGISTERFORM_OUTPUT', 'BEFORE', $this, 'handleForms');
$controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handleForms');
$controller->register_hook('HTML_RESENDPWDFORM_OUTPUT', 'BEFORE', $this, 'handleForms');
$controller->register_hook('AUTH_USER_CHANGE', 'BEFORE', $this, 'handlePasschange');
$controller->register_hook('AUTH_PASSWORD_GENERATE', 'BEFORE', $this, 'handlePassgen');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
if ($this->getConf('supressuserhints')) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleResendPwd');
$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handleResendPwdUI');
}
}
/**
* Handle Ajax for the Password strength check
*
* @param Doku_Event $event
* @param $param
*/
public function handleAjax(Doku_Event $event, $param)
{
if ($event->data !== 'plugin_passpolicy') {
return;
}
//no other ajax call handlers needed
$event->stopPropagation();
$event->preventDefault();
/* @var $INPUT \Input */
global $INPUT;
$pass = $INPUT->post->str('pass');
$user = $INPUT->post->str('user', $_SERVER['REMOTE_USER']);
/** @var helper_plugin_passpolicy $passpolicy */
$passpolicy = $this->loadHelper('passpolicy');
if (!$passpolicy->checkPolicy($pass, $user)) {
// passpolicy not matched, throw error
echo '0';
} else {
echo '1';
}
}
/**
* Print the password policy in forms that allow setting passwords
*
* @param Doku_Event $event event object
* @param mixed $param
*/
public function handleForms(Doku_Event $event, $param)
{
if (is_a($event->data, \dokuwiki\Form\Form::class)) {
// applicable to development snapshot 2020-10-13 or later
$pos = $event->data->findPositionByAttribute('name', 'passchk');
} else {
// applicable to 2020-07-29 "Hogfather" and older
$pos = $event->data->findElementByAttribute('name', 'passchk');
}
if (!$pos) return; // no password repeat field found
/** @var $passpolicy helper_plugin_passpolicy */
$passpolicy = plugin_load('helper', 'passpolicy');
$html = '<p class="passpolicy_hint">' . $passpolicy->explainPolicy() . '</p>';
if (is_a($event->data, \dokuwiki\Form\Form::class)) {
$event->data->addHTML($html, ++$pos);
} else {
$event->data->insertElement(++$pos, $html);
}
}
/**
* Check if a new password matches the set password policy
*
* @param Doku_Event $event event object
* @param mixed $param
*/
public function handlePasschange(Doku_Event $event, $param)
{
if ($event->data['type'] == 'create') {
$user = $event->data['params'][0];
$pass = $event->data['params'][1];
} elseif ($event->data['type'] == 'modify') {
$user = $event->data['params'][0];
if (!isset($event->data['params'][1]['pass'])) {
return; //password is not changed, nothing to do
}
$pass = $event->data['params'][1]['pass'];
} else {
return;
}
/** @var $passpolicy helper_plugin_passpolicy */
$passpolicy = plugin_load('helper', 'passpolicy');
if (!$passpolicy->checkPolicy($pass, $user)) {
// passpolicy not matched, throw error and stop modification
msg($this->getLang('badpasspolicy'), -1);
$event->preventDefault();
$event->stopPropagation();
}
}
/**
* Replace default password generator by policy aware one
*
* @param Doku_Event $event event object
* @param mixed $param
* @throws Exception
*/
public function handlePassgen(Doku_Event $event, $param)
{
/** @var $passpolicy helper_plugin_passpolicy */
$passpolicy = plugin_load('helper', 'passpolicy');
$event->data['password'] = $passpolicy->generatePassword($event->data['foruser']);
$event->preventDefault();
}
/**
* Intercept the resendpwd action
*
* This supresses all hints on if a user exists or not
*
* @param Doku_Event $event
* @param $param
*/
public function handleResendPwd(Doku_Event $event, $param)
{
$act = act_clean($event->data);
if ($act != 'resendpwd') return;
$event->preventDefault();
$action = new Resendpwd();
try {
$action->checkPreconditions();
} catch (ActionException $ignored) {
$event->data = 'show';
return;
}
try {
$action->preProcess();
} catch (ActionException $ignored) {
}
$this->fixResendMessages();
}
/**
* Reuse the standard action UI for ResendPwd
*
* @param Doku_Event $event
* @param $param
*/
public function handleResendPwdUI(Doku_Event $event, $param)
{
$act = act_clean($event->data);
if ($act != 'resendpwd') return;
$event->preventDefault();
(new Resendpwd)->tplContent();
}
/**
* Replaces the resendPwd messages with neutral ones
*
* @return void
*/
protected function fixResendMessages()
{
global $MSG;
global $lang;
foreach ((array)$MSG as $key => $info) {
if (
$info['msg'] == $lang['resendpwdnouser'] or
$info['msg'] == $lang['resendpwdconfirm']
) {
unset($MSG[$key]);
msg($this->getLang('resendpwd'), 1);
}
}
}
}