| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * AccessHandler.php | |
| 4 | + * | |
| 5 | + * The AccessHandler 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 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Access; |
| @@ -13,44 +26,113 @@ | ||
| 13 | 26 | use UserAccessManager\UserGroup\UserGroupHandler; |
| 14 | 27 | use UserAccessManager\UserGroup\UserGroupTypeException; |
| 15 | 28 | use UserAccessManager\Wrapper\Wordpress; |
| 16 | 29 | |
| 30 | +/** | |
| 31 | + * Class AccessHandler | |
| 32 | + * @package UserAccessManager\AccessHandler | |
| 33 | + */ | |
| 17 | 34 | class AccessHandler |
| 18 | 35 | { |
| 19 | - private ?array $excludedTerms = null; | |
| 20 | - private ?array $excludedPosts = null; | |
| 21 | - private array $objectAccess = []; | |
| 22 | - private ?array $noneHiddenPostTypes = null; | |
| 36 | + /** | |
| 37 | + * @var Wordpress | |
| 38 | + */ | |
| 39 | + private $wordpress; | |
| 23 | 40 | |
| 41 | + /** | |
| 42 | + * @var MainConfig | |
| 43 | + */ | |
| 44 | + private $mainConfig; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * @var Database | |
| 48 | + */ | |
| 49 | + private $database; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @var ObjectHandler | |
| 53 | + */ | |
| 54 | + private $objectHandler; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * @var UserHandler | |
| 58 | + */ | |
| 59 | + private $userHandler; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * @var UserGroupHandler | |
| 63 | + */ | |
| 64 | + private $userGroupHandler; | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * @var null|array | |
| 68 | + */ | |
| 69 | + private $excludedTerms = null; | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * @var null|array | |
| 73 | + */ | |
| 74 | + private $excludedPosts = null; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * @var array | |
| 78 | + */ | |
| 79 | + private $objectAccess = []; | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * @var null|array | |
| 83 | + */ | |
| 84 | + private $noneHiddenPostTypes = null; | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * AccessHandler constructor. | |
| 88 | + * @param Wordpress $wordpress | |
| 89 | + * @param MainConfig $mainConfig | |
| 90 | + * @param Database $database | |
| 91 | + * @param ObjectHandler $objectHandler | |
| 92 | + * @param UserHandler $userHandler | |
| 93 | + * @param UserGroupHandler $userGroupHandler | |
| 94 | + */ | |
| 24 | 95 | public function __construct( |
| 25 | - private Wordpress $wordpress, | |
| 26 | - private MainConfig $mainConfig, | |
| 27 | - private Database $database, | |
| 28 | - private ObjectHandler $objectHandler, | |
| 29 | - private UserHandler $userHandler, | |
| 30 | - private UserGroupHandler $userGroupHandler | |
| 96 | + Wordpress $wordpress, | |
| 97 | + MainConfig $mainConfig, | |
| 98 | + Database $database, | |
| 99 | + ObjectHandler $objectHandler, | |
| 100 | + UserHandler $userHandler, | |
| 101 | + UserGroupHandler $userGroupHandler | |
| 31 | 102 | ) { |
| 103 | + $this->wordpress = $wordpress; | |
| 104 | + $this->mainConfig = $mainConfig; | |
| 105 | + $this->database = $database; | |
| 106 | + $this->objectHandler = $objectHandler; | |
| 107 | + $this->userHandler = $userHandler; | |
| 108 | + $this->userGroupHandler = $userGroupHandler; | |
| 32 | 109 | } |
| 33 | 110 | |
| 34 | - private function hasAuthorAccess(string $objectType, int|string|null $objectId): bool | |
| 111 | + /** | |
| 112 | + * Checks it the user has access because he is the author. | |
| 113 | + * @param string $objectType | |
| 114 | + * @param int|string $objectId | |
| 115 | + * @return bool | |
| 116 | + */ | |
| 117 | + private function hasAuthorAccess(string $objectType, $objectId): bool | |
| 35 | 118 | { |
| 36 | 119 | if ($this->mainConfig->authorsHasAccessToOwn() === true |
| 37 | 120 | && $this->objectHandler->isPostType($objectType) |
| 38 | 121 | ) { |
| 39 | 122 | $post = $this->objectHandler->getPost($objectId); |
| 40 | - | |
| 41 | - if ($post === false) { | |
| 42 | - return false; | |
| 43 | - } | |
| 44 | - | |
| 45 | - $currentUserId = $this->wordpress->getCurrentUser()->ID; | |
| 46 | - return $currentUserId !== 0 | |
| 47 | - && $currentUserId === (int) $post->post_author; | |
| 123 | + return $post !== false | |
| 124 | + && $this->wordpress->getCurrentUser()->ID === (int) $post->post_author; | |
| 48 | 125 | } |
| 49 | 126 | |
| 50 | 127 | return false; |
| 51 | 128 | } |
| 52 | 129 | |
| 130 | + /** | |
| 131 | + * Checks if the is admin value is set if not grabs it from the wordpress function. | |
| 132 | + * @param null|bool $isAdmin | |
| 133 | + * @return bool | |
| 134 | + */ | |
| 53 | 135 | private function isAdmin(?bool $isAdmin): bool |
| 54 | 136 | { |
| 55 | 137 | return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin; |
| 56 | 138 | } |
| @@ -55,8 +137,11 @@ | ||
| 55 | 137 | return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin; |
| 56 | 138 | } |
| 57 | 139 | |
| 58 | 140 | /** |
| 141 | + * Returns the user user groups filtered by the write access. | |
| 142 | + * @param bool|null $isAdmin If set we force the admin mode. | |
| 143 | + * @return AbstractUserGroup[] | |
| 59 | 144 | * @throws UserGroupTypeException |
| 60 | 145 | */ |
| 61 | 146 | private function getUserUserGroupsForObjectAccess(?bool $isAdmin = null): array |
| 62 | 147 | { |
| @@ -74,12 +159,17 @@ | ||
| 74 | 159 | return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin); |
| 75 | 160 | } |
| 76 | 161 | |
| 77 | 162 | /** |
| 163 | + * Checks if the current_user has access to the given post. | |
| 164 | + * @param string|null $objectType The object type which should be checked. | |
| 165 | + * @param int|string $objectId The id of the object. | |
| 166 | + * @param null|bool $isAdmin If set we force the admin mode. | |
| 167 | + * @return bool | |
| 78 | 168 | * @throws UserGroupTypeException |
| 79 | 169 | * @throws Exception |
| 80 | 170 | */ |
| 81 | - public function checkObjectAccess(?string $objectType, int|string|null $objectId, ?bool $isAdmin = null): bool | |
| 171 | + public function checkObjectAccess(?string $objectType, $objectId, ?bool $isAdmin = null): bool | |
| 82 | 172 | { |
| 83 | 173 | $isAdmin = $this->isAdmin($isAdmin); |
| 84 | 174 | |
| 85 | 175 | if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) { |
| @@ -104,8 +194,12 @@ | ||
| 104 | 194 | return $this->objectAccess[$isAdmin][$objectType][$objectId]; |
| 105 | 195 | } |
| 106 | 196 | |
| 107 | 197 | /** |
| 198 | + * Returns the excluded objects. | |
| 199 | + * @param string $type | |
| 200 | + * @param array $filterTypesMap | |
| 201 | + * @return array | |
| 108 | 202 | * @throws UserGroupTypeException |
| 109 | 203 | * @throws Exception |
| 110 | 204 | */ |
| 111 | 205 | private function getExcludedObjects(string $type, array $filterTypesMap = []): array |
| @@ -136,8 +230,10 @@ | ||
| 136 | 230 | return array_combine($objectIds, $objectIds); |
| 137 | 231 | } |
| 138 | 232 | |
| 139 | 233 | /** |
| 234 | + * Returns the excluded terms for a user. | |
| 235 | + * @return array | |
| 140 | 236 | * @throws UserGroupTypeException |
| 141 | 237 | */ |
| 142 | 238 | public function getExcludedTerms(): ?array |
| 143 | 239 | { |
| @@ -151,8 +247,12 @@ | ||
| 151 | 247 | |
| 152 | 248 | return $this->excludedTerms; |
| 153 | 249 | } |
| 154 | 250 | |
| 251 | + /** | |
| 252 | + * Returns the none hidden post types map. | |
| 253 | + * @return array | |
| 254 | + */ | |
| 155 | 255 | private function getNoneHiddenPostTypes(): ?array |
| 156 | 256 | { |
| 157 | 257 | if ($this->noneHiddenPostTypes === null) { |
| 158 | 258 | $this->noneHiddenPostTypes = []; |
| @@ -171,8 +271,10 @@ | ||
| 171 | 271 | return $this->noneHiddenPostTypes; |
| 172 | 272 | } |
| 173 | 273 | |
| 174 | 274 | /** |
| 275 | + * Returns the excluded posts. | |
| 276 | + * @return array | |
| 175 | 277 | * @throws UserGroupTypeException |
| 176 | 278 | */ |
| 177 | 279 | public function getExcludedPosts(): ?array |
| 178 | 280 | { |