1
0
mirror of https://github.com/flarum/core.git synced 2025-07-23 01:31:40 +02:00

Make traits more generic

Type hinting User should take place in the callbacks. Theoretically
these traits could be used for another project now, where something
else has permissions (like a Sheep class, or a number)
This commit is contained in:
Toby Zerner
2015-07-01 16:49:40 +09:30
parent 52e55bd503
commit c588e55070
3 changed files with 6 additions and 10 deletions

View File

@@ -1,8 +1,6 @@
<?php namespace Flarum\Core\Support; <?php namespace Flarum\Core\Support;
use Flarum\Core\Exceptions\PermissionDeniedException; use Flarum\Core\Exceptions\PermissionDeniedException;
use Flarum\Core\Models\User;
use Closure;
trait Locked trait Locked
{ {
@@ -16,7 +14,7 @@ trait Locked
return array_merge($conditions, $all); return array_merge($conditions, $all);
} }
public static function allow($action, Closure $condition) public static function allow($action, callable $condition)
{ {
foreach ((array) $action as $action) { foreach ((array) $action as $action) {
if (! isset(static::$conditions[$action])) { if (! isset(static::$conditions[$action])) {
@@ -27,7 +25,7 @@ trait Locked
} }
} }
public function can(User $user, $action) public function can($user, $action)
{ {
foreach ($this->getConditions($action) as $condition) { foreach ($this->getConditions($action) as $condition) {
$can = $condition($this, $user, $action); $can = $condition($this, $user, $action);
@@ -48,7 +46,7 @@ trait Locked
* *
* @throws \Flarum\Core\Exceptions\PermissionDeniedException * @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/ */
public function assertCan(User $user, $action) public function assertCan($user, $action)
{ {
if (! $this->can($user, $action)) { if (! $this->can($user, $action)) {
throw new PermissionDeniedException; throw new PermissionDeniedException;

View File

@@ -1,17 +1,15 @@
<?php namespace Flarum\Core\Support; <?php namespace Flarum\Core\Support;
use Flarum\Core\Models\User;
trait VisibleScope trait VisibleScope
{ {
protected static $visibleScopes = []; protected static $visibleScopes = [];
public static function scopeVisible($scope) public static function addVisibleScope(callable $scope)
{ {
static::$visibleScopes[] = $scope; static::$visibleScopes[] = $scope;
} }
public function scopeWhereVisibleTo($query, User $user) public function scopeWhereVisibleTo($query, $user)
{ {
foreach (static::$visibleScopes as $scope) { foreach (static::$visibleScopes as $scope) {
$scope($query, $user); $scope($query, $user);

View File

@@ -82,7 +82,7 @@ class Model implements ExtenderInterface
} }
foreach ($this->scopeVisible as $callback) { foreach ($this->scopeVisible as $callback) {
$model::scopeVisible($callback); $model::addVisiblePostsScope($callback);
} }
foreach ($this->allow as $info) { foreach ($this->allow as $info) {