• Main Page
  • Modules
  • Classes
  • Files
  • File List

classes/user/form/RegistrationForm.inc.php

00001 <?php
00002 
00021 import('lib.pkp.classes.form.Form');
00022 
00023 class RegistrationForm extends Form {
00024 
00026    var $existingUser;
00027 
00029    var $defaultAuth;
00030 
00032    var $captchaEnabled;
00033 
00035    var $implicitAuth;
00036 
00040    function RegistrationForm($site, $existingUser = false) {
00041       parent::Form('user/register.tpl');
00042       $this->implicitAuth = Config::getVar('security', 'implicit_auth');
00043 
00044       if ($this->implicitAuth) {
00045          // If implicit auth - it is always an existing user
00046          $this->existingUser = true;
00047       } else {
00048          $this->existingUser = $existingUser;
00049 
00050          import('lib.pkp.classes.captcha.CaptchaManager');
00051          $captchaManager = new CaptchaManager();
00052          $this->captchaEnabled = ($captchaManager->isEnabled() && Config::getVar('captcha', 'captcha_on_register'))?true:false;
00053 
00054          // Validation checks for this form
00055          $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
00056          $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
00057 
00058          if ($this->existingUser) {
00059             // Existing user -- check login
00060             $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)));
00061          } else {
00062             // New user -- check required profile fields
00063             $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array(), true));
00064             $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
00065             $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00066             $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00067             $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
00068             $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
00069             $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
00070             $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
00071             $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailsDoNotMatch', create_function('$email,$form', 'return $email == $form->getData(\'confirmEmail\');'), array(&$this)));
00072             $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array(), true));
00073             $this->addCheck(new FormValidator($this, 'country', 'required', 'user.profile.form.countryRequired'));
00074             if ($this->captchaEnabled) {
00075                $this->addCheck(new FormValidatorCaptcha($this, 'captcha', 'captchaId', 'common.captchaField.badCaptcha'));
00076             }
00077 
00078             $authDao =& DAORegistry::getDAO('AuthSourceDAO');
00079             $this->defaultAuth =& $authDao->getDefaultPlugin();
00080             if (isset($this->defaultAuth)) {
00081                $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', create_function('$username,$form,$auth', 'return (!$auth->userExists($username) || $auth->authenticate($username, $form->getData(\'password\')));'), array(&$this, $this->defaultAuth)));
00082             }
00083          }
00084       }
00085 
00086       $this->addCheck(new FormValidatorPost($this));
00087    }
00088 
00092    function display(&$request) {
00093       $templateMgr =& TemplateManager::getManager();
00094       $site =& $request->getSite();
00095       $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
00096       $press =& $request->getPress();
00097 
00098       if ($this->captchaEnabled) {
00099          import('lib.pkp.classes.captcha.CaptchaManager');
00100          $captchaManager = new CaptchaManager();
00101          $captcha =& $captchaManager->createCaptcha();
00102          if ($captcha) {
00103             $templateMgr->assign('captchaEnabled', $this->captchaEnabled);
00104             $this->setData('captchaId', $captcha->getId());
00105          }
00106       }
00107 
00108       $countryDao =& DAORegistry::getDAO('CountryDAO');
00109       $countries =& $countryDao->getCountries();
00110       $templateMgr->assign_by_ref('countries', $countries);
00111 
00112       $userDao =& DAORegistry::getDAO('UserDAO');
00113       $templateMgr->assign('genderOptions', $userDao->getGenderOptions());
00114 
00115       if ($press) {
00116          $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00117 
00118          $templateMgr->assign_by_ref('reviewerUserGroups', $userGroupDao->getByRoleId($press->getId(), ROLE_ID_REVIEWER));
00119          $templateMgr->assign_by_ref('authorUserGroups', $userGroupDao->getByRoleId($press->getId(), ROLE_ID_AUTHOR));
00120 
00121          $templateMgr->assign('privacyStatement', $press->getLocalizedSetting('privacyStatement'));
00122          $templateMgr->assign('allowRegAuthor', $press->getSetting('allowRegAuthor'));
00123          $templateMgr->assign('allowRegReviewer', $press->getSetting('allowRegReviewer'));
00124       }
00125 
00126       $templateMgr->assign('source', $request->getUserVar('source'));
00127 
00128       $site =& $request->getSite();
00129       $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
00130 
00131       $templateMgr->assign('helpTopicId', 'user.registerAndProfile');
00132       parent::display();
00133    }
00134 
00138    function getLocaleFieldNames() {
00139       $userDao =& DAORegistry::getDAO('UserDAO');
00140       return $userDao->getLocaleFieldNames();
00141    }
00142 
00146    function initData() {
00147       $this->setData('existingUser', $this->existingUser);
00148       $this->setData('userLocales', array());
00149       $this->setData('sendPassword', false);
00150    }
00151 
00155    function readInputData() {
00156       $userVars = array(
00157          'username',
00158          'password',
00159          'password2',
00160          'salutation',
00161          'firstName',
00162          'middleName',
00163          'lastName',
00164          'suffix',
00165          'gender',
00166          'initials',
00167          'country',
00168          'affiliation',
00169          'email',
00170          'confirmEmail',
00171          'userUrl',
00172          'phone',
00173          'fax',
00174          'signature',
00175          'reviewerGroup',
00176          'authorGroup',
00177          'mailingAddress',
00178          'biography',
00179          'interestsTextOnly',
00180          'keywords',
00181          'userLocales',
00182          'registerAsReviewer',
00183          'existingUser',
00184          'sendPassword'
00185       );
00186       if ($this->captchaEnabled) {
00187          $userVars[] = 'captchaId';
00188          $userVars[] = 'captcha';
00189       }
00190 
00191       $this->readUserVars($userVars);
00192 
00193       if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
00194          $this->setData('userLocales', array());
00195       }
00196 
00197       if ($this->getData('username') != null) {
00198          // Usernames must be lowercase
00199          $this->setData('username', strtolower($this->getData('username')));
00200       }
00201 
00202       $keywords = $this->getData('keywords');
00203       if ($keywords != null && is_array($keywords['interests'])) {
00204          // The interests are coming in encoded -- Decode them for DB storage
00205          $this->setData('interestsKeywords', array_map('urldecode', $keywords['interests']));
00206       }
00207    }
00208 
00212    function execute($request) {
00213       $requireValidation = Config::getVar('email', 'require_validation');
00214 
00215       if ($this->existingUser) { // If using implicit auth - we hardwire that we are working on an existing user
00216          // Existing user in the system
00217          $userDao =& DAORegistry::getDAO('UserDAO');
00218 
00219          if ($this->implicitAuth) { // If we are using implicit auth - then use the session username variable - rather than data from the form
00220             $sessionManager =& SessionManager::getManager();
00221             $session =& $sessionManager->getUserSession();
00222 
00223             $user =& $userDao->getByUsername($session->getSessionVar('username'));
00224          } else {
00225             $user =& $userDao->getByUsername($this->getData('username'));
00226          }
00227 
00228          if (!$user) return false;
00229          $userId = $user->getId();
00230 
00231       } else {
00232          // New user
00233          $user = new User();
00234 
00235          $user->setUsername($this->getData('username'));
00236          $user->setSalutation($this->getData('salutation'));
00237          $user->setFirstName($this->getData('firstName'));
00238          $user->setMiddleName($this->getData('middleName'));
00239          $user->setInitials($this->getData('initials'));
00240          $user->setLastName($this->getData('lastName'));
00241          $user->setSuffix($this->getData('suffix'));
00242          $user->setGender($this->getData('gender'));
00243          $user->setAffiliation($this->getData('affiliation'), null); // Localized
00244          $user->setSignature($this->getData('signature'), null); // Localized
00245          $user->setEmail($this->getData('email'));
00246          $user->setUrl($this->getData('userUrl'));
00247          $user->setPhone($this->getData('phone'));
00248          $user->setFax($this->getData('fax'));
00249          $user->setMailingAddress($this->getData('mailingAddress'));
00250          $user->setBiography($this->getData('biography'), null); // Localized
00251          $user->setDateRegistered(Core::getCurrentDate());
00252          $user->setCountry($this->getData('country'));
00253          $user->setInlineHelp(1); // default new users to having inline help visible.
00254 
00255          $site =& $request->getSite();
00256          $availableLocales = $site->getSupportedLocales();
00257 
00258          $locales = array();
00259          foreach ($this->getData('userLocales') as $locale) {
00260             if (AppLocale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
00261                array_push($locales, $locale);
00262             }
00263          }
00264          $user->setLocales($locales);
00265 
00266          if (isset($this->defaultAuth)) {
00267             $user->setPassword($this->getData('password'));
00268             // FIXME Check result and handle failures
00269             $this->defaultAuth->doCreateUser($user);
00270             $user->setAuthId($this->defaultAuth->authId);
00271          }
00272          $user->setPassword(Validation::encryptCredentials($this->getData('username'), $this->getData('password')));
00273 
00274          if ($requireValidation) {
00275             // The account should be created in a disabled
00276             // state.
00277             $user->setDisabled(true);
00278             $user->setDisabledReason(__('user.login.accountNotValidated'));
00279          }
00280 
00281          $userDao =& DAORegistry::getDAO('UserDAO');
00282          $userDao->insertUser($user);
00283          $userId = $user->getId();
00284          if (!$userId) {
00285             return false;
00286          }
00287 
00288          // Insert the user interests
00289          $interests = $this->getData('interestsKeywords') ? $this->getData('interestsKeywords') : $this->getData('interestsTextOnly');
00290          import('lib.pkp.classes.user.InterestManager');
00291          $interestManager = new InterestManager();
00292          $interestManager->setInterestsForUser($user, $interests);
00293 
00294          $sessionManager =& SessionManager::getManager();
00295          $session =& $sessionManager->getUserSession();
00296          $session->setSessionVar('username', $user->getUsername());
00297       }
00298 
00299       // User Groups
00300       $press =& Request::getPress();
00301       if ($press) {
00302          $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00303          if ($press->getSetting('allowRegReviewer')) {
00304             $reviewerGroup = $this->getData('reviewerGroup');
00305             $reviewerUserGroupIds = $userGroupDao->getUserGroupIdsByRoleId(ROLE_ID_REVIEWER, $press->getId());
00306 
00307             if (is_array($reviewerGroup)) {
00308                foreach ($reviewerGroup as $groupId => $wantsGroup ) {
00309                   // Validate group id.
00310                   if (!in_array($groupId, $reviewerUserGroupIds)) {
00311                      fatalError('Invalid user group id!');
00312                   }
00313                   if ($wantsGroup) $userGroupDao->assignUserToGroup($userId, $groupId, $press->getId());
00314                }
00315             }
00316          }
00317 
00318          if ($press->getSetting('allowRegAuthor')) {
00319             $authorGroup = $this->getData('authorGroup');
00320             $authorUserGroupIds = $userGroupDao->getUserGroupIdsByRoleId(ROLE_ID_AUTHOR, $press->getId());
00321 
00322             if (isset($authorGroup)) {
00323                // Validate group id.
00324                if (!in_array($authorGroup, $authorUserGroupIds)) {
00325                   fatalError('Invalid user group id!');
00326                }
00327                $userGroupDao->assignUserToGroup($userId, $authorGroup, $press->getId());
00328             }
00329          }
00330       }
00331 
00332       if (!$this->existingUser) {
00333          import('classes.mail.MailTemplate');
00334          if ($requireValidation) {
00335             // Create an access key
00336             import('lib.pkp.classes.security.AccessKeyManager');
00337             $accessKeyManager = new AccessKeyManager();
00338             $accessKey = $accessKeyManager->createKey('RegisterContext', $user->getId(), null, Config::getVar('email', 'validation_timeout'));
00339 
00340             // Send email validation request to user
00341             $mail = new MailTemplate('USER_VALIDATE');
00342             $this->_setMailFrom($request, $mail);
00343             $mail->assignParams(array(
00344                'userFullName' => $user->getFullName(),
00345                'activateUrl' => $request->url($press->getPath(), 'user', 'activateUser', array($this->getData('username'), $accessKey))
00346             ));
00347             $mail->addRecipient($user->getEmail(), $user->getFullName());
00348             $mail->send();
00349             unset($mail);
00350          }
00351          if ($this->getData('sendPassword')) {
00352             // Send welcome email to user
00353             $mail = new MailTemplate('USER_REGISTER');
00354             $this->_setMailFrom($request, $mail);
00355             $mail->assignParams(array(
00356                'username' => $this->getData('username'),
00357                'password' => String::substr($this->getData('password'), 0, 30), // Prevent mailer abuse via long passwords
00358                'userFullName' => $user->getFullName()
00359             ));
00360             $mail->addRecipient($user->getEmail(), $user->getFullName());
00361             $mail->send();
00362             unset($mail);
00363          }
00364       }
00365 
00366       // By default, self-registering readers will receive
00367       // press updates. (The double set is here to prevent a
00368       // duplicate insert error msg if there was a notification entry
00369       // left over from a previous role.)
00370       if (isset($allowedRoles['reader']) && $this->getData($allowedRoles['reader'])) {
00371          $notificationStatusDao =& DAORegistry::getDAO('NotificationStatusDAO');
00372          $notificationStatusDao->setPressNotifications($press->getId(), $userId, false);
00373          $notificationStatusDao->setPressNotifications($press->getId(), $userId, true);
00374       }
00375    }
00376 
00382    function _setMailFrom($request, &$mail) {
00383       $site =& $request->getSite();
00384       $press =& $request->getPress();
00385 
00386       // Set the sender based on the current context
00387       if ($press && $press->getSetting('supportEmail')) {
00388          $mail->setFrom($press->getSetting('supportEmail'), $press->getSetting('supportName'));
00389       } else {
00390          $mail->setFrom($site->getLocalizedContactEmail(), $site->getLocalizedContactName());
00391       }
00392    }
00393 
00394 }
00395 
00396 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1