| 1 |
<?php |
| 2 |
/** |
| 3 |
* MembershipHandler.php |
| 4 |
* |
| 5 |
* The MembershipHandler class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
namespace UserAccessManager\ObjectMembership; |
| 16 |
|
| 17 |
use UserAccessManager\UserGroup\AbstractUserGroup; |
| 18 |
use UserAccessManager\UserGroup\AssignmentInformation; |
| 19 |
use UserAccessManager\UserGroup\AssignmentInformationFactory; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class MembershipHandler |
| 23 |
* |
| 24 |
* @package UserAccessManager\UserGroup |
| 25 |
*/ |
| 26 |
abstract class ObjectMembershipHandler |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @var AssignmentInformationFactory |
| 30 |
*/ |
| 31 |
protected $assignmentInformationFactory; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $generalObjectType; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $assignedObjects; |
| 42 |
|
| 43 |
/** |
| 44 |
* MembershipHandler constructor. |
| 45 |
* |
| 46 |
* @param AssignmentInformationFactory $assignmentInformationFactory |
| 47 |
* |
| 48 |
* @throws \Exception |
| 49 |
*/ |
| 50 |
public function __construct( |
| 51 |
AssignmentInformationFactory $assignmentInformationFactory |
| 52 |
) { |
| 53 |
if ($this->generalObjectType === null) { |
| 54 |
throw new MissingObjectTypeException('Missing general object type, Object type must be set.'); |
| 55 |
} |
| 56 |
|
| 57 |
$this->assignmentInformationFactory = $assignmentInformationFactory; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the object name. |
| 62 |
* |
| 63 |
* @param string $objectId |
| 64 |
* @param string $typeName |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
abstract public function getObjectName($objectId, &$typeName = ''); |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the general object type. |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
public function getGeneralObjectType() |
| 76 |
{ |
| 77 |
return $this->generalObjectType; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the handled objects. |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public function getHandledObjects() |
| 86 |
{ |
| 87 |
return [$this->generalObjectType => $this->generalObjectType]; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns if the object is handled by this handler. |
| 92 |
* |
| 93 |
* @param $objectType |
| 94 |
* |
| 95 |
* @return bool |
| 96 |
*/ |
| 97 |
public function handlesObject($objectType) |
| 98 |
{ |
| 99 |
$objectTypes = $this->getHandledObjects(); |
| 100 |
return isset($objectTypes[$objectType]); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Assigns recursive membership to the assignment information object. |
| 105 |
* |
| 106 |
* @param null|AssignmentInformation $assignmentInformation |
| 107 |
* @param array $recursiveMembership |
| 108 |
*/ |
| 109 |
protected function assignRecursiveMembership( |
| 110 |
&$assignmentInformation, |
| 111 |
array $recursiveMembership |
| 112 |
) { |
| 113 |
if ($assignmentInformation === null) { |
| 114 |
$assignmentInformation = $this->assignmentInformationFactory->createAssignmentInformation(); |
| 115 |
} |
| 116 |
|
| 117 |
$assignmentInformation->setRecursiveMembership($recursiveMembership); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Checks for the access and assigns the recursive membership array if access. |
| 122 |
* |
| 123 |
* @param bool $isMember |
| 124 |
* @param array $recursiveMembership |
| 125 |
* @param null|AssignmentInformation $assignmentInformation |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
protected function checkAccessWithRecursiveMembership( |
| 130 |
$isMember, |
| 131 |
array $recursiveMembership, |
| 132 |
&$assignmentInformation |
| 133 |
) { |
| 134 |
if ($isMember === true || count($recursiveMembership) > 0) { |
| 135 |
$this->assignRecursiveMembership($assignmentInformation, $recursiveMembership); |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the assigned objects as array map. |
| 144 |
* |
| 145 |
* @param AbstractUserGroup $userGroup |
| 146 |
* @param string $objectType |
| 147 |
* |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
protected function getSimpleAssignedObjects(AbstractUserGroup $userGroup, $objectType) |
| 151 |
{ |
| 152 |
$objects = $userGroup->getAssignedObjects($objectType); |
| 153 |
|
| 154 |
return array_map( |
| 155 |
function (AssignmentInformation $element) { |
| 156 |
return $element->getType(); |
| 157 |
}, |
| 158 |
$objects |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Checks if the object is a member of the user group. |
| 164 |
* |
| 165 |
* @param AbstractUserGroup $userGroup |
| 166 |
* @param bool $lockRecursive |
| 167 |
* @param string $objectId |
| 168 |
* @param null|AssignmentInformation $assignmentInformation |
| 169 |
* |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
abstract public function isMember( |
| 173 |
AbstractUserGroup $userGroup, |
| 174 |
$lockRecursive, |
| 175 |
$objectId, |
| 176 |
&$assignmentInformation = null |
| 177 |
); |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns the full objects. |
| 181 |
* |
| 182 |
* @param AbstractUserGroup $userGroup |
| 183 |
* @param bool $lockRecursive |
| 184 |
* @param string $objectType |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
abstract public function getFullObjects(AbstractUserGroup $userGroup, $lockRecursive, $objectType = null); |
| 189 |
} |
| 190 |
|