iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$this->getLanguageService()->includeLLFile('EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf');
}
/**
* The main dispatcher function. Collect data and prepare HTML output.
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function dispatch(ServerRequestInterface $request): ResponseInterface
{
$parsedBody = $request->getParsedBody();
$this->conf = [
'page' => $parsedBody['page'] ?? null,
'who' => $parsedBody['who'] ?? null,
'mode' => $parsedBody['mode'] ?? null,
'bits' => (int)($parsedBody['bits'] ?? 0),
'permissions' => (int)($parsedBody['permissions'] ?? 0),
'action' => $parsedBody['action'] ?? null,
'ownerUid' => (int)($parsedBody['ownerUid'] ?? 0),
'username' => $parsedBody['username'] ?? null,
'groupUid' => (int)($parsedBody['groupUid'] ?? 0),
'groupname' => $parsedBody['groupname'] ?? '',
'editLockState' => (int)($parsedBody['editLockState'] ?? 0),
'new_owner_uid' => (int)($parsedBody['newOwnerUid'] ?? 0),
'new_group_uid' => (int)($parsedBody['newGroupUid'] ?? 0),
];
$extPath = ExtensionManagementUtility::extPath('beuser');
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setPartialRootPaths(['default' => ExtensionManagementUtility::extPath('beuser') . 'Resources/Private/Partials']);
$view->assign('pageId', $this->conf['page']);
$response = new HtmlResponse('');
// Basic test for required value
if ($this->conf['page'] <= 0) {
$response->getBody()->write('This script cannot be called directly');
return $response->withStatus(500);
}
$content = '';
// Init TCE for execution of update
$tce = GeneralUtility::makeInstance(DataHandler::class);
// Determine the scripts to execute
switch ($this->conf['action']) {
case 'show_change_owner_selector':
$content = $this->renderUserSelector($this->conf['page'], $this->conf['ownerUid'], $this->conf['username']);
break;
case 'change_owner':
$userId = $this->conf['new_owner_uid'];
if (is_int($userId)) {
// Prepare data to change
$data = [];
$data['pages'][$this->conf['page']]['perms_userid'] = $userId;
// Execute TCE Update
$tce->start($data, []);
$tce->process_datamap();
$view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/PermissionAjax/ChangeOwner.html');
$view->assign('userId', $userId);
$usernameArray = BackendUtility::getUserNames('username', ' AND uid = ' . $userId);
$view->assign('username', $usernameArray[$userId]['username']);
$content = $view->render();
} else {
$response->getBody()->write('An error occurred: No page owner uid specified');
$response = $response->withStatus(500);
}
break;
case 'show_change_group_selector':
$content = $this->renderGroupSelector($this->conf['page'], $this->conf['groupUid'], $this->conf['groupname']);
break;
case 'change_group':
$groupId = $this->conf['new_group_uid'];
if (is_int($groupId)) {
// Prepare data to change
$data = [];
$data['pages'][$this->conf['page']]['perms_groupid'] = $groupId;
// Execute TCE Update
$tce->start($data, []);
$tce->process_datamap();
$view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/PermissionAjax/ChangeGroup.html');
$view->assign('groupId', $groupId);
$groupnameArray = BackendUtility::getGroupNames('title', ' AND uid = ' . $groupId);
$view->assign('groupname', $groupnameArray[$groupId]['title']);
$content = $view->render();
} else {
$response->getBody()->write('An error occurred: No page group uid specified');
$response = $response->withStatus(500);
}
break;
case 'toggle_edit_lock':
// Prepare data to change
$data = [];
$data['pages'][$this->conf['page']]['editlock'] = $this->conf['editLockState'] === 1 ? 0 : 1;
// Execute TCE Update
$tce->start($data, []);
$tce->process_datamap();
$content = $this->renderToggleEditLock($this->conf['page'], $data['pages'][$this->conf['page']]['editlock']);
break;
default:
if ($this->conf['mode'] === 'delete') {
$this->conf['permissions'] = (int)($this->conf['permissions'] - $this->conf['bits']);
} else {
$this->conf['permissions'] = (int)($this->conf['permissions'] + $this->conf['bits']);
}
// Prepare data to change
$data = [];
$data['pages'][$this->conf['page']]['perms_' . $this->conf['who']] = $this->conf['permissions'];
// Execute TCE Update
$tce->start($data, []);
$tce->process_datamap();
$view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/PermissionAjax/ChangePermission.html');
$view->assign('permission', $this->conf['permissions']);
$view->assign('scope', $this->conf['who']);
$content = $view->render();
}
$response->getBody()->write($content);
return $response;
}
/**
* Generate the user selector element
*
* @param int $page The page id to change the user for
* @param int $ownerUid The page owner uid
* @param string $username The username to display
* @return string The html select element
*/
protected function renderUserSelector($page, $ownerUid, $username = '')
{
$page = (int)$page;
$ownerUid = (int)$ownerUid;
// Get usernames
$beUsers = BackendUtility::getUserNames();
// Owner selector:
$options = '';
// Loop through the users
foreach ($beUsers as $uid => $row) {
$uid = (int)$uid;
$selected = $uid === $ownerUid ? ' selected="selected"' : '';
$options .= '';
}
$elementId = 'o_' . $page;
$options = '' . $options;
$selector = '';
$saveButton = ''
. $this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL)->render()
. '';
$cancelButton = ''
. $this->iconFactory->getIcon('actions-close', Icon::SIZE_SMALL)->render()
. '';
return ''
. $selector
. ''
. $saveButton
. $cancelButton
. ''
. '';
}
/**
* Generate the group selector element
*
* @param int $page The page id to change the user for
* @param int $groupUid The page group uid
* @param string $groupname The groupname to display
* @return string The html select element
*/
protected function renderGroupSelector($page, $groupUid, $groupname = '')
{
$page = (int)$page;
$groupUid = (int)$groupUid;
// Get group names
$beGroupsO = $beGroups = BackendUtility::getGroupNames();
// Group selector:
$options = '';
// flag: is set if the page-groupid equals one from the group-list
$userset = 0;
// Loop through the groups
foreach ($beGroups as $uid => $row) {
$uid = (int)$uid;
if ($uid === $groupUid) {
$userset = 1;
$selected = ' selected="selected"';
} else {
$selected = '';
}
$options .= '';
}
// If the group was not set AND there is a group for the page
if (!$userset && $groupUid) {
$options = '' . $options;
}
$elementId = 'g_' . $page;
$options = '' . $options;
$selector = '';
$saveButton = ''
. $this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL)->render()
. '';
$cancelButton = ''
. $this->iconFactory->getIcon('actions-close', Icon::SIZE_SMALL)->render()
. '';
return ''
. $selector
. ''
. $saveButton
. $cancelButton
. ''
. '';
}
/**
* Print the string with the new edit lock state of a page record
*
* @param int $page The TYPO3 page id
* @param string $editLockState The state of the TYPO3 page (locked, unlocked)
* @return string The new edit lock string wrapped in HTML
*/
protected function renderToggleEditLock($page, $editLockState)
{
$page = (int)$page;
if ($editLockState === 1) {
$ret = ''
. $this->iconFactory->getIcon('actions-lock', Icon::SIZE_SMALL)->render() . '';
} else {
$ret = ''
. $this->iconFactory->getIcon('actions-unlock', Icon::SIZE_SMALL)->render() . '';
}
return $ret;
}
/**
* @return \TYPO3\CMS\Core\Localization\LanguageService
*/
protected function getLanguageService()
{
return $GLOBALS['LANG'];
}
/**
* @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
*/
protected function getBackendUser()
{
return $GLOBALS['BE_USER'];
}
}