mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 14:13:37 +08:00
Refactor some model stuff out into traits
This commit is contained in:
parent
c067db09d1
commit
9dd5a742e5
|
@ -22,6 +22,7 @@ class Discussion extends Model
|
||||||
{
|
{
|
||||||
use EventGenerator;
|
use EventGenerator;
|
||||||
use Locked;
|
use Locked;
|
||||||
|
use VisibleScope;
|
||||||
use ValidatesBeforeSave;
|
use ValidatesBeforeSave;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -51,47 +51,6 @@ abstract class Model extends Eloquent
|
||||||
return $dates[$class];
|
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
|
* Get an attribute from the model. If nothing is found, attempt to load
|
||||||
* a custom relation method with this key.
|
* a custom relation method with this key.
|
||||||
|
|
|
@ -4,6 +4,7 @@ use DomainException;
|
||||||
use Flarum\Events\PostWasDeleted;
|
use Flarum\Events\PostWasDeleted;
|
||||||
use Flarum\Core\Model;
|
use Flarum\Core\Model;
|
||||||
use Flarum\Core\Support\Locked;
|
use Flarum\Core\Support\Locked;
|
||||||
|
use Flarum\Core\Support\VisibleScope;
|
||||||
use Flarum\Core\Support\EventGenerator;
|
use Flarum\Core\Support\EventGenerator;
|
||||||
use Flarum\Core\Support\ValidatesBeforeSave;
|
use Flarum\Core\Support\ValidatesBeforeSave;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
@ -15,6 +16,7 @@ class Post extends Model
|
||||||
{
|
{
|
||||||
use EventGenerator;
|
use EventGenerator;
|
||||||
use Locked;
|
use Locked;
|
||||||
|
use VisibleScope;
|
||||||
use ValidatesBeforeSave;
|
use ValidatesBeforeSave;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -11,5 +11,33 @@ use Illuminate\Contracts\Events\Dispatcher;
|
||||||
*/
|
*/
|
||||||
trait Locked
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
19
framework/core/src/Core/Support/VisibleScope.php
Normal file
19
framework/core/src/Core/Support/VisibleScope.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php namespace Flarum\Core\Support;
|
||||||
|
|
||||||
|
use Flarum\Events\ScopeModelVisibility;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Flarum\Core\Users\User;
|
||||||
|
|
||||||
|
trait VisibleScope
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ class User extends Model
|
||||||
use EventGenerator;
|
use EventGenerator;
|
||||||
use Locked;
|
use Locked;
|
||||||
use ValidatesBeforeSave;
|
use ValidatesBeforeSave;
|
||||||
|
use VisibleScope;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user