Total Complexity | 42 |
Total Lines | 245 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PermissionForumHandler 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 PermissionForumHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class PermissionForumHandler extends PermissionHandler |
||
32 | { |
||
33 | protected $templateFilename; |
||
34 | |||
35 | /** |
||
36 | * @param \XoopsDatabase|null $db |
||
37 | */ |
||
38 | public function __construct(\XoopsDatabase $db = null) |
||
39 | { |
||
40 | // $this->PermissionHandler($db); |
||
41 | parent::__construct($db); |
||
42 | $this->templateFilename = XOOPS_VAR_PATH . '/configs/newbb_permission_template.php'; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param bool $fullname |
||
47 | * @return array |
||
48 | */ |
||
49 | public function getValidPerms($fullname = false) |
||
50 | { |
||
51 | static $validPerms = []; |
||
52 | if (isset($validPerms[(int)$fullname])) { |
||
53 | return $validPerms[(int)$fullname]; |
||
54 | } |
||
55 | $items = \array_filter(\array_map('\trim', \explode(',', FORUM_PERM_ITEMS))); |
||
56 | if (!empty($fullname)) { |
||
57 | foreach (\array_keys($items) as $key) { |
||
58 | $items[$key] = 'forum_' . $items[$key]; |
||
59 | } |
||
60 | } |
||
61 | $validPerms[(int)$fullname] = $items; |
||
62 | |||
63 | return $items; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param $mid |
||
68 | * @param int $id |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getValidItems($mid, $id = 0) |
||
72 | { |
||
73 | static $suspension = []; |
||
74 | $full_items = []; |
||
75 | if (empty($mid)) { |
||
76 | return $full_items; |
||
77 | } |
||
78 | |||
79 | require_once \dirname(__DIR__) . '/include/functions.user.php'; |
||
80 | $uid = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
81 | $ip = \Xmf\IPAddress::fromRequest()->asReadable(); |
||
82 | if (!empty($GLOBALS['xoopsModuleConfig']['enable_usermoderate']) && !isset($suspension[$uid][$id]) |
||
83 | && !\newbbIsAdmin($id)) { |
||
84 | /** @var Newbb\ModerateHandler $moderateHandler */ |
||
85 | $moderateHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Moderate'); |
||
86 | if (!$moderateHandler->verifyUser($uid, '', $id)) { |
||
87 | $suspension[$uid][$ip][$id] = 1; |
||
88 | } else { |
||
89 | $suspension[$uid][$ip][$id] = 0; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $items = $this->getValidPerms(); |
||
94 | foreach ($items as $item) { |
||
95 | /* skip access for suspended users */ |
||
96 | //if ( !empty($suspension[$uid][$ip][$id]) && in_array($item, array("post", "reply", "edit", "delete", "addpoll", "vote", "attach", "noapprove", "type")) ) continue; |
||
97 | if (!empty($suspension[$uid][$ip][$id])) { |
||
98 | continue; |
||
99 | } |
||
100 | $full_items[] = "'forum_{$item}'"; |
||
101 | } |
||
102 | |||
103 | return $full_items; |
||
104 | } |
||
105 | |||
106 | /* |
||
107 | * Returns permissions for a certain type |
||
108 | * |
||
109 | * @param int $id id of the item (forum, topic or possibly post) to get permissions for |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | |||
114 | /** |
||
115 | * @param int|array $id |
||
116 | * @return bool|array |
||
117 | */ |
||
118 | public function getPermissions($id = 0) |
||
119 | { |
||
120 | $permissions = []; |
||
121 | if (\is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) { |
||
122 | $modid = $GLOBALS['xoopsModule']->getVar('mid'); |
||
123 | } else { |
||
124 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
125 | $moduleHandler = \xoops_getHandler('module'); |
||
126 | $xoopsNewBB = $moduleHandler->getByDirname('newbb'); |
||
127 | $modid = $xoopsNewBB->getVar('mid'); |
||
128 | unset($xoopsNewBB); |
||
129 | } |
||
130 | |||
131 | // Get user's groups |
||
132 | $groups = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
133 | // Create string of groupid's separated by commas, inserted in a set of brackets |
||
134 | if (\count($groups) < 1) { |
||
135 | return false; |
||
136 | } |
||
137 | // Create criteria for getting only the permissions regarding this module and this user's groups |
||
138 | $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $modid)); |
||
139 | $criteria->add(new \Criteria('gperm_groupid', '(' . \implode(',', $groups) . ')', 'IN')); |
||
140 | if ($id) { |
||
141 | if (\is_array($id)) { |
||
142 | $criteria->add(new \Criteria('gperm_itemid', '(' . \implode(',', $id) . ')', 'IN')); |
||
143 | } else { |
||
144 | $criteria->add(new \Criteria('gperm_itemid', (int)$id)); |
||
145 | } |
||
146 | } |
||
147 | $gperm_names = \implode(', ', $this->getValidItems($modid, $id)); |
||
|
|||
148 | |||
149 | // Add criteria for gpermnames |
||
150 | $criteria->add(new \Criteria('gperm_name', '(' . $gperm_names . ')', 'IN')); |
||
151 | // Get all permission objects in this module and for this user's groups |
||
152 | $userpermissions = $this->getObjects($criteria, true); |
||
153 | |||
154 | // Set the granted permissions to 1 |
||
155 | foreach ($userpermissions as $gperm_id => $gperm) { |
||
156 | $permissions[$gperm->getVar('gperm_itemid')][$gperm->getVar('gperm_name')] = 1; |
||
157 | } |
||
158 | $userpermissions = null; |
||
159 | unset($userpermissions); |
||
160 | |||
161 | // Return the permission array |
||
162 | return $permissions; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param Forum|int $forum |
||
167 | * @param bool $topic_locked |
||
168 | * @param bool $isAdmin |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false) |
||
172 | { |
||
173 | $perm = []; |
||
174 | |||
175 | $forumId = $forum; |
||
176 | if (\is_object($forum)) { |
||
177 | $forumId = $forum->getVar('forum_id'); |
||
178 | } |
||
179 | |||
180 | $permission_set = $this->getPermissions($forumId); |
||
181 | |||
182 | $permItems = $this->getValidPerms(); |
||
183 | foreach ($permItems as $item) { |
||
184 | if ('access' === $item) { |
||
185 | continue; |
||
186 | } |
||
187 | if ($isAdmin |
||
188 | || (isset($permission_set[$forumId]['forum_' . $item]) |
||
189 | && (!$topic_locked |
||
190 | || 'view' === $item))) { |
||
191 | $perm[] = \constant('_MD_NEWBB_CAN_' . mb_strtoupper($item)); |
||
192 | } else { |
||
193 | $perm[] = \constant('_MD_NEWBB_CANNOT_' . mb_strtoupper($item)); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | return $perm; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param $forum_id |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function deleteByForum($forum_id) |
||
205 | { |
||
206 | $forum_id = (int)$forum_id; |
||
207 | if (empty($forum_id)) { |
||
208 | return false; |
||
209 | } |
||
210 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
211 | $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $GLOBALS['xoopsModule']->getVar('mid'))); |
||
212 | $items = $this->getValidPerms(true); |
||
213 | $criteria->add(new \Criteria('gperm_name', "('" . \implode("', '", $items) . "')", 'IN')); |
||
214 | $criteria->add(new \Criteria('gperm_itemid', $forum_id)); |
||
215 | |||
216 | return $grouppermHandler->deleteAll($criteria); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param $forum |
||
221 | * @param int $mid |
||
222 | * @return bool |
||
223 | */ |
||
224 | public function applyTemplate($forum, $mid = 0) |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @return array|false |
||
261 | */ |
||
262 | public function getTemplate() |
||
263 | { |
||
264 | $perms = \Xmf\Yaml::readWrapped($this->templateFilename); |
||
265 | |||
266 | return $perms; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param array $perms |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function setTemplate($perms) |
||
276 | } |
||
277 | } |
||
278 |