Skip to content

Commit

Permalink
Fix styling code
Browse files Browse the repository at this point in the history
  • Loading branch information
datlechin committed May 10, 2024
1 parent 734ca81 commit bbada6d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 74 deletions.
4 changes: 0 additions & 4 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Datlechin\TagPasswords;

use Datlechin\TagPasswords\Access\ScopeDiscussionVisibilityForAbility;
use Datlechin\TagPasswords\Api\Controller\AuthController;
use Datlechin\TagPasswords\Listener\AddDiscussionAttributes;
use Datlechin\TagPasswords\Listener\AddPostAttributes;
Expand Down Expand Up @@ -45,9 +44,6 @@
(new Extend\Routes('api'))
->post('/datlechin/tag-passwords/auth', 'datlechin-tag-passwords.auth', AuthController::class),

(new Extend\ModelVisibility(Discussion::class))
->scopeAll(ScopeDiscussionVisibilityForAbility::class),

(new Extend\Settings())
->default('flarum-tag-passwords.display_unlock_icon', true)
->default('flarum-tag-passwords.display_protected_tag_from_sidebar', true)
Expand Down
23 changes: 0 additions & 23 deletions src/Access/ScopeDiscussionVisibilityForAbility.php

This file was deleted.

21 changes: 5 additions & 16 deletions src/Api/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@

