-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate GetModelIsPrivate, replace with extender (#2587)
- Loading branch information
1 parent
7aa221f
commit 36956a9
Showing
6 changed files
with
236 additions
and
14 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
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,82 @@ | ||
<?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\Extend; | ||
|
||
use Flarum\Extension\Extension; | ||
use Flarum\Foundation\ContainerUtil; | ||
use Illuminate\Contracts\Container\Container; | ||
|
||
/** | ||
* Some models, in particular Discussion and Post, are intended to | ||
* support a "private" mode, wherein they aren't visible unless some | ||
* criteria is met. This can be used to implement anything from | ||
* private discussions to post approvals. | ||
* | ||
* When a model is saved, any "privacy checkers" registered for it will | ||
* be run. If any privacy checkers return `true`, the `is_private` field | ||
* of that model instance will be set to `true`. Otherwise, it will be set to | ||
* `false`. Accordingly, this is only available for models with an `is_private` | ||
* field. | ||
* | ||
* In Flarum core, the Discussion and Post models come with private support. | ||
* Core also contains visibility scopers that hide instances of these models | ||
* with `is_private = true` from queries. Extensions can register custom scopers | ||
* for these classes with the `viewPrivate` ability to grant access to view some | ||
* private instances under some conditions. | ||
*/ | ||
class ModelPrivate implements ExtenderInterface | ||
{ | ||
private $modelClass; | ||
private $checkers = []; | ||
|
||
/** | ||
* @param string $modelClass The ::class attribute of the model you are applying scopers to. | ||
* This model must have a `is_private` field. | ||
*/ | ||
public function __construct(string $modelClass) | ||
{ | ||
$this->modelClass = $modelClass; | ||
} | ||
|
||
/** | ||
* Add a model privacy checker. | ||
* | ||
* @param callable|string $callback | ||
* | ||
* The callback can be a closure or invokable class, and should accept: | ||
* - \Flarum\User\User $actor | ||
* - \Illuminate\Database\Eloquent\Builder $query | ||
* | ||
* It should return `true` if the model instance should be made private. | ||
* | ||
* @return self | ||
*/ | ||
public function checker($callback) | ||
{ | ||
$this->checkers[] = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function extend(Container $container, Extension $extension = null) | ||
{ | ||
if (! class_exists($this->modelClass)) { | ||
return; | ||
} | ||
|
||
$container->extend('flarum.database.model_private_checkers', function ($originalCheckers) use ($container) { | ||
foreach ($this->checkers as $checker) { | ||
$originalCheckers[$this->modelClass][] = ContainerUtil::wrapCallback($checker, $container); | ||
} | ||
|
||
return $originalCheckers; | ||
}); | ||
} | ||
} |
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,121 @@ | ||
<?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\Tests\integration\extenders; | ||
|
||
use Flarum\Discussion\Discussion; | ||
use Flarum\Extend; | ||
use Flarum\Tests\integration\RetrievesAuthorizedUsers; | ||
use Flarum\Tests\integration\TestCase; | ||
use Flarum\User\User; | ||
|
||
class ModelPrivateTest extends TestCase | ||
{ | ||
use RetrievesAuthorizedUsers; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function discussion_isnt_saved_as_private_by_default() | ||
{ | ||
$this->app(); | ||
|
||
$user = User::find(1); | ||
|
||
$discussion = Discussion::start('Some Discussion', $user); | ||
$discussion->save(); | ||
|
||
$this->assertFalse($discussion->is_private); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function discussion_is_saved_as_private_if_privacy_checker_added() | ||
{ | ||
$this->extend( | ||
(new Extend\ModelPrivate(Discussion::class)) | ||
->checker(function ($discussion) { | ||
return $discussion->title === 'Private Discussion'; | ||
}) | ||
); | ||
|
||
$this->app(); | ||
|
||
$user = User::find(1); | ||
|
||
$privateDiscussion = Discussion::start('Private Discussion', $user); | ||
$publicDiscussion = Discussion::start('Public Discussion', $user); | ||
$privateDiscussion->save(); | ||
$publicDiscussion->save(); | ||
|
||
$this->assertTrue($privateDiscussion->is_private); | ||
$this->assertFalse($publicDiscussion->is_private); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function discussion_is_saved_as_private_if_privacy_checker_added_via_invokable_class() | ||
{ | ||
$this->extend( | ||
(new Extend\ModelPrivate(Discussion::class)) | ||
->checker(CustomPrivateChecker::class) | ||
); | ||
|
||
$this->app(); | ||
|
||
$user = User::find(1); | ||
|
||
$privateDiscussion = Discussion::start('Private Discussion', $user); | ||
$publicDiscussion = Discussion::start('Public Discussion', $user); | ||
$privateDiscussion->save(); | ||
$publicDiscussion->save(); | ||
|
||
$this->assertTrue($privateDiscussion->is_private); | ||
$this->assertFalse($publicDiscussion->is_private); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function private_checkers_that_return_false_dont_matter() | ||
{ | ||
$this->extend( | ||
(new Extend\ModelPrivate(Discussion::class)) | ||
->checker(function ($discussion) { | ||
return false; | ||
}) | ||
->checker(CustomPrivateChecker::class) | ||
->checker(function ($discussion) { | ||
return false; | ||
}) | ||
); | ||
|
||
$this->app(); | ||
|
||
$user = User::find(1); | ||
|
||
$privateDiscussion = Discussion::start('Private Discussion', $user); | ||
$publicDiscussion = Discussion::start('Public Discussion', $user); | ||
$privateDiscussion->save(); | ||
$publicDiscussion->save(); | ||
|
||
$this->assertTrue($privateDiscussion->is_private); | ||
$this->assertFalse($publicDiscussion->is_private); | ||
} | ||
} | ||
|
||
class CustomPrivateChecker | ||
{ | ||
public function __invoke($discussion) | ||
{ | ||
return $discussion->title === 'Private Discussion'; | ||
} | ||
} |