PluginProbe
User Access Manager / 2.2.5
User Access Manager v2.2.5
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Controller / Backend / UserGroupController.php

UserGroupController.php in User Access Manager 2.2.5, at src/Controller/Backend/UserGroupController.php

343 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UserGroupController.php
4 *
5 * The UserGroupController class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Controller\Backend;
19
20 use Exception;
21 use UserAccessManager\Config\WordpressConfig;
22 use UserAccessManager\Controller\Controller;
23 use UserAccessManager\Form\FormHelper;
24 use UserAccessManager\Object\ObjectHandler;
25 use UserAccessManager\UserGroup\UserGroup;
26 use UserAccessManager\UserGroup\UserGroupFactory;
27 use UserAccessManager\UserGroup\UserGroupHandler;
28 use UserAccessManager\UserGroup\UserGroupTypeException;
29 use UserAccessManager\Wrapper\Php;
30 use UserAccessManager\Wrapper\Wordpress;
31 use WP_Post_Type;
32 use WP_Taxonomy;
33
34 /**
35 * Class UserGroupController
36 *
37 * @package UserAccessManager\Controller
38 */
39 class UserGroupController extends Controller
40 {
41 use ControllerTabNavigationTrait;
42
43 const INSERT_UPDATE_GROUP_NONCE = 'uamInsertUpdateGroup';
44 const DELETE_GROUP_NONCE = 'uamDeleteGroup';
45 const SET_DEFAULT_USER_GROUPS_NONCE = 'uamSetDefaultUserGroups';
46 const GROUP_USER_GROUPS = 'user_groups';
47 const GROUP_DEFAULT_USER_GROUPS = 'default_user_groups';
48 const DEFAULT_USER_GROUPS_FORM_FIELD = 'default_user_groups';
49
50 /**
51 * @var string
52 */
53 protected $template = 'AdminUserGroup.php';
54
55 /**
56 * @var UserGroupHandler
57 */
58 private $userGroupHandler;
59
60 /**
61 * @var UserGroupFactory
62 */
63 private $userGroupFactory;
64
65 /**
66 * @var FormHelper
67 */
68 private $formHelper;
69
70 /**
71 * @var UserGroup
72 */
73 private $userGroup = null;
74
75 /**
76 * UserGroupController constructor.
77 * @param Php $php
78 * @param Wordpress $wordpress
79 * @param WordpressConfig $wordpressConfig
80 * @param UserGroupHandler $userGroupHandler
81 * @param UserGroupFactory $userGroupFactory
82 * @param FormHelper $formHelper
83 */
84 public function __construct(
85 Php $php,
86 Wordpress $wordpress,
87 WordpressConfig $wordpressConfig,
88 UserGroupHandler $userGroupHandler,
89 UserGroupFactory $userGroupFactory,
90 FormHelper $formHelper
91 ) {
92 parent::__construct($php, $wordpress, $wordpressConfig);
93 $this->userGroupHandler = $userGroupHandler;
94 $this->userGroupFactory = $userGroupFactory;
95 $this->formHelper = $formHelper;
96 }
97
98 /**
99 * Returns the tab groups.
100 * @return array
101 */
102 public function getTabGroups(): array
103 {
104 return [
105 self::GROUP_USER_GROUPS => ['user_groups'],
106 self::GROUP_DEFAULT_USER_GROUPS => array_merge(
107 array_keys($this->wordpress->getPostTypes(['public' => true], 'objects')),
108 array_keys($this->wordpress->getTaxonomies(['public' => true], 'objects')),
109 [ObjectHandler::GENERAL_USER_OBJECT_TYPE]
110 )
111 ];
112 }
113
114 /**
115 * Returns the translated tag group name by the given key.
116 * @param string $key
117 * @return string
118 */
119 public function getGroupText(string $key): string
120 {
121 return $this->formHelper->getText($key);
122 }
123
124 /**
125 * Returns the translated tag group section name by the given key.
126 * @param string $key
127 * @return string
128 */
129 public function getGroupSectionText(string $key): string
130 {
131 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
132 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
133
134 $objectName = $key;
135
136 if ($objectName === ObjectHandler::GENERAL_USER_OBJECT_TYPE) {
137 $objectName = TXT_UAM_USER;
138 } elseif (isset($objects[$key]) === true) {
139 $objectName = $objects[$key]->labels->name;
140
141 if ($objects[$key] instanceof WP_Post_Type) {
142 $objectName .= ' (' . TXT_UAM_POST_TYPE . ')';
143 } elseif ($objects[$key] instanceof WP_Taxonomy) {
144 $objectName .= ' (' . TXT_UAM_TAXONOMY_TYPE . ')';
145 }
146 }
147
148 return $objectName;
149 }
150
151 /**
152 * Returns the a user group object.
153 * @return UserGroup
154 * @throws UserGroupTypeException
155 */
156 public function getUserGroup(): UserGroup
157 {
158 if ($this->userGroup === null) {
159 $userGroupId = $this->getRequestParameter('userGroupId');
160 $this->userGroup = $this->userGroupFactory->createUserGroup($userGroupId);
161 }
162
163 return $this->userGroup;
164 }
165
166 /**
167 * Returns the sort url.
168 * @param string $sort
169 * @return string
170 */
171 public function getSortUrl(string $sort): string
172 {
173 $requestUrl = $this->getRequestUrl();
174 $requestUrl = preg_replace('/&amp;orderby[^&]*/i', '', $requestUrl);
175 $requestUrl = preg_replace('/&amp;order[^&]*/i', '', $requestUrl);
176 $divider = strpos($requestUrl, '?') === false ? '?' : '&amp;';
177 $order = (string) $this->getRequestParameter('order') === 'asc' ? 'desc' : 'asc';
178 return "{$requestUrl}{$divider}orderby={$sort}&amp;order={$order}";
179 }
180
181 /**
182 * Returns all user groups.
183 * @return UserGroup[]
184 * @throws UserGroupTypeException
185 */
186 public function getUserGroups(): array
187 {
188 $userGroups = $this->userGroupHandler->getUserGroups();
189
190 $sort = $this->getRequestParameter('orderby');
191
192 if ($sort !== null) {
193 $reverse = (string) $this->getRequestParameter('order') === 'desc';
194
195 uasort(
196 $userGroups,
197 function (UserGroup $userGroupOne, UserGroup $userGroupTwo) use ($sort, $reverse) {
198 $values = ['', ''];
199 $method = 'get' . ucfirst($sort);
200
201 if (method_exists($userGroupOne, $method) === true) {
202 $values = [(string) $userGroupOne->{$method}(), (string) $userGroupTwo->{$method}()];
203 $values = ($reverse === true) ? array_reverse($values) : $values;
204 }
205
206 return strnatcasecmp($values[0], $values[1]);
207 }
208 );
209 }
210
211 return $userGroups;
212 }
213
214 /**
215 * Returns the wordpress role names.
216 * @return array
217 */
218 public function getRoleNames(): array
219 {
220 $roles = $this->wordpress->getRoles();
221 return $roles->role_names;
222 }
223
224 /**
225 * Action to insert or update a user group.
226 * @throws UserGroupTypeException
227 * @throws Exception
228 */
229 public function insertUpdateUserGroupAction()
230 {
231 $this->verifyNonce(self::INSERT_UPDATE_GROUP_NONCE);
232
233 $userGroupId = $this->getRequestParameter('userGroupId');
234
235 $userGroup = $this->userGroupFactory->createUserGroup($userGroupId);
236
237 // Assign parameters
238 $groupName = (string) $this->getRequestParameter('userGroupName');
239
240 if (trim($groupName) === '') {
241 $this->setUpdateMessage(TXT_UAM_GROUP_NAME_ERROR);
242 return;
243 }
244
245 $userGroup->setName($groupName);
246
247 $userGroupDescription = $this->getRequestParameter('userGroupDescription');
248 $userGroup->setDescription($userGroupDescription);
249
250 $readAccess = $this->getRequestParameter('readAccess');
251 $userGroup->setReadAccess($readAccess);
252
253 $writeAccess = $this->getRequestParameter('writeAccess');
254 $userGroup->setWriteAccess($writeAccess);
255
256 $ipRange = $this->getRequestParameter('ipRange');
257 $userGroup->setIpRange($ipRange);
258
259 if ($userGroup->save() === true) {
260 $roles = $this->getRequestParameter('roles', []);
261
262 $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
263
264 foreach ($roles as $role) {
265 $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, htmlentities($role));
266 }
267
268 if ($userGroupId === null) {
269 $this->setUpdateMessage(TXT_UAM_GROUP_ADDED);
270 } else {
271 $this->setUpdateMessage(TXT_UAM_USER_GROUP_EDIT_SUCCESS);
272 }
273
274 $this->userGroupHandler->addUserGroup($userGroup);
275 }
276 }
277
278 /**
279 * Action to delete user groups.
280 * @throws UserGroupTypeException
281 */
282 public function deleteUserGroupAction()
283 {
284 $this->verifyNonce(self::DELETE_GROUP_NONCE);
285 $userGroups = $this->getRequestParameter('delete', []);
286
287 foreach ($userGroups as $id) {
288 $this->userGroupHandler->deleteUserGroup($id);
289 }
290
291 $this->setUpdateMessage(TXT_UAM_DELETE_GROUP);
292 }
293
294 /**
295 * Checks if the default user group type should be added.
296 * @param array $defaultUserGroups
297 * @param string $userGroupId
298 * @param null|string $fromTime
299 * @param null|string $toTime
300 * @return bool
301 */
302 private function isDefaultTypeAdd(
303 array $defaultUserGroups,
304 string $userGroupId,
305 &$fromTime = null,
306 &$toTime = null
307 ): bool {
308 $userGroupInfo = isset($defaultUserGroups[$userGroupId]) === true ? $defaultUserGroups[$userGroupId] : [];
309
310 if (isset($userGroupInfo['id']) === true && (string) $userGroupInfo['id'] === (string) $userGroupId) {
311 $fromTime = empty($userGroupInfo['fromTime']) === false ? $userGroupInfo['fromTime'] : null;
312 $toTime = empty($userGroupInfo['toTime']) === false ? $userGroupInfo['toTime'] : null;
313 return true;
314 }
315
316 return false;
317 }
318
319 /**
320 * Action to set default user groups.
321 * @throws UserGroupTypeException
322 * @throws Exception
323 */
324 public function setDefaultUserGroupsAction()
325 {
326 $this->verifyNonce(self::SET_DEFAULT_USER_GROUPS_NONCE);
327 $objectType = $this->getCurrentTabGroupSection();
328 $defaultUserGroups = $this->getRequestParameter(self::DEFAULT_USER_GROUPS_FORM_FIELD, []);
329 $userGroups = $this->getUserGroups();
330
331 foreach ($userGroups as $userGroup) {
332 $userGroupId = $userGroup->getId();
333 $userGroup->removeDefaultType($objectType);
334
335 if ($this->isDefaultTypeAdd($defaultUserGroups, $userGroupId, $fromTime, $toTime) === true) {
336 $userGroup->addDefaultType($objectType, $fromTime, $toTime);
337 }
338 }
339
340 $this->setUpdateMessage(TXT_UAM_SET_DEFAULT_USER_GROUP_SUCCESS);
341 }
342 }
343