PluginProbe
User Access Manager / 1.2.6.9
User Access Manager v1.2.6.9
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 | class/UamUserGroup.class.php +702 -1040 1.01.2.6.9 View file →
@@ -8,9 +8,9 @@
8 8 *
9 9 * @category UserAccessManager
10 10 * @package UserAccessManager
11 11 * @author Alexander Schneider <alexanderschneider85@googlemail.com>
12 - * @copyright 2008-2010 Alexander Schneider
12 + * @copyright 2008-2013 Alexander Schneider
13 13 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
14 14 * @version SVN: $Id$
15 15 * @link http://wordpress.org/extend/plugins/user-access-manager/
16 16 */
@@ -26,218 +26,172 @@
26 26 */
27 27
28 28 class UamUserGroup
29 29 {
30 - protected $accessHandler = null;
31 - protected $id = null;
32 - protected $groupName = null;
33 - protected $groupDesc = null;
34 - protected $readAccess = null;
35 - protected $writeAccess = null;
36 - protected $ipRange = null;
37 - protected $roles = array();
38 - protected $users = array(
39 - 'real' => -1,
40 - 'full' => -1
41 - );
42 - protected $singleUsers = array();
43 - protected $categories = array(
44 - 'real' => -1,
45 - 'full' => -1
46 - );
47 - protected $singleCategories = array();
48 - protected $posts = array(
49 - 'real' => -1,
50 - 'full' => -1
51 - );
52 - protected $singlePosts = array();
53 - protected $pages = array(
54 - 'real' => -1,
55 - 'full' => -1
56 - );
57 - protected $files = array(
58 - 'real' => -1,
59 - 'full' => -1
60 - );
61 - private $_assignedUsers = null;
62 - private $_assignedCategories = null;
63 - private $_assignedPosts = null;
64 -
30 + protected $_oAccessHandler = null;
31 + protected $_iId = null;
32 + protected $_sGroupName = null;
33 + protected $_sGroupDesc = null;
34 + protected $_sReadAccess = null;
35 + protected $_sWriteAccess = null;
36 + protected $_sIpRange = null;
37 + protected $_aRoles = array();
38 + protected $_aObjects = null;
39 + protected $_aSingleObjects = null;
40 + protected $_aAssignedObjects = null;
41 + protected $_aPlObjects = array();
42 + protected $_aSetRecursive = array();
43 + protected $_aValidObjectTypes = array();
44 + protected $_aObjectsInCategory = null;
45 + protected $_aObjectIsMember = array();
46 +
65 47 /**
66 - * Consturtor
67 - *
68 - * @param object &$uamAccessHandler The access handler object.
69 - * @param integer $id The id of the user group.
70 - *
71 - * @return null
48 + * Constructor
49 + *
50 + * @param UamAccessHandler $oUamAccessHandler The access handler object.
51 + * @param integer $iId The id of the user group.
72 52 */
73 - function __construct(&$uamAccessHandler, $id = null)
53 + public function __construct(UamAccessHandler &$oUamAccessHandler, $iId = null)
74 54 {
75 - $this->accessHandler = $uamAccessHandler;
55 + $this->_oAccessHandler = $oUamAccessHandler;
76 56
77 - if ($id !== null) {
57 + if ($iId !== null) {
58 + /**
59 + * @var wpdb $wpdb
60 + */
78 61 global $wpdb;
79 62
80 - $this->id = $id;
63 + $this->_iId = $iId;
81 64
82 - $dbUsergroup = $wpdb->get_row(
83 - "SELECT *
84 - FROM " . DB_ACCESSGROUP . "
85 - WHERE ID = " . $this->id . "
86 - LIMIT 1",
65 + $aDbUserGroup = $wpdb->get_row(
66 + "SELECT *
67 + FROM ".DB_ACCESSGROUP."
68 + WHERE ID = ".$this->getId()."
69 + LIMIT 1",
87 70 ARRAY_A
88 71 );
89 72
90 - $this->groupName = $dbUsergroup['groupname'];
91 - $this->groupDesc = $dbUsergroup['groupdesc'];
92 - $this->readAccess = $dbUsergroup['read_access'];
93 - $this->writeAccess = $dbUsergroup['write_access'];
94 - $this->ipRange = $dbUsergroup['ip_range'];
73 + $this->_sGroupName = $aDbUserGroup['groupname'];
74 + $this->_sGroupDesc = $aDbUserGroup['groupdesc'];
75 + $this->_sReadAccess = $aDbUserGroup['read_access'];
76 + $this->_sWriteAccess = $aDbUserGroup['write_access'];
77 + $this->_sIpRange = $aDbUserGroup['ip_range'];
95 78 }
79 +
80 + //Create default values for the objects.
81 + foreach ($this->getAllObjectTypes() as $sObjectType) {
82 + $this->_aAssignedObjects[$sObjectType] = null;
83 + $this->_aObjects[$sObjectType] = array(
84 + 'real' => -1,
85 + 'full' => -1
86 + );
87 +
88 + if ($iId !== null) {
89 + $this->getObjectsFromType($sObjectType);
90 + }
91 + }
96 92 }
97 93
98 94 /**
99 95 * Returns the user access handler object.
100 96 *
101 - * @return object
97 + * @return UamAccessHandler
102 98 */
103 - function &getAccessHandler()
99 + public function &getAccessHandler()
104 100 {
105 - return $this->accessHandler;
101 + return $this->_oAccessHandler;
106 102 }
107 103
108 104 /**
109 105 * Deletes the user group.
110 106 *
111 - * @return null
107 + * @return boolean
112 108 */
113 - function delete()
109 + public function delete()
114 110 {
115 - if ($this->id == null) {
111 + if ($this->_iId == null) {
116 112 return false;
117 113 }
118 -
114 +
115 + /**
116 + * @var wpdb $wpdb
117 + */
119 118 global $wpdb;
120 119
121 120 $wpdb->query(
122 - "DELETE FROM " . DB_ACCESSGROUP . "
123 - WHERE ID = $this->id LIMIT 1"
121 + "DELETE FROM " . DB_ACCESSGROUP . "
122 + WHERE ID = $this->_iId LIMIT 1"
124 123 );
125 124
126 - $this->_deleteRolesFromDb();
127 - $this->_deletePostByTypeFromDb('all');
128 - $this->_deleteCategoriesFromDb();
129 - $this->_deleteUsersFromDb();
125 + foreach ($this->getAllObjectTypes() as $sObjectType) {
126 + $this->_deleteObjectsFromDb($sObjectType);
127 + }
128 +
129 + return true;
130 130 }
131 131
132 132 /**
133 133 * Saves the user group.
134 - *
135 - * @return null;
134 + *
135 + * @param boolean $blRemoveOldAssignments If false we will not remove old entries.
136 + *
137 + * @return null
136 138 */
137 - function save()
139 + public function save($blRemoveOldAssignments = true)
138 140 {
141 + /**
142 + * @var wpdb $wpdb
143 + */
139 144 global $wpdb;
140 145
141 - if ($this->id == null) {
146 + if ($this->_iId == null) {
142 147 $wpdb->query(
143 - "INSERT INTO " . DB_ACCESSGROUP . " (
144 - ID,
145 - groupname,
146 - groupdesc,
147 - read_access,
148 - write_access,
149 - ip_range
150 - )
151 - VALUES (
152 - NULL,
153 - '" . $this->groupName . "',
154 - '" . $this->groupDesc . "',
155 - '" . $this->readAccess . "',
156 - '" . $this->writeAccess . "',
157 - '" . $this->ipRange . "'
158 - )"
148 + "INSERT INTO " . DB_ACCESSGROUP . " (
149 + ID,
150 + groupname,
151 + groupdesc,
152 + read_access,
153 + write_access,
154 + ip_range
155 + )
156 + VALUES (
157 + NULL,
158 + '" . $this->_sGroupName . "',
159 + '" . $this->_sGroupDesc . "',
160 + '" . $this->_sReadAccess . "',
161 + '" . $this->_sWriteAccess . "',
162 + '" . $this->_sIpRange . "'
163 + )"
159 164 );
160 165
161 - $this->id = $wpdb->insert_id;
166 + $this->_iId = $wpdb->insert_id;
162 167 } else {
163 168 $wpdb->query(
164 - "UPDATE " . DB_ACCESSGROUP . "
165 - SET groupname = '" . $this->groupName . "',
166 - groupdesc = '" . $this->groupDesc . "',
167 - read_access = '" . $this->readAccess . "',
168 - write_access = '" . $this->writeAccess . "',
169 - ip_range = '" . $this->ipRange . "'
170 - WHERE ID = " . $this->id
169 + "UPDATE " . DB_ACCESSGROUP . "
170 + SET groupname = '" . $this->_sGroupName . "',
171 + groupdesc = '" . $this->_sGroupDesc . "',
172 + read_access = '" . $this->_sReadAccess . "',
173 + write_access = '" . $this->_sWriteAccess . "',
174 + ip_range = '" . $this->_sIpRange . "'
175 + WHERE ID = " . $this->_iId
171 176 );
172 -
173 - $this->getPosts();
174 - $this->getPages();
175 - $this->getFiles();
176 - $this->getRoles();
177 - $this->getCategories();
178 - $this->getUsers();
179 -
180 - $this->_deletePostByTypeFromDb('all');
181 - $this->_deleteRolesFromDb();
182 - $this->_deleteCategoriesFromDb();
183 - $this->_deleteUsersFromDb();
177 +
178 + if ($blRemoveOldAssignments === true) {
179 + foreach ($this->getAllObjectTypes() as $sObjectType) {
180 + //Delete form database
181 + $this->_deleteObjectsFromDb($sObjectType);
182 + }
183 + }
184 184 }
185 185
186 - foreach ($this->getUsers() as $userKey => $user) {
187 - $wpdb->query(
188 - "INSERT INTO " . DB_ACCESSGROUP_TO_USER . " (
189 - group_id,
190 - user_id
191 - )
192 - VALUES(
193 - '" . $this->id . "',
194 - '" . $userKey . "'
195 - )"
196 - );
186 + foreach ($this->getAllObjectTypes() as $sObjectType) {
187 + $aKeys = $this->_getAssignedObjects($sObjectType);
188 +
189 + if (count($aKeys) > 0) {
190 + $sSql = $this->_getSqlQuery($sObjectType, 'insert', $aKeys);
191 + $wpdb->query($sSql);
192 + }
197 193 }
198 -
199 - foreach ($this->getCategories() as $categoryKey => $category) {
200 - $wpdb->query(
201 - "INSERT INTO " . DB_ACCESSGROUP_TO_CATEGORY . " (
202 - group_id,
203 - category_id
204 - )
205 - VALUES(
206 - '" . $this->id . "',
207 - '" . $categoryKey . "'
208 - )"
209 - );
210 - }
211 -
212 - foreach ($this->getRoles() as $roleKey => $role) {
213 - $wpdb->query(
214 - "INSERT INTO " . DB_ACCESSGROUP_TO_ROLE . " (
215 - group_id,
216 - role_name
217 - )
218 - VALUES(
219 - '" . $this->id . "',
220 - '" . $roleKey . "'
221 - )"
222 - );
223 - }
224 -
225 - $allPosts
226 - = array_merge($this->getPosts(), $this->getPages(), $this->getFiles());
227 -
228 - foreach ($allPosts as $post) {
229 - $wpdb->query(
230 - "INSERT INTO " . DB_ACCESSGROUP_TO_POST . " (
231 - group_id,
232 - post_id
233 - )
234 - VALUES(
235 - '" . $this->id . "',
236 - '" . $post->ID . "'
237 - )"
238 - );
239 - }
240 194 }
241 195
242 196
243 197 /*
@@ -244,15 +198,15 @@
244 198 * Primary values.
245 199 */
246 200
247 201 /**
248 - * Returns the group id.
202 + * Returns the group _iId.
249 203 *
250 204 * @return integer
251 205 */
252 - function getId()
206 + public function getId()
253 207 {
254 - return $this->id;
208 + return $this->_iId;
255 209 }
256 210
257 211 /**
258 212 * Returns the group name.
@@ -258,23 +212,23 @@
258 212 * Returns the group name.
259 213 *
260 214 * @return string
261 215 */
262 - function getGroupName()
216 + public function getGroupName()
263 217 {
264 - return $this->groupName;
218 + return $this->_sGroupName;
265 219 }
266 220
267 221 /**
268 222 * Sets the group name.
269 223 *
270 - * @param string $groupName The new group name.
224 + * @param string $sGroupName The new group name.
271 225 *
272 226 * @return null
273 227 */
274 - function setGroupName($groupName)
228 + public function setGroupName($sGroupName)
275 229 {
276 - $this->groupName = $groupName;
230 + $this->_sGroupName = $sGroupName;
277 231 }
278 232
279 233 /**
280 234 * Returns the group description.
@@ -280,23 +234,23 @@
280 234 * Returns the group description.
281 235 *
282 236 * @return string
283 237 */
284 - function getGroupDesc()
238 + public function getGroupDesc()
285 239 {
286 - return $this->groupDesc;
240 + return $this->_sGroupDesc;
287 241 }
288 242
289 243 /**
290 244 * Sets the group description.
291 245 *
292 - * @param string $groupDesc The new group description.
246 + * @param string $sGroupDesc The new group description.
293 247 *
294 248 * @return null
295 249 */
296 - function setGroupDesc($groupDesc)
250 + public function setGroupDesc($sGroupDesc)
297 251 {
298 - $this->groupDesc = $groupDesc;
252 + $this->_sGroupDesc = $sGroupDesc;
299 253 }
300 254
301 255 /**
302 256 * Returns the read access.
@@ -302,23 +256,23 @@
302 256 * Returns the read access.
303 257 *
304 258 * @return string
305 259 */
306 - function getReadAccess()
260 + public function getReadAccess()
307 261 {
308 - return $this->readAccess;
262 + return $this->_sReadAccess;
309 263 }
310 264
311 265 /**
312 266 * Sets the read access.
313 267 *
314 - * @param string $readAccess The read access.
268 + * @param string $sReadAccess The read access.
315 269 *
316 270 * @return null
317 271 */
318 - function setReadAccess($readAccess)
272 + public function setReadAccess($sReadAccess)
319 273 {
320 - $this->readAccess = $readAccess;
274 + $this->_sReadAccess = $sReadAccess;
321 275 }
322 276
323 277 /**
324 278 * Returns the write access.
@@ -324,1135 +278,843 @@
324 278 * Returns the write access.
325 279 *
326 280 * @return string
327 281 */
328 - function getWriteAccess()
282 + public function getWriteAccess()
329 283 {
330 - return $this->writeAccess;
284 + return $this->_sWriteAccess;
331 285 }
332 286
333 287 /**
334 288 * Sets the write access.
335 289 *
336 - * @param string $writeAccess The wirte access.
290 + * @param string $sWriteAccess The write access.
337 291 *
338 292 * @return null
339 293 */
340 - function setWriteAccess($writeAccess)
294 + public function setWriteAccess($sWriteAccess)
341 295 {
342 - $this->writeAccess = $writeAccess;
296 + $this->_sWriteAccess = $sWriteAccess;
343 297 }
344 298
345 299 /**
346 300 * Returns the ip range.
347 301 *
348 - * @param string $type The return type.
302 + * @param string $sType The return type.
349 303 *
350 - * @return array
304 + * @return array|string
351 305 */
352 - function getIpRange($type = null)
306 + public function getIpRange($sType = null)
353 307 {
354 - if ($type == 'string') {
355 - return $this->ipRange;
308 + if ($sType == 'string') {
309 + return $this->_sIpRange;
356 310 }
357 311
358 - $ipRange = explode(';', $this->ipRange);
312 + $aIpRange = explode(';', $this->_sIpRange);
359 313
360 - if ($ipRange[0] == null) {
314 + if ($aIpRange[0] == null) {
361 315 return null;
362 316 }
363 317
364 - return $ipRange;
318 + return $aIpRange;
365 319 }
366 320
367 321 /**
368 322 * Sets the ip range.
369 323 *
370 - * @param string $ipRange The new ip range.
324 + * @param string|array $mIpRange The new ip range.
371 325 *
372 326 * @return null
373 327 */
374 - function setIpRange($ipRange)
328 + public function setIpRange($mIpRange)
375 329 {
376 - if (is_array($ipRange)) {
377 - $ipRange = implode(';', $ipRange);
330 + if (is_array($mIpRange)) {
331 + $mIpRange = implode(';', $mIpRange);
378 332 }
379 333
380 - $this->ipRange = $ipRange;
334 + $this->_sIpRange = $mIpRange;
381 335 }
382 336
383 -
384 - /*
385 - * Group roles functions.
386 - */
387 -
388 337 /**
389 - * Returns the roles in the group
338 + * Returns all _aObjects types.
390 339 *
391 340 * @return array
392 341 */
393 - function getRoles()
342 + public function getAllObjectTypes()
394 343 {
395 - if ($this->id == null) {
396 - return array();
397 - }
398 -
399 - if ($this->roles != array()) {
400 - return $this->roles;
401 - }
402 -
403 - global $wpdb;
404 -
405 - $dbRoles = $wpdb->get_results(
406 - "SELECT *
407 - FROM " . DB_ACCESSGROUP_TO_ROLE . "
408 - WHERE group_id = " . $this->id,
409 - ARRAY_A
410 - );
411 -
412 - if (isset($dbRoles)) {
413 - foreach ($dbRoles as $dbRole) {
414 - $this->roles[trim($dbRole['role_name'])] = $dbRole;
415 - }
416 - }
417 -
418 - return $this->roles;
344 + return $this->getAccessHandler()->getAllObjectTypes();
419 345 }
420 -
346 +
421 347 /**
422 - * Adds a role to the role group.
423 - *
424 - * @param integer $roleName The role name which should be added.
425 - *
426 - * @return null
348 + * Checks if the object type is a valid one.
349 + *
350 + * @param string $sObjectType The object type to check.
351 + *
352 + * @return boolean
427 353 */
428 - function addRole($roleName)
354 + public function isValidObjectType($sObjectType)
429 355 {
430 - $this->getRoles();
431 -
432 - /**
433 - * $this->roles[$roleName] = &get_role($roleName);
434 - * Makes trouble, but why?
435 - * Error: "Notice: Only variable references should be returned by reference"
436 - */
356 + return $this->getAccessHandler()->isValidObjectType($sObjectType);
357 + }
437 358
438 - $this->roles[$roleName] = array('role_name' => $roleName);
439 - }
440 -
441 359 /**
442 - * Removes a role from the role group.
443 - *
444 - * @param integer $roleName The role name which should be removed.
445 - *
360 + * Sets the recursive object membership.
361 + *
362 + * @param string $sObjectType The object type.
363 + * @param integer $iObjectId The object id.
364 + * @param array $aObjectMembership The membership.
365 + *
446 366 * @return null
447 367 */
448 - function removeRole($roleName)
449 - {
450 - $this->getRoles();
451 - unset($this->roles[$roleName]);
452 - }
453 -
454 - /**
455 - * Unsets the roles.
456 - *
457 - * @param boolean $plusRemove If true also database entrys will remove.
458 - *
459 - * @return null;
460 - */
461 - function unsetRoles($plusRemove = false)
368 + public function setRecursiveMembership($sObjectType, $iObjectId, $aObjectMembership)
462 369 {
463 - if ($plusRemove) {
464 - $this->_deleteRolesFromDb();
370 + if (!isset($this->_aSetRecursive[$sObjectType])) {
371 + $this->_aSetRecursive[$sObjectType] = array();
465 372 }
466 373
467 - $this->roles = array();
374 + $this->_aSetRecursive[$sObjectType][$iObjectId] = $aObjectMembership;
468 375 }
469 -
376 +
470 377 /**
471 - * Removes all roles from the user group.
472 - *
473 - * @return null
474 - */
475 - private function _deleteRolesFromDb()
476 - {
477 - if ($this->id == null) {
478 - return false;
479 - }
480 -
481 - global $wpdb;
482 -
483 - $wpdb->query(
484 - "DELETE FROM " . DB_ACCESSGROUP_TO_ROLE . "
485 - WHERE group_id = ".$this->id
486 - );
487 - }
488 -
489 -
490 - /*
491 - * Group users functions.
492 - */
493 -
494 - /**
495 - * Returns the assigned users.
496 - *
378 + * Returns the recursive membership.
379 + *
380 + * @param string $sObjectType The object type.
381 + * @param integer $iObjectId The object id.
382 + * @param string $sCurObjectType The current object type.
383 + *
497 384 * @return array
498 385 */
499 - private function _getAssignedUsers()
386 + public function getRecursiveMembershipForObjectType($sObjectType, $iObjectId, $sCurObjectType)
500 387 {
501 - if ($this->_assignedUsers !== null) {
502 - return $this->_assignedUsers;
388 + if (isset($this->_aSetRecursive[$sObjectType])
389 + && isset($this->_aSetRecursive[$sObjectType][$iObjectId])
390 + && isset($this->_aSetRecursive[$sObjectType][$iObjectId][$sCurObjectType])
391 + ) {
392 + return $this->_aSetRecursive[$sObjectType][$iObjectId][$sCurObjectType];
503 393 }
504 394
505 - global $wpdb;
506 -
507 - $dbUsers = $wpdb->get_results(
508 - "SELECT user_id
509 - FROM " . DB_ACCESSGROUP_TO_USER . "
510 - WHERE group_id = " . $this->id . "
511 - ORDER BY user_id"
512 - );
513 -
514 - $this->_assignedUsers = array();
515 -
516 - foreach ($dbUsers as $dbUser) {
517 - $this->_assignedUsers[$dbUser->user_id] = $dbUser->user_id;
518 - }
519 -
520 - return $this->_assignedUsers;
395 + return array();
521 396 }
522 -
523 - /**
524 - * Checks it the user is assigned to the group.
525 - *
526 - * @param integer $userId The user id.
527 - *
397 +
398 + /**
399 + * Returns true if the requested object is locked recursive.
400 + *
401 + * @param string $sObjectType The object type.
402 + * @param integer $iObjectId The object id.
403 + *
528 404 * @return boolean
529 405 */
530 - private function _isUserAssignedToGroup($userId)
406 + public function isLockedRecursive($sObjectType, $iObjectId)
531 407 {
532 - if (array_key_exists($userId, $this->_getAssignedUsers())) {
408 + if (isset($this->_aSetRecursive[$sObjectType]) && isset($this->_aSetRecursive[$sObjectType][$iObjectId])) {
533 409 return true;
534 - } else {
535 - return false;
536 410 }
411 +
412 + return false;
537 413 }
414 +
415 +
416 + /*
417 + * Meta functions.
418 + */
538 419
539 420 /**
540 - * Returns a single user.
421 + * Magic method getter.
541 422 *
542 - * @param object $user The user.
543 - * @param string $type The return type. Can be real or full.
423 + * @param string $sName The name of the function
424 + * @param array $aArguments The arguments for the function
544 425 *
545 - * @return object
426 + * @return mixed
546 427 */
547 - private function _getSingleUser($user, $type)
428 + public function __call($sName, $aArguments)
548 429 {
549 - if (!isset($user->ID)) {
550 - return null;
430 + $oUam = $this->getAccessHandler()->getUserAccessManager();
431 +
432 + $sPrefix = '';
433 +
434 + if ($oUam->startsWith($sName, 'add')) {
435 + $sPrefix = 'add';
436 + } elseif ($oUam->startsWith($sName, 'remove')) {
437 + $sPrefix = 'remove';
438 + } elseif ($oUam->startsWith($sName, 'isMember')) {
439 + $sPrefix = 'isMember';
551 440 }
552 441
553 - if (isset($this->singleUsers[$user->ID])) {
554 - return $this->singleUsers[$user->ID];
555 - }
442 + $sObjectType = str_replace($sPrefix, '', $sName);
443 + $sObjectType = strtolower($sObjectType);
556 444
557 - $isRecursiveMember = array();
445 + $iObjectId = $aArguments[0];
558 446
559 - if ($type == 'full') {
560 - global $wpdb;
447 + if ($sPrefix == 'add') {
448 + return $this->addObject(
449 + $sObjectType,
450 + $iObjectId
451 + );
452 + } elseif ($sPrefix == 'remove') {
453 + return $this->removeObject(
454 + $sObjectType,
455 + $iObjectId
456 + );
457 + } elseif ($sPrefix == 'ismember') {
458 + $blWithInfo = $aArguments[1];
561 459
562 - $curUserdata = get_userdata($user->ID);
563 -
564 - if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
565 - $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
566 - } else {
567 - $capabilities = null;
568 - }
569 -
570 - $role = is_array($capabilities) ?
571 - array_keys($capabilities) : 'norole';
572 -
573 - if (array_key_exists($role[0], $this->getRoles())
574 - ) {
575 - $isRecursiveMember
576 - = array('byRole' => array());
577 - $isRecursiveMember['byRole'][]
578 - = $role[0];
579 - }
460 + return $this->objectIsMember(
461 + $sObjectType,
462 + $iObjectId,
463 + $blWithInfo
464 + );
580 465 }
581 466
582 - if ($this->_isUserAssignedToGroup($user->ID)
583 - || $isRecursiveMember != array()
584 - ) {
585 - if ($isRecursiveMember != array()) {
586 - $user->recursiveMember = $isRecursiveMember;
587 - }
588 -
589 - $this->singleUsers[$user->ID] = $user;
590 - } else {
591 - $this->singleUsers[$user->ID] = null;
592 - }
593 -
594 - return $this->singleUsers[$user->ID];
467 + return null;
595 468 }
596 469
597 470 /**
598 - * Returns the users in the group
471 + * Returns the sql query.
599 472 *
600 - * @param string $type The return type. Can be real or full.
601 - *
602 - * @return array
473 + * @param string $sObjectType The object type.
474 + * @param string $sAction The sql action.
475 + * @param array $aKeys The keys for the insert query.
476 + *
477 + * @return string
603 478 */
604 - function getUsers($type = 'real')
479 + protected function _getSqlQuery($sObjectType, $sAction, $aKeys = array())
605 480 {
606 - if ($this->id == null) {
607 - return array();
608 - }
481 + $sSql = '';
609 482
610 - if ($type != 'real'
611 - && $type != 'full'
612 - ) {
613 - return array();
614 - }
615 -
616 - if ($this->users[$type] != -1) {
617 - return $this->users[$type];
618 - } else {
619 - $this->users[$type] = array();
620 - }
621 -
622 - global $wpdb;
623 -
624 - if ($type == 'real') {
625 - $dbUsers = $wpdb->get_results(
626 - "SELECT user_id as ID
627 - FROM " . DB_ACCESSGROUP_TO_USER . "
628 - WHERE group_id = " . $this->id . "
629 - ORDER BY user_id",
630 - ARRAY_A
631 - );
632 - } elseif ($type == 'full') {
633 - $dbUsers = $wpdb->get_results(
634 - "SELECT ID, user_nicename
635 - FROM $wpdb->users
636 - ORDER BY user_nicename"
637 - );
638 - }
639 -
640 - if (isset($dbUsers)) {
641 - foreach ($dbUsers as $dbUser) {
642 - $user = $this->_getSingleUser($dbUser, $type);
643 -
644 - if ($user !== null) {
645 - $this->users[$type][$user->ID] = $user;
646 - }
483 + if ($sAction == 'select') {
484 + $sSql = "SELECT object_id as id
485 + FROM ".DB_ACCESSGROUP_TO_OBJECT."
486 + WHERE group_id = ".$this->getId()."
487 + AND object_type = '".$sObjectType ."'";
488 + } elseif ($sAction == 'delete') {
489 + $sSql = "DELETE FROM ".DB_ACCESSGROUP_TO_OBJECT."
490 + WHERE group_id = ".$this->getId()."
491 + AND object_type = '".$sObjectType ."'";
492 + } elseif ($sAction == 'insert') {
493 + $sSql = "INSERT INTO ".DB_ACCESSGROUP_TO_OBJECT." (
494 + group_id,
495 + object_id,
496 + object_type
497 + ) VALUES ";
498 +
499 + foreach ($aKeys as $sKey) {
500 + $sKey = trim($sKey);
501 + $sSql .= "('".$this->getId()."', '".$sKey."', '".$sObjectType."'), ";
647 502 }
503 +
504 + $sSql = rtrim($sSql, ', ');
505 + $sSql .= " ON DUPLICATE KEY UPDATE group_id = group_id ";
648 506 }
649 507
650 - return $this->users[$type];
508 + return $sSql;
651 509 }
652 510
653 511 /**
654 - * Adds a user to the user group.
512 + * Adds a object of the given type.
655 513 *
656 - * @param integer $userID The user id which should be added.
514 + * @param string $sObjectType The object type.
515 + * @param integer $iObjectId The object _iId.
657 516 *
658 517 * @return null
659 518 */
660 - function addUser($userID)
519 + public function addObject($sObjectType, $iObjectId)
661 520 {
662 - $this->getUsers();
663 - $this->users['real'][$userID] = get_userdata($userID);
664 - $this->users['full'] = -1;
521 + if (!$this->isValidObjectType($sObjectType)) {
522 + return;
523 + }
524 +
525 + $this->getAccessHandler()->unsetUserGroupsForObject();
526 + $this->getObjectsFromType($sObjectType);
527 +
528 + $oObject = new stdClass();
529 + $oObject->iId = $iObjectId;
530 +
531 + $this->_aObjects[$sObjectType]['real'][$iObjectId] = $oObject;
532 + $this->_aObjects[$sObjectType]['full'] = -1;
533 +
534 + $this->_aAssignedObjects[$sObjectType][$iObjectId] = $iObjectId;
665 535 }
666 536
667 537 /**
668 - * Removes a user from the user group.
538 + * Removes a object of the given type.
669 539 *
670 - * @param integer $userID The user id which should be removed.
540 + * @param string $sObjectType The object type.
541 + * @param integer $sObjectId The object _iId.
671 542 *
672 543 * @return null
673 544 */
674 - function removeUser($userID)
545 + public function removeObject($sObjectType, $sObjectId)
675 546 {
676 - $this->getUsers();
677 - unset($this->users['real'][$userID]);
678 - $this->users['full'] = -1;
547 + if (!$this->isValidObjectType($sObjectType)) {
548 + return;
549 + }
550 +
551 + $this->getAccessHandler()->unsetUserGroupsForObject();
552 + $this->getObjectsFromType($sObjectType);
553 +
554 + unset($this->_aObjects[$sObjectType]['real'][$sObjectId]);
555 + $this->_aObjects[$sObjectType]['full'] = -1;
556 +
557 + unset($this->_aSingleObjects[$sObjectType][$sObjectId]);
558 + unset($this->_aAssignedObjects[$sObjectType][$sObjectId]);
679 559 }
680 560
681 561 /**
682 - * Unsets the users.
562 + * Returns the assigned _aObjects.
683 563 *
684 - * @param boolean $plusRemove If true also database entrys will remove.
564 + * @param string $sObjectType The object type.
685 565 *
686 - * @return null;
566 + * @return array
687 567 */
688 - function unsetUsers($plusRemove = false)
568 + protected function _getAssignedObjects($sObjectType)
689 569 {
690 - if ($plusRemove) {
691 - $this->_deleteUsersFromDb();
570 + if ($this->_aAssignedObjects[$sObjectType] !== null) {
571 + return $this->_aAssignedObjects[$sObjectType];
692 572 }
693 573
694 - $this->users = array(
695 - 'real' => array(),
696 - 'full' => array(),
697 - );
698 - }
699 -
700 - /**
701 - * Removes all users from the user group.
702 - *
703 - * @return null
704 - */
705 - private function _deleteUsersFromDb()
706 - {
707 - global $wpdb;
574 + $sCacheKey = '_getAssignedObjects|'.$sObjectType.'|'.$this->getId();
575 + $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
576 + $aAssignedObjects = $oUserAccessManager->getFromCache($sCacheKey);
577 +
578 + if ($aAssignedObjects !== null) {
579 + $this->_aAssignedObjects[$sObjectType] = $aAssignedObjects;
580 + } else {
581 + /**
582 + * @var wpdb $wpdb
583 + */
584 + global $wpdb;
585 +
586 + $aDbObjects = $wpdb->get_results(
587 + $this->_getSqlQuery($sObjectType, 'select')
588 + );
589 +
590 + $this->_aAssignedObjects[$sObjectType] = array();
591 +
592 + foreach ($aDbObjects as $oDbObject) {
593 + $this->_aAssignedObjects[$sObjectType][$oDbObject->id]
594 + = $oDbObject->id;
595 + }
596 +
597 + $oUserAccessManager->addToCache($sCacheKey, $this->_aAssignedObjects[$sObjectType]);
598 + }
708 599
709 - $wpdb->query(
710 - "DELETE FROM " . DB_ACCESSGROUP_TO_USER . "
711 - WHERE group_id = ".$this->id
712 - );
600 + return $this->_aAssignedObjects[$sObjectType];
713 601 }
714 602
715 603 /**
716 - * Checks if the given user is a member of the group.
604 + * Checks if the object is assigned to the group.
717 605 *
718 - * @param interger $userId The id of the user which should be checked.
719 - * @param boolean $withInfo If true then we return additional infos.
606 + * @param string $sObjectType The object type.
607 + * @param integer $iObjectId The object _iId.
720 608 *
721 609 * @return boolean
722 610 */
723 - function userIsMember($userId, $withInfo = false)
611 + protected function _isObjectAssignedToGroup($sObjectType, $iObjectId)
724 612 {
725 - $user->ID = $userId;
726 - $user = $this->_getSingleUser($user, 'full');
727 -
728 - if ($user !== null) {
729 - if (isset($user->recursiveMember)
730 - && $withInfo
731 - ) {
732 - return $user->recursiveMember;
733 - }
734 -
735 - return true;
736 - }
737 -
738 - return false;
613 + $aAssignedObjects = $this->_getAssignedObjects($sObjectType);
614 + return isset($aAssignedObjects[$iObjectId]);
739 615 }
740 616
741 -
742 - /*
743 - * Group categories functions.
744 - */
745 -
746 617 /**
747 - * Returns the assigned categories.
618 + * Unset the objects.
748 619 *
749 - * @return array
620 + * @param string $sObjectType The object type.
621 + * @param boolean $blPlusRemove If true also database entries will remove.
622 + *
623 + * @return null;
750 624 */
751 - private function _getAssignedCategories()
625 + public function unsetObjects($sObjectType, $blPlusRemove = false)
752 626 {
753 - if ($this->_assignedCategories !== null) {
754 - return $this->_assignedCategories;
627 + if (!$this->isValidObjectType($sObjectType)) {
628 + return;
755 629 }
756 630
757 - global $wpdb;
758 -
759 - $dbCategories = $wpdb->get_results(
760 - "SELECT *
761 - FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
762 - WHERE group_id = " . $this->id . "
763 - ORDER BY category_id"
631 + if ($blPlusRemove) {
632 + $this->_deleteObjectsFromDb($sObjectType);
633 + }
634 +
635 + $this->_aAssignedObjects[$sObjectType] = array();
636 + $this->getAccessHandler()->getUserAccessManager()->flushCache();
637 +
638 + $this->_aObjects[$sObjectType] = array(
639 + 'real' => array(),
640 + 'full' => array(),
764 641 );
765 -
766 - $this->_assignedCategories = array();
767 -
768 - foreach ($dbCategories as $dbCategory) {
769 - $this->_assignedCategories[$dbCategory->category_id]
770 - = $dbCategory->category_id;
771 - }
772 -
773 - return $this->_assignedCategories;
774 642 }
775 643
776 - /**
777 - * Checks it the category is assigned to the group.
644 + /**
645 + * Removes all _aObjects from the user group.
778 646 *
779 - * @param integer $categoryId The category id.
647 + * @param string $sObjectType The object type.
780 648 *
781 - * @return boolean
649 + * @return null
782 650 */
783 - private function _isCategoryAssignedToGroup($categoryId)
651 + protected function _deleteObjectsFromDb($sObjectType)
784 652 {
785 - if (array_key_exists($categoryId, $this->_getAssignedCategories())) {
786 - return true;
787 - } else {
788 - return false;
653 + if (isset($this->_iId)) {
654 + /**
655 + * @var wpdb $wpdb
656 + */
657 + global $wpdb;
658 +
659 + $sQuery = $this->_getSqlQuery($sObjectType, 'delete');
660 + $wpdb->query($sQuery);
789 661 }
790 662 }
791 663
792 664 /**
793 - * Returns a single category.
665 + * Checks if the given object is a member of the group.
794 666 *
795 - * @param object $category The category.
796 - * @param string $type The return type. Can be real or full.
667 + * @param string $sObjectType The object type.
668 + * @param integer $iObjectId The _iId of the object which should be checked.
669 + * @param boolean $blWithInfo If true then we return additional info.
797 670 *
798 - * @return object
671 + * @return boolean|array
799 672 */
800 - private function _getSingleCategory($category, $type)
673 + public function objectIsMember($sObjectType, $iObjectId, $blWithInfo = false)
801 674 {
802 - if (!isset($category->term_id)) {
803 - return null;
804 - }
805 -
806 - if (isset($this->singleCategories[$category->term_id])) {
807 - return $this->singleCategories[$category->term_id];
808 - }
809 -
810 - $isRecursiveMember = array();
811 -
812 - $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
813 - $uamOptions = $userAccessManager->getAdminOptions();
814 -
815 - if ($uamOptions['lock_recursive'] == 'true'
816 - && $type == 'full'
817 - ) {
818 - if ($category->parent != 0) {
819 - $parentCategory = get_post($category->parent);
820 -
821 - $parentCategory = $this->_getSingleCategory(
822 - $parentCategory,
823 - $type
824 - );
675 + $sCacheKey = $sObjectType.'|'.$iObjectId;
676 + $sCacheKey .= ($blWithInfo) ? '|wi' : '|ni';
825 677
826 - if ($parentCategory !== null) {
827 - if (isset($parentCategory->recursiveMember)) {
828 - $isRecursiveMember['byCategory'][]
829 - = $parentCategory;
678 + if (!isset($this->_aObjectIsMember[$sCacheKey])) {
679 + $this->_aObjectIsMember[$sCacheKey] = false;
680 +
681 + if ($this->isValidObjectType($sObjectType)) {
682 + $oObject = $this->_getSingleObject($sObjectType, $iObjectId, 'full');
683 +
684 + if ($oObject !== null) {
685 + if ($blWithInfo && isset($oObject->recursiveMember)) {
686 + $this->_aObjectIsMember[$sCacheKey] = $oObject->recursiveMember;
830 687 } else {
831 - $isRecursiveMember['byCategory'][]
832 - = $parentCategory->term_id;
688 + $this->_aObjectIsMember[$sCacheKey] = true;
833 689 }
834 690 }
835 691 }
836 692 }
837 693
838 - if ($this->_isCategoryAssignedToGroup($category->term_id)
839 - || $isRecursiveMember != array()
840 - ) {
841 - if ($isRecursiveMember != array()) {
842 - $category->recursiveMember = $isRecursiveMember;
843 - }
844 -
845 - $this->singleCategories[$category->term_id] = $category;
846 - } else {
847 - $this->singleCategories[$category->term_id] = null;
848 - }
694 + return $this->_aObjectIsMember[$sCacheKey];
695 + }
849 696
850 - return $this->singleCategories[$category->term_id];
851 - }
852 -
853 697 /**
854 - * Returns the categories in the group
698 + * Returns all objects of the given type.
855 699 *
856 - * @param string $type The return type. Can be real or full.
700 + * @param string $sObjectType The object type.
701 + * @param string $sType The return type, could be real or full.
857 702 *
858 703 * @return array
859 704 */
860 - function getCategories($type = 'real')
705 + function getObjectsFromType($sObjectType, $sType = 'real')
861 706 {
862 - if ($this->id == null) {
863 - return array();
707 + if (!$this->isValidObjectType($sObjectType)) {
708 + return null;
864 709 }
865 710
866 - if ($type != 'real'
867 - && $type != 'full'
868 - ) {
711 + if ($this->_iId == null || $sType != 'real' && $sType != 'full') {
869 712 return array();
870 713 }
871 714
872 - if ($this->categories[$type] != -1) {
873 - return $this->categories[$type];
715 + if (isset($this->_aObjects[$sObjectType])
716 + && isset($this->_aObjects[$sObjectType][$sType])
717 + && $this->_aObjects[$sObjectType][$sType] != -1
718 + ) {
719 + return $this->_aObjects[$sObjectType][$sType];
874 720 } else {
875 - $this->categories[$type] = array();
721 + $this->_aObjects[$sObjectType][$sType] = array();
876 722 }
723 +
724 + $aObjectIds = $this->_getAssignedObjects($sObjectType);
725 +
726 + foreach ($aObjectIds as $sObjectId) {
727 + $oObject = $this->_getSingleObject($sObjectType, $sObjectId, $sType);
728 +
729 + if ($oObject !== null) {
730 + $this->_aObjects[$sObjectType][$sType][$oObject->id] = $oObject;
731 + }
732 + }
877 733
878 - global $wpdb;
879 - $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
880 - $uamOptions = $userAccessManager->getAdminOptions();
881 -
882 - $dbCategories = $wpdb->get_results(
883 - "SELECT *
884 - FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
885 - WHERE group_id = " . $this->id . "
886 - ORDER BY category_id",
887 - ARRAY_A
888 - );
889 -
890 - if (isset($dbCategories)) {
891 - foreach ($dbCategories as $dbCategorie) {
892 - $category = get_category($dbCategorie['category_id']);
893 -
894 - if ($category != null) {
895 - if ($uamOptions['lock_recursive'] == 'true'
896 - && $type == 'full'
897 - ) {
898 - //We have to remove the filter to get all categories
899 - $removeSucc = remove_filter(
900 - 'get_terms',
901 - array(
902 - $this->getAccessHandler()->getUserAccessManager(),
903 - 'showCategory'
904 - )
905 - );
906 -
907 - if ($removeSucc) {
908 - $args = array(
909 - 'child_of' => $category->term_id,
910 - 'hide_empty' => false
911 - );
912 -
913 - $categoryChilds = get_categories($args);
914 -
915 - add_filter(
916 - 'get_terms',
917 - array(&$userAccessManager, 'showCategory')
918 - );
919 -
920 - foreach ($categoryChilds as $categoryChild) {
921 - $categoryChild->recursiveMember
922 - = array('byCategory' => array());
923 - $categoryChild->recursiveMember['byCategory'][]
924 - = $category->term_id;
925 - $this->categories[$type][$categoryChild->term_id]
926 - = $categoryChild;
927 - }
928 - }
929 - }
930 -
931 - $this->categories[$type][$category->term_id] = $category;
932 - }
734 + if ($sType == 'full' && $this->getAccessHandler()->isPostableType($sObjectType) && $sObjectType != 'role') {
735 + if ($sObjectType == 'category') {
736 + $this->_aObjects[$sObjectType][$sType] = $this->getFullCategories($this->_aObjects[$sObjectType][$sType]);
737 + } elseif ($sObjectType == 'user') {
738 + $this->_aObjects[$sObjectType][$sType] = $this->getFullUsers();
739 + } else {
740 + $oPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
741 + $this->_aObjects[$sObjectType][$sType] = $oPlObject['reference']->{$oPlObject['getFullObjects']}(
742 + $this->_aObjects[$sObjectType][$sType],
743 + $this
744 + );
933 745 }
934 746 }
935 747
936 - return $this->categories[$type];
748 + return $this->_aObjects[$sObjectType][$sType];
937 749 }
938 750
939 751 /**
940 - * Adds a category to the category group.
752 + * Returns a single object.
941 753 *
942 - * @param integer $categoryID The category id which should be added.
754 + * @param string $sObjectType The object type.
755 + * @param integer $iObjectId The _iId of the object which should be checked.
756 + * @param string $sType The return type. Can be real or full.
943 757 *
944 - * @return null
758 + * @return object
945 759 */
946 - function addCategory($categoryID)
760 + protected function _getSingleObject($sObjectType, $iObjectId, $sType)
947 761 {
948 - $this->getCategories();
949 - $this->categories['real'][$categoryID] = get_category($categoryID);
950 - $this->categories['full'] = -1;
762 + if (!isset($this->_aSingleObjects[$sObjectType])
763 + || !isset($this->_aSingleObjects[$sObjectType][$iObjectId])
764 + ) {
765 + $this->_aSingleObjects[$sObjectType][$iObjectId] = null;
766 + $aIsRecursiveMember = array();
767 +
768 + if ($sType == 'full' && $sObjectType != 'role') {
769 + if ($sObjectType == 'category') {
770 + $aIsRecursiveMember = $this->_getFullCategory($iObjectId);
771 + } elseif ($sObjectType == 'user') {
772 + $aIsRecursiveMember = $this->_getFullUser($iObjectId);
773 + } elseif ($this->getAccessHandler()->isPostableType($sObjectType)) {
774 + $aIsRecursiveMember = $this->_getFullPost($sObjectType, $iObjectId);
775 + } else {
776 + $aIsRecursiveMember = $this->_getFullPlObject($sObjectType, $iObjectId);
777 + }
778 + }
779 +
780 + if ($aIsRecursiveMember != array()
781 + || $this->_isObjectAssignedToGroup($sObjectType, $iObjectId)
782 + ) {
783 + $oObject = new stdClass();
784 + $oObject->id = $iObjectId;
785 +
786 + if ($aIsRecursiveMember != array()) {
787 + $oObject->recursiveMember = $aIsRecursiveMember;
788 + }
789 +
790 + $this->_aSingleObjects[$sObjectType][$iObjectId] = $oObject;
791 + }
792 + }
793 +
794 + return $this->_aSingleObjects[$sObjectType][$iObjectId];
951 795 }
796 +
952 797
953 - /**
954 - * Removes a category from the category group.
955 - *
956 - * @param integer $categoryID The category id which should be removed.
957 - *
958 - * @return null
798 + /*
799 + * Group users functions.
959 800 */
960 - function removeCategory($categoryID)
961 - {
962 - $this->getCategories();
963 - unset($this->categories['real'][$categoryID]);
964 - $this->categories['full'] = -1;
965 - }
966 801
967 802 /**
968 - * Unsets the categories.
803 + * Returns a single user.
969 804 *
970 - * @param boolean $plusRemove If true also database entrys will remove.
805 + * @param integer $iObjectId The object _iId.
971 806 *
972 - * @return null;
807 + * @return array
973 808 */
974 - function unsetCategories($plusRemove = false)
809 + protected function _getFullUser($iObjectId)
975 810 {
976 - if ($plusRemove) {
977 - $this->_deleteCategoriesFromDb();
978 - }
811 + $aIsRecursiveMember = array();
979 812
980 - $this->categories = array(
981 - 'real' => array(),
982 - 'full' => array(),
983 - );
984 - }
985 -
986 - /**
987 - * Removes all categories from the user group.
988 - *
989 - * @return null
990 - */
991 - private function _deleteCategoriesFromDb()
992 - {
993 813 global $wpdb;
994 814
995 - $wpdb->query(
996 - "DELETE FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
997 - WHERE group_id = ".$this->id
998 - );
999 - }
1000 -
1001 - /**
1002 - * Checks if the given post is a member of the group.
1003 - *
1004 - * @param interger $categoryId The id of the post which should be checked.
1005 - * @param boolean $withInfo If true then we return additional infos.
1006 - *
1007 - * @return boolean
1008 - */
1009 - function categoryIsMember($categoryId, $withInfo = false)
1010 - {
1011 - $category = get_category($categoryId);
1012 - $category = $this->_getSingleCategory($category, 'full');
815 + $oCurUserData = get_userdata($iObjectId);
1013 816
1014 - if ($category !== null) {
1015 - if (isset($category->recursiveMember)
1016 - && $withInfo
1017 - ) {
1018 - return $category->recursiveMember;
817 + if (isset($oCurUserData->{$wpdb->prefix . "capabilities"})) {
818 + $aCapabilities = $oCurUserData->{$wpdb->prefix . "capabilities"};
819 + } else {
820 + $aCapabilities = array();
821 + }
822 +
823 + $aRoles = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
824 + $aObjects = $this->getObjectsFromType('role');
825 +
826 + foreach ($aRoles as $sRole) {
827 + if (isset($aObjects[$sRole])) {
828 + $oRoleObject = new stdClass();
829 + $oRoleObject->name = $sRole;
830 +
831 + $aIsRecursiveMember = array('role' => array());
832 + $aIsRecursiveMember['role'][] = $oRoleObject;
1019 833 }
1020 -
1021 - return true;
1022 834 }
1023 835
1024 - /*$categories = $this->getCategories('full');
1025 -
1026 - if (array_key_exists($categoryId, $categories)) {
1027 - if (isset($categories[$categoryId]->recursiveMember)
1028 - && $withInfo
1029 - ) {
1030 - return $categories[$categoryId]->recursiveMember;
1031 - }
1032 -
1033 - return true;
1034 - }*/
1035 -
1036 - return false;
836 + return $aIsRecursiveMember;
1037 837 }
1038 838
1039 -
1040 - /*
1041 - * Group posts functions.
1042 - */
1043 -
1044 839 /**
1045 - * Returns the assigned posts
840 + * Returns the users in the group
1046 841 *
1047 842 * @return array
1048 843 */
1049 - private function _getAssignedPosts()
844 + public function getFullUsers()
1050 845 {
1051 - if ($this->_assignedPosts !== null) {
1052 - return $this->_assignedPosts;
846 + $sCacheKey = 'getFullUsers';
847 + $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
848 + $aFullUsers = $oUserAccessManager->getFromCache($sCacheKey);
849 +
850 + if ($aFullUsers === null) {
851 + /**
852 + * @var wpdb $wpdb
853 + */
854 + global $wpdb;
855 +
856 + $aDbUsers = $wpdb->get_results(
857 + "SELECT ID, user_nicename
858 + FROM ".$wpdb->users
859 + );
860 +
861 + $aFullUsers = array();
862 +
863 + if (isset($aDbUsers)) {
864 + foreach ($aDbUsers as $oDbUser) {
865 + $oUser = $this->_getSingleObject('user', $oDbUser->ID, 'full');
866 +
867 + if ($oUser !== null) {
868 + $aFullUsers[$oUser->id] = $oUser;
869 + }
870 + }
871 + }
872 +
873 + $oUserAccessManager->addToCache($sCacheKey, $aFullUsers);
1053 874 }
1054 875
1055 - global $wpdb;
1056 -
1057 - $dbPosts = $wpdb->get_results(
1058 - "SELECT post_id
1059 - FROM " . DB_ACCESSGROUP_TO_POST . "
1060 - WHERE group_id = " . $this->id
1061 - );
1062 -
1063 - $this->_assignedPosts = array();
1064 -
1065 - foreach ($dbPosts as $dbPost) {
1066 - $this->_assignedPosts[$dbPost->post_id] = $dbPost->post_id;
1067 - }
1068 -
1069 - return $this->_assignedPosts;
876 + return $aFullUsers;
1070 877 }
1071 878
1072 - /**
1073 - * Checks it the post is assigned to the group.
1074 - *
1075 - * @param integer $postId The post id.
1076 - *
1077 - * @return boolean
879 +
880 + /*
881 + * Group categories functions.
1078 882 */
1079 - private function _isPostAssignedToGroup($postId)
1080 - {
1081 - if (array_key_exists($postId, $this->_getAssignedPosts())) {
1082 - return true;
1083 - } else {
1084 - return false;
1085 - }
1086 - }
1087 883
1088 884 /**
1089 - * Returns the membership of a single post.
885 + * Returns a single category.
1090 886 *
1091 - * @param object $post The post object.
1092 - * @param string $type The return type.
1093 - * @param string $postType The post type needed for the intern representation.
887 + * @param integer $iObjectId The object _iId.
1094 888 *
1095 889 * @return object
1096 890 */
1097 - function _getSinglePost($post, $type, $postType)
891 + protected function _getFullCategory($iObjectId)
1098 892 {
1099 - if (!isset($post->ID)) {
1100 - return null;
1101 - }
893 + $oCategory = $this->getAccessHandler()->getUserAccessManager()->getCategory($iObjectId);
894 +
895 + $aIsRecursiveMember = array();
1102 896
1103 - if (isset($this->singlePosts[$post->ID])) {
1104 - return $this->singlePosts[$post->ID];
1105 - }
1106 -
1107 - $isRecursiveMember = array();
1108 -
1109 - $userAccessManager = &$this->getAccessHandler()->getUserAccessManager();
1110 - $uamOptions = $userAccessManager->getAdminOptions();
1111 -
1112 - if ($type == 'full') {
1113 - foreach ($this->getCategories('full') as $category) {
1114 - if (in_category($category->cat_ID, $post->ID)) {
1115 - $isRecursiveMember['byCategory'][] = $category->cat_ID;
1116 - //break;
1117 - }
1118 - }
897 + $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
898 + $aUamOptions = $oUserAccessManager->getAdminOptions();
1119 899
1120 - if (($postType == 'page'
1121 - || $postType == 'file')
1122 - && $uamOptions['lock_recursive'] == 'true'
900 + if ($aUamOptions['lock_recursive'] == 'true') {
901 + if (isset($oCategory->parent)
902 + && !is_null($oCategory->parent)
1123 903 ) {
1124 - if ($post->post_parent != 0) {
1125 - $parentPost = get_post($post->post_parent);
1126 -
1127 - $parentPost = $this->_getSinglePost(
1128 - $parentPost,
1129 - $type,
1130 - $parentPost->post_type
1131 - );
904 + $oParentCategory = $this->_getSingleObject(
905 + 'category',
906 + $oCategory->parent,
907 + 'full'
908 + );
1132 909
1133 - if ($parentPost !== null) {
1134 - if (isset($parentPost->recursiveMember)) {
1135 - $isRecursiveMember['byPost'][]
1136 - = $parentPost;
1137 - } else {
1138 - $isRecursiveMember['byPost'][]
1139 - = $parentPost->ID;
1140 - }
1141 - }
910 + if ($oParentCategory !== null) {
911 + $oCurCategory = $this->getAccessHandler()->getUserAccessManager()->getCategory($iObjectId);
912 + $oParentCategory->name = $oCurCategory->name;
913 + $aIsRecursiveMember['category'][] = $oParentCategory;
1142 914 }
1143 915 }
1144 916 }
1145 917
1146 - if ($this->_isPostAssignedToGroup($post->ID)
1147 - || $isRecursiveMember != array()
1148 - ) {
1149 - if ($isRecursiveMember != array()) {
1150 - $post->recursiveMember = $isRecursiveMember;
1151 - }
1152 - $this->singlePosts[$post->ID] = $post;
1153 - } else {
1154 - $this->singlePosts[$post->ID] = null;
1155 - }
1156 -
1157 - return $this->singlePosts[$post->ID];
918 + return $aIsRecursiveMember;
1158 919 }
1159 920
1160 921 /**
1161 - * Returns the posts by the given type in the group
922 + * Returns the categories in the group
1162 923 *
1163 - * @param string $postType The type of the post.
1164 - * @param string $type The return type. Can be real or full.
924 + * @param array $aCategories The real categories.
1165 925 *
1166 926 * @return array
1167 927 */
1168 - private function _getPostByType($postType, $type)
928 + public function getFullCategories($aCategories)
1169 929 {
1170 - if ($type != 'real'
1171 - && $type != 'full'
1172 - ) {
1173 - return array();
1174 - }
1175 -
1176 - if ($postType == 'file') {
1177 - $wpType = 'attachment';
1178 - } else {
1179 - $wpType = $postType;
1180 - }
1181 -
1182 - if ($this->{$postType.'s'}[$type] != -1) {
1183 - return $this->{$postType.'s'}[$type];
1184 - } else {
1185 - $this->{$postType.'s'}[$type] = array();
1186 - }
1187 -
1188 - global $wpdb;
930 + $aUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
931 + $aUamOptions = $aUserAccessManager->getAdminOptions();
1189 932
1190 - $posts = $wpdb->get_results(
1191 - "SELECT ID, post_parent
1192 - FROM $wpdb->posts
1193 - WHERE post_type = '".$wpType."'"
1194 - );
1195 -
1196 - if (isset($posts)) {
1197 - foreach ($posts as $post) {
1198 - $post = $this->_getSinglePost($post, $type, $postType);
1199 -
1200 - if ($post !== null) {
1201 - $this->{$postType.'s'}[$type][$post->ID] = $post;
933 + foreach ($aCategories as $oCategory) {
934 + if ($oCategory != null) {
935 + if ($aUamOptions['lock_recursive'] == 'true') {
936 + //We have to remove the filter to get all categories
937 + $blRemoveSuccess = remove_filter(
938 + 'get_terms',
939 + array(
940 + $this->getAccessHandler()->getUserAccessManager(),
941 + 'showCategory'
942 + )
943 + );
944 +
945 + if ($blRemoveSuccess) {
946 + $aArgs = array(
947 + 'child_of' => $oCategory->id,
948 + 'hide_empty' => false
949 + );
950 +
951 + $aCategoryChildren = get_categories($aArgs);
952 +
953 + add_filter(
954 + 'get_terms',
955 + array($aUserAccessManager, 'showCategory')
956 + );
957 +
958 +
959 + foreach ($aCategoryChildren as $oCategoryChild) {
960 + $oCurCategoryChild = new stdClass();
961 + $oCurCategoryChild->id = $oCategoryChild->term_id;
962 + $oCurCategoryChild->name = $oCategoryChild->name;
963 +
964 + $oCurCategoryChild->recursiveMember = array('category' => array());
965 + $oCurCategoryChild->recursiveMember['category'][] = $oCurCategoryChild;
966 + $aCategories[$oCurCategoryChild->id] = $oCurCategoryChild;
967 + }
968 + }
1202 969 }
970 +
971 + $aCategories[$oCategory->id] = $oCategory;
1203 972 }
1204 973 }
1205 974
1206 - return $this->{$postType.'s'}[$type];
975 + return $aCategories;
1207 976 }
1208 977
978 +
979 + /*
980 + * Group posts functions.
981 + */
982 +
1209 983 /**
1210 - * Removes all post of all types from the user group.
984 + * Checks if the give post in the give category.
1211 985 *
1212 - * @param string $postType The type which should be deleted.
986 + * @param integer $iPostId The post _iId.
987 + * @param integer $iCategoryId The category _iId.
1213 988 *
1214 - * @return null;
989 + * @return boolean
1215 990 */
1216 - private function _deletePostByTypeFromDb($postType)
991 + protected function _isPostInCategory($iPostId, $iCategoryId)
1217 992 {
1218 - if ($this->id == null) {
1219 - return false;
1220 - }
1221 -
1222 - global $wpdb;
1223 -
1224 - if ($postType == 'all') {
1225 - $wpdb->query(
1226 - "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
1227 - WHERE group_id = ".$this->id
1228 - );
1229 - } else {
1230 - if ($type == 'post') {
1231 - $curPostTypes = $this->getPosts();
1232 - } elseif ($type == 'page') {
1233 - $curPostTypes = $this->getPages();
1234 - } elseif ($type == 'file') {
1235 - $curPostTypes = $this->getFiles();
1236 - }
1237 -
1238 - foreach ($curPostTypes as $id => $post) {
1239 - $wpdb->query(
1240 - "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
1241 - WHERE group_id = ".$this->id."
1242 - AND post_id = ".$id."
1243 - LIMIT 1"
993 + if ($this->_aObjectsInCategory === null) {
994 + $this->_aObjectsInCategory = array();
995 + $sCacheKey = '_isPostInCategory';
996 + $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
997 + $aObjectsInCategory = $oUserAccessManager->getFromCache($sCacheKey);
998 +
999 + if ($aObjectsInCategory === null) {
1000 + /**
1001 + * @var wpdb $wpdb
1002 + */
1003 + global $wpdb;
1004 +
1005 + $aDbObjects = $wpdb->get_results(
1006 + "SELECT tr.object_id AS objectId, tt.term_id AS categoryId
1007 + FROM ".$wpdb->term_relationships." AS tr,
1008 + ".$wpdb->term_taxonomy." AS tt
1009 + WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
1010 + AND tt.taxonomy = 'category'"
1244 1011 );
1012 +
1013 + foreach ($aDbObjects as $oDbObject) {
1014 + if (!isset($this->_aObjectsInCategory[$oDbObject->objectId])) {
1015 + $this->_aObjectsInCategory[$oDbObject->objectId] = array();
1016 + }
1017 +
1018 + $this->_aObjectsInCategory[$oDbObject->objectId][$oDbObject->categoryId] = $oDbObject->categoryId;
1019 + }
1020 +
1021 + $oUserAccessManager->addToCache($sCacheKey, $this->_aObjectsInCategory);
1022 + } else {
1023 + $this->_aObjectsInCategory = $aObjectsInCategory;
1245 1024 }
1246 1025 }
1026 +
1027 + return (isset($this->_aObjectsInCategory[$iPostId])
1028 + && isset($this->_aObjectsInCategory[$iPostId][$iCategoryId]));
1247 1029 }
1248 1030
1249 1031 /**
1250 - * Returns the pages in the group
1032 + * Returns the membership of a single post.
1251 1033 *
1252 - * @param string $type The return type. Can be real or full.
1034 + * @param string $sPostType The post type needed for the intern representation.
1035 + * @param integer $iObjectId The object _iId.
1253 1036 *
1254 - * @return array
1037 + * @return object
1255 1038 */
1256 - function getPosts($type = 'real')
1039 + protected function _getFullPost($sPostType, $iObjectId)
1257 1040 {
1258 - return $this->_getPostByType('post', $type);
1259 - }
1260 -
1261 - /**
1262 - * Adds a post to the post group.
1263 - *
1264 - * @param integer $postID The post id which should be added.
1265 - *
1266 - * @return null
1267 - */
1268 - function addPost($postID)
1269 - {
1270 - $this->getPosts();
1271 - $this->posts['real'][$postID] = get_post($postID);
1272 - $this->posts['full'] = -1;
1273 - }
1274 -
1275 - /**
1276 - * Removes a post from the post group.
1277 - *
1278 - * @param integer $postID The post id which should be removed.
1279 - *
1280 - * @return null
1281 - */
1282 - function removePost($postID)
1283 - {
1284 - $this->getPosts();
1285 - unset($this->posts['real'][$postID]);
1286 - $this->posts['full'] = -1;
1287 - }
1288 -
1289 - /**
1290 - * Unsets the posts.
1291 - *
1292 - * @param boolean $plusRemove If true also database entrys will remove.
1293 - *
1294 - * @return null;
1295 - */
1296 - function unsetPosts($plusRemove = false)
1297 - {
1298 - if ($plusRemove) {
1299 - $this->_deletePostByTypeFromDb('post');
1041 + $aIsRecursiveMember = array();
1042 + $oPost = $this->getAccessHandler()->getUserAccessManager()->getPost($iObjectId);
1043 + $aUamOptions = $this->getAccessHandler()->getUserAccessManager()->getAdminOptions();
1044 +
1045 + foreach ($this->getObjectsFromType('category', 'full') as $oCategory) {
1046 + if ($this->_isPostInCategory($oPost->ID, $oCategory->id)) {
1047 + $oCategoryObject = $this->getAccessHandler()->getUserAccessManager()->getCategory($oCategory->id);
1048 + $oCategory->name = $oCategoryObject->name;
1049 +
1050 + $aIsRecursiveMember['category'][] = $oCategory;
1051 + }
1300 1052 }
1301 1053
1302 - $this->posts = array(
1303 - 'real' => array(),
1304 - 'full' => array(),
1305 - );;
1306 - }
1054 + if ($oPost->post_parent == 0
1055 + && $oPost->post_type == 'post'
1056 + && $this->getAccessHandler()->getUserAccessManager()->getWpOption('show_on_front') == 'page'
1057 + && $this->getAccessHandler()->getUserAccessManager()->getWpOption('page_for_posts') != $iObjectId
1058 + ) {
1059 + $iParentId = $this->getAccessHandler()->getUserAccessManager()->getWpOption('page_for_posts');
1060 + } else {
1061 + $iParentId = $oPost->post_parent;
1062 + }
1063 +
1064 + if ($iParentId != 0
1065 + && $aUamOptions['lock_recursive'] == 'true'
1066 + ) {
1067 + $oParent = $this->getAccessHandler()->getUserAccessManager()->getPost($iParentId);
1068 +
1069 + $oParentPost = $this->_getSingleObject(
1070 + $oParent->post_type,
1071 + $iParentId,
1072 + 'full'
1073 + );
1307 1074
1308 - /**
1309 - * Returns the pages in the group
1310 - *
1311 - * @param string $type The return type. Can be real or full.
1312 - *
1313 - * @return array
1314 - */
1315 - function getPages($type = 'real')
1316 - {
1317 - return $this->_getPostByType('page', $type);
1075 + if ($oParentPost !== null) {
1076 + $oPostObject = $this->getAccessHandler()->getUserAccessManager()->getPost($oParentPost->id);
1077 + $oParentPost->name = $oPostObject->post_title;
1078 +
1079 + $aIsRecursiveMember[$oParent->post_type][] = $oParentPost;
1080 + }
1081 + }
1082 +
1083 + return $aIsRecursiveMember;
1318 1084 }
1085 +
1319 1086
1320 - /**
1321 - * Adds a page to the page group.
1322 - *
1323 - * @param integer $pageID The page id which should be added.
1324 - *
1325 - * @return null
1087 + /*
1088 + * Group pluggable _aObjects functions.
1326 1089 */
1327 - function addPage($pageID)
1328 - {
1329 - $this->getPages();
1330 - $this->pages['real'][$pageID] = get_post($pageID);
1331 - $this->pages['full'] = -1;
1332 - }
1333 1090
1334 1091 /**
1335 - * Removes a page from the page group.
1092 + * Returns a the recursive membership for a pluggable object.
1336 1093 *
1337 - * @param integer $pageID The page id which should be removed.
1094 + * @param string $sObjectType The pluggable object type.
1095 + * @param integer $iObjectId The object _iId.
1338 1096 *
1339 - * @return null
1340 - */
1341 - function removePage($pageID)
1342 - {
1343 - $this->getPages();
1344 - unset($this->pages['real'][$pageID]);
1345 - $this->pages['full'] = -1;
1346 - }
1347 -
1348 - /**
1349 - * Unsets the pages.
1350 - *
1351 - * @param boolean $plusRemove If true also database entrys will remove.
1352 - *
1353 - * @return null;
1354 - */
1355 - function unsetPages($plusRemove = false)
1356 - {
1357 - if ($plusRemove) {
1358 - $this->_deletePostByTypeFromDb('page');
1359 - }
1360 -
1361 - $this->pages = array(
1362 - 'real' => array(),
1363 - 'full' => array(),
1364 - );;
1365 - }
1366 -
1367 - /**
1368 - * Returns the files in the group
1369 - *
1370 - * @param string $type The return type. Can be real or full.
1371 - *
1372 1097 * @return array
1373 1098 */
1374 - function getFiles($type = 'real')
1099 + protected function _getFullPlObject($sObjectType, $iObjectId)
1375 1100 {
1376 - return $this->_getPostByType('file', $type);
1377 - }
1378 -
1379 - /**
1380 - * Adds a file to the file group.
1381 - *
1382 - * @param integer $fileID The file id which should be added.
1383 - *
1384 - * @return null
1385 - */
1386 - function addFile($fileID)
1387 - {
1388 - $this->getFiles();
1389 - $this->files['real'][$fileID] = get_post($fileID);
1390 - $this->files['full'] = -1;
1391 - }
1392 -
1393 - /**
1394 - * Removes a file from the file group.
1395 - *
1396 - * @param integer $fileID The file id which should be removed.
1397 - *
1398 - * @return null
1399 - */
1400 - function removeFile($fileID)
1401 - {
1402 - $this->getFiles();
1403 - unset($this->files['real'][$fileID]);
1404 - $this->files['full'] = -1;
1405 - }
1406 -
1407 - /**
1408 - * Unsets the files.
1409 - *
1410 - * @param boolean $plusRemove If true also database entrys will remove.
1411 - *
1412 - * @return null;
1413 - */
1414 - function unsetFiles($plusRemove = false)
1415 - {
1416 - if ($plusRemove) {
1417 - $this->_deletePostByTypeFromDb('file');
1418 - }
1101 + $blIsRecursiveMember = array();
1419 1102
1420 - $this->files = array(
1421 - 'real' => array(),
1422 - 'full' => array(),
1423 - );
1424 - }
1425 -
1426 - /**
1427 - * Checks if the given post is a member of the group.
1428 - *
1429 - * @param interger $postId The id of the post which should be checked.
1430 - * @param boolean $withInfo If true then we return additional infos.
1431 - *
1432 - * @return boolean
1433 - */
1434 - function postIsMember($postId, $withInfo = false)
1435 - {
1436 - $post = get_post($postId);
1103 + $oPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
1437 1104
1438 - if ($post->post_type == 'attachment') {
1439 - $postType = 'file';
1440 - } else {
1441 - $postType = $post->post_type;
1442 - }
1443 -
1444 - $post = $this->_getSinglePost($post, 'full', $postType);
1445 -
1446 - if ($post !== null) {
1447 - if (isset($post->recursiveMember)
1448 - && $withInfo
1449 - ) {
1450 - return $post->recursiveMember;
1105 + if (isset($oPlObject['reference'])
1106 + && isset($oPlObject['getFull'])
1107 + ) {
1108 + $aPlRecMember = $oPlObject['reference']->{$oPlObject['getFull']}(
1109 + $iObjectId,
1110 + $this
1111 + );
1112 +
1113 + if (is_array($aPlRecMember)) {
1114 + $blIsRecursiveMember = $aPlRecMember;
1451 1115 }
1452 -
1453 - return true;
1454 1116 }
1455 -
1456 - return false;
1117 +
1118 + return $blIsRecursiveMember;
1457 1119 }
1458 1120 }