classes/manager/form/UserManagementForm.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00016 // $Id$
00017 
00018 
00019 import('form.Form');
00020 
00021 class UserManagementForm extends Form {
00022 
00024    var $userId;
00025 
00029    function UserManagementForm($userId = null) {
00030       parent::Form('manager/people/userProfileForm.tpl');
00031 
00032       $this->userId = isset($userId) ? (int) $userId : null;
00033       $site =& Request::getSite();
00034 
00035       // Validation checks for this form
00036       if ($userId == null) {
00037          $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
00038          $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.account.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array($this->userId, true), true));
00039          $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.account.form.usernameAlphaNumeric'));
00040          $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
00041          $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.account.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00042          $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.account.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00043       } else {
00044          $this->addCheck(new FormValidatorLength($this, 'password', 'optional', 'user.account.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00045          $this->addCheck(new FormValidatorCustom($this, 'password', 'optional', 'user.account.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00046       }
00047       $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
00048       $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
00049       $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
00050       $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
00051       $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.account.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array($this->userId, true), true));
00052       $this->addCheck(new FormValidatorPost($this));
00053    }
00054 
00058    function display() {
00059       $userDao =& DAORegistry::getDAO('UserDAO');
00060       $templateMgr =& TemplateManager::getManager();
00061       $site =& Request::getSite();
00062       $schedConf =& Request::getSchedConf();
00063 
00064       $templateMgr->assign('genderOptions', $userDao->getGenderOptions());
00065       $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
00066       $templateMgr->assign('source', Request::getUserVar('source'));
00067       $templateMgr->assign('userId', $this->userId);
00068       if (isset($this->userId)) {
00069          $user =& $userDao->getUser($this->userId);
00070          $templateMgr->assign('username', $user->getUsername());
00071          $helpTopicId = 'conference.users.index';
00072       } else {
00073          $helpTopicId = 'conference.users.createNewUser';
00074       }
00075 
00076       if($schedConf) {
00077          $templateMgr->assign('roleOptions',
00078             array(
00079                '' => 'manager.people.doNotEnroll',
00080                'director' => 'user.role.director',
00081                'trackDirector' => 'user.role.trackDirector',
00082                'reviewer' => 'user.role.reviewer',
00083                'author' => 'user.role.author',
00084                'reader' => 'user.role.reader'
00085             )
00086          );
00087       } else {
00088          $templateMgr->assign('roleOptions',
00089             array(
00090                '' => 'manager.people.doNotEnroll',
00091                'manager' => 'user.role.manager',
00092             )
00093          );
00094       }
00095       $site =& Request::getSite();
00096       $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
00097 
00098       $templateMgr->assign('helpTopicId', $helpTopicId);
00099 
00100       $countryDao =& DAORegistry::getDAO('CountryDAO');
00101       $countries =& $countryDao->getCountries();
00102       $templateMgr->assign_by_ref('countries', $countries);
00103 
00104       $authDao =& DAORegistry::getDAO('AuthSourceDAO');
00105       $authSources =& $authDao->getSources();
00106       $authSourceOptions = array();
00107       foreach ($authSources->toArray() as $auth) {
00108          $authSourceOptions[$auth->getAuthId()] = $auth->getTitle();
00109       }
00110       if (!empty($authSourceOptions)) {
00111          $templateMgr->assign('authSourceOptions', $authSourceOptions);
00112       }
00113       parent::display();
00114    }
00115 
00119    function initData() {
00120       if (isset($this->userId)) {
00121          $userDao =& DAORegistry::getDAO('UserDAO');
00122          $user =& $userDao->getUser($this->userId);
00123 
00124          if ($user != null) {
00125             $this->_data = array(
00126                'authId' => $user->getAuthId(),
00127                'username' => $user->getUsername(),
00128                'salutation' => $user->getSalutation(),
00129                'firstName' => $user->getFirstName(),
00130                'middleName' => $user->getMiddleName(),
00131                'lastName' => $user->getLastName(),
00132                'initials' => $user->getInitials(),
00133                'gender' => $user->getGender(),
00134                'affiliation' => $user->getAffiliation(),
00135                'email' => $user->getEmail(),
00136                'userUrl' => $user->getUrl(),
00137                'phone' => $user->getPhone(),
00138                'fax' => $user->getFax(),
00139                'mailingAddress' => $user->getMailingAddress(),
00140                'country' => $user->getCountry(),
00141                'biography' => $user->getBiography(null), // Localized
00142                'interests' => $user->getInterests(null), // Localized
00143                'gossip' => $user->getGossip(null), // Localized
00144                'signature' => $user->getSignature(null), // Localized
00145                'userLocales' => $user->getLocales()
00146             );
00147 
00148          } else {
00149             $this->userId = null;
00150          }
00151       }
00152       if (!isset($this->userId)) {
00153          $roleDao =& DAORegistry::getDAO('RoleDAO');
00154          $roleId = Request::getUserVar('roleId');
00155          $roleSymbolic = $roleDao->getRolePath($roleId);
00156 
00157          $this->_data = array(
00158             'enrollAs' => array($roleSymbolic)
00159          );
00160       }
00161    }
00162 
00166    function readInputData() {
00167       $this->readUserVars(array(
00168          'authId',
00169          'enrollAs',
00170          'password',
00171          'password2',
00172          'salutation',
00173          'firstName',
00174          'middleName',
00175          'lastName',
00176          'gender',
00177          'initials',
00178          'affiliation',
00179          'email',
00180          'phone',
00181          'fax',
00182          'mailingAddress',
00183          'country',
00184          'userUrl',
00185          'biography',
00186          'interests',
00187          'gossip',
00188          'signature',
00189          'userLocales',
00190          'generatePassword',
00191          'sendNotify',
00192          'mustChangePassword'
00193       ));
00194       if ($this->userId == null) {
00195          $this->readUserVars(array('username'));
00196       }
00197 
00198       if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
00199          $this->setData('userLocales', array());
00200       }
00201 
00202       if ($this->getData('username') != null) {
00203          // Usernames must be lowercase
00204          $this->setData('username', strtolower($this->getData('username')));
00205       }
00206    }
00207 
00208    function getLocaleFieldNames() {
00209       $userDao =& DAORegistry::getDAO('UserDAO');
00210       return $userDao->getLocaleFieldNames();
00211    }
00212 
00216    function execute() {
00217       $userDao =& DAORegistry::getDAO('UserDAO');
00218       $conference =& Request::getConference();
00219       $schedConf =& Request::getSchedConf();
00220 
00221       if (isset($this->userId)) {
00222          $user =& $userDao->getUser($this->userId);
00223       }
00224 
00225       if (!isset($user)) {
00226          $user = new User();
00227       }
00228 
00229       $user->setSalutation($this->getData('salutation'));
00230       $user->setFirstName($this->getData('firstName'));
00231       $user->setMiddleName($this->getData('middleName'));
00232       $user->setLastName($this->getData('lastName'));
00233       $user->setInitials($this->getData('initials'));
00234       $user->setGender($this->getData('gender'));
00235       $user->setAffiliation($this->getData('affiliation'));
00236       $user->setEmail($this->getData('email'));
00237       $user->setUrl($this->getData('userUrl'));
00238       $user->setPhone($this->getData('phone'));
00239       $user->setFax($this->getData('fax'));
00240       $user->setMailingAddress($this->getData('mailingAddress'));
00241       $user->setCountry($this->getData('country'));
00242       $user->setBiography($this->getData('biography'), null); // Localized
00243       $user->setInterests($this->getData('interests'), null); // Localized
00244       $user->setGossip($this->getData('gossip'), null); // Localized
00245       $user->setSignature($this->getData('signature'), null); // Localized
00246       $user->setMustChangePassword($this->getData('mustChangePassword') ? 1 : 0);
00247       $user->setAuthId((int) $this->getData('authId'));
00248 
00249       $site =& Request::getSite();
00250       $availableLocales = $site->getSupportedLocales();
00251 
00252       $locales = array();
00253       foreach ($this->getData('userLocales') as $locale) {
00254          if (AppLocale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
00255             array_push($locales, $locale);
00256          }
00257       }
00258       $user->setLocales($locales);
00259 
00260       if ($user->getAuthId()) {
00261          $authDao =& DAORegistry::getDAO('AuthSourceDAO');
00262          $auth =& $authDao->getPlugin($user->getAuthId());
00263       }
00264 
00265       if ($user->getId() != null) {
00266          if ($this->getData('password') !== '') {
00267             if (isset($auth)) {
00268                $auth->doSetUserPassword($user->getUsername(), $this->getData('password'));
00269                $user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
00270             } else {
00271                $user->setPassword(Validation::encryptCredentials($user->getUsername(), $this->getData('password')));
00272             }
00273          }
00274 
00275          if (isset($auth)) {
00276             // FIXME Should try to create user here too?
00277             $auth->doSetUserInfo($user);
00278          }
00279 
00280          $userDao->updateObject($user);
00281 
00282       } else {
00283          $user->setUsername($this->getData('username'));
00284          if ($this->getData('generatePassword')) {
00285             $password = Validation::generatePassword();
00286             $sendNotify = true;
00287          } else {
00288             $password = $this->getData('password');
00289             $sendNotify = $this->getData('sendNotify');
00290          }
00291 
00292          if (isset($auth)) {
00293             $user->setPassword($password);
00294             // FIXME Check result and handle failures
00295             $auth->doCreateUser($user);
00296             $user->setAuthId($auth->authId);
00297             $user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
00298          } else {
00299             $user->setPassword(Validation::encryptCredentials($this->getData('username'), $password));
00300          }
00301 
00302          $user->setDateRegistered(Core::getCurrentDate());
00303          $userId = $userDao->insertUser($user);
00304 
00305          if (!empty($this->_data['enrollAs'])) {
00306             foreach ($this->getData('enrollAs') as $roleName) {
00307                // Enroll new user into an initial role
00308                $roleDao =& DAORegistry::getDAO('RoleDAO');
00309                $roleId = $roleDao->getRoleIdFromPath($roleName);
00310                if ($roleId != null) {
00311                   $role = new Role();
00312                   $role->setConferenceId($conference->getId());
00313                   $role->setSchedConfId($schedConf?$schedConf->getId():0);
00314                   $role->setUserId($userId);
00315                   $role->setRoleId($roleId);
00316                   $roleDao->insertRole($role);
00317                }
00318             }
00319          }
00320 
00321          if ($sendNotify) {
00322             // Send welcome email to user
00323             import('mail.MailTemplate');
00324             $mail = new MailTemplate('USER_REGISTER');
00325 
00326             if ($schedConf) $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
00327             elseif ($conference) $mail->setFrom($conference->getSetting('contactEmail'), $conference->getSetting('contactName'));
00328             else {
00329                $site =& Request::getSite();
00330                $mail->setFrom($site->getContactEmail(), $site->getContactName());
00331             }
00332 
00333             $mail->assignParams(array('username' => $this->getData('username'), 'password' => $password));
00334             $mail->addRecipient($user->getEmail(), $user->getFullName());
00335             $mail->send();
00336          }
00337       }
00338    }
00339 }
00340 
00341 ?>

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