/
ModelPrivate.php
81 lines (71 loc) · 2.61 KB
/
ModelPrivate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?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 CommentPost, 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 CommentPost 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 private checkers 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\Database\AbstractModel $instance: An instance of the model.
*
* It should return `true` if the model instance should be made private.
*
* @return self
*/
public function checker($callback): self
{
$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;
});
}
}