From 0b9bcea76149d8a3fca4ce49a6c27a8ca72b2fc8 Mon Sep 17 00:00:00 2001 From: Jason Nugent Date: Fri, 25 Jan 2013 13:18:32 -0400 Subject: [PATCH] *8095* backport Role refactor to Harvester --- classes/admin/form/UserManagementForm.inc.php | 10 ++-- classes/core/PageRouter.inc.php | 2 +- classes/security/Role.inc.php | 43 +++++++------- classes/security/RoleDAO.inc.php | 57 +++---------------- pages/admin/PeopleHandler.inc.php | 20 ++++--- templates/user/index.tpl | 2 +- 6 files changed, 49 insertions(+), 85 deletions(-) diff --git a/classes/admin/form/UserManagementForm.inc.php b/classes/admin/form/UserManagementForm.inc.php index 92867ff9..d2b3646a 100644 --- a/classes/admin/form/UserManagementForm.inc.php +++ b/classes/admin/form/UserManagementForm.inc.php @@ -36,7 +36,7 @@ function UserManagementForm($userId = null) { $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired')); $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array($this->userId, true), true)); $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric')); - + if (!Config::getVar('security', 'implicit_auth')) { $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired')); $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength())); @@ -68,7 +68,7 @@ function display() { $user =& $userDao->getById($this->userId); $templateMgr->assign('username', $user->getUsername()); } - + $activeRoles = array( '' => 'admin.people.doNotEnroll', 'submitter' => 'user.role.submitter' @@ -95,7 +95,7 @@ function display() { // Send implicitAuth setting down to template $templateMgr->assign('implicitAuth', Config::getVar('security', 'implicit_auth')); - + $site =& Request::getSite(); $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames()); @@ -154,7 +154,9 @@ function initData() { if (!isset($this->userId)) { $roleDao =& DAORegistry::getDAO('RoleDAO'); $roleId = Request::getUserVar('roleId'); - $roleSymbolic = $roleDao->getRolePath($roleId); + $role =& $roleDao->newDataObject(); + $role->setId($roleId); + $roleSymbolic = $role->getPath(); $this->_data = array( 'enrollAs' => array($roleSymbolic) diff --git a/classes/core/PageRouter.inc.php b/classes/core/PageRouter.inc.php index 8e121086..4a95f0ab 100644 --- a/classes/core/PageRouter.inc.php +++ b/classes/core/PageRouter.inc.php @@ -31,7 +31,7 @@ function redirectHome(&$request) { $roles =& $roleDao->getRolesByUserId($userId); if(count($roles) == 1) { $role = array_shift($roles); - $request->redirect($role->getRolePath()); + $request->redirect($role->getPath()); } else { $request->redirect('user'); } diff --git a/classes/security/Role.inc.php b/classes/security/Role.inc.php index 5a52e783..b74e4b2b 100644 --- a/classes/security/Role.inc.php +++ b/classes/security/Role.inc.php @@ -13,18 +13,19 @@ * @brief Describes user roles within the system and the associated permissions. */ - +import('lib.pkp.classes.security.PKPRole'); /** ID codes for all user roles */ -define('ROLE_ID_SITE_ADMIN', 0x00000001); define('ROLE_ID_SUBMITTER', 0x00000002); -class Role extends DataObject { +class Role extends PKPRole { /** * Constructor. + * @param $roleId for this role. Default to null for backwards + * compatibility */ - function Role() { - parent::DataObject(); + function Role($roleId = null) { + parent::PKPRole($roleId); } /** @@ -32,15 +33,25 @@ function Role() { * @return String the key */ function getRoleName() { - return RoleDAO::getRoleName($this->getData('roleId')); + switch ($this->getId()) { + case ROLE_ID_SUBMITTER: + return 'user.role.submitter' . ($plural ? 's' : ''); + default: + return parent::getRoleName($plural); + } } /** * Get the URL path associated with this role's operations. * @return String the path */ - function getRolePath() { - return RoleDAO::getRolePath($this->getData('roleId')); + function getPath() { + switch ($this->getId()) { + case ROLE_ID_SUBMITTER: + return ROLE_PATH_SUBMITTER; + default: + return parent::getPath(); + } } // @@ -62,22 +73,6 @@ function getUserId() { function setUserId($userId) { return $this->setData('userId', $userId); } - - /** - * Get role ID of this role. - * @return int - */ - function getRoleId() { - return $this->getData('roleId'); - } - - /** - * Set role ID of this role. - * @param $roleId int - */ - function setRoleId($roleId) { - return $this->setData('roleId', $roleId); - } } ?> diff --git a/classes/security/RoleDAO.inc.php b/classes/security/RoleDAO.inc.php index 93baa5c5..57ea407a 100644 --- a/classes/security/RoleDAO.inc.php +++ b/classes/security/RoleDAO.inc.php @@ -25,6 +25,15 @@ function RoleDAO() { $this->userDao =& DAORegistry::getDAO('UserDAO'); } + /** + * Create new data object. + * @return Role + */ + function &newDataObject() { + $dataObject = new Role(); + return $dataObject; + } + /** * Retrieve a role. * @param $userId int @@ -230,39 +239,6 @@ function userHasRole($userId, $roleId) { return $returner; } - /** - * Get the i18n key name associated with the specified role. - * @param $roleId int - * @param $plural boolean get the plural form of the name - * @return string - */ - static function getRoleName($roleId, $plural = false) { - switch ($roleId) { - case ROLE_ID_SITE_ADMIN: - return 'user.role.siteAdmin' . ($plural ? 's' : ''); - case ROLE_ID_SUBMITTER: - return 'user.role.submitter' . ($plural ? 's' : ''); - default: - return ''; - } - } - - /** - * Get the URL path associated with the specified role's operations. - * @param $roleId int - * @return string - */ - static function getRolePath($roleId) { - switch ($roleId) { - case ROLE_ID_SITE_ADMIN: - return 'admin'; - case ROLE_ID_SUBMITTER: - return 'submitter'; - default: - return ''; - } - } - /** * Get a role's ID based on its path. * @param $rolePath string @@ -278,21 +254,6 @@ function getRoleIdFromPath($rolePath) { return null; } } - - /** - * Map a column heading value to a database value for sorting - * @param string - * @return string - */ - function getSortMapping($heading) { - switch ($heading) { - case 'username': return 'u.username'; - case 'name': return 'u.last_name'; - case 'email': return 'u.email'; - default: return null; - } - } - } ?> diff --git a/pages/admin/PeopleHandler.inc.php b/pages/admin/PeopleHandler.inc.php index bb8f9995..7fda451d 100644 --- a/pages/admin/PeopleHandler.inc.php +++ b/pages/admin/PeopleHandler.inc.php @@ -9,7 +9,7 @@ * @class PeopleHandler * @ingroup pages_admin * - * @brief Handle requests for people management functions. + * @brief Handle requests for people management functions. */ @@ -20,7 +20,7 @@ class PeopleHandler extends AdminHandler { /** * Display list of people in the selected role. * @param $args array first parameter is the role ID to display - */ + */ function people($args, &$request) { $this->validate(); $this->setupTemplate($request, true); @@ -41,7 +41,7 @@ function people($args, &$request) { $roleId = 0; $roleName = 'admin.people.allUsers'; } - + $sort = $request->getUserVar('sort'); $sort = isset($sort) ? $sort : 'name'; $sortDirection = $request->getUserVar('sortDirection'); @@ -85,7 +85,9 @@ function people($args, &$request) { USER_FIELD_EMAIL => 'user.email' ); $templateMgr->assign('fieldOptions', $fieldOptions); - $templateMgr->assign('rolePath', $roleDao->getRolePath($roleId)); + $role =& $roleDao->newDataObject(); + $role->setId($roleId); + $templateMgr->assign('rolePath', $role->getPath()); $templateMgr->assign('alphaList', explode(' ', __('common.alphaList'))); $templateMgr->assign('roleSymbolic', $roleSymbolic); $templateMgr->assign('sort', $sort); @@ -121,7 +123,7 @@ function enrollSearch($args, &$request) { $searchType = USER_FIELD_INITIAL; $search = $searchInitial; } - + $sort = $request->getUserVar('sort'); $sort = isset($sort) ? $sort : 'name'; $sortDirection = $request->getUserVar('sortDirection'); @@ -168,7 +170,9 @@ function enroll($args, &$request) { } $roleDao =& DAORegistry::getDAO('RoleDAO'); - $rolePath = $roleDao->getRolePath($roleId); + $role =& $roleDao->newDataObject(); + $role->setId($roleId); + $rolePath = $role->getPath(); if ($users != null && is_array($users) && $rolePath != '' && $rolePath != 'admin') { for ($i=0; $ideleteRoleByUserId($request->getUserVar('userId'), $roleId); } - $request->redirect(null, 'people', $roleDao->getRolePath($roleId) . 's'); + $role =& $roleDao->newDataObject(); + $role->setId($roleId); + $request->redirect(null, 'people', $role->getPath() . 's'); } /** diff --git a/templates/user/index.tpl b/templates/user/index.tpl index 2c2e7c0b..850883ae 100644 --- a/templates/user/index.tpl +++ b/templates/user/index.tpl @@ -18,7 +18,7 @@