-
-
Notifications
You must be signed in to change notification settings - Fork 262
Feat/4311 category permission enforcement #4553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c30eb95
cdfc810
00f4613
4cd7d38
ed1e49f
fbc0a1e
1236a9e
dba40af
af4a177
836e189
b8b2a45
64af5e1
abc49af
996e9ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Filters linear category trees to the categories a user may act on. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * @package phpMyFAQ | ||
| * @author Thorsten Rinne <thorsten@phpmyfaq.de> | ||
| * @copyright 2026 phpMyFAQ Team | ||
| * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
| * @link https://www.phpmyfaq.de | ||
| * @since 2026-08-10 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace phpMyFAQ\Category; | ||
|
|
||
| /** | ||
| * Filters linear category trees down to the categories a user may act on, | ||
| * based on group-level category restrictions. Null means unrestricted. | ||
| */ | ||
| final class CategoryTreeRestrictionFilter | ||
| { | ||
| /** | ||
| * @param array<int, array<string, mixed>> $categoryTree Linear tree from Category::getCategoryTree() | ||
| * @param array<int>|null $allowedCategoryIds Null = unrestricted | ||
| * @return array<int, array<string, mixed>> | ||
| */ | ||
| public static function filter(array $categoryTree, ?array $allowedCategoryIds): array | ||
| { | ||
| if ($allowedCategoryIds === null) { | ||
| return $categoryTree; | ||
| } | ||
|
|
||
| return array_values(array_filter($categoryTree, static fn(array $entry): bool => in_array( | ||
| needle: (int) $entry['id'], | ||
| haystack: $allowedCategoryIds, | ||
| strict: true, | ||
| ))); | ||
| } | ||
|
|
||
| /** | ||
| * Filters a nested tree (map of categoryId => children map) as produced by | ||
| * Category\Order::getCategoryTree() and Category::buildAdminCategoryTree(). | ||
| * Removing a node removes its whole subtree. | ||
| * | ||
| * @param array<array-key, mixed> $categoryTree | ||
| * @param array<int>|null $allowedCategoryIds Null = unrestricted | ||
| * @return array<array-key, mixed> | ||
| */ | ||
| public static function filterNested(array $categoryTree, ?array $allowedCategoryIds): array | ||
| { | ||
| if ($allowedCategoryIds === null) { | ||
| return $categoryTree; | ||
| } | ||
|
|
||
| $filtered = []; | ||
| foreach ($categoryTree as $categoryId => $children) { | ||
|
Check warning on line 62 in phpmyfaq/src/phpMyFAQ/Category/CategoryTreeRestrictionFilter.php
|
||
| if (!in_array(needle: (int) $categoryId, haystack: $allowedCategoryIds, strict: true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $filtered[$categoryId] = is_array($children) | ||
| ? self::filterNested($children, $allowedCategoryIds) | ||
| : $children; | ||
| } | ||
|
|
||
| return $filtered; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.