From: <gem...@li...> - 2012-03-29 17:08:41
|
Revision: 577 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=577&view=rev Author: matijsdejong Date: 2012-03-29 17:08:31 +0000 (Thu, 29 Mar 2012) Log Message: ----------- Organization base url now determines the layout when not logged in and url confirms Temp error messages out in IndexAction.php needs different solution updated fields from gems__organizations.20.sql (not all earlier patches came through to this definition) Added filters to FormBridge Added documentation Modified Paths: -------------- trunk/library/classes/Gems/Default/IndexAction.php trunk/library/classes/Gems/Default/OrganizationAction.php trunk/library/classes/Gems/Filter/DutchZipcode.php trunk/library/classes/Gems/User/Form/LoginForm.php trunk/library/classes/Gems/User/UserLoader.php trunk/library/classes/Gems/Util/DbLookup.php trunk/library/classes/MUtil/Form.php trunk/library/classes/MUtil/Model/FormBridge.php trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php trunk/library/configs/db/tables/gems__organizations.20.sql trunk/library/snippets/Organization/OrganizationEditSnippet.php Added Paths: ----------- trunk/library/classes/Gems/Filter/TrailingSlash.php Modified: trunk/library/classes/Gems/Default/IndexAction.php =================================================================== --- trunk/library/classes/Gems/Default/IndexAction.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/Gems/Default/IndexAction.php 2012-03-29 17:08:31 UTC (rev 577) @@ -298,6 +298,7 @@ /** * Fix current locale in cookies */ + MUtil_Echo::track($user->getLocale(), $this->basepath->getBasePath()); Gems_Cookies::setLocale($user->getLocale(), $this->basepath->getBasePath()); /** @@ -320,10 +321,12 @@ $this->_reroute(array('controller' => null, 'action' => null), true); } return; - } else { + } /* + else { //Now present the user with an error message - $errors = $form->getErrorMessages(); - $this->addMessage($errors); + // $errors = MUtil_Ra::flatten($form->getMessages()); + // $this->addMessage($errors); + MUtil_Echo::track($errors); //Also log the error to the log table //when the project has logging enabled @@ -331,7 +334,7 @@ $msg = sprintf('Failed login for : %s (%s) - %s', $request->getParam($form->usernameFieldName), $request->getParam($form->organizationFieldName), $logErrors); $log = Gems_AccessLog::getLog(); $log->log('loginFail', $this->getRequest(), $msg, null, true); - } + } // */ } $this->view->form = $form; } Modified: trunk/library/classes/Gems/Default/OrganizationAction.php =================================================================== --- trunk/library/classes/Gems/Default/OrganizationAction.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/Gems/Default/OrganizationAction.php 2012-03-29 17:08:31 UTC (rev 577) @@ -115,15 +115,18 @@ $model->set('gor_contact_name', 'label', $this->_('Contact name'), 'size', 25); $model->set('gor_contact_email', 'label', $this->_('Contact email'), 'size', 50, 'validator', 'SimpleEmail'); if ($this->escort instanceof Gems_Project_Layout_MultiLayoutInterface) { - $model->setIfExists( - 'gor_style', 'label', $this->_('Style'), + $model->setIfExists('gor_style', + 'label', $this->_('Style'), 'multiOptions', MUtil_Lazy::call(array($this->escort, 'getStyles')) ); - $model->setIfExists( - 'gor_url_base', 'label', $this->_('Default url'), + $model->setIfExists('gor_url_base', + 'label', $this->_("Default url's"), 'size', 50, - 'description', sprintf($this->_('Always switch to this organization when %s is accessed from this url'), $this->project->getName()) + 'description', sprintf($this->_("Always switch to this organization when %s is accessed from one of these space separated url's. The first is used for mails."), $this->project->getName()) ); + if ($detailed) { + $model->setIfExists('gor_url_base', 'filter', 'TrailingSlash'); + } } $model->set( 'gor_iso_lang', 'label', $this->_('Language'), Modified: trunk/library/classes/Gems/Filter/DutchZipcode.php =================================================================== --- trunk/library/classes/Gems/Filter/DutchZipcode.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/Gems/Filter/DutchZipcode.php 2012-03-29 17:08:31 UTC (rev 577) @@ -1,31 +1,31 @@ <?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 + +/** + * 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. - * + * * @version $Id$ * @package Gems * @subpackage Filter @@ -39,9 +39,16 @@ * @subpackage Filter * @copyright Copyright (c) 2011 Erasmus MC * @license New BSD License - */ + */ class Gems_Filter_DutchZipcode implements Zend_Filter_Interface { + /** + * Returns the result of filtering $value + * + * @param mixed $value + * @throws Zend_Filter_Exception If filtering $value is impossible + * @return mixed + */ public function filter($value) { // perform some transformation upon $value to arrive on $valueFiltered @@ -51,7 +58,7 @@ $valueFiltered = substr_replace($valueFiltered, ' ', 4, 0); } } - + return $valueFiltered; } } Added: trunk/library/classes/Gems/Filter/TrailingSlash.php =================================================================== --- trunk/library/classes/Gems/Filter/TrailingSlash.php (rev 0) +++ trunk/library/classes/Gems/Filter/TrailingSlash.php 2012-03-29 17:08:31 UTC (rev 577) @@ -0,0 +1,68 @@ +<?php + +/** + * Copyright (c) 2012, 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 Filter + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: TrailingSlash.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * Removes slashes from both the end of string and words + * + * @package Gems + * @subpackage Filter + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +class Gems_Filter_TrailingSlash implements Zend_Filter_Interface +{ + /** + * Returns the result of filtering $value + * + * @param mixed $value + * @throws Zend_Filter_Exception If filtering $value is impossible + * @return mixed + */ + public function filter($value) + { + $values = explode(' ', $value); + + foreach ($values as &$val) { + if (substr($val, -1) === '/') { + $val = substr($val, 0, -1); + } + } + + return implode(' ', $values); + } +} Modified: trunk/library/classes/Gems/User/Form/LoginForm.php =================================================================== --- trunk/library/classes/Gems/User/Form/LoginForm.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/Gems/User/Form/LoginForm.php 2012-03-29 17:08:31 UTC (rev 577) @@ -155,13 +155,12 @@ */ public function getCurrentOrganizationId() { - // Url determines organization first. - $uri = $this->util->getCurrentURI(); - // MUtil_Echo::track($uri); + $userLoader = $this->loader->getUserLoader(); - if ($orgId = $this->util->getDbLookup()->getOrganizationForUrl($uri)) { + // Url determines organization first. + if ($orgId = $userLoader->getOrganizationIdByUrl()) { $this->_organizationFromUrl = true; - $this->loader->getCurrentUser()->setCurrentOrganization($orgId); + $userLoader->getCurrentUser()->setCurrentOrganization($orgId); return $orgId; } @@ -170,7 +169,7 @@ return $orgId; } - return $this->loader->getCurrentUser()->getCurrentOrganizationId(); + return $userLoader->getCurrentUser()->getCurrentOrganizationId(); } /** Modified: trunk/library/classes/Gems/User/UserLoader.php =================================================================== --- trunk/library/classes/Gems/User/UserLoader.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/Gems/User/UserLoader.php 2012-03-29 17:08:31 UTC (rev 577) @@ -68,6 +68,12 @@ public $allowStaffEmailLogin = true; /** + * + * @var Zend_Cache_Core + */ + protected $cache; + + /** * Allows sub classes of Gems_Loader_LoaderAbstract to specify the subdirectory where to look for. * * @var string $cascade An optional subdirectory where this subclass always loads from. @@ -104,6 +110,12 @@ protected $translate; /** + * + * @var Gems_Util + */ + protected $util; + + /** * There can be only one, current user that is. * * @var Gems_User_User @@ -268,7 +280,7 @@ if (substr($defName, -10, 10) != 'Definition') { $defName .= 'Definition'; } - + self::$currentUser = $this->_loadClass('User', true, array($this->session, $this->_getClass($defName))); } else { self::$currentUser = $this->getUser(null, null); @@ -302,7 +314,7 @@ * Returns an organization object, initiated from the database or from * self::$_noOrganization when the database does not yet exist. * - * @param int $organizationId Optional, uses current user when empty + * @param int $organizationId Optional, uses current user or url when empty * @return Gems_User_Organization */ public function getOrganization($organizationId = null) @@ -310,7 +322,15 @@ static $organizations = array(); if (null === $organizationId) { - $organizationId = intval(self::getCurrentUser()->getCurrentOrganizationId()); + $user = $this->getCurrentUser(); + + if (! $user->isActive()) { + $organizationId = $this->getOrganizationIdByUrl(); + } + + if (! $organizationId) { + $organizationId = intval($user->getCurrentOrganizationId()); + } } if (! isset($organizations[$organizationId])) { @@ -321,6 +341,49 @@ } /** + * Returns the current organization according to the current site url. + * + * @static array $url An array of url => orgId values + * @return int An organization id or null + */ + public function getOrganizationIdByUrl() + { + static $urls; + + if (! is_array($urls)) { + if ($this->cache) { + $cacheId = GEMS_PROJECT_NAME . '__' . get_class($this) . '__organizations_url'; + $urls = $this->cache->load($cacheId); + } else { + $cacheId = false; + } + + if (! $urls) { + $data = $this->db->fetchPairs("SELECT gor_id_organization, gor_url_base FROM gems__organizations WHERE gor_active=1 AND gor_url_base IS NOT NULL"); + $urls = array(); + foreach ($data as $orgId => $urlsBase) { + foreach (explode(' ', $urlsBase) as $url) { + if ($url) { + $urls[$url] = $orgId; + } + } + } + + if ($cacheId) { + $this->cache->save($urls, $cacheId, array('organization', 'organizations')); + } + } + // MUtil_Echo::track($urls); + } + + $current = $this->util->getCurrentURI(); + + if (isset($urls[$current])) { + return $urls[$current]; + } + } + + /** * Get password weakness checker. * * @return Gems_User_PasswordChecker Modified: trunk/library/classes/Gems/Util/DbLookup.php =================================================================== --- trunk/library/classes/Gems/Util/DbLookup.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/Gems/Util/DbLookup.php 2012-03-29 17:08:31 UTC (rev 577) @@ -280,21 +280,6 @@ return $organizations; } - /** - * Returns the organization - * @param string $url - * @return int|null the organization - */ - public function getOrganizationForUrl($url) - { - try { - $url = trim($this->db->quote($url), "'"); - return $this->db->fetchOne("SELECT gor_id_organization FROM gems__organizations WHERE gor_active=1 AND CONCAT(' ', gor_url_base, ' ') LIKE '% $url %'"); - } catch (Exception $e) { - return null; - } - } - public function getRoles() { $roles = array(); Modified: trunk/library/classes/MUtil/Form.php =================================================================== --- trunk/library/classes/MUtil/Form.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/MUtil/Form.php 2012-03-29 17:08:31 UTC (rev 577) @@ -35,7 +35,7 @@ */ /** - * + * * @package MUtil * @subpackage Form * @copyright Copyright (c) 2011 Erasmus MC Modified: trunk/library/classes/MUtil/Model/FormBridge.php =================================================================== --- trunk/library/classes/MUtil/Model/FormBridge.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/MUtil/Model/FormBridge.php 2012-03-29 17:08:31 UTC (rev 577) @@ -107,9 +107,17 @@ } } + /** + * Add the element to the for and apply any filters & validators + * + * @param string $name + * @param Zend_Form_Element $element + * @return Zend_Form_Element + */ protected function _addToForm($name, Zend_Form_Element $element) { $this->form->addElement($element); + $this->_applyFilters($name, $element); $this->_applyValidators($name, $element); // MUtil_Echo::r($element->getOrder(), $element->getName()); @@ -117,6 +125,41 @@ return $element; } + /** + * Apply the filters for element $name to the element + * + * @param string $name + * @param Zend_Form_Element $element + */ + protected function _applyFilters($name, Zend_Form_Element $element) + { + $filters = $this->model->get($name, 'filters'); + + if ($filter = $this->model->get($name, 'filter')) { + if ($filters) { + array_unshift($filters, $filter); + } else { + $filters = array($filter); + } + } + + if ($filters) { + foreach ($filters as $filter) { + if (is_array($filter)) { + call_user_func_array(array($element, 'addFilter'), $filter); + } else { + $element->addFilter($filter); + } + } + } + } + + /** + * Apply the validators for element $name to the element + * + * @param string $name + * @param Zend_Form_Element $element + */ protected function _applyValidators($name, Zend_Form_Element $element) { $validators = $this->model->get($name, 'validators'); @@ -505,6 +548,7 @@ } $element = new Zend_Form_Element_Password($name, $options); + $this->_applyFilters($name, $element); $this->_applyValidators($name, $element); $this->form->addElement($element); @@ -515,6 +559,7 @@ if (isset($repeatLabel)) { $repeatElement = new Zend_Form_Element_Password($repeatName, $repeatOptions); $this->form->addElement($repeatElement); + $this->_applyFilters($name, $repeatElement); if ($stringlength) { $repeatElement->addValidator('StringLength', true, $stringlength); Modified: trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php =================================================================== --- trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php 2012-03-29 17:08:31 UTC (rev 577) @@ -110,6 +110,13 @@ return $this; } + + /** + * Displays the content + * + * @param string $value + * @return string + */ public function format($value) { // MUtil_Echo::track($value); Modified: trunk/library/configs/db/tables/gems__organizations.20.sql =================================================================== --- trunk/library/configs/db/tables/gems__organizations.20.sql 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/configs/db/tables/gems__organizations.20.sql 2012-03-29 17:08:31 UTC (rev 577) @@ -2,11 +2,13 @@ CREATE TABLE if not exists gems__organizations ( gor_id_organization bigint unsigned not null auto_increment, - gor_name varchar(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null, - gor_code varchar(20) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, - gor_location varchar(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, - gor_url varchar(127) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, - gor_task varchar(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gor_name varchar(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null, + gor_code varchar(20) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gor_user_class varchar(30) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null default 'StaffUser' + gor_location varchar(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gor_url varchar(127) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gor_url_base varchar(1270) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + gor_task varchar(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, -- A commy separated list of organization numbers that can look at respondents in this organization gor_accessible_by text CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, Modified: trunk/library/snippets/Organization/OrganizationEditSnippet.php =================================================================== --- trunk/library/snippets/Organization/OrganizationEditSnippet.php 2012-03-29 14:39:03 UTC (rev 576) +++ trunk/library/snippets/Organization/OrganizationEditSnippet.php 2012-03-29 17:08:31 UTC (rev 577) @@ -48,6 +48,12 @@ { /** * + * @var Zend_Cache_Core + */ + protected $cache; + + /** + * * @var Gems_Loader */ protected $loader; @@ -72,7 +78,7 @@ } //Need the first two together for validation $bridge->addHtml('org')->b($this->_('Organization')); - $this->addItems($bridge, 'gor_name', 'gor_id_organization', 'gor_location', 'gor_url', 'gor_active'); + $this->addItems($bridge, 'gor_name', 'gor_id_organization', 'gor_location', 'gor_url', 'gor_url_base', 'gor_active'); $bridge->addHtml('contact')->b($this->_('Contact')); $bridge->addHtml('contact_desc')->i($this->_('The contact details for this organization, used for emailing.')); $this->addItems($bridge, 'gor_contact_name', 'gor_contact_email'); @@ -103,7 +109,9 @@ //Strip self unset($allowedOrgs[$this->formData['gor_id_organization']]); $display = join(', ', $allowedOrgs); - $bridge->addExhibitor('allowed', 'value', $display, 'label', $this->_('Can access')); + if ($display) { + $bridge->addExhibitor('allowed', 'value', $display, 'label', $this->_('Can access')); + } } $this->addItems($bridge, 'gor_user_class'); @@ -126,8 +134,7 @@ public function afterSave($changed) { - $org = $this->loader->getOrganization($changed['gor_id_organization']); - $org->invalidateCache(); + $this->cache->clean('all', array('organization', 'organizations')); // Make sure any changes in the allowed list are reflected. $this->loader->getCurrentUser()->refreshAllowedOrganizations(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |