1
0
mirror of https://github.com/flarum/core.git synced 2025-10-21 11:46:05 +02:00

Rework public API based on events

This commit is contained in:
Toby Zerner
2015-07-18 22:59:47 +09:30
parent 5085c09c30
commit 57650fa648
136 changed files with 1157 additions and 1245 deletions

View File

@@ -1,7 +1,7 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Posts\PostRepository;
use Flarum\Core\Posts\Events\PostWillBeDeleted;
use Flarum\Events\PostWillBeDeleted;
use Flarum\Core\Support\DispatchesEvents;
class DeletePostHandler

View File

@@ -1,7 +1,7 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Posts\PostRepository;
use Flarum\Core\Posts\Events\PostWillBeSaved;
use Flarum\Events\PostWillBeSaved;
use Flarum\Core\Support\DispatchesEvents;
use Flarum\Core\Posts\CommentPost;

View File

@@ -1,6 +1,6 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Posts\Events\PostWillBeSaved;
use Flarum\Events\PostWillBeSaved;
use Flarum\Core\Discussions\DiscussionRepository;
use Flarum\Core\Posts\CommentPost;
use Flarum\Core\Support\DispatchesEvents;

View File

@@ -2,10 +2,10 @@
use DomainException;
use Flarum\Core\Formatter\FormatterManager;
use Flarum\Core\Posts\Events\PostWasPosted;
use Flarum\Core\Posts\Events\PostWasRevised;
use Flarum\Core\Posts\Events\PostWasHidden;
use Flarum\Core\Posts\Events\PostWasRestored;
use Flarum\Events\PostWasPosted;
use Flarum\Events\PostWasRevised;
use Flarum\Events\PostWasHidden;
use Flarum\Events\PostWasRestored;
use Flarum\Core\Users\User;
/**

View File

@@ -1,21 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
class PostWasDeleted
{
/**
* The post that was deleted.
*
* @var Post
*/
public $post;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
}

View File

@@ -1,21 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\CommentPost;
class PostWasHidden
{
/**
* The post that was hidden.
*
* @var CommentPost
*/
public $post;
/**
* @param CommentPost $post
*/
public function __construct(CommentPost $post)
{
$this->post = $post;
}
}

View File

@@ -1,21 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
class PostWasPosted
{
/**
* The post that was posted.
*
* @var Post
*/
public $post;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
}

View File

@@ -1,21 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\CommentPost;
class PostWasRestored
{
/**
* The post that was restored.
*
* @var CommentPost
*/
public $post;
/**
* @param CommentPost $post The post that was restored.
*/
public function __construct(CommentPost $post)
{
$this->post = $post;
}
}

View File

@@ -1,21 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\CommentPost;
class PostWasRevised
{
/**
* The post that was revised.
*
* @var CommentPost
*/
public $post;
/**
* @param CommentPost $post The post that was revised.
*/
public function __construct(CommentPost $post)
{
$this->post = $post;
}
}

View File

@@ -1,40 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class PostWillBeDeleted
{
/**
* The post that is going to be deleted.
*
* @var Post
*/
public $post;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* Any user input associated with the command.
*
* @var array
*/
public $data;
/**
* @param Post $post
* @param User $actor
* @param array $data
*/
public function __construct(Post $post, User $actor, array $data)
{
$this->post = $post;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -1,40 +0,0 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class PostWillBeSaved
{
/**
* The post that will be saved.
*
* @var Post
*/
public $post;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to update on the post.
*
* @var array
*/
public $data;
/**
* @param Post $post
* @param User $actor
* @param array $data
*/
public function __construct(Post $post, User $actor, array $data = [])
{
$this->post = $post;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -1,7 +1,7 @@
<?php namespace Flarum\Core\Posts;
use DomainException;
use Flarum\Core\Posts\Events\PostWasDeleted;
use Flarum\Events\PostWasDeleted;
use Flarum\Core\Model;
use Flarum\Core\Support\Locked;
use Flarum\Core\Support\EventGenerator;
@@ -42,7 +42,7 @@ class Post extends Model
/**
* {@inheritdoc}
*/
protected static $dateAttributes = ['time', 'edit_time', 'hide_time'];
protected $dates = ['time', 'edit_time', 'hide_time'];
/**
* A map of post types, as specified in the `type` column, to their
@@ -161,6 +161,7 @@ class Post extends Model
&& isset(static::$models[$attributes['type']])
&& class_exists($class = static::$models[$attributes['type']])
) {
/** @var Post $instance */
$instance = new $class;
$instance->exists = true;
$instance->setRawAttributes($attributes, true);

View File

@@ -2,6 +2,9 @@
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
use Flarum\Events\ModelAllow;
use Flarum\Events\RegisterPostTypes;
use Flarum\Events\ScopePostVisibility;
use Flarum\Support\ServiceProvider;
use Flarum\Extend;
@@ -14,45 +17,73 @@ class PostsServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->extend([
new Extend\PostType('Flarum\Core\Posts\CommentPost'),
new Extend\PostType('Flarum\Core\Posts\DiscussionRenamedPost')
]);
Post::setValidator($this->app->make('validator'));
CommentPost::setFormatter($this->app->make('flarum.formatter'));
Post::allow('*', function ($post, $user, $action) {
return $post->discussion->can($user, $action.'Posts') ?: null;
$this->registerPostTypes();
$events = $this->app->make('events');
$events->listen(ModelAllow::class, function (ModelAllow $event) {
if ($event->model instanceof Post) {
$post = $event->model;
$action = $event->action;
$actor = $event->actor;
if ($action === 'view' &&
(! $post->hide_user_id || $post->can($actor, 'edit'))) {
return true;
}
// A post is allowed to be edited if the user has permission to moderate
// the discussion which it's in, or if they are the author and the post
// hasn't been deleted by someone else.
if ($action === 'edit' &&
($post->discussion->can($actor, 'editPosts') ||
($post->user_id == $actor->id &&
(! $post->hide_user_id || $post->hide_user_id == $actor->id)))) {
return true;
}
if ($post->discussion->can($actor, $action.'Posts')) {
return true;
}
}
});
// When fetching a discussion's posts: if the user doesn't have permission
// to moderate the discussion, then they can't see posts that have been
// hidden by someone other than themself.
Discussion::addPostVisibilityScope(function ($query, User $user, Discussion $discussion) {
if (! $discussion->can($user, 'editPosts')) {
$query->where(function ($query) use ($user) {
$events->listen(ScopePostVisibility::class, function (ScopePostVisibility $event) {
$user = $event->actor;
if (! $event->discussion->can($user, 'editPosts')) {
$event->query->where(function ($query) use ($user) {
$query->whereNull('hide_user_id')
->orWhere('hide_user_id', $user->id);
->orWhere('hide_user_id', $user->id);
});
}
});
}
Post::allow('view', function ($post, $user) {
return ! $post->hide_user_id || $post->can($user, 'edit') ?: null;
});
/**
* Register post types.
*
* @return void
*/
public function registerPostTypes()
{
$models = [
'Flarum\Core\Posts\CommentPost',
'Flarum\Core\Posts\DiscussionRenamedPost'
];
// A post is allowed to be edited if the user has permission to moderate
// the discussion which it's in, or if they are the author and the post
// hasn't been deleted by someone else.
Post::allow('edit', function ($post, $user) {
if ($post->discussion->can($user, 'editPosts') ||
($post->user_id == $user->id && (! $post->hide_user_id || $post->hide_user_id == $user->id))
) {
return true;
}
});
event(new RegisterPostTypes($models));
foreach ($models as $model) {
Post::setModel($model::$type, $model);
}
}
/**