classes/user/form/CreateAccountForm.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00020 // $Id$
00021 
00022 
00023 import('form.Form');
00024 
00025 class CreateAccountForm extends Form {
00027    var $existingUser;
00028 
00030    var $defaultAuth;
00031 
00033    var $captchaEnabled;
00034 
00038    function CreateAccountForm() {
00039       parent::Form('user/createAccount.tpl');
00040 
00041       $this->existingUser = Request::getUserVar('existingUser') ? 1 : 0;
00042 
00043       import('captcha.CaptchaManager');
00044       $captchaManager = new CaptchaManager();
00045       $this->captchaEnabled = ($captchaManager->isEnabled() && Config::getVar('captcha', 'captcha_on_register'))?true:false;
00046 
00047       // Validation checks for this form
00048       $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
00049       $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
00050 
00051       if ($this->existingUser) {
00052          // Existing user -- check login
00053          $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)));
00054       } else {
00055          // New user -- check required profile fields
00056          $site =& Request::getSite();
00057 
00058          $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.account.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array(), true));
00059          $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.account.form.usernameAlphaNumeric'));
00060          $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.account.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00061          $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.account.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00062          $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
00063          $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
00064          $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
00065          $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
00066          $this->addCheck(new FormValidator($this, 'affiliation', 'required', 'user.profile.form.affiliationRequired'));
00067          $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.account.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array(), true));
00068          if ($this->captchaEnabled) {
00069             $this->addCheck(new FormValidatorCaptcha($this, 'captcha', 'captchaId', 'common.captchaField.badCaptcha'));
00070          }
00071 
00072          $authDao =& DAORegistry::getDAO('AuthSourceDAO');
00073          $this->defaultAuth =& $authDao->getDefaultPlugin();
00074          if (isset($this->defaultAuth)) {
00075             $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)));
00076          }
00077       }
00078 
00079       $this->addCheck(new FormValidatorPost($this));
00080    }
00081 
00085    function display() {
00086       $templateMgr =& TemplateManager::getManager();
00087       $site =& Request::getSite();
00088       $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
00089       $conference =& Request::getConference();
00090       $schedConf =& Request::getSchedConf();
00091 
00092       if ($this->captchaEnabled) {
00093          import('captcha.CaptchaManager');
00094          $captchaManager = new CaptchaManager();
00095          $captcha =& $captchaManager->createCaptcha();
00096          if ($captcha) {
00097             $templateMgr->assign('captchaEnabled', $this->captchaEnabled);
00098             $this->setData('captchaId', $captcha->getId());
00099          }
00100       }
00101 
00102       $countryDao =& DAORegistry::getDAO('CountryDAO');
00103       $countries =& $countryDao->getCountries();
00104       $templateMgr->assign_by_ref('countries', $countries);
00105 
00106       import('schedConf.SchedConfAction');
00107       
00108       $userDao =& DAORegistry::getDAO('UserDAO');
00109       $templateMgr->assign('genderOptions', $userDao->getGenderOptions());
00110 
00111       $templateMgr->assign('privacyStatement', $conference->getLocalizedSetting('privacyStatement'));
00112       $templateMgr->assign('enableOpenAccessNotification', $schedConf->getSetting('enableOpenAccessNotification')==1?1:0);
00113       $templateMgr->assign('allowRegReader', SchedConfAction::allowRegReader($schedConf));
00114       $templateMgr->assign('allowRegAuthor', SchedConfAction::allowRegAuthor($schedConf));
00115       $templateMgr->assign('allowRegReviewer', SchedConfAction::allowRegReviewer($schedConf));
00116       $templateMgr->assign('source', Request::getUserVar('source'));
00117       $templateMgr->assign('pageHierarchy', array(
00118          array(Request::url(null, 'index', 'index'), $conference->getConferenceTitle(), true),
00119          array(Request::url(null, null, 'index'), $schedConf->getSchedConfTitle(), true)));
00120 
00121       $site =& Request::getSite();
00122       $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
00123 
00124       $templateMgr->assign('helpTopicId', 'conference.users.index');    
00125       parent::display();
00126    }
00127 
00128    function getLocaleFieldNames() {
00129       $userDao =& DAORegistry::getDAO('UserDAO');
00130       return $userDao->getLocaleFieldNames();
00131    }
00132 
00136    function initData() {
00137       $this->setData('createAsReader', 1);
00138       if (Request::getUserVar('requiresAuthor')) $this->setData('createAsAuthor', 1);
00139       $this->setData('existingUser', $this->existingUser);
00140       $this->setData('userLocales', array());
00141       $this->setData('sendPassword', 1);
00142    }
00143 
00147    function readInputData() {
00148       $userVars = array(
00149          'username', 'password', 'password2',
00150          'salutation', 'firstName', 'middleName', 'lastName',
00151          'gender', 'initials', 'country',
00152          'affiliation', 'email', 'userUrl', 'phone', 'fax', 'signature',
00153          'mailingAddress', 'biography', 'interests', 'userLocales',
00154          'createAsReader', 'openAccessNotification', 'createAsAuthor',
00155          'createAsReviewer', 'existingUser', 'sendPassword'
00156       );
00157       if ($this->captchaEnabled) {
00158          $userVars[] = 'captchaId';
00159          $userVars[] = 'captcha';
00160       }
00161 
00162       $this->readUserVars($userVars);
00163 
00164       if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
00165          $this->setData('userLocales', array());
00166       }
00167 
00168       if ($this->getData('username') != null) {
00169          // Usernames must be lowercase
00170          $this->setData('username', strtolower($this->getData('username')));
00171       }
00172    }
00173 
00178    function sendConfirmationEmail($user, $password, $sendPassword) {
00179       $schedConf =& Request::getSchedConf();
00180       import('mail.MailTemplate');
00181       if (Config::getVar('email', 'require_validation')) {
00182          // Create an access key
00183          import('security.AccessKeyManager');
00184          $accessKeyManager = new AccessKeyManager();
00185          $accessKey = $accessKeyManager->createKey('RegisterContext', $user->getId(), null, Config::getVar('email', 'validation_timeout'));
00186 
00187          // Send email validation request to user
00188          $mail = new MailTemplate('USER_VALIDATE');
00189          $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
00190          $mail->assignParams(array(
00191             'userFullName' => $user->getFullName(),
00192             'activateUrl' => Request::url(null, null, 'user', 'activateUser', array($user->getUsername(), $accessKey))
00193          ));
00194          $mail->addRecipient($user->getEmail(), $user->getFullName());
00195          $mail->send();
00196          unset($mail);
00197       }
00198       if ($sendPassword) {
00199          // Send welcome email to user
00200          $mail = new MailTemplate('USER_REGISTER');
00201          $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
00202          $mail->assignParams(array(
00203             'username' => $user->getUsername(),
00204             'password' => String::substr($password, 0, 30), // Prevent mailer abuse via long passwords
00205          ));
00206          $mail->addRecipient($user->getEmail(), $user->getFullName());
00207          $mail->send();
00208          unset($mail);
00209       }
00210    }
00211 
00215    function execute() {
00216       $requireValidation = Config::getVar('email', 'require_validation');
00217       if ($this->existingUser) {
00218          // Existing user in the system
00219          $userDao =& DAORegistry::getDAO('UserDAO');
00220          $user =& $userDao->getUserByUsername($this->getData('username'));
00221          if ($user == null) {
00222             return false;
00223          }
00224 
00225          $userId = $user->getId();
00226 
00227       } else {
00228          // New user
00229          $user = new User();
00230 
00231          $user->setUsername($this->getData('username'));
00232          $user->setSalutation($this->getData('salutation'));
00233          $user->setFirstName($this->getData('firstName'));
00234          $user->setMiddleName($this->getData('middleName'));
00235          $user->setInitials($this->getData('initials'));
00236          $user->setLastName($this->getData('lastName'));
00237          $user->setGender($this->getData('gender'));
00238          $user->setAffiliation($this->getData('affiliation'));
00239          $user->setSignature($this->getData('signature'), null); // Localized
00240          $user->setEmail($this->getData('email'));
00241          $user->setUrl($this->getData('userUrl'));
00242          $user->setPhone($this->getData('phone'));
00243          $user->setFax($this->getData('fax'));
00244          $user->setMailingAddress($this->getData('mailingAddress'));
00245          $user->setBiography($this->getData('biography'), null); // Localized
00246          $user->setInterests($this->getData('interests'), null); // Localized
00247          $user->setDateRegistered(Core::getCurrentDate());
00248          $user->setCountry($this->getData('country'));
00249 
00250          $site =& Request::getSite();
00251          $availableLocales = $site->getSupportedLocales();
00252 
00253          $locales = array();
00254          foreach ($this->getData('userLocales') as $locale) {
00255             if (AppLocale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
00256                array_push($locales, $locale);
00257             }
00258          }
00259          $user->setLocales($locales);
00260 
00261          if (isset($this->defaultAuth)) {
00262             $user->setPassword($this->getData('password'));
00263             // FIXME Check result and handle failures
00264             $this->defaultAuth->doCreateUser($user);
00265             $user->setAuthId($this->defaultAuth->authId);
00266          }
00267          $user->setPassword(Validation::encryptCredentials($this->getData('username'), $this->getData('password')));
00268 
00269          if ($requireValidation) {
00270             // The account should be created in a disabled
00271             // state.
00272             $user->setDisabled(true);
00273             $user->setDisabledReason(__('user.login.accountNotValidated'));
00274          }
00275 
00276          $userDao =& DAORegistry::getDAO('UserDAO');
00277          $userDao->insertUser($user);
00278          $userId = $user->getId();
00279          if (!$userId) {
00280             return false;
00281          }
00282 
00283          $sessionManager =& SessionManager::getManager();
00284          $session =& $sessionManager->getUserSession();
00285          $session->setSessionVar('username', $user->getUsername());
00286 
00287       }
00288 
00289       $conference =& Request::getConference();
00290       $schedConf =& Request::getSchedConf();
00291 
00292       $roleDao =& DAORegistry::getDAO('RoleDAO');
00293 
00294       // Roles users are allowed to register themselves in
00295       $allowedRoles = array('reader' => 'createAsReader', 'author' => 'createAsAuthor', 'reviewer' => 'createAsReviewer');
00296 
00297       import('schedConf.SchedConfAction');
00298       if (!SchedConfAction::allowRegReader($schedConf)) {
00299          unset($allowedRoles['reader']);
00300       }
00301       if (!SchedConfAction::allowRegAuthor($schedConf)) {
00302          unset($allowedRoles['author']);
00303       }
00304       if (!SchedConfAction::allowRegReviewer($schedConf)) {
00305          unset($allowedRoles['reviewer']);
00306       }
00307 
00308       foreach ($allowedRoles as $k => $v) {
00309          $roleId = $roleDao->getRoleIdFromPath($k);
00310          if ($this->getData($v) && !$roleDao->roleExists($conference->getId(), $schedConf->getId(), $userId, $roleId)) {
00311             $role = new Role();
00312             $role->setConferenceId($conference->getId());
00313             $role->setSchedConfId($schedConf->getId());
00314             $role->setUserId($userId);
00315             $role->setRoleId($roleId);
00316             $roleDao->insertRole($role);
00317 
00318          }
00319       }
00320 
00321       if (!$this->existingUser) {
00322          $this->sendConfirmationEmail($user, $this->getData('password'), $this->getData('sendPassword'));
00323       }
00324 
00325       if (isset($allowedRoles['reader']) && $this->getData('openAccessNotification')) {
00326          $userSettingsDao =& DAORegistry::getDAO('UserSettingsDAO');
00327          $userSettingsDao->updateSetting($userId, 'openAccessNotification', true, 'bool', $conference->getId());
00328       }
00329    }
00330 
00331 }
00332 
00333 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7