| Total Complexity | 65 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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); |
||
| 29 | class PermissionHandler extends \XoopsGroupPermHandler |
||
| 30 | { |
||
| 31 | protected Cache $cacheHelper; |
||
| 32 | /** @var array|null */ |
||
| 33 | private array $_handler; |
||
| 34 | |||
| 35 | /** @var Helper|null $helper |
||
| 36 | * @readonly */ |
||
| 37 | private ?Helper $helper; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param \XoopsDatabase|null $db |
||
| 41 | * @param Helper|null $helper |
||
| 42 | */ |
||
| 43 | public function __construct(\XoopsDatabase $db = null, Helper $helper = null) |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $name |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | public function loadHandler(string $name) |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param bool $fullname |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function getValidForumPerms(bool $fullname = false): array |
||
| 75 | { |
||
| 76 | /** @var PermissionForumHandler $handler */ |
||
| 77 | $handler = $this->loadHandler('Forum'); |
||
| 78 | |||
| 79 | return $handler->getValidPerms($fullname); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param int|Forum $forum |
||
| 84 | * @param bool $topic_locked |
||
| 85 | * @param bool $isAdmin |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function getPermissionTable($forum = 0, bool $topic_locked = false, bool $isAdmin = false): array |
||
| 89 | { |
||
| 90 | /** @var PermissionForumHandler $handler */ |
||
| 91 | $handler = $this->loadHandler('Forum'); |
||
| 92 | $perm = $handler->getPermissionTable($forum, $topic_locked, $isAdmin); |
||
| 93 | |||
| 94 | return $perm; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param int $forum_id |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function deleteByForum(int $forum_id): bool |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param int $cat_id |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function deleteByCategory(int $cat_id):bool |
||
| 115 | { |
||
| 116 | $this->cacheHelper->delete('permission_category'); |
||
| 117 | /** @var PermissionCategoryHandler $handler */ |
||
| 118 | $handler = $this->loadHandler('Category'); |
||
| 119 | |||
| 120 | return $handler->deleteByCategory($cat_id); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param int $category |
||
| 125 | * @param array $groups |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function setCategoryPermission(int $category, array $groups = []): bool |
||
| 129 | { |
||
| 130 | $this->cacheHelper->delete('permission_category'); |
||
| 131 | /** @var PermissionCategoryHandler $handler */ |
||
| 132 | $handler = $this->loadHandler('Category'); |
||
| 133 | |||
| 134 | return $handler->setCategoryPermission($category, $groups); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $type |
||
| 139 | * @param string $gperm_name |
||
| 140 | * @param int $id |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function getPermission(string $type, string $gperm_name = 'access', int $id = 0): bool |
||
| 144 | { |
||
| 145 | global $xoopsModule; |
||
| 146 | $ret = false; |
||
| 147 | if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) { |
||
| 148 | $ret = true; |
||
| 149 | } |
||
| 150 | |||
| 151 | $groups = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
| 152 | if (!$groups) { |
||
| 153 | $ret = false; |
||
| 154 | } |
||
| 155 | if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) { |
||
| 156 | $ret = false; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (\count(\array_intersect($allowed_groups, $groups)) > 0) { |
||
| 160 | $ret = true; |
||
| 161 | } |
||
| 162 | |||
| 163 | return $ret; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $permName |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function &getCategories(string $permName = 'access'): array |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $permName |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getForums(string $permName = 'access'): array |
||
| 182 | { |
||
| 183 | $ret = $this->getAllowedItems('forum', "forum_{$permName}"); |
||
| 184 | |||
| 185 | return $ret; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $type |
||
| 190 | * @param string $permName |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getAllowedItems(string $type, string $permName): array |
||
| 194 | { |
||
| 195 | $ret = []; |
||
| 196 | |||
| 197 | $groups = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
| 198 | if ((is_countable($groups) ? \count($groups) : 0) < 1) { |
||
| 199 | return $ret; |
||
| 200 | } |
||
| 201 | |||
| 202 | if (!$_cachedPerms = $this->loadPermData($permName)) { |
||
| 203 | return $ret; |
||
| 204 | } |
||
| 205 | |||
| 206 | $allowed_items = []; |
||
| 207 | foreach ($_cachedPerms as $id => $allowed_groups) { |
||
| 208 | if (0 == $id || empty($allowed_groups)) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | |||
| 212 | if (\array_intersect($groups, $allowed_groups)) { |
||
| 213 | $allowed_items[$id] = 1; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | unset($_cachedPerms); |
||
| 217 | $ret = \array_keys($allowed_items); |
||
| 218 | |||
| 219 | return $ret; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $gperm_name |
||
| 224 | * @param int $id |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function getGroups(string $gperm_name, int $id = 0): array |
||
| 228 | { |
||
| 229 | $_cachedPerms = $this->loadPermData($gperm_name); |
||
| 230 | $groups = empty($_cachedPerms[$id]) ? [] : \array_unique($_cachedPerms[$id]); |
||
| 231 | unset($_cachedPerms); |
||
| 232 | |||
| 233 | return $groups; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $permName |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function createPermData(string $permName = 'forum_all'): array |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $permName |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | public function &loadPermData(string $permName = 'forum_access'): array |
||
| 297 | { |
||
| 298 | if (!$perms = $this->cacheHelper->read("permission_{$permName}")) { |
||
| 299 | $perms = $this->createPermData($permName); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $perms; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $perm |
||
| 307 | * @param int $itemid |
||
| 308 | * @param int $groupid |
||
| 309 | * @param int|null $mid |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function validateRight(string $perm, int $itemid, int $groupid, ?int $mid = null): bool |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check permission (directly) |
||
| 336 | * |
||
| 337 | * @param string $gperm_name Name of permission |
||
| 338 | * @param int $gperm_itemid ID of an item |
||
| 339 | * @param int|array $gperm_groupid A group ID or an array of group IDs |
||
| 340 | * @param int $gperm_modid ID of a module |
||
| 341 | * |
||
| 342 | * @return bool TRUE if permission is enabled |
||
| 343 | */ |
||
| 344 | public function myCheckRight(string $gperm_name, int $gperm_itemid, $gperm_groupid, int $gperm_modid = 1): bool |
||
| 345 | { |
||
| 346 | $ret = false; |
||
| 347 | $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid)); |
||
| 348 | $criteria->add(new \Criteria('gperm_name', $gperm_name)); |
||
| 349 | $gperm_itemid = (int)$gperm_itemid; |
||
| 350 | if ($gperm_itemid > 0) { |
||
| 351 | $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid)); |
||
| 352 | } |
||
| 353 | if (\is_array($gperm_groupid)) { |
||
| 354 | $criteria2 = new \CriteriaCompo(); |
||
| 355 | foreach ($gperm_groupid as $gid) { |
||
| 356 | $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR'); |
||
| 357 | } |
||
| 358 | $criteria->add($criteria2); |
||
| 359 | } else { |
||
| 360 | $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid)); |
||
| 361 | } |
||
| 362 | if ($this->getCount($criteria) > 0) { |
||
| 363 | $ret = true; |
||
| 364 | } |
||
| 365 | |||
| 366 | return $ret; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $perm |
||
| 371 | * @param int $itemid |
||
| 372 | * @param int $groupid |
||
| 373 | * @param int|null $mid |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function deleteRight(string $perm, int $itemid, int $groupid, ?int $mid = null): bool |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param int $forum |
||
| 410 | * @param int $mid |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function applyTemplate(int $forum, int $mid = 0): bool |
||
| 414 | { |
||
| 415 | $this->cacheHelper->delete('permission_forum'); |
||
| 416 | /** @var PermissionForumHandler $handler */ |
||
| 417 | $handler = $this->loadHandler('Forum'); |
||
| 418 | |||
| 419 | return $handler->applyTemplate($forum, $mid); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | public function getTemplate(): array |
||
| 426 | { |
||
| 427 | /** @var PermissionForumHandler $handler */ |
||
| 428 | $handler = $this->loadHandler('Forum'); |
||
| 429 | $template = $handler->getTemplate(); |
||
| 430 | |||
| 431 | return $template; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param array $perms |
||
| 436 | * @param int $groupid |
||
| 437 | * @return bool|int |
||
| 438 | */ |
||
| 439 | public function setTemplate(array $perms, int $groupid = 0) |
||
| 445 | } |
||
| 446 | } |
||
| 447 |