mirror of
https://github.com/flarum/core.git
synced 2025-07-20 00:01:17 +02:00
feat(likes): Option to prevent users liking their own posts (#3534)
* Option to prevent users liking their own posts * test: user can only like own post if setting ON Co-authored-by: Sami Mazouz <ilyasmazouz@gmail.com>
This commit is contained in:
@@ -7,13 +7,14 @@
|
|||||||
* LICENSE file that was distributed with this source code.
|
* LICENSE file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Likes;
|
||||||
|
|
||||||
use Flarum\Api\Controller;
|
use Flarum\Api\Controller;
|
||||||
use Flarum\Api\Serializer\BasicUserSerializer;
|
use Flarum\Api\Serializer\BasicUserSerializer;
|
||||||
use Flarum\Api\Serializer\PostSerializer;
|
use Flarum\Api\Serializer\PostSerializer;
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
use Flarum\Likes\Event\PostWasLiked;
|
use Flarum\Likes\Event\PostWasLiked;
|
||||||
use Flarum\Likes\Event\PostWasUnliked;
|
use Flarum\Likes\Event\PostWasUnliked;
|
||||||
use Flarum\Likes\Listener;
|
|
||||||
use Flarum\Likes\Notification\PostLikedBlueprint;
|
use Flarum\Likes\Notification\PostLikedBlueprint;
|
||||||
use Flarum\Likes\Query\LikedByFilter;
|
use Flarum\Likes\Query\LikedByFilter;
|
||||||
use Flarum\Post\Event\Deleted;
|
use Flarum\Post\Event\Deleted;
|
||||||
@@ -64,4 +65,10 @@ return [
|
|||||||
|
|
||||||
(new Extend\Filter(PostFilterer::class))
|
(new Extend\Filter(PostFilterer::class))
|
||||||
->addFilter(LikedByFilter::class),
|
->addFilter(LikedByFilter::class),
|
||||||
|
|
||||||
|
(new Extend\Settings())
|
||||||
|
->default('flarum-likes.like_own_post', true),
|
||||||
|
|
||||||
|
(new Extend\Policy())
|
||||||
|
->modelPolicy(Post::class, Access\LikePostPolicy::class),
|
||||||
];
|
];
|
||||||
|
@@ -1,12 +1,20 @@
|
|||||||
import app from 'flarum/admin/app';
|
import app from 'flarum/admin/app';
|
||||||
|
|
||||||
app.initializers.add('flarum-likes', () => {
|
app.initializers.add('flarum-likes', () => {
|
||||||
app.extensionData.for('flarum-likes').registerPermission(
|
app.extensionData
|
||||||
{
|
.for('flarum-likes')
|
||||||
icon: 'far fa-thumbs-up',
|
.registerPermission(
|
||||||
label: app.translator.trans('flarum-likes.admin.permissions.like_posts_label'),
|
{
|
||||||
permission: 'discussion.likePosts',
|
icon: 'far fa-thumbs-up',
|
||||||
},
|
label: app.translator.trans('flarum-likes.admin.permissions.like_posts_label'),
|
||||||
'reply'
|
permission: 'discussion.likePosts',
|
||||||
);
|
},
|
||||||
|
'reply'
|
||||||
|
)
|
||||||
|
.registerSetting({
|
||||||
|
setting: 'flarum-likes.like_own_post',
|
||||||
|
type: 'bool',
|
||||||
|
label: app.translator.trans('flarum-likes.admin.settings.like_own_posts_label'),
|
||||||
|
help: app.translator.trans('flarum-likes.admin.settings.like_own_posts_help'),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -11,6 +11,10 @@ flarum-likes:
|
|||||||
permissions:
|
permissions:
|
||||||
like_posts_label: Like posts
|
like_posts_label: Like posts
|
||||||
|
|
||||||
|
settings:
|
||||||
|
like_own_posts_help: When enabled, subject to permission, users may 'like' their own posts on the forum. To prevent users placing a 'like' on their own posts, disable this setting.
|
||||||
|
like_own_posts_label: Users may like their own posts
|
||||||
|
|
||||||
# Translations in this namespace are used by the forum user interface.
|
# Translations in this namespace are used by the forum user interface.
|
||||||
forum:
|
forum:
|
||||||
|
|
||||||
|
35
extensions/likes/src/Access/LikePostPolicy.php
Normal file
35
extensions/likes/src/Access/LikePostPolicy.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Likes\Access;
|
||||||
|
|
||||||
|
use Flarum\Post\Post;
|
||||||
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
|
use Flarum\User\Access\AbstractPolicy;
|
||||||
|
use Flarum\User\User;
|
||||||
|
|
||||||
|
class LikePostPolicy extends AbstractPolicy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var SettingsRepositoryInterface
|
||||||
|
*/
|
||||||
|
protected $settings;
|
||||||
|
|
||||||
|
public function __construct(SettingsRepositoryInterface $settings)
|
||||||
|
{
|
||||||
|
$this->settings = $settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function like(User $actor, Post $post)
|
||||||
|
{
|
||||||
|
if ($actor->id === $post->user_id && ! (bool) $this->settings->get('flarum-likes.like_own_post')) {
|
||||||
|
return $this->deny();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -48,7 +48,10 @@ class LikePostTest extends TestCase
|
|||||||
['user_id' => 3, 'group_id' => 5]
|
['user_id' => 3, 'group_id' => 5]
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function rewriteDefaultPermissionsAfterBoot()
|
||||||
|
{
|
||||||
$this->database()->table('group_permission')->where('permission', 'discussion.likePosts')->delete();
|
$this->database()->table('group_permission')->where('permission', 'discussion.likePosts')->delete();
|
||||||
$this->database()->table('group_permission')->insert(['permission' => 'discussion.likePosts', 'group_id' => 5]);
|
$this->database()->table('group_permission')->insert(['permission' => 'discussion.likePosts', 'group_id' => 5]);
|
||||||
}
|
}
|
||||||
@@ -57,8 +60,14 @@ class LikePostTest extends TestCase
|
|||||||
* @dataProvider allowedUsersToLike
|
* @dataProvider allowedUsersToLike
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function can_like_a_post_if_allowed(int $postId, ?int $authenticatedAs, string $message)
|
public function can_like_a_post_if_allowed(int $postId, ?int $authenticatedAs, string $message, bool $canLikeOwnPost = null)
|
||||||
{
|
{
|
||||||
|
if (! is_null($canLikeOwnPost)) {
|
||||||
|
$this->setting('flarum-likes.like_own_post', $canLikeOwnPost);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->rewriteDefaultPermissionsAfterBoot();
|
||||||
|
|
||||||
$response = $this->sendLikeRequest($postId, $authenticatedAs);
|
$response = $this->sendLikeRequest($postId, $authenticatedAs);
|
||||||
|
|
||||||
$post = CommentPost::query()->find($postId);
|
$post = CommentPost::query()->find($postId);
|
||||||
@@ -71,8 +80,14 @@ class LikePostTest extends TestCase
|
|||||||
* @dataProvider unallowedUsersToLike
|
* @dataProvider unallowedUsersToLike
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function cannot_like_a_post_if_not_allowed(int $postId, ?int $authenticatedAs, string $message)
|
public function cannot_like_a_post_if_not_allowed(int $postId, ?int $authenticatedAs, string $message, bool $canLikeOwnPost = null)
|
||||||
{
|
{
|
||||||
|
if (! is_null($canLikeOwnPost)) {
|
||||||
|
$this->setting('flarum-likes.like_own_post', $canLikeOwnPost);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->rewriteDefaultPermissionsAfterBoot();
|
||||||
|
|
||||||
$response = $this->sendLikeRequest($postId, $authenticatedAs);
|
$response = $this->sendLikeRequest($postId, $authenticatedAs);
|
||||||
|
|
||||||
$post = CommentPost::query()->find($postId);
|
$post = CommentPost::query()->find($postId);
|
||||||
@@ -85,8 +100,14 @@ class LikePostTest extends TestCase
|
|||||||
* @dataProvider allowedUsersToLike
|
* @dataProvider allowedUsersToLike
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function can_dislike_a_post_if_liked_and_allowed(int $postId, ?int $authenticatedAs, string $message)
|
public function can_dislike_a_post_if_liked_and_allowed(int $postId, ?int $authenticatedAs, string $message, bool $canLikeOwnPost = null)
|
||||||
{
|
{
|
||||||
|
if (! is_null($canLikeOwnPost)) {
|
||||||
|
$this->setting('flarum-likes.like_own_post', $canLikeOwnPost);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->rewriteDefaultPermissionsAfterBoot();
|
||||||
|
|
||||||
$this->sendLikeRequest($postId, $authenticatedAs);
|
$this->sendLikeRequest($postId, $authenticatedAs);
|
||||||
$response = $this->sendLikeRequest($postId, $authenticatedAs, false);
|
$response = $this->sendLikeRequest($postId, $authenticatedAs, false);
|
||||||
|
|
||||||
@@ -101,7 +122,7 @@ class LikePostTest extends TestCase
|
|||||||
return [
|
return [
|
||||||
[1, 1, 'Admin can like any post'],
|
[1, 1, 'Admin can like any post'],
|
||||||
[1, 3, 'User with permission can like other posts'],
|
[1, 3, 'User with permission can like other posts'],
|
||||||
[6, 3, 'User with permission can like own post']
|
[5, 3, 'User with permission can like own post by default'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +130,9 @@ class LikePostTest extends TestCase
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[1, null, 'Guest cannot like any post'],
|
[1, null, 'Guest cannot like any post'],
|
||||||
[1, 2, 'User without permission cannot like any post']
|
[1, 2, 'User without permission cannot like any post'],
|
||||||
|
[5, 3, 'User with permission cannot like own post if setting off', false],
|
||||||
|
[6, 1, 'Admin cannot like own post if setting off', false],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user