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;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
use Flarum\Core\Formatter\FormatterManager;
|
|
|
|
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-03-25 21:26:38 +08:00
|
|
|
use League\Flysystem\Adapter\Local;
|
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-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();
|
|
|
|
|
|
|
|
$bus->mapUsing(function ($command) {
|
|
|
|
return Bus::simpleMapping(
|
|
|
|
$command, 'Flarum\Core\Commands', 'Flarum\Core\Handlers\Commands'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the service provider.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
2015-03-28 13:13:31 +08:00
|
|
|
$this->app->register('Flarum\Core\Notifications\NotificationServiceProvider');
|
|
|
|
|
2015-02-24 18:03:18 +08:00
|
|
|
// 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');
|
|
|
|
|
|
|
|
$this->app->singleton('flarum.formatter', function () {
|
|
|
|
$formatter = new FormatterManager($this->app);
|
|
|
|
$formatter->add('basic', 'Flarum\Core\Formatter\BasicFormatter');
|
|
|
|
return $formatter;
|
|
|
|
});
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$this->app->when('Flarum\Core\Handlers\Commands\UploadAvatarCommandHandler')
|
|
|
|
->needs('League\Flysystem\FilesystemInterface')
|
2015-03-27 05:01:58 +08:00
|
|
|
->give(function(Container $app) {
|
2015-03-29 19:57:37 +08:00
|
|
|
return $app->make('Illuminate\Contracts\Filesystem\Factory')->disk('flarum-avatars')->getDriver();
|
2015-03-27 05:01:58 +08:00
|
|
|
});
|
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()
|
|
|
|
{
|
|
|
|
Forum::grantPermission(function ($grant, $user, $permission) {
|
|
|
|
return $user->hasPermission($permission, 'forum');
|
|
|
|
});
|
|
|
|
|
|
|
|
Post::grantPermission(function ($grant, $user, $permission) {
|
|
|
|
return $user->hasPermission($permission, 'post');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Grant view access to a post only if the user can also view the
|
|
|
|
// discussion which the post is in. Also, the if the post is hidden,
|
|
|
|
// the user must have edit permissions too.
|
|
|
|
Post::grantPermission('view', function ($grant) {
|
|
|
|
$grant->whereCan('view', 'discussion');
|
|
|
|
});
|
|
|
|
|
|
|
|
Post::demandPermission('view', function ($demand) {
|
|
|
|
$demand->whereNull('hide_user_id')
|
|
|
|
->orWhereCan('edit');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Allow a user to edit their own post, unless it has been hidden by
|
|
|
|
// someone else.
|
|
|
|
Post::grantPermission('edit', function ($grant, $user) {
|
|
|
|
$grant->whereCan('editOwn')
|
|
|
|
->where('user_id', $user->id);
|
|
|
|
});
|
|
|
|
|
|
|
|
Post::demandPermission('editOwn', function ($demand, $user) {
|
|
|
|
$demand->whereNull('hide_user_id');
|
|
|
|
if ($user) {
|
|
|
|
$demand->orWhere('hide_user_id', $user->id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
User::grantPermission(function ($grant, $user, $permission) {
|
|
|
|
return $user->hasPermission($permission, 'forum');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Grant view access to a user if the user can view the forum.
|
|
|
|
User::grantPermission('view', function ($grant, $user) {
|
|
|
|
$grant->whereCan('view', 'forum');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Allow a user to edit their own account.
|
|
|
|
User::grantPermission('edit', function ($grant, $user) {
|
|
|
|
$grant->where('id', $user->id);
|
|
|
|
});
|
|
|
|
|
|
|
|
Discussion::grantPermission(function ($grant, $user, $permission) {
|
|
|
|
return $user->hasPermission($permission, 'discussion');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Grant view access to a discussion if the user can view the forum.
|
|
|
|
Discussion::grantPermission('view', function ($grant, $user) {
|
|
|
|
$grant->whereCan('view', 'forum');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Allow a user to edit their own discussion.
|
|
|
|
Discussion::grantPermission('edit', function ($grant, $user) {
|
|
|
|
if ($user->hasPermission('editOwn', 'discussion')) {
|
|
|
|
$grant->where('start_user_id', $user->id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|