PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/manager/form/UserManagementForm.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 341 lines | 265 code | 41 blank | 35 comment | 42 complexity | 04ab4bc0195539c77203447795cfee5f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file UserManagementForm.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class UserManagementForm
  9. * @ingroup manager_form
  10. *
  11. * @brief Form for conference managers to edit user profiles.
  12. *
  13. */
  14. // $Id$
  15. import('form.Form');
  16. class UserManagementForm extends Form {
  17. /** The ID of the user being edited */
  18. var $userId;
  19. /**
  20. * Constructor.
  21. */
  22. function UserManagementForm($userId = null) {
  23. parent::Form('manager/people/userProfileForm.tpl');
  24. $this->userId = isset($userId) ? (int) $userId : null;
  25. $site =& Request::getSite();
  26. // Validation checks for this form
  27. if ($userId == null) {
  28. $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
  29. $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.account.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array($this->userId, true), true));
  30. $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.account.form.usernameAlphaNumeric'));
  31. $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
  32. $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.account.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
  33. $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.account.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
  34. } else {
  35. $this->addCheck(new FormValidatorLength($this, 'password', 'optional', 'user.account.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
  36. $this->addCheck(new FormValidatorCustom($this, 'password', 'optional', 'user.account.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
  37. }
  38. $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
  39. $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
  40. $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
  41. $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
  42. $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.account.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array($this->userId, true), true));
  43. $this->addCheck(new FormValidatorPost($this));
  44. }
  45. /**
  46. * Display the form.
  47. */
  48. function display() {
  49. $userDao =& DAORegistry::getDAO('UserDAO');
  50. $templateMgr =& TemplateManager::getManager();
  51. $site =& Request::getSite();
  52. $schedConf =& Request::getSchedConf();
  53. $templateMgr->assign('genderOptions', $userDao->getGenderOptions());
  54. $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
  55. $templateMgr->assign('source', Request::getUserVar('source'));
  56. $templateMgr->assign('userId', $this->userId);
  57. if (isset($this->userId)) {
  58. $user =& $userDao->getUser($this->userId);
  59. $templateMgr->assign('username', $user->getUsername());
  60. $helpTopicId = 'conference.users.index';
  61. } else {
  62. $helpTopicId = 'conference.users.createNewUser';
  63. }
  64. if($schedConf) {
  65. $templateMgr->assign('roleOptions',
  66. array(
  67. '' => 'manager.people.doNotEnroll',
  68. 'director' => 'user.role.director',
  69. 'trackDirector' => 'user.role.trackDirector',
  70. 'reviewer' => 'user.role.reviewer',
  71. 'author' => 'user.role.author',
  72. 'reader' => 'user.role.reader'
  73. )
  74. );
  75. } else {
  76. $templateMgr->assign('roleOptions',
  77. array(
  78. '' => 'manager.people.doNotEnroll',
  79. 'manager' => 'user.role.manager',
  80. )
  81. );
  82. }
  83. $site =& Request::getSite();
  84. $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
  85. $templateMgr->assign('helpTopicId', $helpTopicId);
  86. $countryDao =& DAORegistry::getDAO('CountryDAO');
  87. $countries =& $countryDao->getCountries();
  88. $templateMgr->assign_by_ref('countries', $countries);
  89. $authDao =& DAORegistry::getDAO('AuthSourceDAO');
  90. $authSources =& $authDao->getSources();
  91. $authSourceOptions = array();
  92. foreach ($authSources->toArray() as $auth) {
  93. $authSourceOptions[$auth->getAuthId()] = $auth->getTitle();
  94. }
  95. if (!empty($authSourceOptions)) {
  96. $templateMgr->assign('authSourceOptions', $authSourceOptions);
  97. }
  98. parent::display();
  99. }
  100. /**
  101. * Initialize form data from current user profile.
  102. */
  103. function initData() {
  104. if (isset($this->userId)) {
  105. $userDao =& DAORegistry::getDAO('UserDAO');
  106. $user =& $userDao->getUser($this->userId);
  107. if ($user != null) {
  108. $this->_data = array(
  109. 'authId' => $user->getAuthId(),
  110. 'username' => $user->getUsername(),
  111. 'salutation' => $user->getSalutation(),
  112. 'firstName' => $user->getFirstName(),
  113. 'middleName' => $user->getMiddleName(),
  114. 'lastName' => $user->getLastName(),
  115. 'initials' => $user->getInitials(),
  116. 'gender' => $user->getGender(),
  117. 'affiliation' => $user->getAffiliation(),
  118. 'email' => $user->getEmail(),
  119. 'userUrl' => $user->getUrl(),
  120. 'phone' => $user->getPhone(),
  121. 'fax' => $user->getFax(),
  122. 'mailingAddress' => $user->getMailingAddress(),
  123. 'country' => $user->getCountry(),
  124. 'biography' => $user->getBiography(null), // Localized
  125. 'interests' => $user->getInterests(null), // Localized
  126. 'gossip' => $user->getGossip(null), // Localized
  127. 'signature' => $user->getSignature(null), // Localized
  128. 'userLocales' => $user->getLocales()
  129. );
  130. } else {
  131. $this->userId = null;
  132. }
  133. }
  134. if (!isset($this->userId)) {
  135. $roleDao =& DAORegistry::getDAO('RoleDAO');
  136. $roleId = Request::getUserVar('roleId');
  137. $roleSymbolic = $roleDao->getRolePath($roleId);
  138. $this->_data = array(
  139. 'enrollAs' => array($roleSymbolic)
  140. );
  141. }
  142. }
  143. /**
  144. * Assign form data to user-submitted data.
  145. */
  146. function readInputData() {
  147. $this->readUserVars(array(
  148. 'authId',
  149. 'enrollAs',
  150. 'password',
  151. 'password2',
  152. 'salutation',
  153. 'firstName',
  154. 'middleName',
  155. 'lastName',
  156. 'gender',
  157. 'initials',
  158. 'affiliation',
  159. 'email',
  160. 'phone',
  161. 'fax',
  162. 'mailingAddress',
  163. 'country',
  164. 'userUrl',
  165. 'biography',
  166. 'interests',
  167. 'gossip',
  168. 'signature',
  169. 'userLocales',
  170. 'generatePassword',
  171. 'sendNotify',
  172. 'mustChangePassword'
  173. ));
  174. if ($this->userId == null) {
  175. $this->readUserVars(array('username'));
  176. }
  177. if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
  178. $this->setData('userLocales', array());
  179. }
  180. if ($this->getData('username') != null) {
  181. // Usernames must be lowercase
  182. $this->setData('username', strtolower($this->getData('username')));
  183. }
  184. }
  185. function getLocaleFieldNames() {
  186. $userDao =& DAORegistry::getDAO('UserDAO');
  187. return $userDao->getLocaleFieldNames();
  188. }
  189. /**
  190. * Register a new user.
  191. */
  192. function execute() {
  193. $userDao =& DAORegistry::getDAO('UserDAO');
  194. $conference =& Request::getConference();
  195. $schedConf =& Request::getSchedConf();
  196. if (isset($this->userId)) {
  197. $user =& $userDao->getUser($this->userId);
  198. }
  199. if (!isset($user)) {
  200. $user = new User();
  201. }
  202. $user->setSalutation($this->getData('salutation'));
  203. $user->setFirstName($this->getData('firstName'));
  204. $user->setMiddleName($this->getData('middleName'));
  205. $user->setLastName($this->getData('lastName'));
  206. $user->setInitials($this->getData('initials'));
  207. $user->setGender($this->getData('gender'));
  208. $user->setAffiliation($this->getData('affiliation'));
  209. $user->setEmail($this->getData('email'));
  210. $user->setUrl($this->getData('userUrl'));
  211. $user->setPhone($this->getData('phone'));
  212. $user->setFax($this->getData('fax'));
  213. $user->setMailingAddress($this->getData('mailingAddress'));
  214. $user->setCountry($this->getData('country'));
  215. $user->setBiography($this->getData('biography'), null); // Localized
  216. $user->setInterests($this->getData('interests'), null); // Localized
  217. $user->setGossip($this->getData('gossip'), null); // Localized
  218. $user->setSignature($this->getData('signature'), null); // Localized
  219. $user->setMustChangePassword($this->getData('mustChangePassword') ? 1 : 0);
  220. $user->setAuthId((int) $this->getData('authId'));
  221. $site =& Request::getSite();
  222. $availableLocales = $site->getSupportedLocales();
  223. $locales = array();
  224. foreach ($this->getData('userLocales') as $locale) {
  225. if (AppLocale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
  226. array_push($locales, $locale);
  227. }
  228. }
  229. $user->setLocales($locales);
  230. if ($user->getAuthId()) {
  231. $authDao =& DAORegistry::getDAO('AuthSourceDAO');
  232. $auth =& $authDao->getPlugin($user->getAuthId());
  233. }
  234. if ($user->getId() != null) {
  235. if ($this->getData('password') !== '') {
  236. if (isset($auth)) {
  237. $auth->doSetUserPassword($user->getUsername(), $this->getData('password'));
  238. $user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
  239. } else {
  240. $user->setPassword(Validation::encryptCredentials($user->getUsername(), $this->getData('password')));
  241. }
  242. }
  243. if (isset($auth)) {
  244. // FIXME Should try to create user here too?
  245. $auth->doSetUserInfo($user);
  246. }
  247. $userDao->updateObject($user);
  248. } else {
  249. $user->setUsername($this->getData('username'));
  250. if ($this->getData('generatePassword')) {
  251. $password = Validation::generatePassword();
  252. $sendNotify = true;
  253. } else {
  254. $password = $this->getData('password');
  255. $sendNotify = $this->getData('sendNotify');
  256. }
  257. if (isset($auth)) {
  258. $user->setPassword($password);
  259. // FIXME Check result and handle failures
  260. $auth->doCreateUser($user);
  261. $user->setAuthId($auth->authId);
  262. $user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
  263. } else {
  264. $user->setPassword(Validation::encryptCredentials($this->getData('username'), $password));
  265. }
  266. $user->setDateRegistered(Core::getCurrentDate());
  267. $userId = $userDao->insertUser($user);
  268. if (!empty($this->_data['enrollAs'])) {
  269. foreach ($this->getData('enrollAs') as $roleName) {
  270. // Enroll new user into an initial role
  271. $roleDao =& DAORegistry::getDAO('RoleDAO');
  272. $roleId = $roleDao->getRoleIdFromPath($roleName);
  273. if ($roleId != null) {
  274. $role = new Role();
  275. $role->setConferenceId($conference->getId());
  276. $role->setSchedConfId($schedConf?$schedConf->getId():0);
  277. $role->setUserId($userId);
  278. $role->setRoleId($roleId);
  279. $roleDao->insertRole($role);
  280. }
  281. }
  282. }
  283. if ($sendNotify) {
  284. // Send welcome email to user
  285. import('mail.MailTemplate');
  286. $mail = new MailTemplate('USER_REGISTER');
  287. if ($schedConf) $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
  288. elseif ($conference) $mail->setFrom($conference->getSetting('contactEmail'), $conference->getSetting('contactName'));
  289. else {
  290. $site =& Request::getSite();
  291. $mail->setFrom($site->getContactEmail(), $site->getContactName());
  292. }
  293. $mail->assignParams(array('username' => $this->getData('username'), 'password' => $password));
  294. $mail->addRecipient($user->getEmail(), $user->getFullName());
  295. $mail->send();
  296. }
  297. }
  298. }
  299. }
  300. ?>