mirror of
https://github.com/flarum/core.git
synced 2025-10-20 19:27:14 +02:00
Rework public API based on events
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Events\ModelAllow;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
/**
|
||||
* 'Lock' an object, allowing the permission of a user to perform an action to
|
||||
@@ -9,75 +11,5 @@ use Flarum\Core\Users\User;
|
||||
*/
|
||||
trait Locked
|
||||
{
|
||||
/**
|
||||
* @var callable[]
|
||||
*/
|
||||
protected static $conditions = [];
|
||||
|
||||
/**
|
||||
* Get the condition callbacks for the specified action.
|
||||
*
|
||||
* @param string $action
|
||||
* @return callable[]
|
||||
*/
|
||||
protected static function getConditions($action)
|
||||
{
|
||||
$conditions = array_get(static::$conditions, $action, []);
|
||||
$all = array_get(static::$conditions, '*', []);
|
||||
|
||||
return array_merge($conditions, $all);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow the specified action if the given condition is satisfied.
|
||||
*
|
||||
* @param string $action
|
||||
* @param callable $condition The condition callback. Parameters are the
|
||||
* object that is locked, the user performing the action,
|
||||
* and the name of the action. This condition will be ignored if it
|
||||
* returns null; otherwise, the return value will determine whether or
|
||||
* not the action is allowed.
|
||||
*/
|
||||
public static function allow($action, callable $condition)
|
||||
{
|
||||
foreach ((array)$action as $action) {
|
||||
static::$conditions[$action][] = $condition;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
foreach ($this->getConditions($action) as $condition) {
|
||||
$can = $condition($this, $actor, $action);
|
||||
|
||||
if ($can !== null) {
|
||||
return $can;
|
||||
}
|
||||
}
|
||||
|
||||
return 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
<?php namespace Flarum\Core\Support;
|
||||
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Flarum\Events\ModelValidator;
|
||||
use Illuminate\Validation\Factory;
|
||||
use Illuminate\Contracts\Validation\ValidationException;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
@@ -79,13 +80,11 @@ trait ValidatesBeforeSave
|
||||
*/
|
||||
protected function makeValidator()
|
||||
{
|
||||
$dirty = $this->getDirty();
|
||||
$rules = $this->expandUniqueRules($this->rules);
|
||||
|
||||
$rules = $this->expandUniqueRules(array_only($this->rules, array_keys($dirty)));
|
||||
$validator = static::$validator->make($this->getAttributes(), $rules);
|
||||
|
||||
$validator = static::$validator->make($dirty, $rules);
|
||||
|
||||
// TODO: event
|
||||
event(new ModelValidator($this, $validator));
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
@@ -1,40 +0,0 @@
|
||||
<?php namespace Flarum\Core\Support;
|
||||
|
||||
use Flarum\Core\Users\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* Add a query scope to an Eloquent model that filters out records that a user
|
||||
* is not allowed to view.
|
||||
*/
|
||||
trait VisibleScope
|
||||
{
|
||||
/**
|
||||
* @var callable[]
|
||||
*/
|
||||
protected static $visibleScopes = [];
|
||||
|
||||
/**
|
||||
* Add a callback to scope a query to only include records that are visible
|
||||
* to a user.
|
||||
*
|
||||
* @param callable $scope
|
||||
*/
|
||||
public static function addVisibleScope(callable $scope)
|
||||
{
|
||||
static::$visibleScopes[] = $scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include records that are visible to a user.
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param User $user
|
||||
*/
|
||||
public function scopeWhereVisibleTo(Builder $query, User $user)
|
||||
{
|
||||
foreach (static::$visibleScopes as $scope) {
|
||||
$scope($query, $user);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user