PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/user/form/CreateAccountForm.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 333 lines | 229 code | 53 blank | 51 comment | 33 complexity | 4f2c65d8a9f155db47bef537d9fee45a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @defgroup user_form
  4. */
  5. /**
  6. * @file CreateAccountForm.inc.php
  7. *
  8. * Copyright (c) 2000-2012 John Willinsky
  9. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  10. *
  11. * @class CreateAccountForm
  12. * @ingroup user_form
  13. *
  14. * @brief Form for user account creation.
  15. *
  16. */
  17. // $Id$
  18. import('form.Form');
  19. class CreateAccountForm extends Form {
  20. /** @var boolean user is already registered with another conference */
  21. var $existingUser;
  22. /** @var AuthPlugin default authentication source, if specified */
  23. var $defaultAuth;
  24. /** @var boolean whether or not captcha is enabled for this form */
  25. var $captchaEnabled;
  26. /**
  27. * Constructor.
  28. */
  29. function CreateAccountForm() {
  30. parent::Form('user/createAccount.tpl');
  31. $this->existingUser = Request::getUserVar('existingUser') ? 1 : 0;
  32. import('captcha.CaptchaManager');
  33. $captchaManager = new CaptchaManager();
  34. $this->captchaEnabled = ($captchaManager->isEnabled() && Config::getVar('captcha', 'captcha_on_register'))?true:false;
  35. // Validation checks for this form
  36. $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
  37. $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
  38. if ($this->existingUser) {
  39. // Existing user -- check login
  40. $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.login.loginError', create_function('$username,$form', 'return Validation::checkCredentials($form->getData(\'username\'), $form->getData(\'password\'));'), array(&$this)));
  41. } else {
  42. // New user -- check required profile fields
  43. $site =& Request::getSite();
  44. $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.account.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array(), true));
  45. $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.account.form.usernameAlphaNumeric'));
  46. $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.account.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
  47. $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.account.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
  48. $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
  49. $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
  50. $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
  51. $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
  52. $this->addCheck(new FormValidator($this, 'affiliation', 'required', 'user.profile.form.affiliationRequired'));
  53. $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.account.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array(), true));
  54. if ($this->captchaEnabled) {
  55. $this->addCheck(new FormValidatorCaptcha($this, 'captcha', 'captchaId', 'common.captchaField.badCaptcha'));
  56. }
  57. $authDao =& DAORegistry::getDAO('AuthSourceDAO');
  58. $this->defaultAuth =& $authDao->getDefaultPlugin();
  59. if (isset($this->defaultAuth)) {
  60. $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.account.form.usernameExists', create_function('$username,$form,$auth', 'return (!$auth->userExists($username) || $auth->authenticate($username, $form->getData(\'password\')));'), array(&$this, $this->defaultAuth)));
  61. }
  62. }
  63. $this->addCheck(new FormValidatorPost($this));
  64. }
  65. /**
  66. * Display the form.
  67. */
  68. function display() {
  69. $templateMgr =& TemplateManager::getManager();
  70. $site =& Request::getSite();
  71. $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
  72. $conference =& Request::getConference();
  73. $schedConf =& Request::getSchedConf();
  74. if ($this->captchaEnabled) {
  75. import('captcha.CaptchaManager');
  76. $captchaManager = new CaptchaManager();
  77. $captcha =& $captchaManager->createCaptcha();
  78. if ($captcha) {
  79. $templateMgr->assign('captchaEnabled', $this->captchaEnabled);
  80. $this->setData('captchaId', $captcha->getId());
  81. }
  82. }
  83. $countryDao =& DAORegistry::getDAO('CountryDAO');
  84. $countries =& $countryDao->getCountries();
  85. $templateMgr->assign_by_ref('countries', $countries);
  86. import('schedConf.SchedConfAction');
  87. $userDao =& DAORegistry::getDAO('UserDAO');
  88. $templateMgr->assign('genderOptions', $userDao->getGenderOptions());
  89. $templateMgr->assign('privacyStatement', $conference->getLocalizedSetting('privacyStatement'));
  90. $templateMgr->assign('enableOpenAccessNotification', $schedConf->getSetting('enableOpenAccessNotification')==1?1:0);
  91. $templateMgr->assign('allowRegReader', SchedConfAction::allowRegReader($schedConf));
  92. $templateMgr->assign('allowRegAuthor', SchedConfAction::allowRegAuthor($schedConf));
  93. $templateMgr->assign('allowRegReviewer', SchedConfAction::allowRegReviewer($schedConf));
  94. $templateMgr->assign('source', Request::getUserVar('source'));
  95. $templateMgr->assign('pageHierarchy', array(
  96. array(Request::url(null, 'index', 'index'), $conference->getConferenceTitle(), true),
  97. array(Request::url(null, null, 'index'), $schedConf->getSchedConfTitle(), true)));
  98. $site =& Request::getSite();
  99. $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
  100. $templateMgr->assign('helpTopicId', 'conference.users.index');
  101. parent::display();
  102. }
  103. function getLocaleFieldNames() {
  104. $userDao =& DAORegistry::getDAO('UserDAO');
  105. return $userDao->getLocaleFieldNames();
  106. }
  107. /**
  108. * Initialize default data.
  109. */
  110. function initData() {
  111. $this->setData('createAsReader', 1);
  112. if (Request::getUserVar('requiresAuthor')) $this->setData('createAsAuthor', 1);
  113. $this->setData('existingUser', $this->existingUser);
  114. $this->setData('userLocales', array());
  115. $this->setData('sendPassword', 1);
  116. }
  117. /**
  118. * Assign form data to user-submitted data.
  119. */
  120. function readInputData() {
  121. $userVars = array(
  122. 'username', 'password', 'password2',
  123. 'salutation', 'firstName', 'middleName', 'lastName',
  124. 'gender', 'initials', 'country',
  125. 'affiliation', 'email', 'userUrl', 'phone', 'fax', 'signature',
  126. 'mailingAddress', 'biography', 'interests', 'userLocales',
  127. 'createAsReader', 'openAccessNotification', 'createAsAuthor',
  128. 'createAsReviewer', 'existingUser', 'sendPassword'
  129. );
  130. if ($this->captchaEnabled) {
  131. $userVars[] = 'captchaId';
  132. $userVars[] = 'captcha';
  133. }
  134. $this->readUserVars($userVars);
  135. if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
  136. $this->setData('userLocales', array());
  137. }
  138. if ($this->getData('username') != null) {
  139. // Usernames must be lowercase
  140. $this->setData('username', strtolower($this->getData('username')));
  141. }
  142. }
  143. /**
  144. * Send the registration confirmation email.
  145. * @param $user object
  146. */
  147. function sendConfirmationEmail($user, $password, $sendPassword) {
  148. $schedConf =& Request::getSchedConf();
  149. import('mail.MailTemplate');
  150. if (Config::getVar('email', 'require_validation')) {
  151. // Create an access key
  152. import('security.AccessKeyManager');
  153. $accessKeyManager = new AccessKeyManager();
  154. $accessKey = $accessKeyManager->createKey('RegisterContext', $user->getId(), null, Config::getVar('email', 'validation_timeout'));
  155. // Send email validation request to user
  156. $mail = new MailTemplate('USER_VALIDATE');
  157. $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
  158. $mail->assignParams(array(
  159. 'userFullName' => $user->getFullName(),
  160. 'activateUrl' => Request::url(null, null, 'user', 'activateUser', array($user->getUsername(), $accessKey))
  161. ));
  162. $mail->addRecipient($user->getEmail(), $user->getFullName());
  163. $mail->send();
  164. unset($mail);
  165. }
  166. if ($sendPassword) {
  167. // Send welcome email to user
  168. $mail = new MailTemplate('USER_REGISTER');
  169. $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
  170. $mail->assignParams(array(
  171. 'username' => $user->getUsername(),
  172. 'password' => String::substr($password, 0, 30), // Prevent mailer abuse via long passwords
  173. ));
  174. $mail->addRecipient($user->getEmail(), $user->getFullName());
  175. $mail->send();
  176. unset($mail);
  177. }
  178. }
  179. /**
  180. * Register a new user.
  181. */
  182. function execute() {
  183. $requireValidation = Config::getVar('email', 'require_validation');
  184. if ($this->existingUser) {
  185. // Existing user in the system
  186. $userDao =& DAORegistry::getDAO('UserDAO');
  187. $user =& $userDao->getUserByUsername($this->getData('username'));
  188. if ($user == null) {
  189. return false;
  190. }
  191. $userId = $user->getId();
  192. } else {
  193. // New user
  194. $user = new User();
  195. $user->setUsername($this->getData('username'));
  196. $user->setSalutation($this->getData('salutation'));
  197. $user->setFirstName($this->getData('firstName'));
  198. $user->setMiddleName($this->getData('middleName'));
  199. $user->setInitials($this->getData('initials'));
  200. $user->setLastName($this->getData('lastName'));
  201. $user->setGender($this->getData('gender'));
  202. $user->setAffiliation($this->getData('affiliation'));
  203. $user->setSignature($this->getData('signature'), null); // Localized
  204. $user->setEmail($this->getData('email'));
  205. $user->setUrl($this->getData('userUrl'));
  206. $user->setPhone($this->getData('phone'));
  207. $user->setFax($this->getData('fax'));
  208. $user->setMailingAddress($this->getData('mailingAddress'));
  209. $user->setBiography($this->getData('biography'), null); // Localized
  210. $user->setInterests($this->getData('interests'), null); // Localized
  211. $user->setDateRegistered(Core::getCurrentDate());
  212. $user->setCountry($this->getData('country'));
  213. $site =& Request::getSite();
  214. $availableLocales = $site->getSupportedLocales();
  215. $locales = array();
  216. foreach ($this->getData('userLocales') as $locale) {
  217. if (AppLocale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
  218. array_push($locales, $locale);
  219. }
  220. }
  221. $user->setLocales($locales);
  222. if (isset($this->defaultAuth)) {
  223. $user->setPassword($this->getData('password'));
  224. // FIXME Check result and handle failures
  225. $this->defaultAuth->doCreateUser($user);
  226. $user->setAuthId($this->defaultAuth->authId);
  227. }
  228. $user->setPassword(Validation::encryptCredentials($this->getData('username'), $this->getData('password')));
  229. if ($requireValidation) {
  230. // The account should be created in a disabled
  231. // state.
  232. $user->setDisabled(true);
  233. $user->setDisabledReason(__('user.login.accountNotValidated'));
  234. }
  235. $userDao =& DAORegistry::getDAO('UserDAO');
  236. $userDao->insertUser($user);
  237. $userId = $user->getId();
  238. if (!$userId) {
  239. return false;
  240. }
  241. $sessionManager =& SessionManager::getManager();
  242. $session =& $sessionManager->getUserSession();
  243. $session->setSessionVar('username', $user->getUsername());
  244. }
  245. $conference =& Request::getConference();
  246. $schedConf =& Request::getSchedConf();
  247. $roleDao =& DAORegistry::getDAO('RoleDAO');
  248. // Roles users are allowed to register themselves in
  249. $allowedRoles = array('reader' => 'createAsReader', 'author' => 'createAsAuthor', 'reviewer' => 'createAsReviewer');
  250. import('schedConf.SchedConfAction');
  251. if (!SchedConfAction::allowRegReader($schedConf)) {
  252. unset($allowedRoles['reader']);
  253. }
  254. if (!SchedConfAction::allowRegAuthor($schedConf)) {
  255. unset($allowedRoles['author']);
  256. }
  257. if (!SchedConfAction::allowRegReviewer($schedConf)) {
  258. unset($allowedRoles['reviewer']);
  259. }
  260. foreach ($allowedRoles as $k => $v) {
  261. $roleId = $roleDao->getRoleIdFromPath($k);
  262. if ($this->getData($v) && !$roleDao->roleExists($conference->getId(), $schedConf->getId(), $userId, $roleId)) {
  263. $role = new Role();
  264. $role->setConferenceId($conference->getId());
  265. $role->setSchedConfId($schedConf->getId());
  266. $role->setUserId($userId);
  267. $role->setRoleId($roleId);
  268. $roleDao->insertRole($role);
  269. }
  270. }
  271. if (!$this->existingUser) {
  272. $this->sendConfirmationEmail($user, $this->getData('password'), $this->getData('sendPassword'));
  273. }
  274. if (isset($allowedRoles['reader']) && $this->getData('openAccessNotification')) {
  275. $userSettingsDao =& DAORegistry::getDAO('UserSettingsDAO');
  276. $userSettingsDao->updateSetting($userId, 'openAccessNotification', true, 'bool', $conference->getId());
  277. }
  278. }
  279. }
  280. ?>