From: <gem...@li...> - 2011-11-03 17:53:27
|
Revision: 176 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=176&view=rev Author: matijsdejong Date: 2011-11-03 17:53:20 +0000 (Thu, 03 Nov 2011) Log Message: ----------- Add user changes for #31 Added Paths: ----------- branches/newUser2/classes/Gems/User/User.php trunk/library/configs/db/tables/gems__user_ids.10.sql trunk/library/configs/db/tables/gems__user_logins.10.sql trunk/library/configs/db/tables/gems__user_passwords.50.sql Added: branches/newUser2/classes/Gems/User/User.php =================================================================== --- branches/newUser2/classes/Gems/User/User.php (rev 0) +++ branches/newUser2/classes/Gems/User/User.php 2011-11-03 17:53:20 UTC (rev 176) @@ -0,0 +1,293 @@ +<?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 <COPYRIGHT HOLDER> 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$ + */ + +/** + * User object that mimicks the old $this->session behaviour + * + * @package Gems + * @subpackage User + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +class Gems_User_User extends MUtil_Registry_TargetAbstract +{ + /** + * + * @var ArrayObject or Zend_Session_Namespace + */ + private $_vars; + + /** + * + * @var Gems_User_UserLoader + */ + protected $userLoader; + + /** + * Creates the class for this user. + * + * @param mixed $settings Array, Zend_Session_Namespace or ArrayObject for this user. + */ + public function __construct($settings) + { + if (is_array($settings)) { + $this->_vars = new ArrayObject($settings); + $this->_vars->setFlags(ArrayObject::STD_PROP_LIST); + } else { + $this->_vars = $settings; + } + + // Checks if this is the current user + if (! $this->_vars instanceof Zend_Session_Namespace) { + $sessionStore = self::_getSessionStore(); + + if (($sessionStore->__get('user_name') == $this->_vars->offsetGet('user_name')) && + ($sessionStore->__get('user_organization_id') == $this->_vars->offsetGet('user_organization_id'))) { + + // 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(); + } + } + } + + /** + * Get a variable + * + * @deprecated Since 1.5, left for backwards compatibility with $this->session + * @param string $name + * @return mixed + */ + public function __get($name) + { + return $this->_getVar($name); + } + + /** + * Check for existence of a variable + * + * @deprecated Since 1.5, left for backwards compatibility with $this->session + * @param string $name + * @return boolean + */ + public function __isset($name) + { + return $this->_hasVar($name); + } + + /** + * Sett a variable value + * + * @deprecated Since 1.5, left for backwards compatibility with $this->session + * @param string $name + * @param mixed $value + */ + public function __set($name, $value) + { + $this->_setVar($name, $value); + } + + /** + * unsets a variable + * + * @deprecated Since 1.5, left for backwards compatibility with $this->session + * @param string $name + * @return void + */ + public function __unset($name) + { + $this->_unsetVar($name); + } + + /** + * Returns the session namespace containing user data. + * + * @staticvar Zend_Session_Namespace $session + * @return Zend_Session_Namespace + */ + private static function _getSessionStore() + { + static $session; + + if (! $session) { + $session = new Zend_Session_Namespace('gems.' . GEMS_PROJECT_NAME . '.userdata', true); + } + + return $session; + } + + /** + * Get a value in whatever store is used by this object. + * + * @param string $name + * @return mixed + */ + protected function _getVar($name) + { + $store = $this->_getVariableStore(); + + if ($store instanceof Zend_Session_Namespace) { + if ($store->__isset($name)) { + return $store->__get($name); + } + } else { + if ($store->offsetExists($name)) { + return $store->offsetSet($name); + } + } + + return null; + } + + /** + * The store currently used. + * + * @return ArrayObject or Zend_Session_Namespace + */ + private function _getVariableStore() + { + return $this->_vars; + } + + /** + * Checks for existence of a value in whatever store is used by this object. + * + * @param string $name + * @return boolean + */ + protected function _hasVar($name) + { + $store = $this->_getVariableStore(); + + if ($store instanceof Zend_Session_Namespace) { + return $store->__isset($name); + } else { + return $store->offsetExists($name); + } + } + + /** + * Sets a value in whatever store is used by this object. + * + * @param string $name + * @param mixed $value + * @return void + */ + protected function _setVar($name, $value) + { + $store = $this->_getVariableStore(); + + if ($store instanceof Zend_Session_Namespace) { + $store->__set($name, $value); + } else { + $store->offsetSet($name, $value); + } + } + + /** + * Sets a value in whatever store is used by this object. + * + * @param string $name + * @return void + */ + protected function _unsetVar($name) + { + $store = $this->_getVariableStore(); + + if ($store instanceof Zend_Session_Namespace) { + $store->__unset($name); + } else { + $store->offsetUnset($name, $value); + } + } + + /** + * Returns the data for the current user from the session store. + * + * @return Zend_Session_Namespace + */ + public static function getCurrentUserData() + { + return self::_getSessionStore(); + } + + /** + * + * @return string + */ + protected function getLoginName() + { + return $this->getVar('user_login'); + } + + /** + * + * @return int + */ + protected function getOrganizationId() + { + return $this->_getVar('user_organization_id'); + } + + /** + * Set this user as the current user. + * + * This means that the data about this user will be stored in a session. + * + * @return Gems_User_UserAbstract + */ + public function setAsCurrentUser() + { + // Get the current variables + $oldStore = $this->_getVariableStore(); + + // When $oldStore is a Zend_Session_Namespace, then this user is already the current user. + if (! $oldStore instanceof Zend_Session_Namespace) { + $this->_vars = self::_getSessionStore(); + + // Clean up what is there now. + $this->_vars->unsetAll(); + + foreach ($oldStore as $name => $value) { + $this->_vars->__set($name, $value); + } + } + + return $this; + } +} Copied: trunk/library/configs/db/tables/gems__user_ids.10.sql (from rev 170, trunk/library/configs/db/tables/gems__users.10.sql) =================================================================== --- trunk/library/configs/db/tables/gems__user_ids.10.sql (rev 0) +++ trunk/library/configs/db/tables/gems__user_ids.10.sql 2011-11-03 17:53:20 UTC (rev 176) @@ -0,0 +1,12 @@ + +-- Table containing the users that are allowed to login +-- +CREATE TABLE if not exists gems__user_ids ( + gui_id_user bigint unsigned not null, + + gui_created timestamp not null, + + PRIMARY KEY (gui_id_user) + ) + ENGINE=InnoDB + CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; Copied: trunk/library/configs/db/tables/gems__user_logins.10.sql (from rev 170, trunk/library/configs/db/tables/gems__users.10.sql) =================================================================== --- trunk/library/configs/db/tables/gems__user_logins.10.sql (rev 0) +++ trunk/library/configs/db/tables/gems__user_logins.10.sql 2011-11-03 17:53:20 UTC (rev 176) @@ -0,0 +1,22 @@ + +-- Table containing the users that are allowed to login +-- +CREATE TABLE if not exists gems__user_logins ( + gul_id_user bigint unsigned not null auto_increment, + + gul_login varchar(30) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null, + gul_id_organization bigint not null references gems__organizations (gor_id_organization), + + gul_user_class varchar(30) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null default 'NoLogin', + + gul_changed timestamp not null default current_timestamp on update current_timestamp, + gul_changed_by bigint unsigned not null, + gul_created timestamp not null, + gul_created_by bigint unsigned not null, + + PRIMARY KEY (gsl_id_user), + UNIQUE (gsl_login, gsl_id_organization) + ) + ENGINE=InnoDB + AUTO_INCREMENT = 10001 + CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; Copied: trunk/library/configs/db/tables/gems__user_passwords.50.sql (from rev 170, trunk/library/configs/db/tables/gems__users.10.sql) =================================================================== --- trunk/library/configs/db/tables/gems__user_passwords.50.sql (rev 0) +++ trunk/library/configs/db/tables/gems__user_passwords.50.sql 2011-11-03 17:53:20 UTC (rev 176) @@ -0,0 +1,22 @@ + +-- Table containing the users that are allowed to login +-- +CREATE TABLE if not exists gems__user_passwords ( + gup_id_user bigint unsigned not null references gems__user_logins (gul_id_user), + + gup_password varchar(32) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gup_failed_logins int(11) unsigned not null default 0, + gup_last_failed timestamp null, + gup_reset_key varchar(64) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gup_reset_requested timestamp null, + gup_reset_required boolean not null default 0, + + gup_changed timestamp not null default current_timestamp on update current_timestamp, + gup_changed_by bigint unsigned not null, + gup_created timestamp not null, + gup_created_by bigint unsigned not null, + + PRIMARY KEY (gup_id_user) + ) + ENGINE=InnoDB + CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |