PluginProbe
User Access Manager / 2.1.3
User Access Manager v2.1.3
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.1.3, at src/Controller/Backend/UserGroupController.php

298 lines 8.6 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 namespace UserAccessManager\Controller\Backend;
16
17 use UserAccessManager\Access\AccessHandler;
18 use UserAccessManager\Config\WordpressConfig;
19 use UserAccessManager\Controller\Controller;
20 use UserAccessManager\Form\FormHelper;
21 use UserAccessManager\Object\ObjectHandler;
22 use UserAccessManager\UserGroup\UserGroupFactory;
23 use UserAccessManager\Wrapper\Php;
24 use UserAccessManager\Wrapper\Wordpress;
25
26 /**
27 * Class UserGroupController
28 *
29 * @package UserAccessManager\Controller
30 */
31 class UserGroupController extends Controller
32 {
33 use ControllerTabNavigationTrait;
34
35 const INSERT_UPDATE_GROUP_NONCE = 'uamInsertUpdateGroup';
36 const DELETE_GROUP_NONCE = 'uamDeleteGroup';
37 const SET_DEFAULT_USER_GROUPS_NONCE = 'uamSetDefaultUserGroups';
38 const GROUP_USER_GROUPS = 'user_groups';
39 const GROUP_DEFAULT_USER_GROUPS = 'default_user_groups';
40 const DEFAULT_USER_GROUPS_FORM_FIELD = 'default_user_groups';
41
42 /**
43 * @var string
44 */
45 protected $template = 'AdminUserGroup.php';
46
47 /**
48 * @var AccessHandler
49 */
50 private $accessHandler;
51
52 /**
53 * @var UserGroupFactory
54 */
55 private $userGroupFactory;
56
57 /**
58 * @var FormHelper
59 */
60 private $formHelper;
61
62 /**
63 * @var \UserAccessManager\UserGroup\UserGroup
64 */
65 private $userGroup = null;
66
67 /**
68 * UserGroupController constructor.
69 *
70 * @param Php $php
71 * @param Wordpress $wordpress
72 * @param WordpressConfig $wordpressConfig
73 * @param AccessHandler $userHandler
74 * @param UserGroupFactory $userGroupFactory
75 * @param FormHelper $formHelper
76 */
77 public function __construct(
78 Php $php,
79 Wordpress $wordpress,
80 WordpressConfig $wordpressConfig,
81 AccessHandler $userHandler,
82 UserGroupFactory $userGroupFactory,
83 FormHelper $formHelper
84 ) {
85 parent::__construct($php, $wordpress, $wordpressConfig);
86 $this->accessHandler = $userHandler;
87 $this->userGroupFactory = $userGroupFactory;
88 $this->formHelper = $formHelper;
89 }
90
91 /**
92 * Returns the tab groups.
93 *
94 * @return array
95 */
96 public function getTabGroups()
97 {
98 return [
99 self::GROUP_USER_GROUPS => ['user_groups'],
100 self::GROUP_DEFAULT_USER_GROUPS => array_merge(
101 array_keys($this->wordpress->getPostTypes(['public' => true], 'objects')),
102 array_keys($this->wordpress->getTaxonomies(['public' => true], 'objects')),
103 [ObjectHandler::GENERAL_USER_OBJECT_TYPE]
104 )
105 ];
106 }
107
108 /**
109 * Returns the translated tag group name by the given key.
110 *
111 * @param string $key
112 *
113 * @return string
114 */
115 public function getGroupText($key)
116 {
117 return $this->formHelper->getText($key);
118 }
119
120 /**
121 * Returns the translated tag group section name by the given key.
122 *
123 * @param string $key
124 *
125 * @return string
126 */
127 public function getGroupSectionText($key)
128 {
129 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
130 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
131
132 $objectName = $key;
133
134 if ($objectName === ObjectHandler::GENERAL_USER_OBJECT_TYPE) {
135 $objectName = TXT_UAM_USER;
136 } elseif (isset($objects[$key]) === true) {
137 $objectName = $objects[$key]->labels->name;
138
139 if ($objects[$key] instanceof \WP_Post_Type) {
140 $objectName .= ' ('.TXT_UAM_POST_TYPE.')';
141 } elseif ($objects[$key] instanceof \WP_Taxonomy) {
142 $objectName .= ' ('.TXT_UAM_TAXONOMY_TYPE.')';
143 }
144 }
145
146 return $objectName;
147 }
148
149 /**
150 * Returns the a user group object.
151 *
152 * @return \UserAccessManager\UserGroup\UserGroup
153 */
154 public function getUserGroup()
155 {
156 if ($this->userGroup === null) {
157 $userGroupId = $this->getRequestParameter('userGroupId');
158 $this->userGroup = $this->userGroupFactory->createUserGroup($userGroupId);
159 }
160
161 return $this->userGroup;
162 }
163
164 /**
165 * Returns all user groups.
166 *
167 * @return \UserAccessManager\UserGroup\UserGroup[]
168 */
169 public function getUserGroups()
170 {
171 return $this->accessHandler->getUserGroups();
172 }
173
174 /**
175 * Returns the wordpress role names.
176 *
177 * @return array
178 */
179 public function getRoleNames()
180 {
181 $roles = $this->wordpress->getRoles();
182 return $roles->role_names;
183 }
184
185 /**
186 * Action to insert or update a user group.
187 */
188 public function insertUpdateUserGroupAction()
189 {
190 $this->verifyNonce(self::INSERT_UPDATE_GROUP_NONCE);
191
192 $userGroupId = $this->getRequestParameter('userGroupId');
193
194 $userGroup = $this->userGroupFactory->createUserGroup($userGroupId);
195
196 // Assign parameters
197 $groupName = $this->getRequestParameter('userGroupName');
198
199 if (trim($groupName) === '') {
200 $this->setUpdateMessage(TXT_UAM_GROUP_NAME_ERROR);
201 return;
202 }
203
204 $userGroup->setName($groupName);
205
206 $userGroupDescription = $this->getRequestParameter('userGroupDescription');
207 $userGroup->setDescription($userGroupDescription);
208
209 $readAccess = $this->getRequestParameter('readAccess');
210 $userGroup->setReadAccess($readAccess);
211
212 $writeAccess = $this->getRequestParameter('writeAccess');
213 $userGroup->setWriteAccess($writeAccess);
214
215 $ipRange = $this->getRequestParameter('ipRange');
216 $userGroup->setIpRange($ipRange);
217
218 if ($userGroup->save() === true) {
219 $roles = $this->getRequestParameter('roles', []);
220
221 $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
222
223 foreach ($roles as $role) {
224 $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, htmlentities($role));
225 }
226
227 if ($userGroupId === null) {
228 $this->userGroup = $userGroup;
229 $this->setUpdateMessage(TXT_UAM_GROUP_ADDED);
230 } else {
231 $this->setUpdateMessage(TXT_UAM_USER_GROUP_EDIT_SUCCESS);
232 }
233
234 $this->accessHandler->addUserGroup($userGroup);
235 }
236 }
237
238 /**
239 * Action to delete user groups.
240 */
241 public function deleteUserGroupAction()
242 {
243 $this->verifyNonce(self::DELETE_GROUP_NONCE);
244 $userGroups = $this->getRequestParameter('delete', []);
245
246 foreach ($userGroups as $id) {
247 $this->accessHandler->deleteUserGroup($id);
248 }
249
250 $this->setUpdateMessage(TXT_UAM_DELETE_GROUP);
251 }
252
253 /**
254 * Checks if the default user group type should be added.
255 *
256 * @param array $defaultUserGroups
257 * @param string $userGroupId
258 * @param null|string $fromTime
259 * @param null|string $toTime
260 *
261 * @return bool
262 */
263 private function isDefaultTypeAdd(array $defaultUserGroups, $userGroupId, &$fromTime = null, &$toTime = null)
264 {
265 $userGroupInfo = isset($defaultUserGroups[$userGroupId]) === true ? $defaultUserGroups[$userGroupId] : [];
266
267 if (isset($userGroupInfo['id']) === true && (string)$userGroupInfo['id'] === (string)$userGroupId) {
268 $fromTime = empty($userGroupInfo['fromTime']) === false ? $userGroupInfo['fromTime'] : null;
269 $toTime = empty($userGroupInfo['toTime']) === false ? $userGroupInfo['toTime'] : null;
270 return true;
271 }
272
273 return false;
274 }
275
276 /**
277 * Action to set default user groups.
278 */
279 public function setDefaultUserGroupsAction()
280 {
281 $this->verifyNonce(self::SET_DEFAULT_USER_GROUPS_NONCE);
282 $objectType = $this->getCurrentTabGroupSection();
283 $defaultUserGroups = $this->getRequestParameter(self::DEFAULT_USER_GROUPS_FORM_FIELD, []);
284 $userGroups = $this->getUserGroups();
285
286 foreach ($userGroups as $userGroup) {
287 $userGroupId = $userGroup->getId();
288 $userGroup->removeDefaultType($objectType);
289
290 if ($this->isDefaultTypeAdd($defaultUserGroups, $userGroupId, $fromTime, $toTime) === true) {
291 $userGroup->addDefaultType($objectType, $fromTime, $toTime);
292 }
293 }
294
295 $this->setUpdateMessage(TXT_UAM_SET_DEFAULT_USER_GROUP_SUCCESS);
296 }
297 }
298