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;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Flarum\Core\Models\User;
use Closure;
trait Locked
{
@ -16,7 +14,7 @@ trait Locked
return array_merge($conditions, $all);
}
public static function allow($action, Closure $condition)
public static function allow($action, callable $condition)
{
foreach ((array) $action as $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) {
$can = $condition($this, $user, $action);
@ -48,7 +46,7 @@ trait Locked
*
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function assertCan(User $user, $action)
public function assertCan($user, $action)
{
if (! $this->can($user, $action)) {
throw new PermissionDeniedException;

View File

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

View File

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