-
-
Notifications
You must be signed in to change notification settings - Fork 98
fix: implement more fine grained permission checks #416
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
Open
DavideIadeluca
wants to merge
15
commits into
master
Choose a base branch
from
di/permission-issues
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4b2f96a
feat: introduce new permissions
DavideIadeluca 58bac16
fix: use more fine grained permission checks
DavideIadeluca d4470e9
Apply fixes from StyleCI
StyleCIBot d7e61e1
Merge branch 'master' into di/permission-issues
DavideIadeluca 02ddbaa
chore: use different category for permissions affecting user itself
DavideIadeluca 899dedc
test: adjust tests
DavideIadeluca 0c4c8f1
chore: reflect in frontend that download permission for hiding shared…
DavideIadeluca 170113d
refactor: improve hide model policy
DavideIadeluca 571d9e3
refactor: restructure deletion model policy
DavideIadeluca e31997b
chore: make more readable
DavideIadeluca 7e29011
chore: remove unused consts
DavideIadeluca 6afdaf4
Apply fixes from StyleCI
StyleCIBot d6154b7
refactor: use model policy checks in command handler
DavideIadeluca 40d5af1
chore: migrate permissions
DavideIadeluca 066973b
Apply fixes from StyleCI
StyleCIBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
js/src/admin/extenders/customizeGetRequiredPermissions.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import app from 'flarum/admin/app'; | ||
| import { override } from 'flarum/common/extend'; | ||
|
|
||
| export default function customizeGetRequiredPermissions() { | ||
| override(app, 'getRequiredPermissions', (original, permission: string) => { | ||
| const required = original(permission) || []; | ||
|
|
||
| if (permission === 'fof-upload.hideSharedUploads') { | ||
| if (!required.includes('fof-upload.download')) { | ||
| required.push('fof-upload.download'); | ||
| } | ||
| } | ||
|
|
||
| return required; | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
migrations/2025_11_07_000000_grant_hide_own_to_members.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of fof/upload. | ||
| * | ||
| * Copyright (c) FriendsOfFlarum. | ||
| * Copyright (c) Flagrow. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Flarum\Database\Migration; | ||
| use Flarum\Group\Group; | ||
|
|
||
| return Migration::addPermissions([ | ||
| 'fof-upload.hideUserUploads' => Group::MEMBER_ID, | ||
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of fof/upload. | ||
| * | ||
| * Copyright (c) FriendsOfFlarum. | ||
| * Copyright (c) Flagrow. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Illuminate\Database\Schema\Builder; | ||
|
|
||
| return [ | ||
| 'up' => function (Builder $schema) { | ||
| $db = $schema->getConnection(); | ||
|
|
||
| $groups = $db->table('group_permission') | ||
| ->where('permission', 'fof-upload.deleteUserUploads') | ||
| ->pluck('group_id'); | ||
|
|
||
| foreach ($groups as $gid) { | ||
| foreach ([ | ||
| 'fof-upload.deleteOtherUsersUploads', | ||
| 'fof-upload.hideOtherUsersUploads', | ||
| ] as $perm) { | ||
| $db->table('group_permission')->updateOrInsert( | ||
| ['group_id' => $gid, 'permission' => $perm], | ||
| [] | ||
| ); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| 'down' => function (Builder $schema) { | ||
| $schema->getConnection() | ||
| ->table('group_permission') | ||
| ->whereIn('permission', [ | ||
| 'fof-upload.deleteOtherUsersUploads', | ||
| 'fof-upload.hideOtherUsersUploads', | ||
| ]) | ||
| ->delete(); | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
|
|
||
| use Flarum\Extend; | ||
| use Flarum\Foundation\Paths; | ||
| use Flarum\Group\Group; | ||
| use Flarum\Testing\integration\RetrievesAuthorizedUsers; | ||
| use FoF\Upload\File; | ||
| use FoF\Upload\Tests\EnhancedTestCase; | ||
|
|
@@ -32,19 +33,29 @@ public function setUp(): void | |
| $this->prepareDatabase([ | ||
| 'users' => [ | ||
| $this->normalUser(), | ||
| ['id' => 3, 'username' => 'normal2', 'email' => '[email protected]'], | ||
| ['id' => 4, 'username' => 'moderator', 'email' => '[email protected]'], | ||
| ['id' => 3, 'username' => 'normal2', 'email' => '[email protected]', 'is_email_confirmed' => 1], | ||
| ['id' => 4, 'username' => 'moderator', 'email' => '[email protected]', 'is_email_confirmed' => 1], | ||
| ], | ||
| 'fof_upload_files' => [ | ||
| ['id' => 1, 'base_name' => 'test_file.abc', 'uuid' => 'abc-123', 'path' => 'path/test_file.abc', 'url' => 'http://localhost/test_file.abc', 'type' => 'test/file', 'size' => 123, 'upload_method' => 'local', 'actor_id' => 2, 'shared' => false], | ||
| ['id' => 2, 'base_name' => 'test_file2.abc', 'uuid' => 'def-456', 'path' => 'path/test_file2.abc', 'url' => 'http://localhost/test_file2.abc', 'type' => 'test/file', 'size' => 123, 'upload_method' => 'local', 'shared' => true], | ||
| ], | ||
| 'group_user' => [ | ||
| ['user_id' => 4, 'group_id' => 4], | ||
| ['user_id' => 4, 'group_id' => Group::MODERATOR_ID], | ||
| ], | ||
| 'group_permission' => [ | ||
| ['group_id' => 4, 'permission' => 'fof-upload.deleteUserUploads'], | ||
| ['group_id' => 4, 'permission' => 'fof-upload.viewUserUploads'], | ||
| // General permissions | ||
| ['group_id' => Group::MEMBER_ID, 'permission' => 'fof-upload.download'], | ||
| ['group_id' => Group::MEMBER_ID, 'permission' => 'fof-upload.viewUserUploads'], | ||
| ['group_id' => Group::MODERATOR_ID, 'permission' => 'fof-upload.download'], | ||
|
|
||
| // Hiding permissions | ||
| ['group_id' => Group::MEMBER_ID, 'permission' => 'fof-upload.hideUserUploads'], | ||
| ['group_id' => Group::MODERATOR_ID, 'permission' => 'fof-upload.hideOtherUsersUploads'], | ||
| ['group_id' => Group::MODERATOR_ID, 'permission' => 'fof-upload.hideSharedUploads'], | ||
|
|
||
| // Deletion permissions | ||
| ['group_id' => Group::MODERATOR_ID, 'permission' => 'fof-upload.deleteUserUploads'], | ||
| ], | ||
| ]); | ||
| } | ||
|
|
@@ -248,9 +259,9 @@ public function admin_can_hide_shared_files() | |
| /** | ||
| * @test | ||
| */ | ||
| public function moderator_cannot_hide_shared_files() | ||
| public function moderator_can_hide_shared_files() | ||
| { | ||
| $uuid = 'def-456'; | ||
| $uuid = $this->uploadSharedFileAndGetUuid(); | ||
|
|
||
| $response = $this->send( | ||
| $this->request( | ||
|
|
@@ -264,8 +275,7 @@ public function moderator_cannot_hide_shared_files() | |
| ] | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertEquals(403, $response->getStatusCode()); | ||
| $this->assertEquals(200, $response->getStatusCode()); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This right here is more of a POC of how we can handle cases where other permissions are prerequisites for another permission, similiar like Tags does it for example (permission to create discussion in a tag can only be granted to a new group when the
viewForumpermissions has that group already.Open for feedback if this should be extended to cover more cases in fof/upload or if I should abandon this. See https://github.com/flarum/framework/blob/097b3c5baa025b46b1fe96a3191ce25fb9a062e7/extensions/tags/js/src/admin/addTagsPermissionScope.tsx#L34-L46 for a similar implementation in the framework