PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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
← All changes | src/UserGroup/UserGroupAssignmentHandler.php +88 -13 2.3.202.2.2 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * UserGroupAssignmentHandler.php
4 + *
5 + * The UserGroupAssignmentHandler 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\UserGroup;
@@ -7,36 +20,84 @@
7 20 use Exception;
8 21 use UserAccessManager\User\UserHandler;
9 22 use UserAccessManager\Util\DateUtil;
10 23
24 +/**
25 + * Class UserGroupAssignmentHandler
26 + *
27 + * @package UserAccessManager\UserGroup
28 + */
11 29 class UserGroupAssignmentHandler
12 30 {
31 + /**
32 + * @var DateUtil
33 + */
34 + protected $dateUtil;
35 +
36 + /**
37 + * @var UserHandler
38 + */
39 + protected $userHandler;
40 +
41 + /**
42 + * @var UserGroupHandler
43 + */
44 + protected $userGroupHandler;
45 +
46 + /**
47 + * @var UserGroupFactory
48 + */
49 + protected $userGroupFactory;
50 +
51 + /**
52 + * UserGroupAssignmentHandler constructor.
53 + * @param DateUtil $dateUtil
54 + * @param UserHandler $userHandler
55 + * @param UserGroupHandler $userGroupHandler
56 + * @param UserGroupFactory $userGroupFactory
57 + */
13 58 public function __construct(
14 - protected DateUtil $dateUtil,
15 - protected UserHandler $userHandler,
16 - protected UserGroupHandler $userGroupHandler,
17 - protected UserGroupFactory $userGroupFactory
59 + DateUtil $dateUtil,
60 + UserHandler $userHandler,
61 + UserGroupHandler $userGroupHandler,
62 + UserGroupFactory $userGroupFactory
18 63 ) {
64 + $this->dateUtil = $dateUtil;
65 + $this->userHandler = $userHandler;
66 + $this->userGroupHandler = $userGroupHandler;
67 + $this->userGroupFactory = $userGroupFactory;
19 68 }
20 69
70 + /**
71 + * Processes the date parameter.
72 + * @param array $data
73 + * @param string $name
74 + * @return null|string
75 + */
21 76 private function getDateParameter(array $data, string $name): ?string
22 77 {
23 - $value = (string) ($data[$name] ?? '');
78 + $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
79 + && (string) $data[$name]['date'] !== '' && (string) $data[$name]['time'] !== '';
24 80
25 - return ($value !== '') ? $value : null;
81 + return ($isValid === true) ? (string) $data[$name]['date'] . 'T' . $data[$name]['time'] : null;
26 82 }
27 83
28 84 /**
85 + * Updates the user groups for the given object.
86 + * @param AbstractUserGroup[] $filteredUserGroups
87 + * @param string $objectType
88 + * @param int|string $objectId
89 + * @param array $addUserGroups
90 + * @param array $removeUserGroups
29 91 * @throws Exception
30 92 */
31 93 private function setUserGroups(
32 94 array $filteredUserGroups,
33 95 string $objectType,
34 - int|string|null $objectId,
96 + $objectId,
35 97 array $addUserGroups,
36 98 array $removeUserGroups
37 - ): void {
38 - /** @var UserGroup $userGroup */
99 + ) {
39 100 foreach ($filteredUserGroups as $groupId => $userGroup) {
40 101 if (isset($removeUserGroups[$groupId]) === true) {
41 102 $userGroup->removeObject($objectType, $objectId);
42 103 }
@@ -54,12 +115,16 @@
54 115 }
55 116 }
56 117
57 118 /**
119 + * Sets the dynamic user groups for the given object.
120 + * @param string $objectType
121 + * @param int|string $objectId
122 + * @param array $addDynamicUserGroups
58 123 * @throws UserGroupAssignmentException
59 124 * @throws UserGroupTypeException
60 125 */
61 - private function setDynamicGroups(string $objectType, int|string|null $objectId, array $addDynamicUserGroups): void
126 + private function setDynamicGroups(string $objectType, $objectId, array $addDynamicUserGroups)
62 127 {
63 128 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
64 129 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
65 130
@@ -81,12 +146,16 @@
81 146 }
82 147 }
83 148
84 149 /**
150 + * Sets the default user groups for the given object.
151 + * @param AbstractUserGroup[] $filteredUserGroups
152 + * @param string $objectType
153 + * @param int|string $objectId
85 154 * @throws UserGroupTypeException
86 155 * @throws Exception
87 156 */
88 - private function setDefaultGroups(array $filteredUserGroups, string $objectType, int|string|null $objectId): void
157 + private function setDefaultGroups(array $filteredUserGroups, string $objectType, $objectId)
89 158 {
90 159 /**
91 160 * @var UserGroup[] $userGroupsToCheck
92 161 */
@@ -104,8 +173,14 @@
104 173 }
105 174 }
106 175
107 176 /**
177 + * Saves the object data to the database.
178 + * @param string $objectType
179 + * @param int|string $objectId
180 + * @param array $addUserGroups
181 + * @param array $removeUserGroups
182 + * @param array $addDynamicUserGroups
108 183 * @throws UserGroupAssignmentException
109 184 * @throws UserGroupTypeException
110 185 * @throws Exception
111 186 */
@@ -110,13 +185,13 @@
110 185 * @throws Exception
111 186 */
112 187 public function assignObjectToUserGroups(
113 188 string $objectType,
114 - int|string|null $objectId,
189 + $objectId,
115 190 array $addUserGroups,
116 191 array $removeUserGroups,
117 192 array $addDynamicUserGroups
118 - ): void {
193 + ) {
119 194 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
120 195 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
121 196
122 197 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {