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