| Total Complexity | 62 |
| Total Lines | 398 |
| 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 |
||
| 30 | class PermissionHandler extends \XoopsGroupPermHandler |
||
| 31 | { |
||
| 32 | /** @var \Xmf\Module\Helper\Cache */ |
||
| 33 | protected $cacheHelper; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | private $_handler; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param \XoopsDatabase|null $db |
||
| 40 | */ |
||
| 41 | public function __construct(\XoopsDatabase $db = null) |
||
| 42 | { |
||
| 43 | $this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
||
| 44 | |||
| 45 | $this->db = $db; |
||
| 46 | parent::__construct($db); |
||
|
|
|||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param $name |
||
| 51 | * @return mixed |
||
| 52 | */ |
||
| 53 | public function loadHandler($name) |
||
| 54 | { |
||
| 55 | if (!isset($this->_handler[$name])) { |
||
| 56 | // require_once __DIR__ . "/permission.{$name}.php"; |
||
| 57 | $className = '\\XoopsModules\\Newbb\\Permission' . \ucfirst($name) . 'Handler'; |
||
| 58 | $this->_handler[$name] = new $className($this->db); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $this->_handler[$name]; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param bool $fullname |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | public function getValidForumPerms($fullname = false) |
||
| 69 | { |
||
| 70 | $handler = $this->loadHandler('Forum'); |
||
| 71 | |||
| 72 | return $handler->getValidPerms($fullname); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param int $forum |
||
| 77 | * @param bool $topic_locked |
||
| 78 | * @param bool $isAdmin |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false) |
||
| 82 | { |
||
| 83 | $handler = $this->loadHandler('Forum'); |
||
| 84 | $perm = $handler->getPermissionTable($forum, $topic_locked, $isAdmin); |
||
| 85 | |||
| 86 | return $perm; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param $forum_id |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function deleteByForum($forum_id) |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $cat_id |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function deleteByCategory($cat_id) |
||
| 106 | { |
||
| 107 | $this->cacheHelper->delete('permission_category'); |
||
| 108 | $handler = $this->loadHandler('Category'); |
||
| 109 | |||
| 110 | return $handler->deleteByCategory($cat_id); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $category |
||
| 115 | * @param array $groups |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | public function setCategoryPermission($category, array $groups = []) |
||
| 119 | { |
||
| 120 | $this->cacheHelper->delete('permission_category'); |
||
| 121 | $handler = $this->loadHandler('Category'); |
||
| 122 | |||
| 123 | return $handler->setCategoryPermission($category, $groups); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param $type |
||
| 128 | * @param string $gperm_name |
||
| 129 | * @param int $id |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function getPermission($type, $gperm_name = 'access', $id = 0) |
||
| 133 | { |
||
| 134 | global $xoopsModule; |
||
| 135 | $ret = false; |
||
| 136 | if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) { |
||
| 137 | $ret = true; |
||
| 138 | } |
||
| 139 | |||
| 140 | $groups = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
| 141 | if (!$groups) { |
||
| 142 | $ret = false; |
||
| 143 | } |
||
| 144 | if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) { |
||
| 145 | $ret = false; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (\count(\array_intersect($allowed_groups, $groups)) > 0) { |
||
| 149 | $ret = true; |
||
| 150 | } |
||
| 151 | |||
| 152 | return $ret; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $perm_name |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function &getCategories($perm_name = 'access') |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $perm_name |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function getForums($perm_name = 'access') |
||
| 171 | { |
||
| 172 | $ret = $this->getAllowedItems('forum', "forum_{$perm_name}"); |
||
| 173 | |||
| 174 | return $ret; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param $type |
||
| 179 | * @param $perm_name |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function getAllowedItems($type, $perm_name) |
||
| 183 | { |
||
| 184 | $ret = []; |
||
| 185 | |||
| 186 | $groups = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
| 187 | if (\count($groups) < 1) { |
||
| 188 | return $ret; |
||
| 189 | } |
||
| 190 | |||
| 191 | if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) { |
||
| 192 | return $ret; |
||
| 193 | } |
||
| 194 | |||
| 195 | $allowed_items = []; |
||
| 196 | foreach ($_cachedPerms as $id => $allowed_groups) { |
||
| 197 | if (0 == $id || empty($allowed_groups)) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (\array_intersect($groups, $allowed_groups)) { |
||
| 202 | $allowed_items[$id] = 1; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | unset($_cachedPerms); |
||
| 206 | $ret = \array_keys($allowed_items); |
||
| 207 | |||
| 208 | return $ret; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param $gperm_name |
||
| 213 | * @param int $id |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function getGroups($gperm_name, $id = 0) |
||
| 217 | { |
||
| 218 | $_cachedPerms = $this->loadPermData($gperm_name); |
||
| 219 | $groups = empty($_cachedPerms[$id]) ? [] : \array_unique($_cachedPerms[$id]); |
||
| 220 | unset($_cachedPerms); |
||
| 221 | |||
| 222 | return $groups; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $perm_name |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function createPermData($perm_name = 'forum_all') |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $perm_name |
||
| 281 | * @return array|mixed|null |
||
| 282 | */ |
||
| 283 | public function &loadPermData($perm_name = 'forum_access') |
||
| 284 | { |
||
| 285 | if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) { |
||
| 286 | $perms = $this->createPermData($perm_name); |
||
| 287 | } |
||
| 288 | |||
| 289 | return $perms; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $perm |
||
| 294 | * @param $itemid |
||
| 295 | * @param $groupid |
||
| 296 | * @param null $mid |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function validateRight($perm, $itemid, $groupid, $mid = null) |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Check permission (directly) |
||
| 323 | * |
||
| 324 | * @param string $gperm_name Name of permission |
||
| 325 | * @param int $gperm_itemid ID of an item |
||
| 326 | * @param int /array $gperm_groupid A group ID or an array of group IDs |
||
| 327 | * @param int $gperm_modid ID of a module |
||
| 328 | * |
||
| 329 | * @return bool TRUE if permission is enabled |
||
| 330 | */ |
||
| 331 | public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1) |
||
| 332 | { |
||
| 333 | $ret = false; |
||
| 334 | $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid)); |
||
| 335 | $criteria->add(new \Criteria('gperm_name', $gperm_name)); |
||
| 336 | $gperm_itemid = (int)$gperm_itemid; |
||
| 337 | if ($gperm_itemid > 0) { |
||
| 338 | $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid)); |
||
| 339 | } |
||
| 340 | if (\is_array($gperm_groupid)) { |
||
| 341 | $criteria2 = new \CriteriaCompo(); |
||
| 342 | foreach ($gperm_groupid as $gid) { |
||
| 343 | $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR'); |
||
| 344 | } |
||
| 345 | $criteria->add($criteria2); |
||
| 346 | } else { |
||
| 347 | $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid)); |
||
| 348 | } |
||
| 349 | if ($this->getCount($criteria) > 0) { |
||
| 350 | $ret = true; |
||
| 351 | } |
||
| 352 | |||
| 353 | return $ret; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param $perm |
||
| 358 | * @param $itemid |
||
| 359 | * @param $groupid |
||
| 360 | * @param null $mid |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | public function deleteRight($perm, $itemid, $groupid, $mid = null) |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param $forum |
||
| 397 | * @param int $mid |
||
| 398 | * @return mixed |
||
| 399 | */ |
||
| 400 | public function applyTemplate($forum, $mid = 0) |
||
| 401 | { |
||
| 402 | $this->cacheHelper->delete('permission_forum'); |
||
| 403 | $handler = $this->loadHandler('Forum'); |
||
| 404 | |||
| 405 | return $handler->applyTemplate($forum, $mid); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | public function getTemplate() |
||
| 412 | { |
||
| 413 | $handler = $this->loadHandler('Forum'); |
||
| 414 | $template = $handler->getTemplate(); |
||
| 415 | |||
| 416 | return $template; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param $perms |
||
| 421 | * @return mixed |
||
| 422 | */ |
||
| 423 | public function setTemplate($perms) |
||
| 428 | } |
||
| 429 | } |
||
| 430 |