From: <gem...@li...> - 2011-11-10 17:15:22
|
Revision: 200 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=200&view=rev Author: matijsdejong Date: 2011-11-10 17:15:11 +0000 (Thu, 10 Nov 2011) Log Message: ----------- Almost ready with #31. The user can edit his/her account and password. getAllowedOrganizations moved to User and UserLoader GemsEscort cleaned up TODO: lost password Modified Paths: -------------- branches/newUser2/classes/Gems/Default/AskAction.php branches/newUser2/classes/Gems/Default/ExportAction.php branches/newUser2/classes/Gems/Default/IndexAction.php branches/newUser2/classes/Gems/Default/OptionAction.php branches/newUser2/classes/Gems/Default/OrganizationAction.php branches/newUser2/classes/Gems/Project/Organization/MultiOrganizationInterface.php branches/newUser2/classes/Gems/User/User.php branches/newUser2/classes/Gems/User/UserLoader.php branches/newUser2/classes/GemsEscort.php Added Paths: ----------- branches/newUser2/classes/Gems/User/UserPasswordValidator.php Modified: branches/newUser2/classes/Gems/Default/AskAction.php =================================================================== --- branches/newUser2/classes/Gems/Default/AskAction.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/Default/AskAction.php 2011-11-10 17:15:11 UTC (rev 200) @@ -82,13 +82,14 @@ /*************** * Get the url * ***************/ - $url = $token->getUrl($language, $this->session->user_id ? $this->session->user_id : $respId); + $user = $this->loader->getCurrentUser(); + $url = $token->getUrl($language, $user->getUserId() ? $user->getUserId() : $respId); /************************ * Optional user logout * ************************/ - if (isset($this->session->user_logout) && $this->session->user_logout) { - $this->escort->afterLogout(); + if ($user->isLogoutOnSurvey()) { + $user->unsetAsCurrentUser(); } /*********************************** Modified: branches/newUser2/classes/Gems/Default/ExportAction.php =================================================================== --- branches/newUser2/classes/Gems/Default/ExportAction.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/Default/ExportAction.php 2011-11-10 17:15:11 UTC (rev 200) @@ -144,7 +144,7 @@ { //Read some data from tables, initialize defaults... $surveys = $this->db->fetchPairs('SELECT gsu_id_survey, gsu_survey_name FROM gems__surveys WHERE gsu_active = 1 ORDER BY gsu_survey_name'); - $organizations = $this->escort->getAllowedOrganizations(); + $organizations = $this->loader->getCurrentUser()->getAllowedOrganizations(); $types = $this->export->getExportClasses(); //Create the basic form @@ -230,7 +230,7 @@ $answerModel = $survey->getAnswerModel($language); //Now add the organization id => name mapping - $answerModel->set('organizationid', 'multiOptions', $this->escort->getAllowedOrganizations()); + $answerModel->set('organizationid', 'multiOptions', $this->loader->getCurrentUser()->getAllowedOrganizations()); if (count($answers) === 0) { $answers[0] = array('' => sprintf($this->_('No %s found.'), $this->getTopic(0))); Modified: branches/newUser2/classes/Gems/Default/IndexAction.php =================================================================== --- branches/newUser2/classes/Gems/Default/IndexAction.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/Default/IndexAction.php 2011-11-10 17:15:11 UTC (rev 200) @@ -66,21 +66,6 @@ public $menu; /** - * Extension point, use different auth adapter if needed depending on the provided formValues - * - * This could be an organization passed in the login-form or something else. - * - * @param array $formValues - * @return Zend_Auth_Adapter_Interface - */ - protected function _getAuthAdapter($formValues) { - $adapter = new Zend_Auth_Adapter_DbTable($this->db, 'gems__staff', 'gsf_login', 'gsf_password'); - $adapter->setIdentity($formValues['userlogin']); - $adapter->setCredential($this->escort->passwordHash(null, $formValues['password'], false)); - return $adapter; - } - - /** * New version of login form * * @return Gems_Form @@ -89,8 +74,6 @@ { Gems_Html::init(); - $this->track[] = 'Get login form.'; - $delayFactor = (isset($this->project->account) && isset($this->project->account['delayFactor']) ? $this->project->account['delayFactor'] : null); $form = new Gems_Form(array('labelWidthFactor' => $this->labelWidthFactor)); Modified: branches/newUser2/classes/Gems/Default/OptionAction.php =================================================================== --- branches/newUser2/classes/Gems/Default/OptionAction.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/Default/OptionAction.php 2011-11-10 17:15:11 UTC (rev 200) @@ -1,6 +1,5 @@ <?php - /** * Copyright (c) 2011, Erasmus MC * All rights reserved. @@ -26,58 +25,47 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** * - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 - * @package Gems + * + * @package Gems * @subpackage Default + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ */ /** * - * @author Matijs de Jong - * @package Gems + * @package Gems * @subpackage Default + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.1 */ class Gems_Default_OptionAction extends Gems_Controller_BrowseEditAction { public $autoFilter = false; /** - * Adds elements from the model to the bridge that creates the form. * - * Overrule this function to add different elements to the browse table, without - * having to recode the core table building code. - * - * @param MUtil_Model_FormBridge $bridge - * @param MUtil_Model_ModelAbstract $model - * @param array $data The data that will later be loaded into the form - * @param optional boolean $new Form should be for a new element - * @return void|array When an array of new values is return, these are used to update the $data array in the calling function + * @var Gems_Project_ProjectSettings */ - protected function addFormElements(MUtil_Model_FormBridge $bridge, MUtil_Model_ModelAbstract $model, array $data, $new = false) - { - $bridge->addHidden( 'gus_id_user'); - $bridge->addHidden( 'gus_id_organization'); - $bridge->addHidden( 'gsf_id_user'); - $bridge->addExhibitor('gus_login', array('size' => 15, 'minlength' => 4)); - $bridge->addText( 'gsf_first_name'); - $bridge->addText( 'gsf_surname_prefix'); - $bridge->addText( 'gsf_last_name'); - $bridge->addText( 'gsf_email', array('size' => 30)); + public $project; - $bridge->addRadio( 'gsf_gender', 'separator', ''); - - $bridge->addSelect( 'gsf_iso_lang', array('label' => $this->_('Language'), 'multiOptions' => $this->util->getLocalized()->getLanguages())); - } - + /** + * Hook to perform action after a record (with changes) was saved + * + * As the data was already saved, it can NOT be changed anymore + * + * @param array $data + * @param boolean $isNew + * @return boolean True when you want to display the default 'saved' messages + */ public function afterSave(array $data, $isNew) { - $this->escort->loadLoginInfo($data['gus_login']); + // Reload the current user data + $this->loader->getUser($data['gsf_login'], $data['gsf_id_organization']); } public function changePasswordAction() @@ -87,20 +75,26 @@ *************/ $form = $this->createForm(); - $sql = "SELECT CASE WHEN gus_password IS NULL THEN 0 ELSE 1 END FROM gems__users WHERE gus_id_user = ? AND gus_id_organization = ?"; - if ($this->db->fetchOne($sql, array($this->session->user_id, $this->session->user_organization_id))) { - // Veld current password + $user = $this->loader->getCurrentUser(); + + $sql = "SELECT CASE WHEN gup_password IS NULL THEN 0 ELSE 1 END FROM gems__user_passwords WHERE gup_id_user = ?"; + + if ($this->db->fetchOne($sql, $user->getUserLoginId())) { + // Field current password + // + // This is only used when the password is already set, which may not always be the case + // e.g. when using embedded login in Pulse. $element = new Zend_Form_Element_Password('old_password'); $element->setLabel($this->_('Current password')); $element->setAttrib('size', 10); $element->setAttrib('maxlength', 20); $element->setRenderPassword(true); $element->setRequired(true); - $element->addValidator(new Gems_Validate_GemsPasswordUsername($this->session->user_login, 'old_password', $this->db)); + $element->addValidator(new Gems_User_UserPasswordValidator($user, $this->translate)); $form->addElement($element); } - // Veld new password + // Field new password $element = new Zend_Form_Element_Password('new_password'); $element->setLabel($this->_('New password')); $element->setAttrib('size', 10); @@ -111,7 +105,7 @@ $element->addValidator(new MUtil_Validate_IsConfirmed('repeat_password', $this->_('Repeat password'))); $form->addElement($element); - // Veld repeat password + // Field repeat password $element = new Zend_Form_Element_Password('repeat_password'); $element->setLabel($this->_('Repeat password')); $element->setAttrib('size', 10); @@ -131,20 +125,15 @@ ****************/ if ($this->_request->isPost() && $form->isValid($_POST)) { - $data['gus_id_user'] = $this->session->user_id; - $data['gus_id_organization'] = $this->session->user_organization_id; - $data['gus_password'] = $this->escort->passwordHash(null, $_POST['new_password']); + $data['gup_id_user'] = $user->getUserLoginId(); + $data['gup_password'] = $this->project->getValueHash($_POST['new_password']); - $this->getModel()->save($data); + $model = new MUtil_Model_TableModel('gems__user_passwords'); + Gems_Model::setChangeFieldsByPrefix($model, 'gup', $user->getUserId()); - // $data = $_POST; - // $data['name'] = ''; - // $data['type'] = $this->_('raw'); - - // $results = array(); - // $this->_runScript($data, $results); + $model->save($data); $this->addMessage($this->_('New password is active.')); - $this->afterSaveRoute($this->getRequest()); + $this->_reroute(array($this->getRequest()->getActionKey() => 'edit')); } else { if (isset($_POST['old_password'])) { @@ -185,23 +174,23 @@ */ public function createModel($detailed, $action) { - $model = new Gems_Model_UserModel('staff', 'gems__staff', array('gus_id_user' => 'gsf_id_user'), 'gsf'); - $model->copyKeys(); + $model = $this->loader->getModels()->getStaffModel(); - $model->set('gus_login', 'label', $this->_('Login Name')); - $model->set('gsf_email', 'label', $this->_('E-Mail')); - $model->set('gsf_first_name', 'label', $this->_('First name')); - $model->set('gsf_surname_prefix', 'label', $this->_('Surname prefix'), 'description', 'de, van der, \'t, etc...'); - $model->set('gsf_last_name', 'label', $this->_('Last name'), 'required', true); + $model->set('gsf_login', 'label', $this->_('Login Name'), 'elementClass', 'Exhibitor'); + $model->set('gsf_email', 'label', $this->_('E-Mail'), 'size', 30); + $model->set('gsf_first_name', 'label', $this->_('First name')); + $model->set('gsf_surname_prefix', 'label', $this->_('Surname prefix'), 'description', 'de, van der, \'t, etc...'); + $model->set('gsf_last_name', 'label', $this->_('Last name'), 'required', true); + $model->set('gsf_gender', 'label', $this->_('Gender'), 'multiOptions', $this->util->getTranslated()->getGenders(), + 'elementClass', 'Radio', 'separator', ''); + $model->set('gsf_iso_lang', 'label', $this->_('Language'), 'multiOptions', $this->util->getLocalized()->getLanguages()); - $model->set('gsf_gender', 'label', $this->_('Gender'), 'multiOptions', $this->util->getTranslated()->getGenders()); - return $model; } public function editAction() { - $this->getModel()->setFilter(array('gus_id_user' => $this->session->user_id)); + $this->getModel()->setFilter(array('gsf_id_user' => $this->loader->getCurrentUser()->getUserId())); if ($form = $this->processForm()) { $this->html->h3(sprintf($this->_('Options'), $this->getTopic())); @@ -222,7 +211,7 @@ WHERE glac.glac_name = 'index.login' ORDER BY glua.glua_created DESC LIMIT 10"; - $activity = $this->db->fetchAll($sql, $this->session->user_id); + $activity = $this->db->fetchAll($sql, $this->loader->getCurrentUser()->getUserId()); foreach (array_keys($activity) as $key) { $date = new MUtil_Date($activity[$key]['glua_created']); Modified: branches/newUser2/classes/Gems/Default/OrganizationAction.php =================================================================== --- branches/newUser2/classes/Gems/Default/OrganizationAction.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/Default/OrganizationAction.php 2011-11-10 17:15:11 UTC (rev 200) @@ -50,7 +50,7 @@ $url = base64_decode($request->getParam('current_uri')); $oldOrgId = $this->session->user_organization_id; - $allowedOrganizations = $this->escort->getAllowedOrganizations(); + $allowedOrganizations = $this->loader->getCurrentUser()->getAllowedOrganizations(); if ($orgId = array_search($org, $allowedOrganizations)) { $this->session->user_organization_id = $orgId; $this->session->user_organization_name = $allowedOrganizations[$orgId]; Modified: branches/newUser2/classes/Gems/Project/Organization/MultiOrganizationInterface.php =================================================================== --- branches/newUser2/classes/Gems/Project/Organization/MultiOrganizationInterface.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/Project/Organization/MultiOrganizationInterface.php 2011-11-10 17:15:11 UTC (rev 200) @@ -1,34 +1,34 @@ <?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - + /** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** * Marker interface for Pulse Projects having respondents * in only multiple organization. * @@ -49,9 +49,4 @@ interface Gems_Project_Organization_MultiOrganizationInterface { public function getUserOrganization(); - - /** - * Return an array of OrgId=>OrgName with all organizations the currently logged in user has access to - */ - public function getAllowedOrganizations($userId = null); } Modified: branches/newUser2/classes/Gems/User/User.php =================================================================== --- branches/newUser2/classes/Gems/User/User.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/User/User.php 2011-11-10 17:15:11 UTC (rev 200) @@ -191,9 +191,19 @@ if (! $this->_vars instanceof Zend_Session_Namespace) { $sessionStore = $this->session; - if (($sessionStore->__get('user_name') == $this->_vars->offsetGet('user_name')) && - ($sessionStore->__get('user_organization_id') == $this->_vars->offsetGet('user_organization_id'))) { + $notCurrent = true; + foreach (array('user_id', 'user_organization_id') as $key) { + if ($sessionStore->__isset($key) && $this->_vars->offsetGet($key)) { + $notCurrent = $sessionStore->__get($key) != $this->_vars->offsetGet($key); + } else { + $notCurrent = $sessionStore->__isset($key) || $this->_vars->offsetGet($key); + } + if ($notCurrent) { + break; + } + } + if (! $notCurrent) { // When this is the case, use the Zend_Session_Namespace object with the current set values // This way changes to this user object are reflected in the CurrentUser object and vice versa. $this->setAsCurrentUser(); @@ -203,6 +213,16 @@ } /** + * Get an array of OrgId => Org Name for all allowed organizations for the current loggedin user + * + * @return array + */ + public function getAllowedOrganizations() + { + return $this->_getVar('allowedOrgs'); + } + + /** * Returns the full user name (first, prefix, last). * * @return string @@ -251,7 +271,12 @@ } /** + * Returns the user id, that identifies this user within this installation. * + * One user id might be connected to multiple logins for multiple organizations. + * + * YES! This is the one you need, not getUserLoginId(). + * * @return int */ public function getUserId() @@ -260,7 +285,24 @@ } /** + * Returns the User package user id, that is unique for each login / organization id + * combination, but does not directly identify this person. * + * In other words, this is not the id you use to track who changed what. It is only + * used by parts of the User package. + * + * @return int + */ + public function getUserLoginId() + { + if ($this->_hasVar('user_login_id')) { + return $this->_getVar('user_login_id'); + } + return 0; + } + + /** + * * @return boolean True when a user can log in. */ public function isActive() @@ -279,6 +321,16 @@ } /** + * True when this user requires a logout after answering a survey + * + * @return boolean + */ + public function isLogoutOnSurvey() + { + return (boolean) $this->_getVar('user_logout'); + } + + /** * Set this user as the current user. * * This means that the data about this user will be stored in a session. Modified: branches/newUser2/classes/Gems/User/UserLoader.php =================================================================== --- branches/newUser2/classes/Gems/User/UserLoader.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/Gems/User/UserLoader.php 2011-11-10 17:15:11 UTC (rev 200) @@ -268,6 +268,16 @@ } /** + * Get an array of OrgId => Org Name for all allowed organizations for the current loggedin user + * + * @return array + */ + public function getAllowedOrganizations() + { + return $this->db->fetchPairs("SELECT gor_id_organization, gor_name FROM gems__organizations WHERE gor_active = 1 ORDER BY gor_name"); + } + + /** * Get the currently loggin in user * * @return Gems_User_User @@ -319,7 +329,8 @@ ->where('gsf_login = ?') ->limit(1); - //For a multi-layout project we need to select the appropriate style too + // For a multi-layout project we need to select the appropriate style too, + // but as PATCHES may not be in effect we have to try two selects $select2 = clone $select; $select2->columns(array('user_style' => 'gor_style'), 'gems__organizations'); @@ -370,8 +381,9 @@ protected function getStaffUser($login_name, $organization) { $select = new Zend_Db_Select($this->db); - $select->from('gems__user_logins', array('user_login' => 'gul_login')) + $select->from('gems__user_logins', array('user_login_id' => 'gul_id_user')) ->join('gems__staff', 'gul_login = gsf_login AND gul_id_organization = gsf_id_organization', array( + 'user_login' => 'gsf_login', 'user_id' => 'gsf_id_user', 'user_email'=>'gsf_email', 'user_group'=>'gsf_id_primary_group', @@ -433,6 +445,11 @@ $values['user_active'] = true; } + if (! isset($values['allowedOrgs'])) { + //Load the allowed organizations + $values['allowedOrgs'] = $this->getAllowedOrganizations(); + } + return $this->_loadClass('User', true, array($values)); } Added: branches/newUser2/classes/Gems/User/UserPasswordValidator.php =================================================================== --- branches/newUser2/classes/Gems/User/UserPasswordValidator.php (rev 0) +++ branches/newUser2/classes/Gems/User/UserPasswordValidator.php 2011-11-10 17:15:11 UTC (rev 200) @@ -0,0 +1,118 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package Gems + * @subpackage User + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id: Sample.php 203 2011-07-07 12:51:32Z matijs $ + */ + +/** + * + * + * @package Gems + * @subpackage User + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +class Gems_User_UserPasswordValidator implements Zend_Validate_Interface +{ + /** + * + * @var Gems_User_User + */ + private $_user; + + /** + * + * @var Zend_Translate + */ + private $_translate; + + /** + * + * @var boolean + */ + private $_valid = false; + + /** + * + * @param Gems_User_User $user The user to check + * @param Zend_Translate $translate Optional translator + */ + public function __construct(Gems_User_User $user, Zend_Translate $translate = null) + { + $this->_user = $user; + $this->_translate = $translate ? $translate : new MUtil_Translate_Adapter_Potemkin(); + } + + /** + * Returns true if and only if $value meets the validation requirements + * + * If $value fails validation, then this method returns false, and + * getMessages() will return an array of messages that explain why the + * validation failed. + * + * @param mixed $value + * @param mixed $content + * @return boolean + * @throws Zend_Validate_Exception If validation of $value is impossible + */ + public function isValid($value, $context = array()) + { + $this->_valid = $this->_user->checkPassword($value); + + return $this->_valid; + } + + /** + * Returns an array of messages that explain why the most recent isValid() + * call returned false. The array keys are validation failure message identifiers, + * and the array values are the corresponding human-readable message strings. + * + * If isValid() was never called or if the most recent isValid() call + * returned true, then this method returns an empty array. + * + * @return array + */ + public function getMessages() + { + if ($this->_valid) { + return array(); + + } else { + return array($this->_translate->_('Wrong password.')); + } + + + } +} Modified: branches/newUser2/classes/GemsEscort.php =================================================================== --- branches/newUser2/classes/GemsEscort.php 2011-11-10 15:47:29 UTC (rev 199) +++ branches/newUser2/classes/GemsEscort.php 2011-11-10 17:15:11 UTC (rev 200) @@ -791,7 +791,7 @@ 'controller' => 'organization', 'action' => 'change-ui'), null, true); $orgSwitch->raw('<form method="get" action="' . $url . '"><div><input type="hidden" name="current_uri" value="' . $currentUri . '" /><select name="org" onchange="javascript:this.form.submit();">'); - foreach ($this->getAllowedOrganizations() as $id => $org) { + foreach ($this->getLoader()->getCurrentUser()->getAllowedOrganizations() as $id => $org) { $selected = ''; if ($id == $this->session->user_organization_id) { $selected = ' selected="selected"'; @@ -924,43 +924,6 @@ } } - public function afterLogin($userName = null) - { - if (empty($userName)) { - $userName = $_POST['userlogin']; - } - - /** - * Reset number of failed logins - */ - try { - $sql = "UPDATE gems__user_logins SET gul_failed_logins = 0, gul_last_failed = NULL, gul_changed = CURRENT_TIMESTAMP WHERE gul_login = ?"; - $this->db->query($sql, array($userName)); - } catch (Exception $e) { - // swallow exception - } - } - - public function afterFailedLogin() - { - /** - * Store the failed login attempt - */ - try { - if (isset($_POST['userlogin'])) { - $sql = "UPDATE gems__user_logins SET gul_failed_logins = gul_failed_logins + 1, gul_last_failed = NOW(), gul_changed = CURRENT_TIMESTAMP WHERE gul_login = ?"; - $this->db->query($sql, array($_POST['userlogin'])); - } - } catch (Exception $e) { - // swallow exception - } - } - - public function afterLogout() - { - $this->session->unsetAll(); - } - /** * Hook 2: Called in $this->run(). * @@ -1091,27 +1054,7 @@ } /** - * Get an array of OrgId => Org Name for all allowed organizations for the current loggedin user * - * @@TODO Make ui to store allowed orgs in staff controller and change function to read these - * - * @return array - */ - public function getAllowedOrganizations($userId = null) - { - if (is_null($userId)) $userId = $this->session->user_id; - if ($userId == $this->session->user_id && isset($this->session->allowedOrgs)) { - //If user is current user, read from session - $allowedOrganizations = $this->session->allowedOrgs; - } else { - $allowedOrganizations = $this->db->fetchPairs("SELECT gor_id_organization, gor_name FROM gems__organizations WHERE gor_active = 1 ORDER BY gor_name"); - } - - return $allowedOrganizations; - } - - /** - * * @return int The current active organization id or 0 when not known */ public function getCurrentOrganization() @@ -1137,11 +1080,9 @@ */ public function getCurrentUserId() { - if (isset($this->session->user_id)) { - return $this->session->user_id; - } else { - return 0; - } + $id = $this->getLoader()->getCurrentUser()->getUserId(); + + return $id ? $id : 0; } public function getDatabasePaths() @@ -1270,52 +1211,11 @@ return false; } - public function loadLoginInfo($userName) - { - /** - * Read the needed parameters from the different tables, lots of renames for backward - * compatibility - */ - $select = new Zend_Db_Select($this->db); - $select->from('gems__staff', array( - 'user_id' => 'gsf_id_user', - 'user_login' => 'gsf_login', - 'user_email'=>'gsf_email', - 'user_group'=>'gsf_id_primary_group', - 'user_locale'=>'gsf_iso_lang', - 'user_logout'=>'gsf_logout_on_survey')) - ->columns(array('user_name'=>"(concat(coalesce(concat(`gems__staff`.`gsf_first_name`,_utf8' '),_utf8''),coalesce(concat(`gems__staff`.`gsf_surname_prefix`,_utf8' '),_utf8''),coalesce(`gems__staff`.`gsf_last_name`,_utf8'')))")) - ->join('gems__groups', 'gsf_id_primary_group = ggp_id_group', array('user_role'=>'ggp_role')) - ->join('gems__organizations', 'gus_id_organization = gor_id_organization', - array('user_organization_id'=>'gor_id_organization', 'user_organization_name'=>'gor_name')) - ->where('ggp_group_active = ?', 1) - ->where('gor_active = ?', 1) - ->where('gus_active = ?', 1) - ->where('gus_login = ?', $userName) - ->limit(1); - - //For a multi-layout project we need to select the appropriate style too - if ($this instanceof Gems_Project_Layout_MultiLayoutInterface) { - $select->columns(array('user_style' => 'gor_style'), 'gems__organizations'); - } - - - if ($result = $this->db->fetchRow($select, array(), Zend_Db::FETCH_ASSOC)) { - // $this->session is a session object so we cannot use $this->session = $result - foreach ($result as $name => $value) { - $this->session->$name = $value; - } - - if ($this instanceof Gems_Project_Organization_MultiOrganizationInterface) { - //Load the allowed organizations into the session - $this->session->allowedOrgs = $this->getAllowedOrganizations(); - } - } - } - /** * Return a hashed version of the input value. * + * @deprecated Since 1.5 + * * @param string $name Optional name, is here for ModelAbstract setOnSave compatibility * @param string $value The value to hash. * @param boolean $new Optional is new, is here for ModelAbstract setOnSave compatibility This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |