| Total Complexity | 66 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PermissionHandler 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 PermissionHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 35 | class PermissionHandler extends \XoopsPersistableObjectHandler |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Constructor |
||
| 39 | * |
||
| 40 | * @param null |
||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 43 | { |
||
| 44 | } |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @private function getPermSubmit |
||
| 49 | * returns right to submit for given perm |
||
| 50 | * @param $constantPerm |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | private function getPerm($constantPerm) |
||
| 54 | { |
||
| 55 | global $xoopsUser; |
||
| 56 | |||
| 57 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 58 | $mid = XoopsModule::getByDirname($moduleDirName)->mid(); |
||
| 59 | $currentuid = 0; |
||
| 60 | if (isset($xoopsUser) && \is_object($xoopsUser)) { |
||
| 61 | if ($xoopsUser->isAdmin($mid)) { |
||
| 62 | return true; |
||
| 63 | } |
||
| 64 | $currentuid = $xoopsUser->uid(); |
||
| 65 | } |
||
| 66 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
| 67 | |||
| 68 | $memberHandler = \xoops_getHandler('member'); |
||
| 69 | if (0 == $currentuid) { |
||
| 70 | $my_group_ids = [\XOOPS_GROUP_ANONYMOUS]; |
||
| 71 | } else { |
||
| 72 | $my_group_ids = $memberHandler->getGroupsByUser($currentuid); |
||
|
|
|||
| 73 | } |
||
| 74 | if ($grouppermHandler->checkRight('wgevents_ac', $constantPerm, $my_group_ids, $mid)) { |
||
| 75 | return true; |
||
| 76 | } |
||
| 77 | |||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @private function getPermSubmit |
||
| 83 | * returns right to submit for given perm |
||
| 84 | * @param $constantPerm |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | private function getPermApprove($constantPerm) |
||
| 88 | { |
||
| 89 | if ($this->getPermGlobalApprove()) { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->getPerm($constantPerm); |
||
| 94 | } |
||
| 95 | /** |
||
| 96 | * @private function getPermSubmit |
||
| 97 | * returns right to submit for given perm |
||
| 98 | * @param $constantPerm |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | private function getPermSubmit($constantPerm) |
||
| 102 | { |
||
| 103 | if ($this->getPermGlobalSubmit()) { |
||
| 104 | return true; |
||
| 105 | } |
||
| 106 | |||
| 107 | return $this->getPerm($constantPerm); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @private function getPermView |
||
| 112 | * returns right for view of given perm |
||
| 113 | * @param $constantPerm |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | private function getPermView($constantPerm) |
||
| 117 | { |
||
| 118 | if ($this->getPermGlobalView()) { |
||
| 119 | return true; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this->getPerm($constantPerm); |
||
| 123 | } |
||
| 124 | /** |
||
| 125 | * @public function permGlobalApprove |
||
| 126 | * returns right for global approve |
||
| 127 | * |
||
| 128 | * @param null |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function getPermGlobalApprove() |
||
| 132 | { |
||
| 133 | return $this->getPerm(Constants::PERM_GLOBAL_APPROVE); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @public function permGlobalSubmit |
||
| 138 | * returns right for global submit |
||
| 139 | * |
||
| 140 | * @param null |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function getPermGlobalSubmit() |
||
| 144 | { |
||
| 145 | if ($this->getPermGlobalApprove()) { |
||
| 146 | return true; |
||
| 147 | } |
||
| 148 | return $this->getPerm(Constants::PERM_GLOBAL_SUBMIT); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @public function permGlobalView |
||
| 153 | * returns right for global view |
||
| 154 | * |
||
| 155 | * @param null |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function getPermGlobalView() |
||
| 159 | { |
||
| 160 | if ($this->getPermGlobalApprove() || $this->getPermGlobalSubmit()) { |
||
| 161 | return true; |
||
| 162 | } |
||
| 163 | return $this->getPerm(Constants::PERM_GLOBAL_APPROVE); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @public function getPermEventsApprove |
||
| 168 | * returns right for approve events |
||
| 169 | * @param null |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function getPermEventsApprove() |
||
| 173 | { |
||
| 174 | if ($this->getPermGlobalApprove()) { |
||
| 175 | return true; |
||
| 176 | } |
||
| 177 | return $this->getPermApprove(Constants::PERM_EVENTS_APPROVE); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @public function getPermEventsApproveAuto |
||
| 182 | * returns right for approve events |
||
| 183 | * @param null |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function getPermEventsApproveAuto() |
||
| 187 | { |
||
| 188 | if ($this->getPermEventsApprove()) { |
||
| 189 | return true; |
||
| 190 | } |
||
| 191 | return $this->getPermApprove(Constants::PERM_EVENTS_APPROVE_AUTO); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @public function getPermEventsSubmit |
||
| 196 | * returns right for submit events |
||
| 197 | * @param null |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function getPermEventsSubmit() |
||
| 201 | { |
||
| 202 | if ($this->getPermGlobalSubmit()) { |
||
| 203 | return true; |
||
| 204 | } |
||
| 205 | return $this->getPermSubmit(Constants::PERM_EVENTS_SUBMIT); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @public function getPermEventsEdit |
||
| 210 | * returns right for edit/delete events |
||
| 211 | * - User must have perm to submit and must be owner |
||
| 212 | * - transaction is not closed |
||
| 213 | * @param $evSubmitter |
||
| 214 | * @param $evStatus |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function getPermEventsEdit($evSubmitter, $evStatus) |
||
| 218 | { |
||
| 219 | global $xoopsUser, $xoopsModule; |
||
| 220 | |||
| 221 | if ($this->getPermEventsApprove()) { |
||
| 222 | return true; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (Constants::STATUS_LOCKED == $evStatus) { |
||
| 226 | return false; |
||
| 227 | } |
||
| 228 | |||
| 229 | $currentuid = 0; |
||
| 230 | if (isset($xoopsUser) && \is_object($xoopsUser)) { |
||
| 231 | if (isset($xoopsModule) && \is_object($xoopsModule) && $xoopsUser->isAdmin($xoopsModule->mid())) { |
||
| 232 | return true; |
||
| 233 | } |
||
| 234 | $currentuid = $xoopsUser->uid(); |
||
| 235 | } |
||
| 236 | return $this->getPermEventsSubmit() && $currentuid == $evSubmitter; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @public function getPermEventsView |
||
| 241 | * returns right for view Event |
||
| 242 | * @param null |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function getPermEventsView() |
||
| 246 | { |
||
| 247 | return $this->getPermView(Constants::PERM_EVENTS_VIEW); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @public function getPermQuestionsAdmin |
||
| 252 | * returns right for submit/edit/delete questions |
||
| 253 | * - User must have perm edit the event |
||
| 254 | * @param $evSubmitter |
||
| 255 | * @param $evStatus |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function getPermQuestionsAdmin($evSubmitter, $evStatus) |
||
| 259 | { |
||
| 260 | if ($this->getPermEventsEdit($evSubmitter, $evStatus)) { |
||
| 261 | return true; |
||
| 262 | } |
||
| 263 | |||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @public function getPermRegistrationsApprove |
||
| 269 | * returns right for approve registrations |
||
| 270 | * - User must have perm edit the event (owner or approver) |
||
| 271 | * @param $evSubmitter |
||
| 272 | * @param $evStatus |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function getPermRegistrationsApprove($evSubmitter, $evStatus) |
||
| 276 | { |
||
| 277 | if ($this->getPermEventsEdit($evSubmitter, $evStatus)) { |
||
| 278 | return true; |
||
| 279 | } |
||
| 280 | |||
| 281 | return false; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @public function getPermRegistrationsVerif |
||
| 286 | * returns right for submit registrations without question verification by mail |
||
| 287 | * - User must have perm to submit registrations |
||
| 288 | * @param null |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function getPermRegistrationsVerif() |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @public function getPermRegistrationsSubmit |
||
| 298 | * returns right for submit registrations |
||
| 299 | * @param null |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public function getPermRegistrationsSubmit() |
||
| 303 | { |
||
| 304 | if ($this->getPermRegistrationsVerif()) { |
||
| 305 | return true; |
||
| 306 | } |
||
| 307 | return $this->getPermSubmit(Constants::PERM_REGISTRATIONS_SUBMIT); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @public function getPermRegistrationsEdit |
||
| 312 | * returns right for edit/delete registrations |
||
| 313 | * - User have permission to edit the event |
||
| 314 | * - User must have perm to submit and must be owner |
||
| 315 | * @param $regIp |
||
| 316 | * @param $regSubmitter |
||
| 317 | * @param $evSubmitter |
||
| 318 | * @param $evStatus |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function getPermRegistrationsEdit($regIp, $regSubmitter, $evSubmitter, $evStatus) |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @public function getPermRegistrationsView |
||
| 336 | * returns right for view Registration |
||
| 337 | * - user must be the submitter_text of registration or same IP must be used |
||
| 338 | * @param $regIp |
||
| 339 | * @param $regSubmitter |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function getPermRegistrationsView($regIp, $regSubmitter) |
||
| 343 | { |
||
| 344 | $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0; |
||
| 345 | if ($regSubmitter == $uidCurrent) { |
||
| 346 | return true; |
||
| 347 | } |
||
| 348 | $ipCurrent = $_SERVER['REMOTE_ADDR']; |
||
| 349 | return $regIp == $ipCurrent; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @public function getPermTextblocksSubmit |
||
| 354 | * returns right for submit textblocks |
||
| 355 | * @param null |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public function getPermTextblocksSubmit() |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @public function getPermTextblocksAdmin |
||
| 365 | * returns right for edit/delete textblocks |
||
| 366 | * - User must have perm to submit and must be owner |
||
| 367 | * @param $tbSubmitter |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | public function getPermTextblocksEdit($tbSubmitter) |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @public function getPermTextblocksAdmin |
||
| 390 | * returns right for edit/delete textblocks |
||
| 391 | * - User must have perm to submit and must be owner |
||
| 392 | * @param $tbSubmitter |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function getPermTextblocksAdmin($tbSubmitter) |
||
| 411 | } |
||
| 412 | |||
| 413 | |||
| 414 | |||
| 415 | |||
| 416 | |||
| 516 |