-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move forum attributes to class to fix redeclare issues
Add check for Tags extension before retrieving scoped permissions Co-Authored-By: Robert Korulczyk <[email protected]>
- Loading branch information
1 parent
92fbaa9
commit b1082b4
Showing
2 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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,59 @@ | ||
<?php | ||
|
||
namespace ClarkWinkelmann\MassActions; | ||
|
||
use Flarum\Api\Serializer\ForumSerializer; | ||
use Flarum\Extension\ExtensionManager; | ||
use Flarum\User\User; | ||
|
||
class ForumAttributes | ||
{ | ||
protected $manager; | ||
|
||
public function __construct(ExtensionManager $manager) | ||
{ | ||
$this->manager = $manager; | ||
} | ||
|
||
public function __invoke(ForumSerializer $serializer): array | ||
{ | ||
return [ | ||
'massControls' => $serializer->getActor()->hasPermission('mass-actions.controls'), | ||
'canHideDiscussionsSometime' => $this->hasGlobalOrScopedPermission($serializer->getActor(), 'discussion.hide'), | ||
'canDeleteDiscussionsSometime' => $this->hasGlobalOrScopedPermission($serializer->getActor(), 'discussion.delete'), | ||
'canLockDiscussionsSometime' => $this->hasGlobalOrScopedPermission($serializer->getActor(), 'discussion.lock') | ||
&& $this->manager->isEnabled('flarum-lock'), | ||
'canStickyDiscussionsSometime' => $this->hasGlobalOrScopedPermission($serializer->getActor(), 'discussion.sticky') | ||
&& $this->manager->isEnabled('flarum-sticky'), | ||
// The tag edit policy is split between moderator permission and self-edit permission | ||
// We will only enable the mass control if self tag edit was set to "indefinitely" | ||
'canTagDiscussionsSometime' => ( | ||
$this->hasGlobalOrScopedPermission($serializer->getActor(), 'discussion.tag') | ||
|| resolve('flarum.settings')->get('allow_tag_change') === '-1' | ||
) | ||
&& $this->manager->isEnabled('flarum-tags'), | ||
]; | ||
} | ||
|
||
protected function hasGlobalOrScopedPermission(User $actor, string $permission): bool | ||
{ | ||
if ($actor->hasPermission($permission)) { | ||
return true; | ||
} | ||
|
||
// Skip tag permission check below if Tags is disabled, otherwise inactive scoped permissions still in the database would be read | ||
if (!$this->manager->isEnabled('flarum-tags')) { | ||
return false; | ||
} | ||
|
||
// Same logic as Tag::scopeWhereHasPermission but without retrieving any tag from the database | ||
// Possible issue: there could be stale scoped permissions still in the database for tag IDs that no longer exist | ||
foreach ($actor->getPermissions() as $thisPermission) { | ||
if (substr($thisPermission, 0, 3) === 'tag' && strpos($thisPermission, $permission) !== false) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |