00001 <?php
00002
00015
00016
00017
00018 import('form.Form');
00019
00020 class UserManagementForm extends Form {
00021
00023 var $userId;
00024
00028 function UserManagementForm($userId = null) {
00029 parent::Form('manager/people/userProfileForm.tpl');
00030
00031 if (!Validation::isJournalManager()) $userId = null;
00032 $this->userId = isset($userId) ? (int) $userId : null;
00033 $site = &Request::getSite();
00034
00035
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.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array($this->userId, true), true));
00039 $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
00040
00041 if (!Config::getVar('security', 'implicit_auth')) {
00042 $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
00043 $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00044 $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00045 }
00046 } else {
00047 $this->addCheck(new FormValidatorLength($this, 'password', 'optional', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00048 $this->addCheck(new FormValidatorCustom($this, 'password', 'optional', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00049 }
00050 $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
00051 $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
00052 $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
00053 $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
00054 $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array($this->userId, true), true));
00055 $this->addCheck(new FormValidatorPost($this));
00056 }
00057
00061 function display() {
00062 $templateMgr = &TemplateManager::getManager();
00063 $site = &Request::getSite();
00064 $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
00065 $templateMgr->assign('source', Request::getUserVar('source'));
00066 $templateMgr->assign('userId', $this->userId);
00067 if (isset($this->userId)) {
00068 $userDao = &DAORegistry::getDAO('UserDAO');
00069 $user = &$userDao->getUser($this->userId);
00070 $templateMgr->assign('username', $user->getUsername());
00071 $helpTopicId = 'journal.users.index';
00072 } else {
00073 $helpTopicId = 'journal.users.createNewUser';
00074 }
00075 if (Validation::isJournalManager()) $templateMgr->assign('roleOptions',
00076 array(
00077 '' => 'manager.people.doNotEnroll',
00078 'manager' => 'user.role.manager',
00079 'editor' => 'user.role.editor',
00080 'sectionEditor' => 'user.role.sectionEditor',
00081 'layoutEditor' => 'user.role.layoutEditor',
00082 'reviewer' => 'user.role.reviewer',
00083 'copyeditor' => 'user.role.copyeditor',
00084 'proofreader' => 'user.role.proofreader',
00085 'author' => 'user.role.author',
00086 'reader' => 'user.role.reader',
00087 'subscriptionManager' => 'user.role.subscriptionManager'
00088 )
00089 );
00090 else $templateMgr->assign('roleOptions',
00091 array(
00092 '' => 'manager.people.doNotEnroll',
00093 'reader' => 'user.role.reader'
00094 )
00095 );
00096
00097
00098 $templateMgr->assign('implicitAuth', Config::getVar('security', 'implicit_auth'));
00099
00100 $site = &Request::getSite();
00101 $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
00102
00103 $templateMgr->assign('helpTopicId', $helpTopicId);
00104
00105 $countryDao =& DAORegistry::getDAO('CountryDAO');
00106 $countries =& $countryDao->getCountries();
00107 $templateMgr->assign_by_ref('countries', $countries);
00108
00109 $authDao = &DAORegistry::getDAO('AuthSourceDAO');
00110 $authSources = &$authDao->getSources();
00111 $authSourceOptions = array();
00112 foreach ($authSources->toArray() as $auth) {
00113 $authSourceOptions[$auth->getAuthId()] = $auth->getTitle();
00114 }
00115 if (!empty($authSourceOptions)) {
00116 $templateMgr->assign('authSourceOptions', $authSourceOptions);
00117 }
00118 parent::display();
00119 }
00120
00124 function initData() {
00125 if (isset($this->userId)) {
00126 $userDao = &DAORegistry::getDAO('UserDAO');
00127 $user = &$userDao->getUser($this->userId);
00128
00129 if ($user != null) {
00130 $this->_data = array(
00131 'authId' => $user->getAuthId(),
00132 'username' => $user->getUsername(),
00133 'salutation' => $user->getSalutation(),
00134 'firstName' => $user->getFirstName(),
00135 'middleName' => $user->getMiddleName(),
00136 'lastName' => $user->getLastName(),
00137 'signature' => $user->getSignature(null),
00138 'initials' => $user->getInitials(),
00139 'gender' => $user->getGender(),
00140 'affiliation' => $user->getAffiliation(),
00141 'email' => $user->getEmail(),
00142 'userUrl' => $user->getUrl(),
00143 'phone' => $user->getPhone(),
00144 'fax' => $user->getFax(),
00145 'mailingAddress' => $user->getMailingAddress(),
00146 'country' => $user->getCountry(),
00147 'biography' => $user->getBiography(null),
00148 'interests' => $user->getInterests(null),
00149 'userLocales' => $user->getLocales()
00150 );
00151
00152 } else {
00153 $this->userId = null;
00154 }
00155 }
00156 if (!isset($this->userId)) {
00157 $roleDao = &DAORegistry::getDAO('RoleDAO');
00158 $roleId = Request::getUserVar('roleId');
00159 $roleSymbolic = $roleDao->getRolePath($roleId);
00160
00161 $this->_data = array(
00162 'enrollAs' => array($roleSymbolic)
00163 );
00164 }
00165 }
00166
00170 function readInputData() {
00171 $this->readUserVars(array(
00172 'authId',
00173 'enrollAs',
00174 'password',
00175 'password2',
00176 'salutation',
00177 'firstName',
00178 'middleName',
00179 'lastName',
00180 'gender',
00181 'initials',
00182 'signature',
00183 'affiliation',
00184 'email',
00185 'userUrl',
00186 'phone',
00187 'fax',
00188 'mailingAddress',
00189 'country',
00190 'biography',
00191 'interests',
00192 'userLocales',
00193 'generatePassword',
00194 'sendNotify',
00195 'mustChangePassword'
00196 ));
00197 if ($this->userId == null) {
00198 $this->readUserVars(array('username'));
00199 }
00200
00201 if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
00202 $this->setData('userLocales', array());
00203 }
00204
00205 if ($this->getData('username') != null) {
00206
00207 $this->setData('username', strtolower($this->getData('username')));
00208 }
00209 }
00210
00211 function getLocaleFieldNames() {
00212 $userDao =& DAORegistry::getDAO('UserDAO');
00213 return $userDao->getLocaleFieldNames();
00214 }
00215
00219 function execute() {
00220 $userDao = &DAORegistry::getDAO('UserDAO');
00221 $journal = &Request::getJournal();
00222
00223 if (isset($this->userId)) {
00224 $user = &$userDao->getUser($this->userId);
00225 }
00226
00227 if (!isset($user)) {
00228 $user = &new User();
00229 }
00230
00231 $user->setSalutation($this->getData('salutation'));
00232 $user->setFirstName($this->getData('firstName'));
00233 $user->setMiddleName($this->getData('middleName'));
00234 $user->setLastName($this->getData('lastName'));
00235 $user->setInitials($this->getData('initials'));
00236 $user->setGender($this->getData('gender'));
00237 $user->setAffiliation($this->getData('affiliation'));
00238 $user->setSignature($this->getData('signature'), null);
00239 $user->setEmail($this->getData('email'));
00240 $user->setUrl($this->getData('userUrl'));
00241 $user->setPhone($this->getData('phone'));
00242 $user->setFax($this->getData('fax'));
00243 $user->setMailingAddress($this->getData('mailingAddress'));
00244 $user->setCountry($this->getData('country'));
00245 $user->setBiography($this->getData('biography'), null);
00246 $user->setInterests($this->getData('interests'), null);
00247 $user->setMustChangePassword($this->getData('mustChangePassword') ? 1 : 0);
00248 $user->setAuthId((int) $this->getData('authId'));
00249
00250 $site = &Request::getSite();
00251 $availableLocales = $site->getSupportedLocales();
00252
00253 $locales = array();
00254 foreach ($this->getData('userLocales') as $locale) {
00255 if (Locale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
00256 array_push($locales, $locale);
00257 }
00258 }
00259 $user->setLocales($locales);
00260
00261 if ($user->getAuthId()) {
00262 $authDao = &DAORegistry::getDAO('AuthSourceDAO');
00263 $auth = &$authDao->getPlugin($user->getAuthId());
00264 }
00265
00266 if ($user->getUserId() != null) {
00267 if ($this->getData('password') !== '') {
00268 if (isset($auth)) {
00269 $auth->doSetUserPassword($user->getUsername(), $this->getData('password'));
00270 $user->setPassword(Validation::encryptCredentials($user->getUserId(), Validation::generatePassword()));
00271 } else {
00272 $user->setPassword(Validation::encryptCredentials($user->getUsername(), $this->getData('password')));
00273 }
00274 }
00275
00276 if (isset($auth)) {
00277
00278 $auth->doSetUserInfo($user);
00279 }
00280
00281 $userDao->updateUser($user);
00282
00283 } else {
00284 $user->setUsername($this->getData('username'));
00285 if ($this->getData('generatePassword')) {
00286 $password = Validation::generatePassword();
00287 $sendNotify = true;
00288 } else {
00289 $password = $this->getData('password');
00290 $sendNotify = $this->getData('sendNotify');
00291 }
00292
00293 if (isset($auth)) {
00294 $user->setPassword($password);
00295
00296 $auth->doCreateUser($user);
00297 $user->setAuthId($auth->authId); $user->setPassword(Validation::encryptCredentials($user->getUserId(), Validation::generatePassword()));
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 $isManager = Validation::isJournalManager();
00306
00307 if (!empty($this->_data['enrollAs'])) {
00308 foreach ($this->getData('enrollAs') as $roleName) {
00309
00310 $roleDao = &DAORegistry::getDAO('RoleDAO');
00311 $roleId = $roleDao->getRoleIdFromPath($roleName);
00312 if (!$isManager && $roleId != ROLE_ID_READER) continue;
00313 if ($roleId != null) {
00314 $role = &new Role();
00315 $role->setJournalId($journal->getJournalId());
00316 $role->setUserId($userId);
00317 $role->setRoleId($roleId);
00318 $roleDao->insertRole($role);
00319 }
00320 }
00321 }
00322
00323 if ($sendNotify) {
00324
00325 import('mail.MailTemplate');
00326 $mail = &new MailTemplate('USER_REGISTER');
00327 $mail->setFrom($journal->getSetting('contactEmail'), $journal->getSetting('contactName'));
00328 $mail->assignParams(array('username' => $this->getData('username'), 'password' => $password, 'userFullName' => $user->getFullName()));
00329 $mail->addRecipient($user->getEmail(), $user->getFullName());
00330 $mail->send();
00331 }
00332 }
00333 }
00334 }
00335
00336 ?>