mirror of
https://github.com/flarum/core.git
synced 2025-10-20 19:27:14 +02:00
Refactor some model stuff out into traits
This commit is contained in:
@@ -11,5 +11,33 @@ use Illuminate\Contracts\Events\Dispatcher;
|
||||
*/
|
||||
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
src/Core/Support/VisibleScope.php
Normal file
19
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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user