| Total Complexity | 43 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PermissionController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PermissionController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class PermissionController extends ActionController |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string prefix for session |
||
| 38 | */ |
||
| 39 | const SESSION_PREFIX = 'tx_Beuser_'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int the current page id |
||
| 43 | */ |
||
| 44 | protected $id; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $returnUrl = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $depth; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Number of levels to enable recursive settings for |
||
| 58 | * |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $getLevels = 10; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $pageInfo = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Backend Template Container |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $defaultViewObjectName = BackendTemplateView::class; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * BackendTemplateContainer |
||
| 77 | * |
||
| 78 | * @var BackendTemplateView |
||
| 79 | */ |
||
| 80 | protected $view; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Initialize action |
||
| 84 | */ |
||
| 85 | protected function initializeAction() |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Initializes view |
||
| 116 | * |
||
| 117 | * @param ViewInterface $view The view to be initialized |
||
| 118 | */ |
||
| 119 | protected function initializeView(ViewInterface $view) |
||
| 120 | { |
||
| 121 | parent::initializeView($view); |
||
| 122 | $this->setPageInfo(); |
||
| 123 | |||
| 124 | // the view of the update action has a different view class |
||
| 125 | if ($view instanceof BackendTemplateView) { |
||
| 126 | $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Beuser/Permissions'); |
||
| 127 | $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip'); |
||
| 128 | |||
| 129 | $this->registerDocHeaderButtons(); |
||
| 130 | $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($this->pageInfo); |
||
| 131 | $this->view->getModuleTemplate()->setFlashMessageQueue($this->getFlashMessageQueue()); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Registers the Icons into the docheader |
||
| 137 | * |
||
| 138 | * @throws \InvalidArgumentException |
||
| 139 | */ |
||
| 140 | protected function registerDocHeaderButtons() |
||
| 141 | { |
||
| 142 | /** @var ButtonBar $buttonBar */ |
||
| 143 | $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar(); |
||
| 144 | $currentRequest = $this->request; |
||
| 145 | $lang = $this->getLanguageService(); |
||
| 146 | |||
| 147 | if ($currentRequest->getControllerActionName() === 'edit') { |
||
| 148 | // CLOSE button: |
||
| 149 | if (!empty($this->returnUrl)) { |
||
| 150 | $closeButton = $buttonBar->makeLinkButton() |
||
| 151 | ->setHref($this->returnUrl) |
||
| 152 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.closeDoc')) |
||
| 153 | ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon( |
||
| 154 | 'actions-close', |
||
| 155 | Icon::SIZE_SMALL |
||
| 156 | )); |
||
| 157 | $buttonBar->addButton($closeButton); |
||
| 158 | } |
||
| 159 | |||
| 160 | // SAVE button: |
||
| 161 | $saveButton = $buttonBar->makeInputButton() |
||
| 162 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc')) |
||
| 163 | ->setName('tx_beuser_system_beusertxpermission[submit]') |
||
| 164 | ->setValue('Save') |
||
| 165 | ->setForm('PermissionControllerEdit') |
||
| 166 | ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon( |
||
| 167 | 'actions-document-save', |
||
| 168 | Icon::SIZE_SMALL |
||
| 169 | )) |
||
| 170 | ->setShowLabelText(true); |
||
| 171 | |||
| 172 | $buttonBar->addButton($saveButton); |
||
| 173 | } |
||
| 174 | |||
| 175 | $shortcutButton = $buttonBar->makeShortcutButton() |
||
| 176 | ->setRouteIdentifier('system_BeuserTxPermission') |
||
| 177 | ->setDisplayName($this->getShortcutTitle()) |
||
| 178 | ->setArguments(['id' => (int)$this->id]); |
||
| 179 | $buttonBar->addButton($shortcutButton); |
||
| 180 | |||
| 181 | $helpButton = $buttonBar->makeHelpButton() |
||
| 182 | ->setModuleName('xMOD_csh_corebe') |
||
| 183 | ->setFieldName('perm_module'); |
||
| 184 | |||
| 185 | $buttonBar->addButton($helpButton); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Index action |
||
| 190 | */ |
||
| 191 | public function indexAction() |
||
| 192 | { |
||
| 193 | if (!$this->id) { |
||
| 194 | $this->pageInfo = ['title' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], 'uid' => 0, 'pid' => 0]; |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($this->getBackendUser()->workspace != 0) { |
||
| 198 | // Adding section with the permission setting matrix: |
||
| 199 | $this->addFlashMessage( |
||
| 200 | LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser') ?? '', |
||
| 201 | LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser') ?? '', |
||
| 202 | FlashMessage::WARNING |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | // depth options |
||
| 207 | $depthOptions = []; |
||
| 208 | $url = $this->uriBuilder->reset()->setArguments([ |
||
| 209 | 'action' => 'index', |
||
| 210 | 'depth' => '${value}', |
||
| 211 | 'id' => $this->id |
||
| 212 | ])->buildBackendUri(); |
||
| 213 | foreach ([1, 2, 3, 4, 10] as $depthLevel) { |
||
| 214 | $levelLabel = $depthLevel === 1 ? 'level' : 'levels'; |
||
| 215 | $depthOptions[$depthLevel] = $depthLevel . ' ' . LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $levelLabel, 'beuser'); |
||
| 216 | } |
||
| 217 | $this->view->assign('currentId', $this->id); |
||
| 218 | $this->view->assign('depthBaseUrl', $url); |
||
| 219 | $this->view->assign('depth', $this->depth); |
||
| 220 | $this->view->assign('depthOptions', $depthOptions); |
||
| 221 | |||
| 222 | $beUserArray = BackendUtility::getUserNames(); |
||
| 223 | $this->view->assign('beUsers', $beUserArray); |
||
| 224 | $beGroupArray = BackendUtility::getGroupNames(); |
||
| 225 | $this->view->assign('beGroups', $beGroupArray); |
||
| 226 | |||
| 227 | /** @var PageTreeView $tree */ |
||
| 228 | $tree = GeneralUtility::makeInstance(PageTreeView::class); |
||
| 229 | $tree->init(); |
||
| 230 | $tree->addField('perms_user', true); |
||
| 231 | $tree->addField('perms_group', true); |
||
| 232 | $tree->addField('perms_everybody', true); |
||
| 233 | $tree->addField('perms_userid', true); |
||
| 234 | $tree->addField('perms_groupid', true); |
||
| 235 | $tree->addField('hidden'); |
||
| 236 | $tree->addField('fe_group'); |
||
| 237 | $tree->addField('starttime'); |
||
| 238 | $tree->addField('endtime'); |
||
| 239 | $tree->addField('editlock'); |
||
| 240 | |||
| 241 | // Create the tree from $this->id |
||
| 242 | if ($this->id) { |
||
| 243 | $tree->tree[] = ['row' => $this->pageInfo, 'HTML' => $tree->getIcon($this->pageInfo)]; |
||
| 244 | } else { |
||
| 245 | $tree->tree[] = ['row' => $this->pageInfo, 'HTML' => $tree->getRootIcon($this->pageInfo)]; |
||
| 246 | } |
||
| 247 | $tree->getTree($this->id, $this->depth); |
||
| 248 | $this->view->assign('viewTree', $tree->tree); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Edit action |
||
| 253 | */ |
||
| 254 | public function editAction() |
||
| 255 | { |
||
| 256 | $this->view->assign('id', $this->id); |
||
| 257 | $this->view->assign('depth', $this->depth); |
||
| 258 | |||
| 259 | if (!$this->id) { |
||
| 260 | $this->pageInfo = ['title' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], 'uid' => 0, 'pid' => 0]; |
||
| 261 | } |
||
| 262 | if ($this->getBackendUser()->workspace != 0) { |
||
| 263 | // Adding FlashMessage with the permission setting matrix: |
||
| 264 | $this->addFlashMessage( |
||
| 265 | LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser') ?? '', |
||
| 266 | LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser') ?? '', |
||
| 267 | FlashMessage::WARNING |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | // Get user names and group names |
||
| 271 | $beGroupArray = BackendUtility::getGroupNames(); |
||
| 272 | $beUserArray = BackendUtility::getUserNames(); |
||
| 273 | |||
| 274 | // Owner selector |
||
| 275 | $beUserDataArray = [0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')]; |
||
| 276 | foreach ($beUserArray as $uid => &$row) { |
||
| 277 | $beUserDataArray[$uid] = $row['username']; |
||
| 278 | } |
||
| 279 | $beUserDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser'); |
||
| 280 | $this->view->assign('currentBeUser', $this->pageInfo['perms_userid']); |
||
| 281 | $this->view->assign('beUserData', $beUserDataArray); |
||
| 282 | |||
| 283 | // Group selector |
||
| 284 | $beGroupDataArray = [0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')]; |
||
| 285 | foreach ($beGroupArray as $uid => $row) { |
||
| 286 | $beGroupDataArray[$uid] = $row['title']; |
||
| 287 | } |
||
| 288 | $beGroupDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser'); |
||
| 289 | $this->view->assign('currentBeGroup', $this->pageInfo['perms_groupid']); |
||
| 290 | $this->view->assign('beGroupData', $beGroupDataArray); |
||
| 291 | $this->view->assign('pageInfo', $this->pageInfo); |
||
| 292 | $this->view->assign('returnUrl', $this->returnUrl); |
||
| 293 | $this->view->assign('recursiveSelectOptions', $this->getRecursiveSelectOptions()); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Update action |
||
| 298 | * |
||
| 299 | * @param array $data |
||
| 300 | * @param array $mirror |
||
| 301 | */ |
||
| 302 | protected function updateAction(array $data, array $mirror) |
||
| 303 | { |
||
| 304 | $dataHandlerInput = []; |
||
| 305 | // Prepare the input data for data handler |
||
| 306 | if (!empty($data['pages'])) { |
||
| 307 | foreach ($data['pages'] as $pageUid => $properties) { |
||
| 308 | // if the owner and group field shouldn't be touched, unset the option |
||
| 309 | if ((int)$properties['perms_userid'] === -1) { |
||
| 310 | unset($properties['perms_userid']); |
||
| 311 | } |
||
| 312 | if ((int)$properties['perms_groupid'] === -1) { |
||
| 313 | unset($properties['perms_groupid']); |
||
| 314 | } |
||
| 315 | $dataHandlerInput[$pageUid] = $properties; |
||
| 316 | if (!empty($mirror['pages'][$pageUid])) { |
||
| 317 | $mirrorPages = GeneralUtility::intExplode(',', $mirror['pages'][$pageUid]); |
||
| 318 | foreach ($mirrorPages as $mirrorPageUid) { |
||
| 319 | $dataHandlerInput[$mirrorPageUid] = $properties; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | $dataHandler = GeneralUtility::makeInstance(DataHandler::class); |
||
| 326 | $dataHandler->start( |
||
| 327 | [ |
||
| 328 | 'pages' => $dataHandlerInput |
||
| 329 | ], |
||
| 330 | [] |
||
| 331 | ); |
||
| 332 | $dataHandler->process_datamap(); |
||
| 333 | |||
| 334 | $this->redirectToUri($this->returnUrl); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication |
||
| 339 | */ |
||
| 340 | protected function getBackendUser() |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Finding tree and offer setting of values recursively. |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | protected function getRecursiveSelectOptions() |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Check if page record exists and set pageInfo |
||
| 389 | */ |
||
| 390 | protected function setPageInfo(): void |
||
| 391 | { |
||
| 392 | $this->pageInfo = BackendUtility::readPageAccess(BackendUtility::getRecord('pages', $this->id) ? $this->id : 0, ' 1=1') ?: []; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Returns LanguageService |
||
| 397 | * |
||
| 398 | * @return \TYPO3\CMS\Core\Localization\LanguageService |
||
| 399 | */ |
||
| 400 | protected function getLanguageService() |
||
| 401 | { |
||
| 402 | return $GLOBALS['LANG']; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the shortcut title for the current page |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function getShortcutTitle(): string |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | } |
||
| 420 |