class AuthController extends AbstractCreateController
{
/**
* {@inheritdoc}
*/
public $serializer = TagSerializer::class;

protected TagRepository $tags;

public function __construct(TagRepository $tags)
{
$this->tags = $tags;
}
public function __construct(protected TagRepository $tags) {}

/**
* {@inheritdoc}
*
* @throws Exception
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = RequestUtil::getActor($request);
Expand All @@ -41,11 +28,13 @@ protected function data(ServerRequestInterface $request, Document $document)
if ($tag->password && $tag->password !== $data['password']) {
throw new Exception('Password is incorrect');
}

if ($tag->protected_groups) {
if (! $this->hasGroup($actor, json_decode($tag->protected_groups))) {
throw new Exception('Access Denied for Tag Access "' . $tag->name . '".');
}
}

if (! $actor->isGuest()) {
$state = $tag->stateFor($actor);
$state->is_unlocked = true;
Expand All @@ -58,9 +47,9 @@ protected function data(ServerRequestInterface $request, Document $document)
*/
public function hasGroup(User $actor, array $protectedGroups): bool
{
foreach ($actor->groups as $id => $permissionGroup) {
foreach ($actor->groups as $group) {
foreach ($protectedGroups as &$protectedGroup) {
if ($permissionGroup->id === (int) $protectedGroup->id) {
if ($group->id == $protectedGroup->id) {
return true;
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/Listener/AddDiscussionAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
use Flarum\Api\Serializer\BasicDiscussionSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Tag;

class AddDiscussionAttributes
{
protected SettingsRepositoryInterface $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function __construct(protected SettingsRepositoryInterface $settings) {}

public function __invoke(BasicDiscussionSerializer $serializer, Discussion $discussion, array $attributes): array
{
Expand All @@ -28,25 +22,30 @@ public function __invoke(BasicDiscussionSerializer $serializer, Discussion $disc
foreach ($discussion->tags as &$tag) {
$isPasswordProtected = (bool) $tag->password;
$isGroupPermissionProtected = (bool) $tag->protected_groups;

if ($isPasswordProtected || $isGroupPermissionProtected) {
if (! $isChecked) {
// Avoid checking the header multiple times, this is used to identify User Page Post
$isUserPage = ReferrerFinder::findUserPagePost($serializer->getRequest());
$isChecked = true;
}

// Only do actor checks if tag has any protection
$isUnlocked = $actor->can('isTagUnlocked', $tag);

if (! $isUnlocked) {
if (! $isProtected) {
$isProtected = true;
}

if ($isPasswordProtected) {
if ($isUserPage) {
$tag->is_unlocked = $isUnlocked;
$tag->is_password_protected = $isPasswordProtected;
$tag->is_group_protected = false;
$tag->password = null;
}

array_push($protectedPasswordTags, $tag);
} else {
if ($isUserPage) {
Expand All @@ -55,12 +54,15 @@ public function __invoke(BasicDiscussionSerializer $serializer, Discussion $disc
$tag->is_group_protected = $isGroupPermissionProtected;
$tag->protected_groups = null;
}

array_push($protectedGroupPermissionTags, $tag);
}
}
}
}

$isProtectedTagDisplayedForDiscussionPage = true;

if ($isProtected) {
if (! $isUserPage && ReferrerFinder::findDiscussion($serializer->getRequest(), $discussion->id)) {
$isProtectedTagDisplayedForDiscussionPage = $actor->hasPermission('flarum-tag-passwords.display_protected_tag_from_discussion_page');
Expand All @@ -69,12 +71,14 @@ public function __invoke(BasicDiscussionSerializer $serializer, Discussion $disc
$restrictData = true;
}
}

if ($restrictData) {
// Discussion slug and title is empty to restricting data from API usage, to ensure compatibility with other extension that are using truncate on string. Slug and title must be an empty string '', for example truncate(discussion.title()) to stop breakage.
$attributes['id'] = null;
$attributes['slug'] = '';
$attributes['title'] = '';
}

$totalProtectedTags = count($protectedPasswordTags) + count($protectedGroupPermissionTags);
$attributes['protectedPasswordTags'] = $protectedPasswordTags;
$attributes['protectedGroupPermissionTags'] = $protectedGroupPermissionTags;
Expand All @@ -83,11 +87,13 @@ public function __invoke(BasicDiscussionSerializer $serializer, Discussion $disc
$isProtectedTagDisplayedForDiscussionList = false;
$isProtectedTagDisplayedForDiscussionAvatar = false;
$isProtectedTagDisplayedForPostList = false;

if ($totalProtectedTags > 0) {
$isProtectedTagDisplayedForDiscussionList = $actor->hasPermission('flarum-tag-passwords.display_protected_tag_from_discussion_list');
$isProtectedTagDisplayedForDiscussionAvatar = $actor->hasPermission('flarum-tag-passwords.display_discussion_avatar');
$isProtectedTagDisplayedForPostList = $actor->hasPermission('flarum-tag-passwords.display_protected_tag_from_post_list');
}

$attributes['isProtectedTagDisplayedForDiscussionList'] = $isProtectedTagDisplayedForDiscussionList;
$attributes['isProtectedTagDisplayedForDiscussionAvatar'] = $isProtectedTagDisplayedForDiscussionAvatar;
$attributes['isProtectedTagDisplayedForPostList'] = $isProtectedTagDisplayedForPostList;
Expand Down
10 changes: 4 additions & 6 deletions src/Listener/AddPostAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,30 @@

class AddPostAttributes
{
protected SettingsRepositoryInterface $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function __construct(protected SettingsRepositoryInterface $settings) {}

public function __invoke(BasicPostSerializer $serializer, Post $post, array $attributes): array
{
$actor = $serializer->getActor();
$isUnlocked = $actor->can('isDiscussionUnlocked', $post->discussion);
$restrictData = false;

if (! $isUnlocked) {
if (ReferrerFinder::findDiscussion($serializer->getRequest(), $post->discussion_id)) {
$restrictData = ! $actor->hasPermission('flarum-tag-passwords.display_protected_tag_from_discussion_page');
} else {
$restrictData = true;
}
}

if ($restrictData) {
// Content is empty to restricting data from API usage, to ensure compatibility with other extension that are using truncate on string. Content must be an empty string '', for example truncate(firstPost.contentPlain()) to stop breakage.
$attributes['id'] = null;
$attributes['content'] = '';
$attributes['contentHtml'] = '';
$attributes['ipAddress'] = null;
}

$attributes['isUnlocked'] = $isUnlocked;

return $attributes;
Expand Down
8 changes: 2 additions & 6 deletions src/Listener/AddTagAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

class AddTagAttributes
{
protected SettingsRepositoryInterface $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function __construct(protected SettingsRepositoryInterface $settings) {}

public function __invoke(TagSerializer $serializer, Tag $tag, array $attributes): array
{
Expand All @@ -38,6 +33,7 @@ public function __invoke(TagSerializer $serializer, Tag $tag, array $attributes)
$isProtectedTagDisplayedForTagsPage = $actor->hasPermission('flarum-tag-passwords.display_protected_tag_from_tags_page');
$isProtectedTagDisplayedForPostList = $actor->hasPermission('flarum-tag-passwords.display_protected_tag_from_post_list');
}

$attributes['isProtectedTagDisplayedForSidebar'] = $isProtectedTagDisplayedForSidebar;
$attributes['isLockedIconDisplayed'] = $isLockedIconDisplayed;
$attributes['isProtectedTagDisplayedForTagsPage'] = $isProtectedTagDisplayedForTagsPage;
Expand Down
10 changes: 4 additions & 6 deletions src/Policy/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@

class DiscussionPolicy extends AbstractPolicy
{
protected SettingsRepositoryInterface $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function __construct(protected SettingsRepositoryInterface $settings) {}

public function isDiscussionUnlocked(User $actor, Discussion $discussion)
{
$tags = $discussion->tags;

foreach ($tags as &$tag) {
$isPasswordProtected = (bool) $tag->password;
$isGroupPermissionProtected = (bool) $tag->protected_groups;

if ($isPasswordProtected || $isGroupPermissionProtected) {
$state = $tag->stateFor($actor);
$isUnlocked = (bool) $state->is_unlocked;

if (! $isUnlocked) {
return false;
}
Expand Down
8 changes: 2 additions & 6 deletions src/Policy/TagPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@

class TagPolicy extends AbstractPolicy
{
protected SettingsRepositoryInterface $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function __construct(protected SettingsRepositoryInterface $settings) {}

public function isTagUnlocked(User $actor, Tag $tag): bool
{
$isPasswordProtected = (bool) $tag->password;
$isGroupProtected = (bool) $tag->protected_groups;

// Avoid checking for is_unlock all the time
if ($isPasswordProtected || $isGroupProtected) {
$state = $tag->stateFor($actor);
Expand Down
6 changes: 6 additions & 0 deletions src/Utils/ReferrerFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public static function findDiscussion(Request $request, int $discussionId): bool
{
// Must check if API is loaded within the direct link of the discussion
$target = $request->getRequestTarget();

if (str_starts_with($target, '/discussions/' . $discussionId)) {
return true;
}

$headers = $request->getHeaders();
$referers = $headers['referer'] ?? [];

foreach ($referers as &$url) {
$urlPath = parse_url($url, PHP_URL_PATH);

if (str_starts_with($urlPath, '/d/' . $discussionId)) {
return true;
}
Expand All @@ -34,8 +38,10 @@ public static function findUserPagePost(Request $request): bool
{
$headers = $request->getHeaders();
$referrer = $headers['referer'] ?? [];

foreach ($referrer as &$url) {
$urlPath = parse_url($url, PHP_URL_PATH);

if (str_starts_with($urlPath, '/u/') && ! str_ends_with($urlPath, '/discussions')) {
return true;
}
Expand Down

0 comments on commit bbada6d

Please sign in to comment.