From: <gem...@li...> - 2012-06-07 13:21:06
|
Revision: 742 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=742&view=rev Author: matijsdejong Date: 2012-06-07 13:20:56 +0000 (Thu, 07 Jun 2012) Log Message: ----------- page + item are now also stored in RequestCache so you really return to the last page #534 After editing / creating a user, you go to 'reset password' form by defaultStaffDefinition + reset button in browse screen #325 Can no longer edit 'higher' role User object now hasAllowedRole() and empty name still generates some name text display Modified Paths: -------------- trunk/library/classes/Gems/Controller/BrowseEditAction.php trunk/library/classes/Gems/Default/StaffAction.php trunk/library/classes/Gems/Html.php trunk/library/classes/Gems/Menu/MenuAbstract.php trunk/library/classes/Gems/Model/StaffModel.php trunk/library/classes/Gems/Model.php trunk/library/classes/Gems/Snippets/ModelTableSnippetAbstract.php trunk/library/classes/Gems/User/User.php trunk/library/classes/GemsEscort.php trunk/library/classes/MUtil/Html/PagePanel.php Modified: trunk/library/classes/Gems/Controller/BrowseEditAction.php =================================================================== --- trunk/library/classes/Gems/Controller/BrowseEditAction.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Controller/BrowseEditAction.php 2012-06-07 13:20:56 UTC (rev 742) @@ -132,6 +132,7 @@ $request = $this->getRequest(); $search = $this->getCachedRequestData(false); $params = array('baseUrl' => $search); + // MUtil_Echo::track($search); // Load the filters $this->_applySearchParameters($model); @@ -164,9 +165,6 @@ $table = $this->getBrowseTable($search); $paginator = $model->loadPaginator(); $table->setRepeater($paginator); - - // Apply request cache to request, so pagination is preserved too - $request->setParams(array_intersect_key($search, array('page'=>1,'items'=>1))); $table->tfrow()->pagePanel($paginator, $request, $this->translate, $params); if (isset($sequence)) { @@ -617,7 +615,7 @@ $this->requestCache->setRequest($this->request); // Button text should not be stored. - $this->requestCache->removeParams(self::SEARCH_BUTTON, /*'page', 'items',*/ 'action'); + $this->requestCache->removeParams(self::SEARCH_BUTTON, 'action'); } $data = $this->requestCache->getProgramParams(); Modified: trunk/library/classes/Gems/Default/StaffAction.php =================================================================== --- trunk/library/classes/Gems/Default/StaffAction.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Default/StaffAction.php 2012-06-07 13:20:56 UTC (rev 742) @@ -44,6 +44,16 @@ */ class Gems_Default_StaffAction extends Gems_Controller_BrowseEditAction { + protected $_instanceId; + protected $_organizations; + + /** + * The current user for detailed actions, set by createModel() + * + * @var Gems_User_User + */ + protected $_user = false; + //@@TODO What if we want a different one per organization? //Maybe check if org has a default and otherwise use this one? public $defaultStaffDefinition = Gems_User_UserLoader::USER_STAFF; @@ -51,9 +61,6 @@ public $filterStandard = array('gsf_active' => 1); public $sortKey = array('name' => SORT_ASC); - protected $_instanceId; - protected $_organizations; - /** * Adds columns from the model to the bridge that creates the browse table. * @@ -106,10 +113,14 @@ } } } - // Add edit button if allowed, otherwise show, again if allowed + // Add edit button if allowed if ($menuItem = $this->findAllowedMenuItem('edit')) { $bridge->addItemLink($menuItem->toActionLinkLower($this->getRequest(), $bridge)); } + // Add reset button if allowed + if ($menuItem = $this->findAllowedMenuItem('reset')) { + $bridge->addItemLink($menuItem->toActionLink($this->getRequest(), $bridge, $this->_('reset'))); + } } /** @@ -128,24 +139,13 @@ { // Sorry, for the time being no password complexity checking on new // users. Can be done, but is to complex for the moment. - if ($new) { - $user = false; - } else { - $user = $this->loader->getUserLoader()->getUserByStaffId($data['gsf_id_user']); - // MUtil_Echo::track($data['gsf_id_user'], $user->getLoginName()); - } // Find out if this group is in the inheritance path of the current user - $allowedGroups = $this->util->getDbLookup()->getAllowedStaffGroups(); - if (!array_key_exists($data['gsf_id_primary_group'], $allowedGroups)) { - //Not allowed to update - $model->set('gsf_id_primary_group', 'elementClass', 'Exhibitor'); - } else { - //Allow only certain groups - $model->set('gsf_id_primary_group', 'multiOptions', $allowedGroups); - } + // and allow those certain groups + $model->set('gsf_id_primary_group', 'multiOptions', $this->util->getDbLookup()->getAllowedStaffGroups()); + if ($new) { - $model->set('gsf_id_primary_group', 'default', $dbLookup->getDefaultGroup()); + $model->set('gsf_id_primary_group', 'default', $this->util->getDbLookup()->getDefaultGroup()); } $ucfirst = new Zend_Filter_Callback('ucfirst'); @@ -302,8 +302,26 @@ */ public function createModel($detailed, $action) { + if ($detailed) { + // Make sure the user is loaded + $this->loadUser(); + + if ($this->_user) { + switch ($action) { + case 'create': + case 'show': + break; + + default: + if (! $this->_user->hasAllowedRole()) { + throw new Gems_Exception($this->_('No access to page'), 403, null, + sprintf($this->_('Access to this page is not allowed for current role: %s.'), $this->loader->getCurrentUser()->getRole())); + } + } + } + } + // MUtil_Model::$verbose = true; - $model = $this->loader->getModels()->getStaffModel(); $model->set('gsf_login', 'label', $this->_('Username')); @@ -340,6 +358,43 @@ return $model; } + /** + * Return an array with route options depending on de $data given. + * + * @param mixed $data array or Zend_Controller_Request_Abstract + * @return mixed array with route options or false when no redirect is found + */ + public function getAfterSaveRoute($data) + { + if (! $this->_user) { + $this->_user = $this->loader->getUser($data['gul_login'], $data['gul_id_organization']); + } + MUtil_Echo::track($this->_user->canSetPassword()); + + if ($this->_user->canSetPassword()) { + if ($currentItem = $this->menu->getCurrent()) { + $controller = $this->_getParam('controller'); + + if ($data instanceof Zend_Controller_Request_Abstract) { + $refData = $data; + $refData->setParam('accessible_role', $this->_user->hasAllowedRole()); + } elseif (is_array($data)) { + $refData = $this->getModel()->getKeyRef($data) + $data; + $refData['accessible_role'] = $this->_user->hasAllowedRole(); + } else { + throw new Gems_Exception_Coding('The variable $data must be an array or a Zend_Controller_Request_Abstract object.'); + } + + // Look for reset + if ($menuItem = $this->menu->findController($controller, 'reset')) { + return $menuItem->toRouteUrl($refData); + } + } + } + + return parent::getAfterSaveRoute($data, $isNew); + } + protected function getAutoSearchElements(MUtil_Model_ModelAbstract $model, array $data) { $elements = parent::getAutoSearchElements($model, $data); @@ -415,20 +470,17 @@ return $this->_('Staff'); } - public function init() + /** + * Load the user selected by the request - if any + */ + protected function loadUser() { - parent::init(); - - //Make sure the menu always has the gsd_id_organization parameter - $orgId = $this->getRequest()->getParam('gsf_id_organization'); - - if (is_null($orgId)) { - //Get the selected gsf_id_organization used in the index - $dataIdx = $this->getCachedRequestData(true, 'index', true); - $orgId = isset($dataIdx['gsf_id_organization']) ? $dataIdx['gsf_id_organization'] : $this->loader->getCurrentUser()->getCurrentOrganizationId(); + if ($staff_id = $this->_getIdParam()) { + $this->_user = $this->loader->getUserLoader()->getUserByStaffId($staff_id); + $source = $this->menu->getParameterSource(); + $source->offsetSet('gsf_id_organization', $this->_user->getBaseOrganizationId()); + $source->offsetSet('accessible_role', $this->_user->hasAllowedRole()); } - - $this->menu->getParameterSource()->offsetSet('gsf_id_organization', $orgId); } /** @@ -436,12 +488,12 @@ */ public function resetAction() { - $staff_id = $this->_getIdParam(); - $user = $this->loader->getUserLoader()->getUserByStaffId($staff_id); + // Make sure the user is loaded + $this->loadUser(); - $this->html->h3(sprintf($this->_('Reset password for: %s'), $user->getFullName())); + $this->html->h3(sprintf($this->_('Reset password for: %s'), $this->_user->getFullName())); - if (! $user->canSetPassword()) { + if (! ($this->_user->hasAllowedRole() && $this->_user->canSetPassword())) { $this->addMessage($this->_('You are not allowed to change this password.')); return; } @@ -449,7 +501,7 @@ /************* * Make form * *************/ - $form = $user->getChangePasswordForm(array('askOld' => false)); + $form = $this->_user->getChangePasswordForm(array('askOld' => false)); /**************** * Process form * @@ -464,12 +516,14 @@ /**************** * Display form * ****************/ - if ($user->isPasswordResetRequired()) { + if ($this->_user->isPasswordResetRequired()) { $this->menu->setVisible(false); } else { $form->addButtons($this->createMenuLinks()); } + $this->beforeFormDisplay($form, false); + $this->html[] = $form; } } Modified: trunk/library/classes/Gems/Html.php =================================================================== --- trunk/library/classes/Gems/Html.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Html.php 2012-06-07 13:20:56 UTC (rev 742) @@ -147,7 +147,8 @@ $args['class'] = new MUtil_Html_ClassArrayAttribute('browselink'); } - // MUtil_Echo::r($args); + // MUtil_Echo::track($args); + // MUtil_Echo::track($panel_args['baseUrl']); $pager = new MUtil_Html_PagePanel($panel_args); $pager[] = $pager->pageLinks( Modified: trunk/library/classes/Gems/Menu/MenuAbstract.php =================================================================== --- trunk/library/classes/Gems/Menu/MenuAbstract.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Menu/MenuAbstract.php 2012-06-07 13:20:56 UTC (rev 742) @@ -395,7 +395,7 @@ if (! $this->escort->hasPrivilege('pr.staff.edit.all')) { $filter = array_keys($this->escort->loader->getCurrentUser()->getAllowedOrganizations()); foreach ($pages as $sub_page) { - $sub_page->setParameterFilter('gsf_id_organization', $filter); + $sub_page->setParameterFilter('gsf_id_organization', $filter, 'accessible_role', 1); } } Modified: trunk/library/classes/Gems/Model/StaffModel.php =================================================================== --- trunk/library/classes/Gems/Model/StaffModel.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Model/StaffModel.php 2012-06-07 13:20:56 UTC (rev 742) @@ -1,8 +1,9 @@ <?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 @@ -13,7 +14,7 @@ * * 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 @@ -24,7 +25,7 @@ * 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. - * + * * The staff model * * @package Gems @@ -47,14 +48,18 @@ */ class Gems_Model_StaffModel extends Gems_Model_ModelAbstract { - /** - * @var Gems_Loader - */ - public $loader; - - public function __construct() + public function __construct(Gems_Loader $loader) { parent::__construct('staff', 'gems__staff', 'gsf'); + + $allowedGroups = $loader->getUtil()->getDbLookup()->getAllowedStaffGroups(); + if ($allowedGroups) { + $expr = new Zend_Db_Expr('CASE WHEN gsf_id_primary_group IN (' . implode(', ', array_keys($allowedGroups)) . ') THEN 1 ELSE 0 END'); + } else { + $expr = new Zend_Db_Expr('0'); + } + $this->addColumn($expr, 'accessible_role'); + $this->set('accessible_role', 'default', 1); } /** Modified: trunk/library/classes/Gems/Model.php =================================================================== --- trunk/library/classes/Gems/Model.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Model.php 2012-06-07 13:20:56 UTC (rev 742) @@ -264,7 +264,7 @@ */ public function getStaffModel() { - $model = $this->_loadClass('StaffModel', true); + $model = $this->_loadClass('StaffModel', true, array($this->loader)); $this->addUserLogin($model, 'gsf_login', 'gsf_id_organization'); $this->setAsGemsUserId($model, 'gsf_id_user'); Modified: trunk/library/classes/Gems/Snippets/ModelTableSnippetAbstract.php =================================================================== --- trunk/library/classes/Gems/Snippets/ModelTableSnippetAbstract.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/Snippets/ModelTableSnippetAbstract.php 2012-06-07 13:20:56 UTC (rev 742) @@ -175,10 +175,11 @@ { if ($this->requestCache) { // Items that should not be stored. - $this->requestCache->removeParams('page', 'items', 'action'); + $this->requestCache->removeParams('action'); if ((! $this->baseUrl)) { $this->baseUrl = $this->requestCache->getProgramParams(); + // MUtil_Echo::track($this->baseUrl); if (MUtil_Registry_Source::$verbose) { MUtil_Echo::track($this->baseUrl); Modified: trunk/library/classes/Gems/User/User.php =================================================================== --- trunk/library/classes/Gems/User/User.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/Gems/User/User.php 2012-06-07 13:20:56 UTC (rev 742) @@ -682,7 +682,7 @@ if (! $name) { // Use obfuscated login name $name = $this->getLoginName(); - $name = substr($name, 0, 3) . str_repeat('*', strlen($name) - 2); + $name = substr($name, 0, 3) . str_repeat('*', max(5, strlen($name) - 2)); } $this->_setVar('user_name', $name); @@ -962,6 +962,20 @@ } /** + * Return true if this user has a role that is accessible by the current user + * + * @return boolean + */ + public function hasAllowedRole() + { + if ($allowedGroups = $this->util->getDbLookup()->getAllowedStaffGroups()) { + return isset($allowedGroups[$this->getGroup()]) ? 1 : 0; + } else { + return 0; + } + } + + /** * Return true if this user has a password. * * @return boolean @@ -1092,7 +1106,7 @@ if ($this->isBlockable()) { $auths['block'] = array($this, 'authorizeBlock'); } - + // organization ip restriction $auths['orgip'] = array($this, 'authorizeOrgIp'); Modified: trunk/library/classes/GemsEscort.php =================================================================== --- trunk/library/classes/GemsEscort.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/GemsEscort.php 2012-06-07 13:20:56 UTC (rev 742) @@ -1577,7 +1577,7 @@ $this->setError( $this->_('No access to page'), 403, - sprintf($this->_('Access to this page is not allowed for current role: %s.'), $this->session->user_role) + sprintf($this->_('Access to this page is not allowed for current role: %s.'), $user->getRole()) ); } else { // No longer logged in Modified: trunk/library/classes/MUtil/Html/PagePanel.php =================================================================== --- trunk/library/classes/MUtil/Html/PagePanel.php 2012-06-07 13:04:33 UTC (rev 741) +++ trunk/library/classes/MUtil/Html/PagePanel.php 2012-06-07 13:20:56 UTC (rev 742) @@ -49,6 +49,10 @@ */ class MUtil_Html_PagePanel extends MUtil_Html_Sequence implements MUtil_Lazy_Procrastinator { + /** + * + * @var array + */ protected $_baseUrl = array(); protected $_currentPage; @@ -201,7 +205,10 @@ if ($param_name = $this->getCurrentPageParam()) { $request = $this->getRequest(); - if ($currentPage = $request->getParam($param_name)) { + if (isset($this->_baseUrl[$param_name])) { + $this->_currentPage = $this->_baseUrl[$param_name]; + // Set cookie + } elseif ($currentPage = $request->getParam($param_name)) { $this->_currentPage = $currentPage; // Set cookie } elseif ($request instanceof Zend_Controller_Request_Http) { @@ -231,9 +238,16 @@ if ($param_name = $this->getItemCountParam()) { $request = $this->getRequest(); - if ($itemCount = $request->getParam($param_name)) { + if (isset($this->_baseUrl[$param_name])) { + $this->_itemCount = $this->_baseUrl[$param_name]; + } elseif ($itemCount = $request->getParam($param_name)) { $this->_itemCount = $itemCount; - setcookie($param_name, $itemCount, time() + (30 * 86400), $this->getCookieLocation()); + } + + if ($this->_itemCount) { + // Store + setcookie($param_name, $this->_itemCount, time() + (30 * 86400), $this->getCookieLocation()); + } elseif ($request instanceof Zend_Controller_Request_Http) { $this->_itemCount = $request->getCookie($param_name, $this->_itemCount); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |