Skip to content
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

Model Visibility Scoping Extender and Tests #2460

Merged
merged 18 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply fixes from StyleCI
[ci skip] [skip ci]
  • Loading branch information
askvortsov1 committed Dec 2, 2020
commit d10d44b8512750310488ce930152b256988c1787
4 changes: 1 addition & 3 deletions src/Database/ScopeVisibilityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Database;

use Flarum\Event\ScopeModelVisibility;
use Flarum\Group\Group;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
Expand All @@ -28,14 +27,13 @@ public static function registerVisibilityScoper($scoper, $ability = null)
$ability = static::$DEFAULT;
}

if (!Arr::has(static::$visibilityScopers, "$model.$ability")) {
if (! Arr::has(static::$visibilityScopers, "$model.$ability")) {
Arr::set(static::$visibilityScopers, "$model.$ability", []);
}

static::$visibilityScopers[$model][$ability][] = $scoper;
}


/**
* Scope a query to only include records that are visible to a user.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Discussion/Access/ScopeDiscussionVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(User $actor, $query)

// Hide hidden discussions, unless they are authored by the current
// user, or the current user has permission to view hidden discussions.
if (!$actor->hasPermission('discussion.hide')) {
if (! $actor->hasPermission('discussion.hide')) {
$query->where(function ($query) use ($actor) {
$query->whereNull('discussions.hidden_at')
->orWhere('discussions.user_id', $actor->id)
Expand All @@ -48,7 +48,7 @@ public function __invoke(User $actor, $query)

// Hide discussions with no comments, unless they are authored by the
// current user, or the user is allowed to edit the discussion's posts.
if (!$actor->hasPermission('discussion.editPosts')) {
if (! $actor->hasPermission('discussion.editPosts')) {
$query->where(function ($query) use ($actor) {
$query->where('discussions.comment_count', '>', 0)
->orWhere('discussions.user_id', $actor->id)
Expand Down
2 changes: 0 additions & 2 deletions src/Discussion/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

namespace Flarum\Discussion;

use Flarum\Event\ScopeModelVisibility;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AbstractPolicy;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Builder;

class DiscussionPolicy extends AbstractPolicy
{
Expand Down
2 changes: 0 additions & 2 deletions src/Discussion/DiscussionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Flarum\Discussion;

use Flarum\Database\AbstractModel;
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Discussion\Access\ScopeDiscussionVisibility;
use Flarum\Discussion\Event\Renamed;
use Flarum\Foundation\AbstractServiceProvider;
Expand Down
6 changes: 3 additions & 3 deletions src/Extend/ModelVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function __construct(string $modelClass)
{
$this->modelClass = $modelClass;

if (!method_exists($modelClass, "registerVisibilityScoper")) {
if (! method_exists($modelClass, 'registerVisibilityScoper')) {
throw new Exception("Model $modelClass cannot be visibility scoped as it does not use Flarum\Database\ScopeVisibilityTrait.");
}
}

/**
* Add a scoper for a given ability
* Add a scoper for a given ability.
*
* @param callable|string $callback
* @param string $ability, defaults to 'view'
Expand All @@ -69,7 +69,7 @@ public function scoper($callback, $ability = 'view')
}

/**
* Add a default scoper
* Add a default scoper.
*
* @param callable|string $callback
*
Expand Down
1 change: 0 additions & 1 deletion src/Group/GroupPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Flarum\User\AbstractPolicy;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;

class GroupPolicy extends AbstractPolicy
{
Expand Down
2 changes: 0 additions & 2 deletions src/Group/GroupServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Flarum\Group;

use Flarum\Database\AbstractModel;
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Group\Access\ScopeGroupVisibility;

Expand Down
1 change: 0 additions & 1 deletion src/Notification/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Carbon\Carbon;
use Flarum\Database\AbstractModel;
use Flarum\Event\ScopeModelVisibility;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
Expand Down
2 changes: 1 addition & 1 deletion src/Post/Access/ScopePostVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __invoke(User $actor, $query)
// Hide hidden posts, unless they are authored by the current user, or
// the current user has permission to view hidden posts in the
// discussion.
if (!$actor->hasPermission('discussion.hidePosts')) {
if (! $actor->hasPermission('discussion.hidePosts')) {
$query->where(function ($query) use ($actor) {
$query->whereNull('posts.hidden_at')
->orWhere('posts.user_id', $actor->id)
Expand Down
2 changes: 0 additions & 2 deletions src/User/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Flarum\User;

use Illuminate\Database\Eloquent\Builder;

class UserPolicy extends AbstractPolicy
{
/**
Expand Down
2 changes: 0 additions & 2 deletions src/User/UserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Flarum\User;

use Flarum\Database\AbstractModel;
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Event\ConfigureUserPreferences;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\ContainerUtil;
Expand Down
17 changes: 8 additions & 9 deletions tests/integration/extenders/ModelVisibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Tests\integration\extenders;

use Carbon\Carbon;
use Egulias\EmailValidator\Warning\Comment;
use Flarum\Discussion\Discussion;
use Flarum\Extend;
use Flarum\Post\CommentPost;
Expand Down Expand Up @@ -75,7 +74,7 @@ public function custom_visibility_scoper_can_stop_user_from_seeing_posts()
$this->extend(
(new Extend\ModelVisibility(CommentPost::class))
->scoper(function (User $user, Builder $query) {
$query->whereRaw("1=0");
$query->whereRaw('1=0');
}, 'view')
);

Expand All @@ -96,7 +95,7 @@ public function custom_visibility_scoper_applies_if_added_to_parent_class()
$this->extend(
(new Extend\ModelVisibility(Post::class))
->scoper(function (User $user, Builder $query) {
$query->whereRaw("1=0");
$query->whereRaw('1=0');
}, 'view')
);

Expand All @@ -117,11 +116,11 @@ public function custom_visibility_scoper_for_class_applied_after_scopers_for_par
$this->extend(
(new Extend\ModelVisibility(CommentPost::class))
->scoper(function (User $user, Builder $query) {
$query->orWhereRaw("1=1");
$query->orWhereRaw('1=1');
}, 'view'),
(new Extend\ModelVisibility(Post::class))
->scoper(function (User $user, Builder $query) {
$query->whereRaw("1=0");
$query->whereRaw('1=0');
}, 'view'),
);

Expand All @@ -142,11 +141,11 @@ public function custom_scoper_works_for_abilities_other_than_view()
$this->extend(
(new Extend\ModelVisibility(Discussion::class))
->scoper(function (User $user, Builder $query) {
$query->whereRaw("1=1");
$query->whereRaw('1=1');
}, 'viewPrivate'),
(new Extend\ModelVisibility(Post::class))
->scoper(function (User $user, Builder $query) {
$query->whereRaw("1=1");
$query->whereRaw('1=1');
}, 'viewPrivate'),
);

Expand All @@ -168,13 +167,13 @@ public function default_scoper_works()
(new Extend\ModelVisibility(Discussion::class))
->defaultScoper(function (User $user, Builder $query, string $ability) {
if ($ability == 'viewPrivate') {
$query->whereRaw("1=1");
$query->whereRaw('1=1');
}
}),
(new Extend\ModelVisibility(Post::class))
->defaultScoper(function (User $user, Builder $query, string $ability) {
if ($ability == 'viewPrivate') {
$query->whereRaw("1=1");
$query->whereRaw('1=1');
}
}),
);
Expand Down