From 9dd5a742e5f07fe5a4dece3380d4f428df6971f5 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 31 Jul 2015 20:09:31 +0930 Subject: [PATCH] Refactor some model stuff out into traits --- .../core/src/Core/Discussions/Discussion.php | 1 + framework/core/src/Core/Model.php | 41 ------------------- framework/core/src/Core/Posts/Post.php | 2 + framework/core/src/Core/Support/Locked.php | 28 +++++++++++++ .../core/src/Core/Support/VisibleScope.php | 19 +++++++++ framework/core/src/Core/Users/User.php | 1 + 6 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 framework/core/src/Core/Support/VisibleScope.php diff --git a/framework/core/src/Core/Discussions/Discussion.php b/framework/core/src/Core/Discussions/Discussion.php index 125297783..a97cddadf 100755 --- a/framework/core/src/Core/Discussions/Discussion.php +++ b/framework/core/src/Core/Discussions/Discussion.php @@ -22,6 +22,7 @@ class Discussion extends Model { use EventGenerator; use Locked; + use VisibleScope; use ValidatesBeforeSave; /** diff --git a/framework/core/src/Core/Model.php b/framework/core/src/Core/Model.php index f141f5238..9e5294901 100755 --- a/framework/core/src/Core/Model.php +++ b/framework/core/src/Core/Model.php @@ -51,47 +51,6 @@ abstract class Model extends Eloquent return $dates[$class]; } - /** - * Check whether or not a user has permission to perform an action, - * according to the collected conditions. - * - * @param User $actor - * @param string $action - * @return bool - */ - public function can(User $actor, $action) - { - $allowed = static::$dispatcher->until(new ModelAllow($this, $actor, $action)); - - return $allowed ?: false; - } - - /** - * Assert that the user has a certain permission for this model, throwing - * an exception if they don't. - * - * @param User $actor - * @param string $action - * @throws PermissionDeniedException - */ - public function assertCan(User $actor, $action) - { - if (! $this->can($actor, $action)) { - throw new PermissionDeniedException; - } - } - - /** - * Scope a query to only include records that are visible to a user. - * - * @param Builder $query - * @param User $actor - */ - public function scopeWhereVisibleTo(Builder $query, User $actor) - { - event(new ScopeModelVisibility($this, $query, $actor)); - } - /** * Get an attribute from the model. If nothing is found, attempt to load * a custom relation method with this key. diff --git a/framework/core/src/Core/Posts/Post.php b/framework/core/src/Core/Posts/Post.php index 536b5a130..9d1c9da2f 100755 --- a/framework/core/src/Core/Posts/Post.php +++ b/framework/core/src/Core/Posts/Post.php @@ -4,6 +4,7 @@ use DomainException; use Flarum\Events\PostWasDeleted; use Flarum\Core\Model; use Flarum\Core\Support\Locked; +use Flarum\Core\Support\VisibleScope; use Flarum\Core\Support\EventGenerator; use Flarum\Core\Support\ValidatesBeforeSave; use Illuminate\Database\Eloquent\Builder; @@ -15,6 +16,7 @@ class Post extends Model { use EventGenerator; use Locked; + use VisibleScope; use ValidatesBeforeSave; /** diff --git a/framework/core/src/Core/Support/Locked.php b/framework/core/src/Core/Support/Locked.php index bce8cd2c3..a25e970ff 100644 --- a/framework/core/src/Core/Support/Locked.php +++ b/framework/core/src/Core/Support/Locked.php @@ -11,5 +11,33 @@ use Illuminate\Contracts\Events\Dispatcher; */ trait Locked { + /** + * Check whether or not a user has permission to perform an action, + * according to the collected conditions. + * + * @param User $actor + * @param string $action + * @return bool + */ + public function can(User $actor, $action) + { + $allowed = static::$dispatcher->until(new ModelAllow($this, $actor, $action)); + return $allowed ?: false; + } + + /** + * Assert that the user has a certain permission for this model, throwing + * an exception if they don't. + * + * @param User $actor + * @param string $action + * @throws PermissionDeniedException + */ + public function assertCan(User $actor, $action) + { + if (! $this->can($actor, $action)) { + throw new PermissionDeniedException; + } + } } diff --git a/framework/core/src/Core/Support/VisibleScope.php b/framework/core/src/Core/Support/VisibleScope.php new file mode 100644 index 000000000..228fd955a --- /dev/null +++ b/framework/core/src/Core/Support/VisibleScope.php @@ -0,0 +1,19 @@ +