Open Monograph Press  3.3.0
UserDetailsForm.inc.php
1 <?php
2 
16 import('lib.pkp.controllers.grid.settings.user.form.UserForm');
17 
18 class UserDetailsForm extends UserForm {
19 
21  var $user;
22 
24  var $author;
25 
32  function __construct($request, $userId = null, $author = null) {
33  parent::__construct('controllers/grid/settings/user/form/userDetailsForm.tpl', $userId);
34 
35  if (isset($author)) {
36  $this->author =& $author;
37  } else {
38  $this->author = null;
39  }
40 
41  // the users register for the site, thus
42  // the site primary locale is the required default locale
43  $site = $request->getSite();
44  $this->addSupportedFormLocale($site->getPrimaryLocale());
45 
46  // Validation checks for this form
47  $form = $this;
48  if ($userId == null) {
49  $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
50  $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array($this->userId, true), true));
51  $this->addCheck(new FormValidatorUsername($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
52 
53  if (!Config::getVar('security', 'implicit_auth')) {
54  $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
55  $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordLengthRestriction', function($password) use ($form, $site) {
56  return $form->getData('generatePassword') || PKPString::strlen($password) >= $site->getMinPasswordLength();
57  }, array(), false, array('length' => $site->getMinPasswordLength())));
58  $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', function($password) use ($form) {
59  return $password == $form->getData('password2');
60  }));
61  }
62  } else {
63  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
64  $this->user = $userDao->getById($userId);
65 
66  $this->addCheck(new FormValidatorCustom($this, 'password', 'optional', 'user.register.form.passwordLengthRestriction', function($password) use ($form, $site) {
67  return $form->getData('generatePassword') || PKPString::strlen($password) >= $site->getMinPasswordLength();
68  }, array(), false, array('length' => $site->getMinPasswordLength())));
69  $this->addCheck(new FormValidatorCustom($this, 'password', 'optional', 'user.register.form.passwordsDoNotMatch', function($password) use ($form) {
70  return $password == $form->getData('password2');
71  }));
72  }
73  $this->addCheck(new FormValidatorLocale($this, 'givenName', 'required', 'user.profile.form.givenNameRequired', $site->getPrimaryLocale()));
74  $this->addCheck(new FormValidatorCustom($this, 'familyName', 'optional', 'user.profile.form.givenNameRequired.locale', function($familyName) use ($form) {
75  $givenNames = $form->getData('givenName');
76  foreach ($familyName as $locale => $value) {
77  if (!empty($value) && empty($givenNames[$locale])) {
78  return false;
79  }
80  }
81  return true;
82  }));
83  $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
84  $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
85  $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array($this->userId, true), true));
86  $this->addCheck(new FormValidatorORCID($this, 'orcid', 'optional', 'user.orcid.orcidInvalid'));
87  $this->addCheck(new FormValidatorPost($this));
88  $this->addCheck(new FormValidatorCSRF($this));
89  }
90 
94  function initData() {
95  $request = Application::get()->getRequest();
96  $context = $request->getContext();
97  $contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
98 
99  $data = array();
100 
101  if (isset($this->user)) {
102  $user = $this->user;
103 
104  import('lib.pkp.classes.user.InterestManager');
105  $interestManager = new InterestManager();
106 
107  $data = array(
108  'authId' => $user->getAuthId(),
109  'username' => $user->getUsername(),
110  'givenName' => $user->getGivenName(null), // Localized
111  'familyName' => $user->getFamilyName(null), // Localized
112  'preferredPublicName' => $user->getPreferredPublicName(null), // Localized
113  'signature' => $user->getSignature(null), // Localized
114  'affiliation' => $user->getAffiliation(null), // Localized
115  'email' => $user->getEmail(),
116  'userUrl' => $user->getUrl(),
117  'phone' => $user->getPhone(),
118  'orcid' => $user->getOrcid(),
119  'mailingAddress' => $user->getMailingAddress(),
120  'country' => $user->getCountry(),
121  'biography' => $user->getBiography(null), // Localized
122  'interests' => $interestManager->getInterestsForUser($user),
123  'userLocales' => $user->getLocales(),
124  );
125  import('classes.core.Services');
126  $userService = Services::get('user');
127  $data['canCurrentUserGossip'] = $userService->canCurrentUserGossip($user->getId());
128  if ($data['canCurrentUserGossip']) {
129  $data['gossip'] = $user->getGossip();
130  }
131  } else if (isset($this->author)) {
133  $data = array(
134  'givenName' => $author->getGivenName(null), // Localized
135  'familyName' => $author->getFamilyName(null), // Localized
136  'affiliation' => $author->getAffiliation(null), // Localized
137  'preferredPublicName' => $author->getPreferredPublicName(null), // Localized
138  'email' => $author->getEmail(),
139  'userUrl' => $author->getUrl(),
140  'orcid' => $author->getOrcid(),
141  'country' => $author->getCountry(),
142  'biography' => $author->getBiography(null), // Localized
143  );
144  } else {
145  $data = array(
146  'mustChangePassword' => true,
147  );
148  }
149  foreach($data as $key => $value) {
150  $this->setData($key, $value);
151  }
152 
153  parent::initData();
154  }
155 
159  function display($request = null, $template = null) {
160  $site = $request->getSite();
161  $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
162  $countries = array();
163  foreach ($isoCodes->getCountries() as $country) {
164  $countries[$country->getAlpha2()] = $country->getLocalName();
165  }
166  asort($countries);
167  $templateMgr = TemplateManager::getManager($request);
168 
169  $templateMgr->assign(array(
170  'minPasswordLength' => $site->getMinPasswordLength(),
171  'source' => $request->getUserVar('source'),
172  'userId' => $this->userId,
173  'sitePrimaryLocale' => $site->getPrimaryLocale(),
174  'availableLocales' => $site->getSupportedLocaleNames(),
175  'countries' => $countries,
176  ));
177 
178  if (isset($this->user)) {
179  $templateMgr->assign('username', $this->user->getUsername());
180  }
181 
182  $authDao = DAORegistry::getDAO('AuthSourceDAO'); /* @var $authDao AuthSourceDAO */
183  $authSources = $authDao->getSources();
184  $authSourceOptions = array();
185  foreach ($authSources->toArray() as $auth) {
186  $authSourceOptions[$auth->getAuthId()] = $auth->getTitle();
187  }
188  if (!empty($authSourceOptions)) {
189  $templateMgr->assign('authSourceOptions', $authSourceOptions);
190  }
191 
192  return parent::display($request, $template);
193  }
194 
195 
200  function readInputData() {
201  parent::readInputData();
202 
203  $this->readUserVars(array(
204  'authId',
205  'password',
206  'password2',
207  'givenName',
208  'familyName',
209  'preferredPublicName',
210  'signature',
211  'affiliation',
212  'email',
213  'userUrl',
214  'phone',
215  'orcid',
216  'mailingAddress',
217  'country',
218  'biography',
219  'gossip',
220  'interests',
221  'userLocales',
222  'generatePassword',
223  'sendNotify',
224  'mustChangePassword'
225  ));
226  if ($this->userId == null) {
227  $this->readUserVars(array('username'));
228  }
229 
230  if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
231  $this->setData('userLocales', array());
232  }
233  }
234 
238  function getLocaleFieldNames() {
239  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
240  return $userDao->getLocaleFieldNames();
241  }
242 
246  function execute(...$functionParams) {
247  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
248  $request = Application::get()->getRequest();
249  $context = $request->getContext();
250 
251  if (!isset($this->user)) {
252  $this->user = $userDao->newDataObject();
253  $this->user->setInlineHelp(1); // default new users to having inline help visible
254  }
255 
256  $this->user->setGivenName($this->getData('givenName'), null); // Localized
257  $this->user->setFamilyName($this->getData('familyName'), null); // Localized
258  $this->user->setPreferredPublicName($this->getData('preferredPublicName'), null); // Localized
259  $this->user->setAffiliation($this->getData('affiliation'), null); // Localized
260  $this->user->setSignature($this->getData('signature'), null); // Localized
261  $this->user->setEmail($this->getData('email'));
262  $this->user->setUrl($this->getData('userUrl'));
263  $this->user->setPhone($this->getData('phone'));
264  $this->user->setOrcid($this->getData('orcid'));
265  $this->user->setMailingAddress($this->getData('mailingAddress'));
266  $this->user->setCountry($this->getData('country'));
267  $this->user->setBiography($this->getData('biography'), null); // Localized
268  $this->user->setMustChangePassword($this->getData('mustChangePassword') ? 1 : 0);
269  $this->user->setAuthId((int) $this->getData('authId'));
270  // Users can never view/edit their own gossip fields
271  import('classes.core.Services');
272  $userService = Services::get('user');
273  if ($userService->canCurrentUserGossip($this->user->getId())) {
274  $this->user->setGossip($this->getData('gossip'));
275  }
276 
277  $site = $request->getSite();
278  $availableLocales = $site->getSupportedLocales();
279 
280  $locales = array();
281  foreach ($this->getData('userLocales') as $locale) {
282  if (AppLocale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
283  array_push($locales, $locale);
284  }
285  }
286  $this->user->setLocales($locales);
287 
288  if ($this->user->getAuthId()) {
289  $authDao = DAORegistry::getDAO('AuthSourceDAO'); /* @var $authDao AuthSourceDAO */
290  $auth =& $authDao->getPlugin($this->user->getAuthId());
291  }
292 
293  parent::execute(...$functionParams);
294 
295  if ($this->user->getId() != null) {
296  if ($this->getData('password') !== '') {
297  if (isset($auth)) {
298  $auth->doSetUserPassword($this->user->getUsername(), $this->getData('password'));
299  $this->user->setPassword(Validation::encryptCredentials($this->user->getId(), Validation::generatePassword())); // Used for PW reset hash only
300  } else {
301  $this->user->setPassword(Validation::encryptCredentials($this->user->getUsername(), $this->getData('password')));
302  }
303  }
304 
305  if (isset($auth)) {
306  // FIXME Should try to create user here too?
307  $auth->doSetUserInfo($this->user);
308  }
309 
310  $userDao->updateObject($this->user);
311 
312  } else {
313  $this->user->setUsername($this->getData('username'));
314  if ($this->getData('generatePassword')) {
315  $password = Validation::generatePassword();
316  $sendNotify = true;
317  } else {
318  $password = $this->getData('password');
319  $sendNotify = $this->getData('sendNotify');
320  }
321 
322  if (isset($auth)) {
323  $this->user->setPassword($password);
324  // FIXME Check result and handle failures
325  $auth->doCreateUser($this->user);
326  $this->user->setAuthId($auth->authId);
327  $this->user->setPassword(Validation::encryptCredentials($this->user->getId(), Validation::generatePassword())); // Used for PW reset hash only
328  } else {
329  $this->user->setPassword(Validation::encryptCredentials($this->getData('username'), $password));
330  }
331 
332  $this->user->setDateRegistered(Core::getCurrentDate());
333  $userId = $userDao->insertObject($this->user);
334 
335  if ($sendNotify) {
336  // Send welcome email to user
337  import('lib.pkp.classes.mail.MailTemplate');
338  $mail = new MailTemplate('USER_REGISTER');
339  $mail->setReplyTo($context->getData('contactEmail'), $context->getData('contactName'));
340  $mail->assignParams(array('username' => $this->getData('username'), 'password' => $password, 'userFullName' => $this->user->getFullName()));
341  $mail->addRecipient($this->user->getEmail(), $this->user->getFullName());
342  if (!$mail->send()) {
343  import('classes.notification.NotificationManager');
344  $notificationMgr = new NotificationManager();
345  $notificationMgr->createTrivialNotification($request->getUser()->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('email.compose.error')));
346  }
347  }
348  }
349 
350  import('lib.pkp.classes.user.InterestManager');
351  $interestManager = new InterestManager();
352  $interestManager->setInterestsForUser($this->user, $this->getData('interests'));
353 
354  return $this->user;
355  }
356 }
357 
358 
UserDetailsForm\display
display($request=null, $template=null)
Definition: UserDetailsForm.inc.php:165
Validation\encryptCredentials
static encryptCredentials($username, $password, $encryption=false, $legacy=false)
Definition: Validation.inc.php:255
FormValidatorUsername
Form validation check for usernames (lowercase alphanumeric with interior dash/underscore.
Definition: FormValidatorUsername.inc.php:19
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
UserDetailsForm\$author
$author
Definition: UserDetailsForm.inc.php:30
FormValidatorLocale
Class to represent a form validation check for localized fields.
Definition: FormValidatorLocale.inc.php:16
UserForm\$userId
$userId
Definition: UserForm.inc.php:24
Form\setData
setData($key, $value=null)
Definition: Form.inc.php:229
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
FormValidatorEmail
Form validation check for email addresses.
Definition: FormValidatorEmail.inc.php:20
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorORCID
Form validation check for ORCID iDs.
Definition: FormValidatorORCID.inc.php:18
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
Validation\generatePassword
static generatePassword($length=null)
Definition: Validation.inc.php:283
MailTemplate
Subclass of Mail for mailing a template email.
Definition: MailTemplate.inc.php:21
PKPString\strlen
static strlen($string)
Definition: PKPString.inc.php:128
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
UserDetailsForm\getLocaleFieldNames
getLocaleFieldNames()
Definition: UserDetailsForm.inc.php:244
UserForm
Base class for user forms.
Definition: UserForm.inc.php:18
Form\addSupportedFormLocale
addSupportedFormLocale($supportedLocale)
Definition: Form.inc.php:364
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
UserDetailsForm\readInputData
readInputData()
Definition: UserDetailsForm.inc.php:206
UserDetailsForm\execute
execute(... $functionParams)
Definition: UserDetailsForm.inc.php:252
InterestManager
Handle user interest functions.
Definition: InterestManager.inc.php:16
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
UserDetailsForm\$user
$user
Definition: UserDetailsForm.inc.php:24
UserDetailsForm
Form for editing user profiles.
Definition: UserDetailsForm.inc.php:18
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
NotificationManager
Definition: NotificationManager.inc.php:19
FormValidatorCSRF
Form validation check to make sure the CSRF token is correct.
Definition: FormValidatorCSRF.inc.php:18
UserDetailsForm\__construct
__construct($request, $userId=null, $author=null)
Definition: UserDetailsForm.inc.php:38
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
FormValidatorUrl
Form validation check for URLs.
Definition: FormValidatorUrl.inc.php:20
FormValidatorCustom
Form validation check with a custom user function performing the validation check.
Definition: FormValidatorCustom.inc.php:18
PKPLocale\isLocaleValid
static isLocaleValid($locale)
Definition: PKPLocale.inc.php:505
UserDetailsForm\initData
initData()
Definition: UserDetailsForm.inc.php:100
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49