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