first commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/roles/UserGroupGridCellProvider.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserGroupGridCellProvider
|
||||
*
|
||||
* @ingroup controllers_grid_settings_roles
|
||||
*
|
||||
* @brief Cell provider for columns in a user group grid.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\roles;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use PKP\controllers\grid\GridCellProvider;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxAction;
|
||||
use PKP\security\RoleDAO;
|
||||
use PKP\userGroup\UserGroup;
|
||||
|
||||
class UserGroupGridCellProvider extends GridCellProvider
|
||||
{
|
||||
/**
|
||||
* Extracts variables for a given column from a data element
|
||||
* so that they may be assigned to template before rendering.
|
||||
*
|
||||
* @param \PKP\controllers\grid\GridRow $row
|
||||
* @param GridColumn $column
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateVarsFromRowColumn($row, $column)
|
||||
{
|
||||
$userGroup = $row->getData(); /** @var UserGroup $userGroup */
|
||||
$columnId = $column->getId();
|
||||
$workflowStages = Application::getApplicationStages();
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
|
||||
$assignedStages = Repo::userGroup()->getAssignedStagesByUserGroupId($userGroup->getContextId(), $userGroup->getId())->toArray();
|
||||
|
||||
switch ($columnId) {
|
||||
case 'name':
|
||||
return ['label' => $userGroup->getLocalizedName()];
|
||||
case 'roleId':
|
||||
$roleNames = Application::getRoleNames(false, [$userGroup->getRoleId()]);
|
||||
return ['label' => __(array_shift($roleNames))];
|
||||
case in_array($columnId, $workflowStages):
|
||||
// Set the state of the select element that will
|
||||
// be used to assign the stage to the user group.
|
||||
$selectDisabled = false;
|
||||
if (in_array($columnId, $roleDao->getForbiddenStages($userGroup->getRoleId()))) {
|
||||
// This stage should not be assigned to the user group.
|
||||
$selectDisabled = true;
|
||||
}
|
||||
|
||||
return ['selected' => in_array($columnId, $assignedStages),
|
||||
'disabled' => $selectDisabled];
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::getTemplateVarsFromRowColumn($row, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridCellProvider::getCellActions()
|
||||
*/
|
||||
public function getCellActions($request, $row, $column, $position = GridHandler::GRID_ACTION_POSITION_DEFAULT)
|
||||
{
|
||||
$workflowStages = Application::getApplicationStages();
|
||||
$columnId = $column->getId();
|
||||
|
||||
if (in_array($columnId, $workflowStages)) {
|
||||
$userGroup = $row->getData(); /** @var UserGroup $userGroup */
|
||||
|
||||
$assignedStages = Repo::userGroup()->getAssignedStagesByUserGroupId($userGroup->getContextId(), $userGroup->getId())->toArray();
|
||||
|
||||
$router = $request->getRouter();
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
|
||||
if (!in_array($columnId, $roleDao->getForbiddenStages($userGroup->getRoleId()))) {
|
||||
if (in_array($columnId, $assignedStages)) {
|
||||
$operation = 'unassignStage';
|
||||
$actionTitleKey = 'grid.userGroup.unassignStage';
|
||||
} else {
|
||||
$operation = 'assignStage';
|
||||
$actionTitleKey = 'grid.userGroup.assignStage';
|
||||
}
|
||||
$actionArgs = array_merge(
|
||||
['stageId' => $columnId],
|
||||
$row->getRequestArgs()
|
||||
);
|
||||
|
||||
$actionUrl = $router->url($request, null, null, $operation, null, $actionArgs);
|
||||
$actionRequest = new AjaxAction($actionUrl);
|
||||
|
||||
$linkAction = new LinkAction(
|
||||
$operation,
|
||||
$actionRequest,
|
||||
__($actionTitleKey),
|
||||
null
|
||||
);
|
||||
|
||||
return [$linkAction];
|
||||
}
|
||||
}
|
||||
|
||||
return parent::getCellActions($request, $row, $column, $position);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,499 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/roles/UserGroupGridHandler.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserGroupGridHandler
|
||||
*
|
||||
* @ingroup controllers_grid_settings
|
||||
*
|
||||
* @brief Handle operations for user group management operations.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\roles;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\core\Request;
|
||||
use APP\facades\Repo;
|
||||
use APP\notification\NotificationManager;
|
||||
use PKP\controllers\grid\feature\PagingFeature;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\controllers\grid\settings\roles\form\UserGroupForm;
|
||||
use PKP\core\JSONMessage;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
use PKP\notification\PKPNotification;
|
||||
use PKP\security\authorization\ContextAccessPolicy;
|
||||
use PKP\security\authorization\internal\WorkflowStageRequiredPolicy;
|
||||
use PKP\security\Role;
|
||||
use PKP\security\RoleDAO;
|
||||
use PKP\userGroup\relationships\UserGroupStage;
|
||||
use PKP\userGroup\UserGroup;
|
||||
use PKP\workflow\WorkflowStageDAO;
|
||||
|
||||
class UserGroupGridHandler extends GridHandler
|
||||
{
|
||||
/** @var int Context id. */
|
||||
private $_contextId;
|
||||
|
||||
/** @var UserGroup User group object handled by some grid operations. */
|
||||
private $_userGroup;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->addRoleAssignment(
|
||||
[Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN],
|
||||
[
|
||||
'fetchGrid',
|
||||
'fetchCategory',
|
||||
'fetchRow',
|
||||
'addUserGroup',
|
||||
'editUserGroup',
|
||||
'updateUserGroup',
|
||||
'removeUserGroup',
|
||||
'assignStage',
|
||||
'unassignStage'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Overridden methods from PKPHandler.
|
||||
//
|
||||
/**
|
||||
* @copydoc PKPHandler::authorize()
|
||||
*/
|
||||
public function authorize($request, &$args, $roleAssignments)
|
||||
{
|
||||
$this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
|
||||
|
||||
$operation = $request->getRequestedOp();
|
||||
$workflowStageRequiredOps = ['assignStage', 'unassignStage'];
|
||||
if (in_array($operation, $workflowStageRequiredOps)) {
|
||||
$this->addPolicy(new WorkflowStageRequiredPolicy($request->getUserVar('stageId')));
|
||||
}
|
||||
|
||||
$userGroupRequiredOps = array_merge($workflowStageRequiredOps, ['editUserGroup', 'removeUserGroup']);
|
||||
if (in_array($operation, $userGroupRequiredOps)) {
|
||||
// Validate the user group object.
|
||||
$userGroupId = $request->getUserVar('userGroupId');
|
||||
|
||||
$userGroup = Repo::userGroup()->get($userGroupId);
|
||||
|
||||
if (!$userGroup) {
|
||||
fatalError('Invalid user group id!');
|
||||
} else {
|
||||
$this->_userGroup = $userGroup;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::authorize($request, $args, $roleAssignments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::initialize()
|
||||
*
|
||||
* @param null|mixed $args
|
||||
*/
|
||||
public function initialize($request, $args = null)
|
||||
{
|
||||
parent::initialize($request, $args);
|
||||
|
||||
$context = $request->getContext();
|
||||
$this->_contextId = $context->getId();
|
||||
|
||||
// Basic grid configuration.
|
||||
$this->setTitle('grid.roles.currentRoles');
|
||||
|
||||
// Add grid-level actions.
|
||||
$router = $request->getRouter();
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'addUserGroup',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'addUserGroup'),
|
||||
__('grid.roles.add'),
|
||||
'modal_add_role'
|
||||
),
|
||||
__('grid.roles.add'),
|
||||
'add_role'
|
||||
)
|
||||
);
|
||||
|
||||
$cellProvider = new UserGroupGridCellProvider();
|
||||
|
||||
$workflowStagesLocales = WorkflowStageDAO::getWorkflowStageTranslationKeys();
|
||||
|
||||
// Set array containing the columns info with the same cell provider.
|
||||
$columnsInfo = [
|
||||
1 => ['id' => 'name', 'title' => 'settings.roles.roleName', 'template' => null],
|
||||
2 => ['id' => 'roleId', 'title' => 'settings.roles.from', 'template' => null]
|
||||
];
|
||||
|
||||
foreach ($workflowStagesLocales as $stageId => $stageTitleKey) {
|
||||
$columnsInfo[] = ['id' => $stageId, 'title' => $stageTitleKey, 'template' => 'controllers/grid/common/cell/selectStatusCell.tpl'];
|
||||
}
|
||||
|
||||
// Add array columns to the grid.
|
||||
foreach ($columnsInfo as $columnInfo) {
|
||||
$this->addColumn(
|
||||
new GridColumn(
|
||||
$columnInfo['id'],
|
||||
$columnInfo['title'],
|
||||
null,
|
||||
$columnInfo['template'],
|
||||
$cellProvider
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::loadData()
|
||||
*/
|
||||
protected function loadData($request, $filter)
|
||||
{
|
||||
$contextId = $this->_getContextId();
|
||||
|
||||
$roleIdFilter = null;
|
||||
$stageIdFilter = null;
|
||||
|
||||
if (!is_array($filter)) {
|
||||
$filter = [];
|
||||
}
|
||||
|
||||
if (isset($filter['selectedRoleId'])) {
|
||||
$roleIdFilter = $filter['selectedRoleId'];
|
||||
}
|
||||
|
||||
if (isset($filter['selectedStageId'])) {
|
||||
$stageIdFilter = $filter['selectedStageId'];
|
||||
}
|
||||
|
||||
$rangeInfo = $this->getGridRangeInfo($request, $this->getId());
|
||||
|
||||
if ($stageIdFilter && $stageIdFilter != 0) {
|
||||
return Repo::userGroup()->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->filterByStageIds([$stageIdFilter])
|
||||
->filterByRoleIds([$roleIdFilter])
|
||||
->limit($rangeInfo->getCount())
|
||||
->offset($rangeInfo->getOffset() + max(0, $rangeInfo->getPage() - 1) * $rangeInfo->getCount())
|
||||
->getMany()
|
||||
->toArray();
|
||||
} elseif ($roleIdFilter && $roleIdFilter != 0) {
|
||||
return Repo::userGroup()->getByRoleIds([$roleIdFilter], $contextId)->toArray();
|
||||
} else {
|
||||
return Repo::userGroup()->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->getMany()
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::getRowInstance()
|
||||
*
|
||||
* @return UserGroupGridRow
|
||||
*/
|
||||
protected function getRowInstance()
|
||||
{
|
||||
return new UserGroupGridRow();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GridHandler::renderFilter()
|
||||
*/
|
||||
public function renderFilter($request, $filterData = [])
|
||||
{
|
||||
// Get filter data.
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
$roleOptions = [0 => 'grid.user.allPermissionLevels'] + Application::getRoleNames(true);
|
||||
|
||||
// Reader roles are not important for stage assignments.
|
||||
if (array_key_exists(Role::ROLE_ID_READER, $roleOptions)) {
|
||||
unset($roleOptions[Role::ROLE_ID_READER]);
|
||||
}
|
||||
|
||||
$filterData = ['roleOptions' => $roleOptions];
|
||||
|
||||
$workflowStages = [0 => 'grid.userGroup.allStages'] + WorkflowStageDAO::getWorkflowStageTranslationKeys();
|
||||
$filterData['stageOptions'] = $workflowStages;
|
||||
|
||||
return parent::renderFilter($request, $filterData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GridHandler::getFilterSelectionData()
|
||||
*
|
||||
* @return array Filter selection data.
|
||||
*/
|
||||
public function getFilterSelectionData($request)
|
||||
{
|
||||
$selectedRoleId = $request->getUserVar('selectedRoleId');
|
||||
$selectedStageId = $request->getUserVar('selectedStageId');
|
||||
|
||||
// Cast or set to grid filter default value (all roles).
|
||||
$selectedRoleId = (is_null($selectedRoleId) ? 0 : (int)$selectedRoleId);
|
||||
$selectedStageId = (is_null($selectedStageId) ? 0 : (int)$selectedStageId);
|
||||
|
||||
return ['selectedRoleId' => $selectedRoleId, 'selectedStageId' => $selectedStageId];
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GridHandler::getFilterForm()
|
||||
*
|
||||
* @return string Filter template.
|
||||
*/
|
||||
protected function getFilterForm()
|
||||
{
|
||||
return 'controllers/grid/settings/roles/userGroupsGridFilter.tpl';
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GridHandler::initFeatures()
|
||||
*/
|
||||
public function initFeatures($request, $args)
|
||||
{
|
||||
return [new PagingFeature()];
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Handler operations.
|
||||
//
|
||||
/**
|
||||
* Handle the add user group operation.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*/
|
||||
public function addUserGroup($args, $request)
|
||||
{
|
||||
return $this->editUserGroup($args, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the edit user group operation.
|
||||
*
|
||||
* @param array $args
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function editUserGroup($args, $request)
|
||||
{
|
||||
$userGroupForm = $this->_getUserGroupForm($request);
|
||||
|
||||
$userGroupForm->initData();
|
||||
|
||||
return new JSONMessage(true, $userGroupForm->fetch($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user group data on database and grid.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function updateUserGroup($args, $request)
|
||||
{
|
||||
$userGroupForm = $this->_getUserGroupForm($request);
|
||||
|
||||
$userGroupForm->readInputData();
|
||||
if ($userGroupForm->validate()) {
|
||||
$notificationMgr = new NotificationManager();
|
||||
$notificationMgr->createTrivialNotification($request->getUser()->getId());
|
||||
$userGroupForm->execute();
|
||||
$json = \PKP\db\DAO::getDataChangedEvent();
|
||||
$json->setGlobalEvent('userGroupUpdated');
|
||||
return $json;
|
||||
} else {
|
||||
return new JSONMessage(true, $userGroupForm->fetch($request));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove user group.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function removeUserGroup($args, $request)
|
||||
{
|
||||
if (!$request->checkCSRF()) {
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
|
||||
$user = $request->getUser();
|
||||
$userGroup = $this->_userGroup;
|
||||
$contextId = $this->_getContextId();
|
||||
$notificationMgr = new NotificationManager();
|
||||
|
||||
$usersAssignedToUserGroupCount = Repo::user()->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->filterByUserGroupIds([$userGroup->getId()])
|
||||
->getCount();
|
||||
|
||||
if ($usersAssignedToUserGroupCount == 0) {
|
||||
if ($userGroup->getData('isDefault')) {
|
||||
// Can't delete default user groups.
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$user->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_WARNING,
|
||||
['contents' => __(
|
||||
'grid.userGroup.cantRemoveDefaultUserGroup',
|
||||
['userGroupName' => $userGroup->getLocalizedName() ]
|
||||
)]
|
||||
);
|
||||
} else {
|
||||
// We can delete, no user assigned yet.
|
||||
Repo::userGroup()->delete($userGroup);
|
||||
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$user->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_SUCCESS,
|
||||
['contents' => __(
|
||||
'grid.userGroup.removed',
|
||||
['userGroupName' => $userGroup->getLocalizedName() ]
|
||||
)]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Can't delete while an user
|
||||
// is still assigned to that user group.
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$user->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_WARNING,
|
||||
['contents' => __(
|
||||
'grid.userGroup.cantRemoveUserGroup',
|
||||
['userGroupName' => $userGroup->getLocalizedName(), 'usersCount' => $usersAssignedToUserGroupCount]
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
$json = \PKP\db\DAO::getDataChangedEvent($userGroup->getId());
|
||||
$json->setGlobalEvent('userGroupUpdated');
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign stage to user group.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*/
|
||||
public function assignStage($args, $request)
|
||||
{
|
||||
return $this->_toggleAssignment($args, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unassign stage to user group.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*/
|
||||
public function unassignStage($args, $request)
|
||||
{
|
||||
return $this->_toggleAssignment($args, $request);
|
||||
}
|
||||
|
||||
//
|
||||
// Private helper methods.
|
||||
//
|
||||
|
||||
/**
|
||||
* Toggle user group stage assignment.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
private function _toggleAssignment($args, $request)
|
||||
{
|
||||
if (!$request->checkCSRF()) {
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
$userGroup = $this->_userGroup;
|
||||
$stageId = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_WORKFLOW_STAGE);
|
||||
$contextId = $this->_getContextId();
|
||||
$operation = $request->getRequestedOp();
|
||||
|
||||
switch ($operation) {
|
||||
case 'assignStage':
|
||||
UserGroupStage::create([
|
||||
'contextId' => $contextId,
|
||||
'userGroupId' => $userGroup->getId(),
|
||||
'stageId' => $stageId
|
||||
]);
|
||||
|
||||
$messageKey = 'grid.userGroup.assignedStage';
|
||||
break;
|
||||
case 'unassignStage':
|
||||
Repo::userGroup()->removeGroupFromStage($contextId, $userGroup->getId(), $stageId);
|
||||
$messageKey = 'grid.userGroup.unassignedStage';
|
||||
break;
|
||||
}
|
||||
|
||||
$notificationMgr = new NotificationManager();
|
||||
$user = $request->getUser();
|
||||
|
||||
$stageLocaleKeys = WorkflowStageDAO::getWorkflowStageTranslationKeys();
|
||||
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$user->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_SUCCESS,
|
||||
['contents' => __(
|
||||
$messageKey,
|
||||
['userGroupName' => $userGroup->getLocalizedName(), 'stageName' => __($stageLocaleKeys[$stageId])]
|
||||
)]
|
||||
);
|
||||
|
||||
return \PKP\db\DAO::getDataChangedEvent($userGroup->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a UserGroupForm instance.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return UserGroupForm
|
||||
*/
|
||||
private function _getUserGroupForm($request)
|
||||
{
|
||||
// Get the user group Id.
|
||||
$userGroupId = (int) $request->getUserVar('userGroupId');
|
||||
|
||||
// Instantiate the files form.
|
||||
$contextId = $this->_getContextId();
|
||||
return new UserGroupForm($contextId, $userGroupId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get context id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function _getContextId()
|
||||
{
|
||||
return $this->_contextId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/roles/UserGroupGridRow.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserGroupGridRow
|
||||
*
|
||||
* @ingroup controllers_grid_settings_roles
|
||||
*
|
||||
* @brief User group grid row definition
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\roles;
|
||||
|
||||
use PKP\controllers\grid\GridRow;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
use PKP\linkAction\request\RemoteActionConfirmationModal;
|
||||
use PKP\userGroup\UserGroup;
|
||||
|
||||
class UserGroupGridRow extends GridRow
|
||||
{
|
||||
//
|
||||
// Overridden methods from GridRow
|
||||
//
|
||||
/**
|
||||
* @copydoc GridRow::initialize()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function initialize($request, $template = null)
|
||||
{
|
||||
parent::initialize($request, $template);
|
||||
|
||||
$userGroup = $this->getData(); /** @var UserGroup $userGroup */
|
||||
assert($userGroup != null);
|
||||
|
||||
$rowId = $this->getId();
|
||||
|
||||
$actionArgs = ['userGroupId' => $userGroup->getId()];
|
||||
$this->setRequestArgs($actionArgs);
|
||||
|
||||
// Only add row actions if this is an existing row.
|
||||
if (!empty($rowId) && is_numeric($rowId)) {
|
||||
$router = $request->getRouter();
|
||||
|
||||
$this->addAction(new LinkAction(
|
||||
'editUserGroup',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'editUserGroup', null, $actionArgs),
|
||||
__('grid.action.edit'),
|
||||
'modal_edit'
|
||||
),
|
||||
__('grid.action.edit'),
|
||||
'edit'
|
||||
));
|
||||
|
||||
$this->addAction(new LinkAction(
|
||||
'removeUserGroup',
|
||||
new RemoteActionConfirmationModal(
|
||||
$request->getSession(),
|
||||
__('settings.roles.removeText'),
|
||||
null,
|
||||
$router->url($request, null, null, 'removeUserGroup', null, $actionArgs)
|
||||
),
|
||||
__('grid.action.remove'),
|
||||
'delete'
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/roles/form/UserGroupForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserGroupForm
|
||||
*
|
||||
* @ingroup controllers_grid_settings_roles_form
|
||||
*
|
||||
* @brief Form to add/edit user group.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\roles\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\core\Request;
|
||||
use APP\facades\Repo;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\core\JSONMessage;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\form\Form;
|
||||
use PKP\security\Role;
|
||||
use PKP\security\RoleDAO;
|
||||
use PKP\security\Validation;
|
||||
use PKP\stageAssignment\StageAssignmentDAO;
|
||||
use PKP\userGroup\relationships\UserGroupStage;
|
||||
use PKP\workflow\WorkflowStageDAO;
|
||||
|
||||
class UserGroupForm extends Form
|
||||
{
|
||||
/** @var int Id of the user group being edited */
|
||||
public $_userGroupId;
|
||||
|
||||
/** @var int The context of the user group being edited */
|
||||
public $_contextId;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $contextId id.
|
||||
* @param int $userGroupId group id.
|
||||
*/
|
||||
public function __construct($contextId, $userGroupId = null)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/roles/form/userGroupForm.tpl');
|
||||
$this->_contextId = $contextId;
|
||||
$this->_userGroupId = $userGroupId;
|
||||
|
||||
// Validation checks for this form
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorLocale($this, 'name', 'required', 'settings.roles.nameRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorLocale($this, 'abbrev', 'required', 'settings.roles.abbrevRequired'));
|
||||
if ($this->getUserGroupId() == null) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'roleId', 'required', 'settings.roles.roleIdRequired'));
|
||||
}
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
//
|
||||
// Getters and Setters
|
||||
//
|
||||
/**
|
||||
* Get the user group id.
|
||||
*
|
||||
* @return int userGroupId
|
||||
*/
|
||||
public function getUserGroupId()
|
||||
{
|
||||
return $this->_userGroupId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context id.
|
||||
*
|
||||
* @return int contextId
|
||||
*/
|
||||
public function getContextId()
|
||||
{
|
||||
return $this->_contextId;
|
||||
}
|
||||
|
||||
//
|
||||
// Implement template methods from Form.
|
||||
//
|
||||
/**
|
||||
* Get all locale field names
|
||||
*/
|
||||
public function getLocaleFieldNames()
|
||||
{
|
||||
return ['name', 'abbrev'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::initData()
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$userGroup = Repo::userGroup()->get($this->getUserGroupId());
|
||||
$stages = WorkflowStageDAO::getWorkflowStageTranslationKeys();
|
||||
$this->setData('stages', $stages);
|
||||
$this->setData('assignedStages', []); // sensible default
|
||||
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
$jsonMessage = new JSONMessage();
|
||||
$jsonMessage->setContent($roleDao->getForbiddenStages());
|
||||
$this->setData('roleForbiddenStagesJSON', $jsonMessage->getString());
|
||||
|
||||
if ($userGroup) {
|
||||
$assignedStages = Repo::userGroup()->getAssignedStagesByUserGroupId($this->getContextId(), $userGroup->getId())->toArray();
|
||||
|
||||
$data = [
|
||||
'userGroupId' => $userGroup->getId(),
|
||||
'roleId' => $userGroup->getRoleId(),
|
||||
'name' => $userGroup->getName(null), //Localized
|
||||
'abbrev' => $userGroup->getAbbrev(null), //Localized
|
||||
'assignedStages' => $assignedStages,
|
||||
'showTitle' => $userGroup->getShowTitle(),
|
||||
'permitSelfRegistration' => $userGroup->getPermitSelfRegistration(),
|
||||
'permitMetadataEdit' => $userGroup->getPermitMetadataEdit(),
|
||||
'recommendOnly' => $userGroup->getRecommendOnly(),
|
||||
];
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$this->setData($field, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['roleId', 'name', 'abbrev', 'assignedStages', 'showTitle', 'permitSelfRegistration', 'recommendOnly', 'permitMetadataEdit']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::fetch()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
$templateMgr->assign('roleOptions', Application::getRoleNames(true));
|
||||
|
||||
// Users can't edit the role once user group is created.
|
||||
// userGroupId is 0 for new User Groups because it is cast to int in UserGroupGridHandler.
|
||||
$disableRoleSelect = ($this->getUserGroupId() > 0) ? true : false;
|
||||
$templateMgr->assign('disableRoleSelect', $disableRoleSelect);
|
||||
$templateMgr->assign('selfRegistrationRoleIds', $this->getPermitSelfRegistrationRoles());
|
||||
$templateMgr->assign('recommendOnlyRoleIds', $this->getRecommendOnlyRoles());
|
||||
$templateMgr->assign('notChangeMetadataEditPermissionRoles', Repo::userGroup()::NOT_CHANGE_METADATA_EDIT_PERMISSION_ROLES);
|
||||
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of roles optionally permitting user self-registration.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPermitSelfRegistrationRoles()
|
||||
{
|
||||
return [Role::ROLE_ID_REVIEWER, Role::ROLE_ID_AUTHOR, Role::ROLE_ID_READER];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of roles optionally permitting recommendOnly option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRecommendOnlyRoles()
|
||||
{
|
||||
return [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionParams)
|
||||
{
|
||||
parent::execute(...$functionParams);
|
||||
|
||||
$request = Application::get()->getRequest();
|
||||
$userGroupId = $this->getUserGroupId();
|
||||
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
|
||||
// Check if we are editing an existing user group or creating another one.
|
||||
if ($userGroupId == null) {
|
||||
$userGroup = Repo::userGroup()->newDataObject();
|
||||
$userGroup->setRoleId($this->getData('roleId'));
|
||||
$userGroup->setContextId($this->getContextId());
|
||||
$userGroup->setDefault(false);
|
||||
$userGroup->setShowTitle(is_null($this->getData('showTitle')) ? false : $this->getData('showTitle'));
|
||||
$userGroup->setPermitSelfRegistration($this->getData('permitSelfRegistration') && in_array($userGroup->getRoleId(), $this->getPermitSelfRegistrationRoles()));
|
||||
$userGroup->setPermitMetadataEdit($this->getData('permitMetadataEdit') && !in_array($this->getData('roleId'), Repo::userGroup()::NOT_CHANGE_METADATA_EDIT_PERMISSION_ROLES));
|
||||
if (in_array($this->getData('roleId'), Repo::userGroup()::NOT_CHANGE_METADATA_EDIT_PERMISSION_ROLES)) {
|
||||
$userGroup->setPermitMetadataEdit(true);
|
||||
}
|
||||
|
||||
$userGroup->setRecommendOnly($this->getData('recommendOnly') && in_array($userGroup->getRoleId(), $this->getRecommendOnlyRoles()));
|
||||
$userGroup = $this->_setUserGroupLocaleFields($userGroup, $request);
|
||||
|
||||
$userGroupId = Repo::userGroup()->add($userGroup);
|
||||
} else {
|
||||
$userGroup = Repo::userGroup()->get($userGroupId);
|
||||
$userGroup = $this->_setUserGroupLocaleFields($userGroup, $request);
|
||||
$userGroup->setShowTitle(is_null($this->getData('showTitle')) ? false : $this->getData('showTitle'));
|
||||
$userGroup->setPermitSelfRegistration($this->getData('permitSelfRegistration') && in_array($userGroup->getRoleId(), $this->getPermitSelfRegistrationRoles()));
|
||||
$userGroup->setPermitMetadataEdit($this->getData('permitMetadataEdit') && !in_array($userGroup->getRoleId(), Repo::userGroup()::NOT_CHANGE_METADATA_EDIT_PERMISSION_ROLES));
|
||||
if (in_array($userGroup->getRoleId(), Repo::userGroup()::NOT_CHANGE_METADATA_EDIT_PERMISSION_ROLES)) {
|
||||
$userGroup->setPermitMetadataEdit(true);
|
||||
} else {
|
||||
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO'); /** @var StageAssignmentDAO $stageAssignmentDao */
|
||||
$allUserAssignments = $stageAssignmentDao
|
||||
->getByUserGroupId($userGroupId, $this->getContextId())
|
||||
->toAssociativeArray();
|
||||
|
||||
foreach ($allUserAssignments as $userAssignment) {
|
||||
$userAssignment->setCanChangeMetadata($userGroup->getPermitMetadataEdit());
|
||||
$stageAssignmentDao->updateObject($userAssignment);
|
||||
}
|
||||
}
|
||||
|
||||
$userGroup->setRecommendOnly($this->getData('recommendOnly') && in_array($userGroup->getRoleId(), $this->getRecommendOnlyRoles()));
|
||||
|
||||
Repo::userGroup()->edit($userGroup, []);
|
||||
}
|
||||
|
||||
// After we have created/edited the user group, we assign/update its stages.
|
||||
$assignedStages = $this->getData('assignedStages');
|
||||
// Always set all stages active for some permission levels.
|
||||
if (in_array($userGroup->getRoleId(), $roleDao->getAlwaysActiveStages())) {
|
||||
$assignedStages = array_keys(WorkflowStageDAO::getWorkflowStageTranslationKeys());
|
||||
}
|
||||
if ($assignedStages) {
|
||||
$this->_assignStagesToUserGroup($userGroupId, $assignedStages);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Private helper methods
|
||||
//
|
||||
/**
|
||||
* Setup the stages assignments to a user group in database.
|
||||
*
|
||||
* @param int $userGroupId User group id that will receive the stages.
|
||||
* @param array $userAssignedStages of stages currently assigned to a user.
|
||||
*/
|
||||
public function _assignStagesToUserGroup($userGroupId, $userAssignedStages)
|
||||
{
|
||||
$contextId = $this->getContextId();
|
||||
|
||||
// Current existing workflow stages.
|
||||
$stages = WorkflowStageDAO::getWorkflowStageTranslationKeys();
|
||||
|
||||
foreach (array_keys($stages) as $stageId) {
|
||||
Repo::userGroup()->removeGroupFromStage($contextId, $userGroupId, $stageId);
|
||||
}
|
||||
|
||||
foreach ($userAssignedStages as $stageId) {
|
||||
// Make sure we don't assign forbidden stages based on
|
||||
// user groups role id. Override in case of some permission levels.
|
||||
$roleId = $this->getData('roleId');
|
||||
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
|
||||
$forbiddenStages = $roleDao->getForbiddenStages($roleId);
|
||||
if (in_array($stageId, $forbiddenStages) && !in_array($roleId, $roleDao->getAlwaysActiveStages())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if is a valid stage.
|
||||
if (in_array($stageId, array_keys($stages))) {
|
||||
UserGroupStage::create([
|
||||
'contextId' => $contextId,
|
||||
'userGroupId' => $userGroupId,
|
||||
'stageId' => $stageId
|
||||
]);
|
||||
} else {
|
||||
fatalError('Invalid stage id');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set locale fields on a User Group object.
|
||||
*
|
||||
* @param \PKP\userGroup\UserGroup $userGroup
|
||||
* @param Request $request
|
||||
*
|
||||
*/
|
||||
public function _setUserGroupLocaleFields($userGroup, $request): \PKP\userGroup\UserGroup
|
||||
{
|
||||
$router = $request->getRouter();
|
||||
$context = $router->getContext($request);
|
||||
$supportedLocales = $context->getSupportedLocaleNames();
|
||||
|
||||
if (!empty($supportedLocales)) {
|
||||
foreach ($context->getSupportedLocaleNames() as $localeKey => $localeName) {
|
||||
$name = $this->getData('name');
|
||||
$abbrev = $this->getData('abbrev');
|
||||
if (isset($name[$localeKey])) {
|
||||
$userGroup->setName($name[$localeKey], $localeKey);
|
||||
}
|
||||
if (isset($abbrev[$localeKey])) {
|
||||
$userGroup->setAbbrev($abbrev[$localeKey], $localeKey);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$localeKey = Locale::getLocale();
|
||||
$userGroup->setName($this->getData('name'), $localeKey);
|
||||
$userGroup->setAbbrev($this->getData('abbrev'), $localeKey);
|
||||
}
|
||||
|
||||
return $userGroup;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user