| 1 | <?php |
| 2 | |
| 3 | namespace wcf\system\category; |
| 4 | |
| 5 | use wcf\data\category\Category; |
| 6 | use wcf\data\user\User; |
| 7 | use wcf\system\cache\builder\CategoryACLOptionCacheBuilder; |
| 8 | use wcf\system\SingletonFactory; |
| 9 | use wcf\system\WCF; |
| 10 | |
| 11 | /** |
| 12 | * Handles the category permissions. |
| 13 | * |
| 14 | * @author Matthias Schmidt |
| 15 | * @copyright 2001-2019 WoltLab GmbH |
| 16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 17 | */ |
| 18 | class CategoryPermissionHandler extends SingletonFactory |
| 19 | { |
| 20 | /** |
| 21 | * cached category acl options |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $categoryPermissions = []; |
| 25 | |
| 26 | /** |
| 27 | * Returns the acl options for the given category and for the given user. |
| 28 | * If no user is given, the active user is used. |
| 29 | * |
| 30 | * @param Category $category |
| 31 | * @param User $user |
| 32 | * @return int[] |
| 33 | */ |
| 34 | public function getPermissions(Category $category, ?User $user = null) |
| 35 | { |
| 36 | if ($user === null) { |
| 37 | $user = WCF::getUser(); |
| 38 | } |
| 39 | |
| 40 | $permissions = []; |
| 41 | if (isset($this->categoryPermissions[$category->categoryID])) { |
| 42 | if (isset($this->categoryPermissions[$category->categoryID]['group'])) { |
| 43 | foreach ($user->getGroupIDs() as $groupID) { |
| 44 | if (isset($this->categoryPermissions[$category->categoryID]['group'][$groupID])) { |
| 45 | foreach ($this->categoryPermissions[$category->categoryID]['group'][$groupID] as $optionName => $optionValue) { |
| 46 | if (isset($permissions[$optionName])) { |
| 47 | $permissions[$optionName] = $permissions[$optionName] || $optionValue; |
| 48 | } else { |
| 49 | $permissions[$optionName] = $optionValue; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if ( |
| 57 | isset($this->categoryPermissions[$category->categoryID]['user']) |
| 58 | && isset($this->categoryPermissions[$category->categoryID]['user'][$user->userID]) |
| 59 | ) { |
| 60 | foreach ($this->categoryPermissions[$category->categoryID]['user'][$user->userID] as $optionName => $optionValue) { |
| 61 | $permissions[$optionName] = $optionValue; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $permissions; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritDoc |
| 71 | */ |
| 72 | protected function init() |
| 73 | { |
| 74 | $this->categoryPermissions = CategoryACLOptionCacheBuilder::getInstance()->getData(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Resets the category permission cache. |
| 79 | */ |
| 80 | public function resetCache() |
| 81 | { |
| 82 | CategoryACLOptionCacheBuilder::getInstance()->reset(); |
| 83 | } |
| 84 | } |