| 1 |
<?php |
| 2 |
/** |
| 3 |
* UamUserGroup.php |
| 4 |
* |
| 5 |
* The UamUserGroup class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @category UserAccessManager |
| 10 |
* @package UserAccessManager |
| 11 |
* @author Alexander Schneider <alexanderschneider85@googlemail.com> |
| 12 |
* @copyright 2008-2016 Alexander Schneider |
| 13 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 14 |
* @version SVN: $Id$ |
| 15 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* The user group class. |
| 20 |
* |
| 21 |
* @category UserAccessManager |
| 22 |
* @package UserAccessManager |
| 23 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 24 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 25 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 26 |
*/ |
| 27 |
|
| 28 |
class UamUserGroup |
| 29 |
{ |
| 30 |
const OBJECTS_FULL = 'full'; |
| 31 |
const OBJECTS_REAL = 'real'; |
| 32 |
|
| 33 |
protected $_oAccessHandler = null; |
| 34 |
protected $_iId = null; |
| 35 |
protected $_sGroupName = null; |
| 36 |
protected $_sGroupDesc = null; |
| 37 |
protected $_sReadAccess = null; |
| 38 |
protected $_sWriteAccess = null; |
| 39 |
protected $_sIpRange = null; |
| 40 |
protected $_aRoles = array(); |
| 41 |
protected $_aObjects = null; |
| 42 |
protected $_aSingleObjects = null; |
| 43 |
protected $_aAssignedObjects = null; |
| 44 |
protected $_aPlObjects = array(); |
| 45 |
protected $_aSetRecursive = array(); |
| 46 |
protected $_aValidObjectTypes = array(); |
| 47 |
protected $_aObjectsInTerm = null; |
| 48 |
protected $_aObjectIsMember = array(); |
| 49 |
|
| 50 |
/** |
| 51 |
* Constructor |
| 52 |
* |
| 53 |
* @param UamAccessHandler $oUamAccessHandler The access handler object. |
| 54 |
* @param integer $iId The id of the user group. |
| 55 |
*/ |
| 56 |
public function __construct(UamAccessHandler &$oUamAccessHandler, $iId = null) |
| 57 |
{ |
| 58 |
$this->_oAccessHandler = $oUamAccessHandler; |
| 59 |
|
| 60 |
if ($iId !== null) { |
| 61 |
$oDatabase = $this->_oAccessHandler->getUserAccessManager()->getDatabase(); |
| 62 |
$this->_iId = $iId; |
| 63 |
|
| 64 |
$aDbUserGroup = $oDatabase->get_row( |
| 65 |
"SELECT * |
| 66 |
FROM ".DB_ACCESSGROUP." |
| 67 |
WHERE ID = {$this->getId()} |
| 68 |
LIMIT 1", |
| 69 |
ARRAY_A |
| 70 |
); |
| 71 |
|
| 72 |
$this->_sGroupName = $aDbUserGroup['groupname']; |
| 73 |
$this->_sGroupDesc = $aDbUserGroup['groupdesc']; |
| 74 |
$this->_sReadAccess = $aDbUserGroup['read_access']; |
| 75 |
$this->_sWriteAccess = $aDbUserGroup['write_access']; |
| 76 |
$this->_sIpRange = $aDbUserGroup['ip_range']; |
| 77 |
} |
| 78 |
|
| 79 |
//Create default values for the objects. |
| 80 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 81 |
$this->_aAssignedObjects[$sObjectType] = null; |
| 82 |
$this->_aObjects[$sObjectType] = array( |
| 83 |
self::OBJECTS_REAL => -1, |
| 84 |
self::OBJECTS_FULL => -1 |
| 85 |
); |
| 86 |
|
| 87 |
if ($iId !== null) { |
| 88 |
$this->getObjectsFromType($sObjectType); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the user access handler object. |
| 95 |
* |
| 96 |
* @return UamAccessHandler |
| 97 |
*/ |
| 98 |
public function &getAccessHandler() |
| 99 |
{ |
| 100 |
return $this->_oAccessHandler; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Deletes the user group. |
| 105 |
* |
| 106 |
* @return boolean |
| 107 |
*/ |
| 108 |
public function delete() |
| 109 |
{ |
| 110 |
if ($this->_iId == null) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 115 |
|
| 116 |
$oDatabase->query( |
| 117 |
"DELETE FROM " . DB_ACCESSGROUP . " |
| 118 |
WHERE ID = {$this->_iId} LIMIT 1" |
| 119 |
); |
| 120 |
|
| 121 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 122 |
$this->_deleteObjectsFromDb($sObjectType); |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Saves the user group. |
| 130 |
* |
| 131 |
* @param boolean $blRemoveOldAssignments If false we will not remove old entries. |
| 132 |
*/ |
| 133 |
public function save($blRemoveOldAssignments = true) |
| 134 |
{ |
| 135 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 136 |
|
| 137 |
if ($this->_iId == null) { |
| 138 |
$sInsertQuery = " |
| 139 |
INSERT INTO " . DB_ACCESSGROUP . " ( |
| 140 |
ID, |
| 141 |
groupname, |
| 142 |
groupdesc, |
| 143 |
read_access, |
| 144 |
write_access, |
| 145 |
ip_range |
| 146 |
) |
| 147 |
VALUES ( |
| 148 |
NULL, |
| 149 |
'{$this->_sGroupName}', |
| 150 |
'{$this->_sGroupDesc}', |
| 151 |
'{$this->_sReadAccess}', |
| 152 |
'{$this->_sWriteAccess}', |
| 153 |
'{$this->_sIpRange}' |
| 154 |
)"; |
| 155 |
|
| 156 |
$oDatabase->query($sInsertQuery); |
| 157 |
|
| 158 |
$this->_iId = $oDatabase->insert_id; |
| 159 |
} else { |
| 160 |
$sAccessGroupTable = DB_ACCESSGROUP; |
| 161 |
$sUpdateQuery = " |
| 162 |
UPDATE {$sAccessGroupTable} |
| 163 |
SET groupname = '{$this->_sGroupName}', |
| 164 |
groupdesc = '{$this->_sGroupDesc}', |
| 165 |
read_access = '{$this->_sReadAccess}', |
| 166 |
write_access = '{$this->_sWriteAccess}', |
| 167 |
ip_range = '{$this->_sIpRange}' |
| 168 |
WHERE ID = {$this->_iId}"; |
| 169 |
|
| 170 |
$oDatabase->query($sUpdateQuery); |
| 171 |
|
| 172 |
if ($blRemoveOldAssignments === true) { |
| 173 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 174 |
//Delete form database |
| 175 |
$this->_deleteObjectsFromDb($sObjectType); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 181 |
$aKeys = $this->_getAssignedObjects($sObjectType); |
| 182 |
|
| 183 |
if (count($aKeys) > 0) { |
| 184 |
$sSql = $this->_getSqlQuery($sObjectType, 'insert', $aKeys); |
| 185 |
$oDatabase->query($sSql); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/* |
| 192 |
* Primary values. |
| 193 |
*/ |
| 194 |
|
| 195 |
/** |
| 196 |
* Returns the group _iId. |
| 197 |
* |
| 198 |
* @return integer |
| 199 |
*/ |
| 200 |
public function getId() |
| 201 |
{ |
| 202 |
return $this->_iId; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns the group name. |
| 207 |
* |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
public function getGroupName() |
| 211 |
{ |
| 212 |
return $this->_sGroupName; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Sets the group name. |
| 217 |
* |
| 218 |
* @param string $sGroupName The new group name. |
| 219 |
*/ |
| 220 |
public function setGroupName($sGroupName) |
| 221 |
{ |
| 222 |
$this->_sGroupName = $sGroupName; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Returns the group description. |
| 227 |
* |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
public function getGroupDesc() |
| 231 |
{ |
| 232 |
return $this->_sGroupDesc; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Sets the group description. |
| 237 |
* |
| 238 |
* @param string $sGroupDesc The new group description. |
| 239 |
*/ |
| 240 |
public function setGroupDesc($sGroupDesc) |
| 241 |
{ |
| 242 |
$this->_sGroupDesc = $sGroupDesc; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Returns the read access. |
| 247 |
* |
| 248 |
* @return string |
| 249 |
*/ |
| 250 |
public function getReadAccess() |
| 251 |
{ |
| 252 |
return $this->_sReadAccess; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Sets the read access. |
| 257 |
* |
| 258 |
* @param string $sReadAccess The read access. |
| 259 |
*/ |
| 260 |
public function setReadAccess($sReadAccess) |
| 261 |
{ |
| 262 |
$this->_sReadAccess = $sReadAccess; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Returns the write access. |
| 267 |
* |
| 268 |
* @return string |
| 269 |
*/ |
| 270 |
public function getWriteAccess() |
| 271 |
{ |
| 272 |
return $this->_sWriteAccess; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Sets the write access. |
| 277 |
* |
| 278 |
* @param string $sWriteAccess The write access. |
| 279 |
*/ |
| 280 |
public function setWriteAccess($sWriteAccess) |
| 281 |
{ |
| 282 |
$this->_sWriteAccess = $sWriteAccess; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Returns the ip range. |
| 287 |
* |
| 288 |
* @param string $sType The return type. |
| 289 |
* |
| 290 |
* @return array|string |
| 291 |
*/ |
| 292 |
public function getIpRange($sType = null) |
| 293 |
{ |
| 294 |
if ($sType == 'string') { |
| 295 |
return $this->_sIpRange; |
| 296 |
} |
| 297 |
|
| 298 |
$aIpRange = explode(';', $this->_sIpRange); |
| 299 |
|
| 300 |
if ($aIpRange[0] == null) { |
| 301 |
return null; |
| 302 |
} |
| 303 |
|
| 304 |
return $aIpRange; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Sets the ip range. |
| 309 |
* |
| 310 |
* @param string|array $mIpRange The new ip range. |
| 311 |
*/ |
| 312 |
public function setIpRange($mIpRange) |
| 313 |
{ |
| 314 |
if (is_array($mIpRange)) { |
| 315 |
$mIpRange = implode(';', $mIpRange); |
| 316 |
} |
| 317 |
|
| 318 |
$this->_sIpRange = $mIpRange; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Returns all _aObjects types. |
| 323 |
* |
| 324 |
* @return array |
| 325 |
*/ |
| 326 |
public function getAllObjectTypes() |
| 327 |
{ |
| 328 |
return $this->getAccessHandler()->getAllObjectTypes(); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Checks if the object type is a valid one. |
| 333 |
* |
| 334 |
* @param string $sObjectType The object type to check. |
| 335 |
* |
| 336 |
* @return boolean |
| 337 |
*/ |
| 338 |
public function isValidObjectType($sObjectType) |
| 339 |
{ |
| 340 |
return $this->getAccessHandler()->isValidObjectType($sObjectType); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Sets the recursive object membership. |
| 345 |
* |
| 346 |
* @param string $sObjectType The object type. |
| 347 |
* @param integer $iObjectId The object id. |
| 348 |
* @param array $aObjectMembership The membership. |
| 349 |
*/ |
| 350 |
public function setRecursiveMembership($sObjectType, $iObjectId, $aObjectMembership) |
| 351 |
{ |
| 352 |
if (!isset($this->_aSetRecursive[$sObjectType])) { |
| 353 |
$this->_aSetRecursive[$sObjectType] = array(); |
| 354 |
} |
| 355 |
|
| 356 |
$this->_aSetRecursive[$sObjectType][$iObjectId] = $aObjectMembership; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Returns the recursive membership. |
| 361 |
* |
| 362 |
* @param string $sObjectType The object type. |
| 363 |
* @param integer $iObjectId The object id. |
| 364 |
* @param string $sCurObjectType The current object type. |
| 365 |
* |
| 366 |
* @return array |
| 367 |
*/ |
| 368 |
public function getRecursiveMembershipForObjectType($sObjectType, $iObjectId, $sCurObjectType) |
| 369 |
{ |
| 370 |
if (isset($this->_aSetRecursive[$sObjectType]) |
| 371 |
&& isset($this->_aSetRecursive[$sObjectType][$iObjectId]) |
| 372 |
&& isset($this->_aSetRecursive[$sObjectType][$iObjectId][$sCurObjectType]) |
| 373 |
) { |
| 374 |
return $this->_aSetRecursive[$sObjectType][$iObjectId][$sCurObjectType]; |
| 375 |
} |
| 376 |
|
| 377 |
return array(); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Returns true if the requested object is locked recursive. |
| 382 |
* |
| 383 |
* @param string $sObjectType The object type. |
| 384 |
* @param integer $iObjectId The object id. |
| 385 |
* |
| 386 |
* @return boolean |
| 387 |
*/ |
| 388 |
public function isLockedRecursive($sObjectType, $iObjectId) |
| 389 |
{ |
| 390 |
if (isset($this->_aSetRecursive[$sObjectType]) && isset($this->_aSetRecursive[$sObjectType][$iObjectId])) { |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
/* |
| 399 |
* Meta functions. |
| 400 |
*/ |
| 401 |
|
| 402 |
/** |
| 403 |
* Magic method getter. |
| 404 |
* |
| 405 |
* @param string $sName The name of the function |
| 406 |
* @param array $aArguments The arguments for the function |
| 407 |
* |
| 408 |
* @return mixed |
| 409 |
*/ |
| 410 |
public function __call($sName, $aArguments) |
| 411 |
{ |
| 412 |
$oUam = $this->getAccessHandler()->getUserAccessManager(); |
| 413 |
|
| 414 |
$sPrefix = ''; |
| 415 |
|
| 416 |
if ($oUam->startsWith($sName, 'add')) { |
| 417 |
$sPrefix = 'add'; |
| 418 |
} elseif ($oUam->startsWith($sName, 'remove')) { |
| 419 |
$sPrefix = 'remove'; |
| 420 |
} elseif ($oUam->startsWith($sName, 'isMember')) { |
| 421 |
$sPrefix = 'isMember'; |
| 422 |
} |
| 423 |
|
| 424 |
$sObjectType = str_replace($sPrefix, '', $sName); |
| 425 |
$sObjectType = strtolower($sObjectType); |
| 426 |
|
| 427 |
$iObjectId = $aArguments[0]; |
| 428 |
|
| 429 |
if ($sPrefix == 'add') { |
| 430 |
$this->addObject( |
| 431 |
$sObjectType, |
| 432 |
$iObjectId |
| 433 |
); |
| 434 |
} elseif ($sPrefix == 'remove') { |
| 435 |
$this->removeObject( |
| 436 |
$sObjectType, |
| 437 |
$iObjectId |
| 438 |
); |
| 439 |
} elseif ($sPrefix == 'ismember') { |
| 440 |
$blWithInfo = $aArguments[1]; |
| 441 |
|
| 442 |
return $this->objectIsMember( |
| 443 |
$sObjectType, |
| 444 |
$iObjectId, |
| 445 |
$blWithInfo |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
return null; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Returns the sql query. |
| 454 |
* |
| 455 |
* @param string $sObjectType The object type. |
| 456 |
* @param string $sAction The sql action. |
| 457 |
* @param array $aKeys The keys for the insert query. |
| 458 |
* |
| 459 |
* @return string |
| 460 |
*/ |
| 461 |
protected function _getSqlQuery($sObjectType, $sAction, $aKeys = array()) |
| 462 |
{ |
| 463 |
$sSql = ''; |
| 464 |
$sAccessGroupToObjectTable = DB_ACCESSGROUP_TO_OBJECT; |
| 465 |
|
| 466 |
if ($sAction == 'select') { |
| 467 |
$sSql = " |
| 468 |
SELECT object_id as id |
| 469 |
FROM {$sAccessGroupToObjectTable} |
| 470 |
WHERE group_id = {$this->getId()} |
| 471 |
AND object_type = '{$sObjectType}'"; |
| 472 |
} elseif ($sAction == 'delete') { |
| 473 |
$sSql = " |
| 474 |
DELETE FROM {$sAccessGroupToObjectTable} |
| 475 |
WHERE group_id = {$this->getId()} |
| 476 |
AND object_type = '{$sObjectType}'"; |
| 477 |
} elseif ($sAction == 'insert') { |
| 478 |
$sSql = " |
| 479 |
INSERT INTO {$sAccessGroupToObjectTable} ( |
| 480 |
group_id, |
| 481 |
object_id, |
| 482 |
object_type |
| 483 |
) VALUES "; |
| 484 |
|
| 485 |
foreach ($aKeys as $sKey) { |
| 486 |
$sKey = trim($sKey); |
| 487 |
$sSql .= "('{$this->getId()}', '{$sKey}', '{$sObjectType}'), "; |
| 488 |
} |
| 489 |
|
| 490 |
$sSql = rtrim($sSql, ', '); |
| 491 |
$sSql .= " ON DUPLICATE KEY UPDATE group_id = group_id "; |
| 492 |
} |
| 493 |
|
| 494 |
return $sSql; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Adds a object of the given type. |
| 499 |
* |
| 500 |
* @param string $sObjectType The object type. |
| 501 |
* @param integer $iObjectId The object id. |
| 502 |
*/ |
| 503 |
public function addObject($sObjectType, $iObjectId) |
| 504 |
{ |
| 505 |
if (!$this->isValidObjectType($sObjectType)) { |
| 506 |
return; |
| 507 |
} |
| 508 |
|
| 509 |
$this->getAccessHandler()->unsetUserGroupsForObject(); |
| 510 |
$this->getObjectsFromType($sObjectType); |
| 511 |
|
| 512 |
$oObject = new stdClass(); |
| 513 |
$oObject->iId = $iObjectId; |
| 514 |
|
| 515 |
$this->_aObjects[$sObjectType][self::OBJECTS_REAL][$iObjectId] = $oObject; |
| 516 |
$this->_aObjects[$sObjectType][self::OBJECTS_FULL] = -1; |
| 517 |
|
| 518 |
$this->_aAssignedObjects[$sObjectType][$iObjectId] = $iObjectId; |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Removes a object of the given type. |
| 523 |
* |
| 524 |
* @param string $sObjectType The object type. |
| 525 |
* @param integer $sObjectId The object id. |
| 526 |
*/ |
| 527 |
public function removeObject($sObjectType, $sObjectId) |
| 528 |
{ |
| 529 |
if (!$this->isValidObjectType($sObjectType)) { |
| 530 |
return; |
| 531 |
} |
| 532 |
|
| 533 |
$this->getAccessHandler()->unsetUserGroupsForObject(); |
| 534 |
$this->getObjectsFromType($sObjectType); |
| 535 |
|
| 536 |
unset($this->_aObjects[$sObjectType][self::OBJECTS_REAL][$sObjectId]); |
| 537 |
$this->_aObjects[$sObjectType][self::OBJECTS_FULL] = -1; |
| 538 |
|
| 539 |
unset($this->_aSingleObjects[$sObjectType][$sObjectId]); |
| 540 |
unset($this->_aAssignedObjects[$sObjectType][$sObjectId]); |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Returns the assigned _aObjects. |
| 545 |
* |
| 546 |
* @param string $sObjectType The object type. |
| 547 |
* |
| 548 |
* @return array |
| 549 |
*/ |
| 550 |
protected function _getAssignedObjects($sObjectType) |
| 551 |
{ |
| 552 |
if ($this->_aAssignedObjects[$sObjectType] !== null) { |
| 553 |
return $this->_aAssignedObjects[$sObjectType]; |
| 554 |
} |
| 555 |
|
| 556 |
$sCacheKey = '_getAssignedObjects|'.$sObjectType.'|'.$this->getId(); |
| 557 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 558 |
$aAssignedObjects = $oUserAccessManager->getFromCache($sCacheKey); |
| 559 |
|
| 560 |
if ($aAssignedObjects !== null) { |
| 561 |
$this->_aAssignedObjects[$sObjectType] = $aAssignedObjects; |
| 562 |
} else { |
| 563 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 564 |
|
| 565 |
$aDbObjects = $oDatabase->get_results( |
| 566 |
$this->_getSqlQuery($sObjectType, 'select') |
| 567 |
); |
| 568 |
|
| 569 |
$this->_aAssignedObjects[$sObjectType] = array(); |
| 570 |
|
| 571 |
foreach ($aDbObjects as $oDbObject) { |
| 572 |
$this->_aAssignedObjects[$sObjectType][$oDbObject->id] = $oDbObject->id; |
| 573 |
} |
| 574 |
|
| 575 |
$oUserAccessManager->addToCache($sCacheKey, $this->_aAssignedObjects[$sObjectType]); |
| 576 |
} |
| 577 |
|
| 578 |
return $this->_aAssignedObjects[$sObjectType]; |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Checks if the object is assigned to the group. |
| 583 |
* |
| 584 |
* @param string $sObjectType The object type. |
| 585 |
* @param integer $iObjectId The object id. |
| 586 |
* |
| 587 |
* @return boolean |
| 588 |
*/ |
| 589 |
protected function _isObjectAssignedToGroup($sObjectType, $iObjectId) |
| 590 |
{ |
| 591 |
$aAssignedObjects = $this->_getAssignedObjects($sObjectType); |
| 592 |
return isset($aAssignedObjects[$iObjectId]); |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* Unset the objects. |
| 597 |
* |
| 598 |
* @param string $sObjectType The object type. |
| 599 |
* @param boolean $blPlusRemove If true also database entries will remove. |
| 600 |
*/ |
| 601 |
public function unsetObjects($sObjectType, $blPlusRemove = false) |
| 602 |
{ |
| 603 |
if (!$this->isValidObjectType($sObjectType)) { |
| 604 |
return; |
| 605 |
} |
| 606 |
|
| 607 |
if ($blPlusRemove) { |
| 608 |
$this->_deleteObjectsFromDb($sObjectType); |
| 609 |
} |
| 610 |
|
| 611 |
$this->_aAssignedObjects[$sObjectType] = array(); |
| 612 |
$this->getAccessHandler()->getUserAccessManager()->flushCache(); |
| 613 |
|
| 614 |
$this->_aObjects[$sObjectType] = array( |
| 615 |
self::OBJECTS_REAL => array(), |
| 616 |
self::OBJECTS_FULL => array(), |
| 617 |
); |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Removes all _aObjects from the user group. |
| 622 |
* |
| 623 |
* @param string $sObjectType The object type. |
| 624 |
*/ |
| 625 |
protected function _deleteObjectsFromDb($sObjectType) |
| 626 |
{ |
| 627 |
if (isset($this->_iId)) { |
| 628 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 629 |
|
| 630 |
$sQuery = $this->_getSqlQuery($sObjectType, 'delete'); |
| 631 |
$oDatabase->query($sQuery); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* Checks if the given object is a member of the group. |
| 637 |
* |
| 638 |
* @param string $sObjectType The object type. |
| 639 |
* @param integer $iObjectId The _iId of the object which should be checked. |
| 640 |
* @param boolean $blWithInfo If true then we return additional info. |
| 641 |
* |
| 642 |
* @return boolean|array |
| 643 |
*/ |
| 644 |
public function objectIsMember($sObjectType, $iObjectId, $blWithInfo = false) |
| 645 |
{ |
| 646 |
$sCacheKey = $sObjectType.'|'.$iObjectId; |
| 647 |
$sCacheKey .= ($blWithInfo) ? '|wi' : '|ni'; |
| 648 |
|
| 649 |
if (!isset($this->_aObjectIsMember[$sCacheKey])) { |
| 650 |
$this->_aObjectIsMember[$sCacheKey] = false; |
| 651 |
|
| 652 |
if ($this->isValidObjectType($sObjectType)) { |
| 653 |
$oObject = $this->_getSingleObject($sObjectType, $iObjectId, self::OBJECTS_FULL); |
| 654 |
|
| 655 |
if ($oObject !== null) { |
| 656 |
if ($blWithInfo && isset($oObject->recursiveMember)) { |
| 657 |
$this->_aObjectIsMember[$sCacheKey] = $oObject->recursiveMember; |
| 658 |
} else { |
| 659 |
$this->_aObjectIsMember[$sCacheKey] = true; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
return $this->_aObjectIsMember[$sCacheKey]; |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Returns all objects of the given type. |
| 670 |
* |
| 671 |
* @param string $sObjectType The object type. |
| 672 |
* @param string $sType The return type, could be real or full. |
| 673 |
* |
| 674 |
* @return array |
| 675 |
*/ |
| 676 |
public function getObjectsFromType($sObjectType, $sType = self::OBJECTS_REAL) |
| 677 |
{ |
| 678 |
if (!$this->isValidObjectType($sObjectType) |
| 679 |
|| $this->_iId == null |
| 680 |
|| $sType != self::OBJECTS_REAL && $sType != self::OBJECTS_FULL |
| 681 |
) { |
| 682 |
return array(); |
| 683 |
} |
| 684 |
|
| 685 |
if (isset($this->_aObjects[$sObjectType]) |
| 686 |
&& isset($this->_aObjects[$sObjectType][$sType]) |
| 687 |
&& $this->_aObjects[$sObjectType][$sType] != -1 |
| 688 |
) { |
| 689 |
return $this->_aObjects[$sObjectType][$sType]; |
| 690 |
} else { |
| 691 |
$this->_aObjects[$sObjectType][$sType] = array(); |
| 692 |
} |
| 693 |
|
| 694 |
$aObjectIds = $this->_getAssignedObjects($sObjectType); |
| 695 |
|
| 696 |
foreach ($aObjectIds as $sObjectId) { |
| 697 |
$oObject = $this->_getSingleObject($sObjectType, $sObjectId, $sType); |
| 698 |
|
| 699 |
if ($oObject !== null) { |
| 700 |
$this->_aObjects[$sObjectType][$sType][$oObject->id] = $oObject; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
if ($sType == self::OBJECTS_FULL |
| 705 |
&& $this->getAccessHandler()->isPostableType($sObjectType) |
| 706 |
&& $sObjectType != UserAccessManager::ROLE_OBJECT_TYPE |
| 707 |
) { |
| 708 |
if ($sObjectType === UserAccessManager::TERM_OBJECT_TYPE) { |
| 709 |
$this->_aObjects[$sObjectType][$sType] = $this->getFullTerms($this->_aObjects[$sObjectType][$sType]); |
| 710 |
} elseif ($sObjectType === UserAccessManager::USER_OBJECT_TYPE) { |
| 711 |
$this->_aObjects[$sObjectType][$sType] = $this->getFullUsers(); |
| 712 |
} else { |
| 713 |
$oPlObject = $this->getAccessHandler()->getPlObject($sObjectType); |
| 714 |
$this->_aObjects[$sObjectType][$sType] = $oPlObject['reference']->{$oPlObject['getFullObjects']}( |
| 715 |
$this->_aObjects[$sObjectType][$sType], |
| 716 |
$this |
| 717 |
); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
return $this->_aObjects[$sObjectType][$sType]; |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Returns a single object. |
| 726 |
* |
| 727 |
* @param string $sObjectType The object type. |
| 728 |
* @param integer $iObjectId The _iId of the object which should be checked. |
| 729 |
* @param string $sType The return type. Can be real or full. |
| 730 |
* |
| 731 |
* @return object |
| 732 |
*/ |
| 733 |
protected function _getSingleObject($sObjectType, $iObjectId, $sType) |
| 734 |
{ |
| 735 |
if (!isset($this->_aSingleObjects[$sObjectType]) |
| 736 |
|| !isset($this->_aSingleObjects[$sObjectType][$iObjectId]) |
| 737 |
) { |
| 738 |
$this->_aSingleObjects[$sObjectType][$iObjectId] = null; |
| 739 |
$aIsRecursiveMember = array(); |
| 740 |
|
| 741 |
if ($sType == self::OBJECTS_FULL && $sObjectType != UserAccessManager::ROLE_OBJECT_TYPE) { |
| 742 |
if ($sObjectType === UserAccessManager::TERM_OBJECT_TYPE) { |
| 743 |
$aIsRecursiveMember = $this->_getFullTerm($iObjectId); |
| 744 |
} elseif ($sObjectType == UserAccessManager::USER_OBJECT_TYPE) { |
| 745 |
$aIsRecursiveMember = $this->_getFullUser($iObjectId); |
| 746 |
} elseif ($this->getAccessHandler()->isPostableType($sObjectType)) { |
| 747 |
$aIsRecursiveMember = $this->_getFullPost($sObjectType, $iObjectId); |
| 748 |
} else { |
| 749 |
$aIsRecursiveMember = $this->_getFullPlObject($sObjectType, $iObjectId); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
if ($aIsRecursiveMember != array() |
| 754 |
|| $this->_isObjectAssignedToGroup($sObjectType, $iObjectId) |
| 755 |
) { |
| 756 |
$oObject = new stdClass(); |
| 757 |
$oObject->id = $iObjectId; |
| 758 |
|
| 759 |
if ($aIsRecursiveMember != array()) { |
| 760 |
$oObject->recursiveMember = $aIsRecursiveMember; |
| 761 |
} |
| 762 |
|
| 763 |
$this->_aSingleObjects[$sObjectType][$iObjectId] = $oObject; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
return $this->_aSingleObjects[$sObjectType][$iObjectId]; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
/* |
| 772 |
* Group users functions. |
| 773 |
*/ |
| 774 |
|
| 775 |
/** |
| 776 |
* Returns a single user. |
| 777 |
* |
| 778 |
* @param integer $iObjectId The object id. |
| 779 |
* |
| 780 |
* @return array |
| 781 |
*/ |
| 782 |
protected function _getFullUser($iObjectId) |
| 783 |
{ |
| 784 |
$aIsRecursiveMember = array(); |
| 785 |
|
| 786 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 787 |
$oCurUserData = $this->getAccessHandler()->getUserAccessManager()->getUser($iObjectId); |
| 788 |
|
| 789 |
if (isset($oCurUserData->{$oDatabase->prefix . "capabilities"})) { |
| 790 |
$aCapabilities = $oCurUserData->{$oDatabase->prefix . "capabilities"}; |
| 791 |
} else { |
| 792 |
$aCapabilities = array(); |
| 793 |
} |
| 794 |
|
| 795 |
$aRoles = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole'); |
| 796 |
$aObjects = $this->getObjectsFromType('role'); |
| 797 |
|
| 798 |
foreach ($aRoles as $sRole) { |
| 799 |
if (isset($aObjects[$sRole])) { |
| 800 |
$oRoleObject = new stdClass(); |
| 801 |
$oRoleObject->name = $sRole; |
| 802 |
|
| 803 |
$aIsRecursiveMember = array(UserAccessManager::ROLE_OBJECT_TYPE => array()); |
| 804 |
$aIsRecursiveMember[UserAccessManager::ROLE_OBJECT_TYPE][] = $oRoleObject; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
return $aIsRecursiveMember; |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* Returns the users in the group |
| 813 |
* |
| 814 |
* @return array |
| 815 |
*/ |
| 816 |
public function getFullUsers() |
| 817 |
{ |
| 818 |
$sCacheKey = 'getFullUsers'; |
| 819 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 820 |
$aFullUsers = $oUserAccessManager->getFromCache($sCacheKey); |
| 821 |
|
| 822 |
if ($aFullUsers === null) { |
| 823 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 824 |
|
| 825 |
$aDbUsers = $oDatabase->get_results( |
| 826 |
"SELECT ID, user_nicename |
| 827 |
FROM ".$oDatabase->users |
| 828 |
); |
| 829 |
|
| 830 |
$aFullUsers = array(); |
| 831 |
|
| 832 |
if (isset($aDbUsers)) { |
| 833 |
foreach ($aDbUsers as $oDbUser) { |
| 834 |
$oUser = $this->_getSingleObject(UserAccessManager::USER_OBJECT_TYPE, $oDbUser->ID, self::OBJECTS_FULL); |
| 835 |
|
| 836 |
if ($oUser !== null) { |
| 837 |
$aFullUsers[$oUser->id] = $oUser; |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
$oUserAccessManager->addToCache($sCacheKey, $aFullUsers); |
| 843 |
} |
| 844 |
|
| 845 |
return $aFullUsers; |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
/* |
| 850 |
* Group categories functions. |
| 851 |
*/ |
| 852 |
|
| 853 |
/** |
| 854 |
* Returns a single category. |
| 855 |
* |
| 856 |
* @param integer $iObjectId The object id. |
| 857 |
* |
| 858 |
* @return object[] |
| 859 |
*/ |
| 860 |
protected function _getFullTerm($iObjectId) |
| 861 |
{ |
| 862 |
$oTerm = $this->getAccessHandler()->getUserAccessManager()->getTerm($iObjectId); |
| 863 |
|
| 864 |
$aIsRecursiveMember = array(); |
| 865 |
|
| 866 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 867 |
$oConfig = $oUserAccessManager->getConfig(); |
| 868 |
|
| 869 |
if ($oConfig->lockRecursive() === true |
| 870 |
&& isset($oTerm->parent) |
| 871 |
&& !is_null($oTerm->parent) |
| 872 |
) { |
| 873 |
$oParentTerm = $this->_getSingleObject( |
| 874 |
UserAccessManager::TERM_OBJECT_TYPE, |
| 875 |
$oTerm->parent, |
| 876 |
self::OBJECTS_FULL |
| 877 |
); |
| 878 |
|
| 879 |
if ($oParentTerm !== null) { |
| 880 |
$oCurrentTerm = $this->getAccessHandler()->getUserAccessManager()->getTerm($iObjectId); |
| 881 |
$oParentTerm->name = $oCurrentTerm->name; |
| 882 |
$aIsRecursiveMember[UserAccessManager::TERM_OBJECT_TYPE][] = $oParentTerm; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
return $aIsRecursiveMember; |
| 887 |
} |
| 888 |
|
| 889 |
/** |
| 890 |
* Returns the terms in the group |
| 891 |
* |
| 892 |
* @param array $aTerms The real terms. |
| 893 |
* |
| 894 |
* @return array |
| 895 |
*/ |
| 896 |
public function getFullTerms($aTerms) |
| 897 |
{ |
| 898 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 899 |
$oConfig = $oUserAccessManager->getConfig(); |
| 900 |
|
| 901 |
foreach ($aTerms as $oTerm) { |
| 902 |
if ($oTerm !== null) { |
| 903 |
if ($oConfig->lockRecursive() === true) { |
| 904 |
$iPriority = has_filter('get_terms', array($this, 'showTerms')); |
| 905 |
|
| 906 |
//We have to remove the filter to get all categories |
| 907 |
$blRemoveSuccess = remove_filter('get_terms', array($oUserAccessManager, 'showTerms'), $iPriority); |
| 908 |
|
| 909 |
if ($blRemoveSuccess) { |
| 910 |
$aArgs = array( |
| 911 |
'child_of' => $oTerm->id, |
| 912 |
'hide_empty' => false |
| 913 |
); |
| 914 |
|
| 915 |
$aTermChildren = get_terms($aArgs); |
| 916 |
add_filter('get_terms', array($oUserAccessManager, 'showTerms'), $iPriority, 2); |
| 917 |
|
| 918 |
foreach ($aTermChildren as $oTermChild) { |
| 919 |
$oCurrentTermChild = new stdClass(); |
| 920 |
$oCurrentTermChild->id = $oTermChild->term_id; |
| 921 |
$oCurrentTermChild->name = $oTermChild->name; |
| 922 |
|
| 923 |
$oCurrentTermChild->recursiveMember = array(UserAccessManager::TERM_OBJECT_TYPE => array()); |
| 924 |
$oCurrentTermChild->recursiveMember[UserAccessManager::TERM_OBJECT_TYPE][] = $oTermChild; |
| 925 |
$aTerms[$oCurrentTermChild->id] = $oCurrentTermChild; |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
$aTerms[$oTerm->id] = $oTerm; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
return $aTerms; |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
/* |
| 939 |
* Group posts functions. |
| 940 |
*/ |
| 941 |
|
| 942 |
/** |
| 943 |
* Checks if the give post in the give term. |
| 944 |
* |
| 945 |
* @param integer $iPostId The post id. |
| 946 |
* @param integer $iTermId The term id. |
| 947 |
* |
| 948 |
* @return boolean |
| 949 |
*/ |
| 950 |
protected function _isPostInTerm($iPostId, $iTermId) |
| 951 |
{ |
| 952 |
if ($this->_aObjectsInTerm === null) { |
| 953 |
$this->_aObjectsInTerm = array(); |
| 954 |
$sCacheKey = '_isPostInTerm'; |
| 955 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 956 |
$aObjectsInCategory = $oUserAccessManager->getFromCache($sCacheKey); |
| 957 |
|
| 958 |
if ($aObjectsInCategory === null) { |
| 959 |
$oDatabase = $this->getAccessHandler()->getUserAccessManager()->getDatabase(); |
| 960 |
|
| 961 |
$aDbObjects = $oDatabase->get_results( |
| 962 |
"SELECT tr.object_id AS objectId, tr.term_taxonomy_id AS termId |
| 963 |
FROM ".$oDatabase->term_relationships." AS tr" |
| 964 |
); |
| 965 |
|
| 966 |
foreach ($aDbObjects as $oDbObject) { |
| 967 |
if (!isset($this->_aObjectsInTerm[$oDbObject->objectId])) { |
| 968 |
$this->_aObjectsInTerm[$oDbObject->objectId] = array(); |
| 969 |
} |
| 970 |
|
| 971 |
$this->_aObjectsInTerm[$oDbObject->objectId][$oDbObject->termId] = $oDbObject->termId; |
| 972 |
} |
| 973 |
|
| 974 |
$oUserAccessManager->addToCache($sCacheKey, $this->_aObjectsInTerm); |
| 975 |
} else { |
| 976 |
$this->_aObjectsInTerm = $aObjectsInCategory; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
return (isset($this->_aObjectsInTerm[$iPostId]) |
| 981 |
&& isset($this->_aObjectsInTerm[$iPostId][$iTermId])); |
| 982 |
} |
| 983 |
|
| 984 |
/** |
| 985 |
* Returns the membership of a single post. |
| 986 |
* |
| 987 |
* @param string $sPostType The post type needed for the intern representation. |
| 988 |
* @param integer $iObjectId The object id. |
| 989 |
* |
| 990 |
* @return array |
| 991 |
*/ |
| 992 |
protected function _getFullPost($sPostType, $iObjectId) |
| 993 |
{ |
| 994 |
$aIsRecursiveMember = array(); |
| 995 |
$oPost = $this->getAccessHandler()->getUserAccessManager()->getPost($iObjectId); |
| 996 |
$oConfig = $this->getAccessHandler()->getUserAccessManager()->getConfig(); |
| 997 |
$aTerms = $this->getObjectsFromType(UserAccessManager::TERM_OBJECT_TYPE, self::OBJECTS_FULL); |
| 998 |
|
| 999 |
foreach ($aTerms as $oTerm) { |
| 1000 |
if ($this->_isPostInTerm($oPost->ID, $oTerm->id)) { |
| 1001 |
$oTermObject = $this->getAccessHandler()->getUserAccessManager()->getTerm($oTerm->id); |
| 1002 |
|
| 1003 |
if ($oTermObject !== null) { |
| 1004 |
$oTerm->name = $oTermObject->name; |
| 1005 |
} |
| 1006 |
|
| 1007 |
$aIsRecursiveMember[UserAccessManager::TERM_OBJECT_TYPE][] = $oTermObject; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
if ($oPost->post_parent == 0 |
| 1012 |
&& $oPost->post_type === UserAccessManager::POST_OBJECT_TYPE |
| 1013 |
&& $oConfig->getWpOption('show_on_front') == UserAccessManager::PAGE_OBJECT_TYPE |
| 1014 |
&& $oConfig->getWpOption('page_for_posts') != $iObjectId |
| 1015 |
) { |
| 1016 |
$iParentId = $oConfig->getWpOption('page_for_posts'); |
| 1017 |
} else { |
| 1018 |
$iParentId = $oPost->post_parent; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if ($iParentId != 0 |
| 1022 |
&& $oConfig->lockRecursive() === true |
| 1023 |
) { |
| 1024 |
$oParent = $this->getAccessHandler()->getUserAccessManager()->getPost($iParentId); |
| 1025 |
|
| 1026 |
$oParentPost = $this->_getSingleObject( |
| 1027 |
$oParent->post_type, |
| 1028 |
$iParentId, |
| 1029 |
self::OBJECTS_FULL |
| 1030 |
); |
| 1031 |
|
| 1032 |
if ($oParentPost !== null) { |
| 1033 |
$oPostObject = $this->getAccessHandler()->getUserAccessManager()->getPost($oParentPost->id); |
| 1034 |
$oParentPost->name = $oPostObject->post_title; |
| 1035 |
|
| 1036 |
$aIsRecursiveMember[$oParent->post_type][] = $oParentPost; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
return $aIsRecursiveMember; |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
/* |
| 1045 |
* Group pluggable _aObjects functions. |
| 1046 |
*/ |
| 1047 |
|
| 1048 |
/** |
| 1049 |
* Returns a the recursive membership for a pluggable object. |
| 1050 |
* |
| 1051 |
* @param string $sObjectType The pluggable object type. |
| 1052 |
* @param integer $iObjectId The object id. |
| 1053 |
* |
| 1054 |
* @return array |
| 1055 |
*/ |
| 1056 |
protected function _getFullPlObject($sObjectType, $iObjectId) |
| 1057 |
{ |
| 1058 |
$blIsRecursiveMember = array(); |
| 1059 |
|
| 1060 |
$oPlObject = $this->getAccessHandler()->getPlObject($sObjectType); |
| 1061 |
|
| 1062 |
if (isset($oPlObject['reference']) |
| 1063 |
&& isset($oPlObject['getFull']) |
| 1064 |
) { |
| 1065 |
$aPlRecMember = $oPlObject['reference']->{$oPlObject['getFull']}( |
| 1066 |
$iObjectId, |
| 1067 |
$this |
| 1068 |
); |
| 1069 |
|
| 1070 |
if (is_array($aPlRecMember)) { |
| 1071 |
$blIsRecursiveMember = $aPlRecMember; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
return $blIsRecursiveMember; |
| 1076 |
} |
| 1077 |
} |