mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 08:43:21 +08: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:
parent
810f79ee77
commit
d414ee33ed
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -82,7 +82,7 @@ class Model implements ExtenderInterface
|
|||
}
|
||||
|
||||
foreach ($this->scopeVisible as $callback) {
|
||||
$model::scopeVisible($callback);
|
||||
$model::addVisiblePostsScope($callback);
|
||||
}
|
||||
|
||||
foreach ($this->allow as $info) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user