Skip to content

Commit

Permalink
*8095* backport Role refactor to Harvester
Browse files Browse the repository at this point in the history
  • Loading branch information
jnugent committed Jan 25, 2013
1 parent 7e66165 commit 0b9bcea
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 85 deletions.
10 changes: 6 additions & 4 deletions classes/admin/form/UserManagementForm.inc.php
Expand Up @@ -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()));
Expand Down Expand Up @@ -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'
Expand All @@ -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());

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion classes/core/PageRouter.inc.php
Expand Up @@ -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');
}
Expand Down
43 changes: 19 additions & 24 deletions classes/security/Role.inc.php
Expand Up @@ -13,34 +13,45 @@
* @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);
}

/**
* Get the i18n key name associated with this 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();
}
}

//
Expand All @@ -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);
}
}

?>
57 changes: 9 additions & 48 deletions classes/security/RoleDAO.inc.php
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}

}

?>
20 changes: 13 additions & 7 deletions pages/admin/PeopleHandler.inc.php
Expand Up @@ -9,7 +9,7 @@
* @class PeopleHandler
* @ingroup pages_admin
*
* @brief Handle requests for people management functions.
* @brief Handle requests for people management functions.
*/


Expand All @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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; $i<count($users); $i++) {
Expand Down Expand Up @@ -197,7 +201,9 @@ function unEnroll($args, &$request) {
$roleDao->deleteRoleByUserId($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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/user/index.tpl
Expand Up @@ -18,7 +18,7 @@
<ul id="roles" class="plain">
{foreach from=$userRoles item=role}
{assign var="hasRole" value=1}
<li>&#187; <a href="{url page=$role->getRolePath()}">{translate key=$role->getRoleName()}</a></li>
<li>&#187; <a href="{url page=$role->getPath()}">{translate key=$role->getRoleName()}</a></li>
{foreachelse}
<li>
{translate key="user.noRoles"}
Expand Down

0 comments on commit 0b9bcea

Please sign in to comment.