2015-02-24 18:03:18 +08:00
|
|
|
<?php namespace Flarum\Core;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Dispatcher as Bus;
|
2015-03-27 05:01:58 +08:00
|
|
|
use Illuminate\Contracts\Container\Container;
|
2015-02-24 18:03:18 +08:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2015-05-15 15:35:46 +08:00
|
|
|
use Flarum\Support\ServiceProvider;
|
2015-02-24 18:03:18 +08:00
|
|
|
use Flarum\Core\Models\CommentPost;
|
|
|
|
use Flarum\Core\Models\Post;
|
|
|
|
use Flarum\Core\Models\Model;
|
|
|
|
use Flarum\Core\Models\Forum;
|
|
|
|
use Flarum\Core\Models\User;
|
|
|
|
use Flarum\Core\Models\Discussion;
|
|
|
|
use Flarum\Core\Search\GambitManager;
|
2015-05-02 06:42:30 +08:00
|
|
|
use Flarum\Core\Events\RegisterDiscussionGambits;
|
2015-05-03 10:36:01 +08:00
|
|
|
use Flarum\Core\Events\RegisterUserGambits;
|
2015-05-20 11:00:27 +08:00
|
|
|
use Flarum\Extend\ActivityType;
|
2015-05-20 10:54:01 +08:00
|
|
|
use Flarum\Extend\NotificationType;
|
2015-06-10 12:53:56 +08:00
|
|
|
use Flarum\Extend\Locale;
|
2015-02-24 18:03:18 +08:00
|
|
|
|
|
|
|
class CoreServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap the application events.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot(Dispatcher $events, Bus $bus)
|
|
|
|
{
|
2015-02-25 13:04:02 +08:00
|
|
|
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum');
|
2015-02-24 18:03:18 +08:00
|
|
|
|
|
|
|
$this->registerEventHandlers($events);
|
|
|
|
$this->registerPostTypes();
|
|
|
|
$this->registerPermissions();
|
|
|
|
$this->registerGambits();
|
|
|
|
$this->setupModels();
|
|
|
|
|
2015-05-11 10:41:19 +08:00
|
|
|
$this->app['flarum.formatter']->add('linkify', 'Flarum\Core\Formatter\LinkifyFormatter');
|
|
|
|
|
2015-02-24 18:03:18 +08:00
|
|
|
$bus->mapUsing(function ($command) {
|
|
|
|
return Bus::simpleMapping(
|
2015-05-19 07:07:22 +08:00
|
|
|
$command,
|
|
|
|
'Flarum\Core\Commands',
|
|
|
|
'Flarum\Core\Handlers\Commands'
|
2015-02-24 18:03:18 +08:00
|
|
|
);
|
|
|
|
});
|
2015-05-20 10:54:01 +08:00
|
|
|
|
|
|
|
$events->subscribe('Flarum\Core\Handlers\Events\DiscussionRenamedNotifier');
|
2015-05-20 11:00:27 +08:00
|
|
|
$events->subscribe('Flarum\Core\Handlers\Events\UserActivitySyncer');
|
|
|
|
|
2015-05-20 10:54:01 +08:00
|
|
|
$this->extend(
|
|
|
|
(new NotificationType('Flarum\Core\Notifications\DiscussionRenamedNotification', 'Flarum\Api\Serializers\DiscussionBasicSerializer'))
|
|
|
|
->enableByDefault('alert'),
|
2015-05-20 11:00:27 +08:00
|
|
|
(new ActivityType('Flarum\Core\Activity\PostedActivity', 'Flarum\Api\Serializers\PostBasicSerializer')),
|
|
|
|
(new ActivityType('Flarum\Core\Activity\StartedDiscussionActivity', 'Flarum\Api\Serializers\PostBasicSerializer')),
|
|
|
|
(new ActivityType('Flarum\Core\Activity\JoinedActivity', 'Flarum\Api\Serializers\UserBasicSerializer'))
|
2015-05-20 10:54:01 +08:00
|
|
|
);
|
2015-06-10 12:53:56 +08:00
|
|
|
|
|
|
|
foreach (['en'] as $locale) {
|
|
|
|
$dir = __DIR__.'/../../locale/'.$locale;
|
|
|
|
|
|
|
|
$this->extend(
|
|
|
|
(new Locale($locale))
|
|
|
|
->translations($dir.'/translations.yml')
|
|
|
|
->config($dir.'/config.php')
|
|
|
|
->js($dir.'/config.js')
|
|
|
|
);
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the service provider.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
// Register a singleton entity that represents this forum. This entity
|
|
|
|
// will be used to check for global forum permissions (like viewing the
|
|
|
|
// forum, registering, and starting discussions.)
|
|
|
|
$this->app->singleton('flarum.forum', 'Flarum\Core\Models\Forum');
|
|
|
|
|
2015-05-11 10:41:19 +08:00
|
|
|
$this->app->singleton('flarum.formatter', 'Flarum\Core\Formatter\FormatterManager');
|
2015-02-24 18:03:18 +08:00
|
|
|
|
2015-06-10 12:53:56 +08:00
|
|
|
$this->app->singleton('flarum.localeManager', 'Flarum\Locale\LocaleManager');
|
|
|
|
|
2015-06-17 11:17:49 +08:00
|
|
|
$this->app->singleton('Flarum\Support\Actor');
|
|
|
|
|
2015-02-24 18:03:18 +08:00
|
|
|
$this->app->bind(
|
|
|
|
'Flarum\Core\Repositories\DiscussionRepositoryInterface',
|
|
|
|
'Flarum\Core\Repositories\EloquentDiscussionRepository'
|
|
|
|
);
|
|
|
|
$this->app->bind(
|
|
|
|
'Flarum\Core\Repositories\PostRepositoryInterface',
|
|
|
|
'Flarum\Core\Repositories\EloquentPostRepository'
|
|
|
|
);
|
|
|
|
$this->app->bind(
|
|
|
|
'Flarum\Core\Repositories\UserRepositoryInterface',
|
|
|
|
'Flarum\Core\Repositories\EloquentUserRepository'
|
|
|
|
);
|
2015-03-17 14:36:12 +08:00
|
|
|
$this->app->bind(
|
|
|
|
'Flarum\Core\Repositories\ActivityRepositoryInterface',
|
|
|
|
'Flarum\Core\Repositories\EloquentActivityRepository'
|
|
|
|
);
|
2015-03-25 21:26:38 +08:00
|
|
|
|
2015-06-04 09:41:56 +08:00
|
|
|
$this->app->bind(
|
|
|
|
'Flarum\Core\Search\Discussions\Fulltext\DriverInterface',
|
|
|
|
'Flarum\Core\Search\Discussions\Fulltext\MySqlFulltextDriver'
|
|
|
|
);
|
|
|
|
|
2015-05-19 07:56:39 +08:00
|
|
|
$avatarFilesystem = function (Container $app) {
|
|
|
|
return $app->make('Illuminate\Contracts\Filesystem\Factory')->disk('flarum-avatars')->getDriver();
|
|
|
|
};
|
|
|
|
|
2015-03-25 21:26:38 +08:00
|
|
|
$this->app->when('Flarum\Core\Handlers\Commands\UploadAvatarCommandHandler')
|
|
|
|
->needs('League\Flysystem\FilesystemInterface')
|
2015-05-19 07:56:39 +08:00
|
|
|
->give($avatarFilesystem);
|
|
|
|
|
|
|
|
$this->app->when('Flarum\Core\Handlers\Commands\DeleteAvatarCommandHandler')
|
|
|
|
->needs('League\Flysystem\FilesystemInterface')
|
|
|
|
->give($avatarFilesystem);
|
2015-05-20 10:54:01 +08:00
|
|
|
|
|
|
|
$this->app->bind(
|
|
|
|
'Flarum\Core\Repositories\NotificationRepositoryInterface',
|
|
|
|
'Flarum\Core\Repositories\EloquentNotificationRepository'
|
|
|
|
);
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function registerGambits()
|
|
|
|
{
|
2015-03-12 08:07:02 +08:00
|
|
|
$this->app->when('Flarum\Core\Search\Discussions\DiscussionSearcher')
|
|
|
|
->needs('Flarum\Core\Search\GambitManager')
|
|
|
|
->give(function () {
|
|
|
|
$gambits = new GambitManager($this->app);
|
|
|
|
$gambits->add('Flarum\Core\Search\Discussions\Gambits\AuthorGambit');
|
|
|
|
$gambits->add('Flarum\Core\Search\Discussions\Gambits\UnreadGambit');
|
|
|
|
$gambits->setFulltextGambit('Flarum\Core\Search\Discussions\Gambits\FulltextGambit');
|
2015-05-02 06:42:30 +08:00
|
|
|
|
|
|
|
event(new RegisterDiscussionGambits($gambits));
|
|
|
|
|
2015-03-12 08:07:02 +08:00
|
|
|
return $gambits;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->app->when('Flarum\Core\Search\Users\UserSearcher')
|
|
|
|
->needs('Flarum\Core\Search\GambitManager')
|
|
|
|
->give(function () {
|
|
|
|
$gambits = new GambitManager($this->app);
|
|
|
|
$gambits->setFulltextGambit('Flarum\Core\Search\Users\Gambits\FulltextGambit');
|
2015-05-03 10:36:01 +08:00
|
|
|
|
|
|
|
event(new RegisterUserGambits($gambits));
|
|
|
|
|
2015-03-12 08:07:02 +08:00
|
|
|
return $gambits;
|
|
|
|
});
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function registerPostTypes()
|
|
|
|
{
|
2015-05-05 12:58:40 +08:00
|
|
|
Post::addType('Flarum\Core\Models\CommentPost');
|
|
|
|
Post::addType('Flarum\Core\Models\DiscussionRenamedPost');
|
2015-02-24 18:03:18 +08:00
|
|
|
|
|
|
|
CommentPost::setFormatter($this->app['flarum.formatter']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerEventHandlers($events)
|
|
|
|
{
|
|
|
|
$events->subscribe('Flarum\Core\Handlers\Events\DiscussionMetadataUpdater');
|
|
|
|
$events->subscribe('Flarum\Core\Handlers\Events\UserMetadataUpdater');
|
|
|
|
$events->subscribe('Flarum\Core\Handlers\Events\EmailConfirmationMailer');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setupModels()
|
|
|
|
{
|
|
|
|
Model::setForum($this->app['flarum.forum']);
|
|
|
|
Model::setValidator($this->app['validator']);
|
|
|
|
|
|
|
|
User::setHasher($this->app['hash']);
|
2015-03-12 08:08:18 +08:00
|
|
|
User::setFormatter($this->app['flarum.formatter']);
|
2015-03-28 09:20:02 +08:00
|
|
|
|
|
|
|
User::registerPreference('discloseOnline', 'boolval', true);
|
|
|
|
User::registerPreference('indexProfile', 'boolval', true);
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function registerPermissions()
|
|
|
|
{
|
2015-06-16 16:03:56 +08:00
|
|
|
Forum::allow('*', function ($forum, $user, $action) {
|
|
|
|
if ($user->hasPermission('forum.'.$action)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
Post::allow('*', function ($post, $user, $action) {
|
2015-06-18 11:08:24 +08:00
|
|
|
if ($post->discussion->can($user, $action.'Posts')) {
|
2015-06-16 16:03:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
// 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::scopeVisiblePosts(function ($query, User $user, Discussion $discussion) {
|
|
|
|
if (! $discussion->can($user, 'editPosts')) {
|
|
|
|
$query->where(function ($query) use ($user) {
|
2015-05-21 14:23:59 +08:00
|
|
|
$query->whereNull('hide_user_id')
|
|
|
|
->orWhere('hide_user_id', $user->id);
|
2015-06-16 16:03:56 +08:00
|
|
|
});
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
Post::allow('view', function ($post, $user) {
|
|
|
|
if (! $post->hide_user_id || $post->can($user, 'edit')) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
User::allow('*', function ($discussion, $user, $action) {
|
|
|
|
if ($user->hasPermission('user.'.$action)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
User::allow(['edit', 'delete'], function ($user, $actor) {
|
|
|
|
if ($user->id == $actor->id) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-06-16 16:03:56 +08:00
|
|
|
Discussion::allow('*', function ($discussion, $user, $action) {
|
|
|
|
if ($user->hasPermission('discussion.'.$action)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
|
|
|
|
2015-05-15 15:35:46 +08:00
|
|
|
// Allow a user to rename their own discussion.
|
2015-06-16 16:03:56 +08:00
|
|
|
Discussion::allow('rename', function ($discussion, $user) {
|
|
|
|
if ($discussion->start_user_id == $user->id) {
|
|
|
|
return true;
|
|
|
|
// @todo add limitations to time etc. according to a config setting
|
|
|
|
}
|
2015-02-24 18:03:18 +08:00
|
|
|
});
|
2015-06-24 07:43:54 +08:00
|
|
|
|
|
|
|
Discussion::allow('delete', function ($discussion, $user) {
|
|
|
|
if ($discussion->start_user_id == $user->id && $discussion->participants_count == 1) {
|
|
|
|
return true;
|
|
|
|
// @todo add limitations to time etc. according to a config setting
|
|
|
|
}
|
|
|
|
});
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
}
|