/
UserManagementForm.inc.php
executable file
·337 lines (287 loc) · 11.3 KB
/
UserManagementForm.inc.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* @file classes/manager/form/UserManagementForm.inc.php
*
* Copyright (c) 2003-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class UserManagementForm
* @ingroup manager_form
*
* @brief Form for press managers to edit user profiles.
*/
// $Id$
import('form.Form');
class UserManagementForm extends Form {
/** The ID of the user being edited */
var $userId;
/**
* Constructor.
*/
function UserManagementForm($userId = null) {
parent::Form('manager/people/userProfileForm.tpl');
if (!Validation::isPressManager()) $userId = null;
$this->userId = isset($userId) ? (int) $userId : null;
$site =& Request::getSite();
// Validation checks for this form
if ($userId == null) {
$this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
$this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array($this->userId, true), true));
$this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
if (!Config::getVar('security', 'implicit_auth')) {
$this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
$this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
$this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
}
} else {
$this->addCheck(new FormValidatorLength($this, 'password', 'optional', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
$this->addCheck(new FormValidatorCustom($this, 'password', 'optional', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
}
$this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
$this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
$this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
$this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
$this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array($this->userId, true), true));
$this->addCheck(new FormValidatorPost($this));
}
/**
* Display the form.
*/
function display(&$args, &$request) {
$userDao =& DAORegistry::getDAO('UserDAO');
$templateMgr =& TemplateManager::getManager();
$site =& $request->getSite();
$templateMgr->assign('genderOptions', $userDao->getGenderOptions());
$templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
$templateMgr->assign('source', Request::getUserVar('source'));
$templateMgr->assign('userId', $this->userId);
if (isset($this->userId)) {
$user =& $userDao->getUser($this->userId);
$templateMgr->assign('username', $user->getUsername());
$helpTopicId = 'press.users.index';
} else {
$helpTopicId = 'press.users.createNewUser';
}
$press =& $request->getPress();
$userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
$options = array();
$options[''] = Locale::translate('manager.people.doNotEnroll');
if (Validation::isPressManager()) {
$userGroups =& $userGroupDao->getByPressId($press->getId());
foreach ($userGroups->toArray() as $userGroup) {
$options[$userGroup->getId()] = $userGroup->getLocalizedName();
}
} else {
$userGroup =& $userGroupDao->getDefaultByRoleId($press->getId(), ROLE_ID_READER);
$options[$userGroup->getId()] = $userGroup->getLocalizedName();
}
$templateMgr->assign('userGroupOptions', $options);
// Send implicitAuth setting down to template
$templateMgr->assign('implicitAuth', Config::getVar('security', 'implicit_auth'));
$templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
$templateMgr->assign('helpTopicId', $helpTopicId);
$countryDao =& DAORegistry::getDAO('CountryDAO');
$countries =& $countryDao->getCountries();
$templateMgr->assign_by_ref('countries', $countries);
$authDao =& DAORegistry::getDAO('AuthSourceDAO');
$authSources =& $authDao->getSources();
$authSourceOptions = array();
foreach ($authSources->toArray() as $auth) {
$authSourceOptions[$auth->getAuthId()] = $auth->getTitle();
}
if (!empty($authSourceOptions)) {
$templateMgr->assign('authSourceOptions', $authSourceOptions);
}
parent::display();
}
/**
* Initialize form data from current user profile.
*/
function initData(&$args, &$request) {
if (isset($this->userId)) {
$userDao =& DAORegistry::getDAO('UserDAO');
$user =& $userDao->getUser($this->userId);
if ($user != null) {
$this->_data = array(
'authId' => $user->getAuthId(),
'username' => $user->getUsername(),
'salutation' => $user->getSalutation(),
'firstName' => $user->getFirstName(),
'middleName' => $user->getMiddleName(),
'lastName' => $user->getLastName(),
'signature' => $user->getSignature(null), // Localized
'initials' => $user->getInitials(),
'gender' => $user->getGender(),
'affiliation' => $user->getAffiliation(),
'email' => $user->getEmail(),
'userUrl' => $user->getUrl(),
'phone' => $user->getPhone(),
'fax' => $user->getFax(),
'mailingAddress' => $user->getMailingAddress(),
'country' => $user->getCountry(),
'biography' => $user->getBiography(null), // Localized
'interests' => $user->getInterests(null), // Localized
'gossip' => $user->getGossip(null), // Localized
'userLocales' => $user->getLocales()
);
} else {
$this->userId = null;
}
}
if (!isset($this->userId)) {
$roleDao =& DAORegistry::getDAO('RoleDAO');
$userGroupId = $request->getUserVar('userGroupId');
$this->_data = array(
'enrollAs' => array($userGroupId)
);
}
}
/**
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->readUserVars(array(
'authId',
'enrollAs',
'password',
'password2',
'salutation',
'firstName',
'middleName',
'lastName',
'gender',
'initials',
'signature',
'affiliation',
'email',
'userUrl',
'phone',
'fax',
'mailingAddress',
'country',
'biography',
'interests',
'gossip',
'userLocales',
'generatePassword',
'sendNotify',
'mustChangePassword'
));
if ($this->userId == null) {
$this->readUserVars(array('username'));
}
if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
$this->setData('userLocales', array());
}
if ($this->getData('username') != null) {
// Usernames must be lowercase
$this->setData('username', strtolower($this->getData('username')));
}
}
function getLocaleFieldNames() {
$userDao =& DAORegistry::getDAO('UserDAO');
return $userDao->getLocaleFieldNames();
}
/**
* Register a new user.
*/
function execute(&$args, &$request) {
$userDao =& DAORegistry::getDAO('UserDAO');
$press =& $request->getPress();
if (isset($this->userId)) {
$user =& $userDao->getUser($this->userId);
}
if (!isset($user)) {
$user = new User();
}
$user->setSalutation($this->getData('salutation'));
$user->setFirstName($this->getData('firstName'));
$user->setMiddleName($this->getData('middleName'));
$user->setLastName($this->getData('lastName'));
$user->setInitials($this->getData('initials'));
$user->setGender($this->getData('gender'));
$user->setAffiliation($this->getData('affiliation'));
$user->setSignature($this->getData('signature'), null); // Localized
$user->setEmail($this->getData('email'));
$user->setUrl($this->getData('userUrl'));
$user->setPhone($this->getData('phone'));
$user->setFax($this->getData('fax'));
$user->setMailingAddress($this->getData('mailingAddress'));
$user->setCountry($this->getData('country'));
$user->setBiography($this->getData('biography'), null); // Localized
$user->setInterests($this->getData('interests'), null); // Localized
$user->setGossip($this->getData('gossip'), null); // Localized
$user->setMustChangePassword($this->getData('mustChangePassword') ? 1 : 0);
$user->setAuthId((int) $this->getData('authId'));
$site =& $request->getSite();
$availableLocales = $site->getSupportedLocales();
$locales = array();
foreach ($this->getData('userLocales') as $locale) {
if (Locale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
array_push($locales, $locale);
}
}
$user->setLocales($locales);
if ($user->getAuthId()) {
$authDao =& DAORegistry::getDAO('AuthSourceDAO');
$auth =& $authDao->getPlugin($user->getAuthId());
}
if ($user->getId() != null) {
if ($this->getData('password') !== '') {
if (isset($auth)) {
$auth->doSetUserPassword($user->getUsername(), $this->getData('password'));
$user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
} else {
$user->setPassword(Validation::encryptCredentials($user->getUsername(), $this->getData('password')));
}
}
if (isset($auth)) {
// FIXME Should try to create user here too?
$auth->doSetUserInfo($user);
}
$userDao->updateObject($user);
} else {
$user->setUsername($this->getData('username'));
if ($this->getData('generatePassword')) {
$password = Validation::generatePassword();
$sendNotify = true;
} else {
$password = $this->getData('password');
$sendNotify = $this->getData('sendNotify');
}
if (isset($auth)) {
$user->setPassword($password);
// FIXME Check result and handle failures
$auth->doCreateUser($user);
$user->setAuthId($auth->authId);
$user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
} else {
$user->setPassword(Validation::encryptCredentials($this->getData('username'), $password));
}
$user->setDateRegistered(Core::getCurrentDate());
$userId = $userDao->insertUser($user);
$isManager = Validation::isPressManager();
if (!empty($this->_data['enrollAs'])) {
$userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
foreach ($this->getData('enrollAs') as $userGroupId) {
// Enroll new user into an initial role
$userGroup =& $userGroupDao->getById($userGroupId);
if (!$isManager && $userGroup->getRoleId() != ROLE_ID_READER) continue;
if ($userGroupId != null) {
$userGroupDao->assignUserToGroup($userId, $userGroupId);
}
}
}
if ($sendNotify) {
// Send welcome email to user
import('mail.MailTemplate');
$mail = new MailTemplate('USER_REGISTER');
$mail->setFrom($press->getSetting('contactEmail'), $press->getSetting('contactName'));
$mail->assignParams(array('username' => $this->getData('username'), 'password' => $password, 'userFullName' => $user->getFullName()));
$mail->addRecipient($user->getEmail(), $user->getFullName());
$mail->send();
}
}
}
}
?>