UserGroupAssignmentHandler.php
6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* UserGroupAssignmentHandler.php
*
* The UserGroupAssignmentHandler class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\UserGroup;
use Exception;
use UserAccessManager\User\UserHandler;
use UserAccessManager\Util\DateUtil;
/**
* Class UserGroupAssignmentHandler
*
* @package UserAccessManager\UserGroup
*/
class UserGroupAssignmentHandler
{
/**
* @var DateUtil
*/
protected $dateUtil;
/**
* @var UserHandler
*/
protected $userHandler;
/**
* @var UserGroupHandler
*/
protected $userGroupHandler;
/**
* @var UserGroupFactory
*/
protected $userGroupFactory;
/**
* UserGroupAssignmentHandler constructor.
* @param DateUtil $dateUtil
* @param UserHandler $userHandler
* @param UserGroupHandler $userGroupHandler
* @param UserGroupFactory $userGroupFactory
*/
public function __construct(
DateUtil $dateUtil,
UserHandler $userHandler,
UserGroupHandler $userGroupHandler,
UserGroupFactory $userGroupFactory
) {
$this->dateUtil = $dateUtil;
$this->userHandler = $userHandler;
$this->userGroupHandler = $userGroupHandler;
$this->userGroupFactory = $userGroupFactory;
}
/**
* Processes the date parameter.
* @param array $data
* @param string $name
* @return null|string
*/
private function getDateParameter(array $data, string $name): ?string
{
$isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
&& (string) $data[$name]['date'] !== '' && (string) $data[$name]['time'] !== '';
return ($isValid === true) ? (string) $data[$name]['date'] . 'T' . $data[$name]['time'] : null;
}
/**
* Updates the user groups for the given object.
* @param AbstractUserGroup[] $filteredUserGroups
* @param string $objectType
* @param int|string $objectId
* @param array $addUserGroups
* @param array $removeUserGroups
* @throws Exception
*/
private function setUserGroups(
array $filteredUserGroups,
string $objectType,
$objectId,
array $addUserGroups,
array $removeUserGroups
) {
foreach ($filteredUserGroups as $groupId => $userGroup) {
if (isset($removeUserGroups[$groupId]) === true) {
$userGroup->removeObject($objectType, $objectId);
}
if (isset($addUserGroups[$groupId]['id']) === true
&& (int) $addUserGroups[$groupId]['id'] === (int) $groupId
) {
$userGroup->addObject(
$objectType,
$objectId,
$this->getDateParameter($addUserGroups[$groupId], 'fromDate'),
$this->getDateParameter($addUserGroups[$groupId], 'toDate')
);
}
}
}
/**
* Sets the dynamic user groups for the given object.
* @param string $objectType
* @param int|string $objectId
* @param array $addDynamicUserGroups
* @throws UserGroupAssignmentException
* @throws UserGroupTypeException
*/
private function setDynamicGroups(string $objectType, $objectId, array $addDynamicUserGroups)
{
foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
$dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
if (count($dynamicUserGroupData) === 2
&& $addDynamicUserGroup['id'] === $dynamicUserGroupKey
) {
$dynamicUserGroup = $this->userGroupFactory->createDynamicUserGroup(
$dynamicUserGroupData[0],
$dynamicUserGroupData[1]
);
$dynamicUserGroup->addObject(
$objectType,
$objectId,
$this->getDateParameter($addDynamicUserGroup, 'fromDate'),
$this->getDateParameter($addDynamicUserGroup, 'toDate')
);
}
}
}
/**
* Sets the default user groups for the given object.
* @param AbstractUserGroup[] $filteredUserGroups
* @param string $objectType
* @param int|string $objectId
* @throws UserGroupTypeException
* @throws Exception
*/
private function setDefaultGroups(array $filteredUserGroups, string $objectType, $objectId)
{
/**
* @var UserGroup[] $userGroupsToCheck
*/
$userGroupsToCheck = array_diff_key($this->userGroupHandler->getFullUserGroups(), $filteredUserGroups);
foreach ($userGroupsToCheck as $userGroupToCheck) {
if ($userGroupToCheck->isDefaultGroupForObjectType($objectType, $fromTime, $toTime) === true) {
$userGroupToCheck->addObject(
$objectType,
$objectId,
$this->dateUtil->getDateFromTime($fromTime),
$this->dateUtil->getDateFromTime($toTime)
);
}
}
}
/**
* Saves the object data to the database.
* @param string $objectType
* @param int|string $objectId
* @param array $addUserGroups
* @param array $removeUserGroups
* @param array $addDynamicUserGroups
* @throws UserGroupAssignmentException
* @throws UserGroupTypeException
* @throws Exception
*/
public function assignObjectToUserGroups(
string $objectType,
$objectId,
array $addUserGroups,
array $removeUserGroups,
array $addDynamicUserGroups
) {
$filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
$this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
$this->setDynamicGroups($objectType, $objectId, $addDynamicUserGroups);
} else {
$this->setDefaultGroups($filteredUserGroups, $objectType, $objectId);
}
$this->userGroupHandler->unsetUserGroupsForObject();
}
}