mirror of
https://github.com/flarum/core.git
synced 2025-07-28 20:20:34 +02:00
Split up old CoreServiceProvider
This commit is contained in:
106
src/User/UserServiceProvider.php
Normal file
106
src/User/UserServiceProvider.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\User;
|
||||
|
||||
use Flarum\Event\ConfigureUserPreferences;
|
||||
use Flarum\Event\GetPermission;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class UserServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('flarum.gate', function ($app) {
|
||||
return new Gate($app, function () {
|
||||
throw new RuntimeException('You must set the gate user with forUser()');
|
||||
});
|
||||
});
|
||||
|
||||
$this->app->alias('flarum.gate', 'Illuminate\Contracts\Auth\Access\Gate');
|
||||
$this->app->alias('flarum.gate', 'Flarum\User\Gate');
|
||||
|
||||
$this->registerAvatarsFilesystem();
|
||||
}
|
||||
|
||||
protected function registerAvatarsFilesystem()
|
||||
{
|
||||
$avatarsFilesystem = function (Container $app) {
|
||||
return $app->make('Illuminate\Contracts\Filesystem\Factory')->disk('flarum-avatars')->getDriver();
|
||||
};
|
||||
|
||||
$this->app->when('Flarum\Core\Command\UploadAvatarHandler')
|
||||
->needs('League\Flysystem\FilesystemInterface')
|
||||
->give($avatarsFilesystem);
|
||||
|
||||
$this->app->when('Flarum\Core\Command\DeleteAvatarHandler')
|
||||
->needs('League\Flysystem\FilesystemInterface')
|
||||
->give($avatarsFilesystem);
|
||||
|
||||
$this->app->when('Flarum\Core\Command\RegisterUserHandler')
|
||||
->needs('League\Flysystem\FilesystemInterface')
|
||||
->give($avatarsFilesystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->app->make('flarum.gate')->before(function (User $actor, $ability, $model = null) {
|
||||
// Fire an event so that core and extension policies can hook into
|
||||
// this permission query and explicitly grant or deny the
|
||||
// permission.
|
||||
$allowed = $this->app->make('events')->until(
|
||||
new GetPermission($actor, $ability, $model)
|
||||
);
|
||||
|
||||
if (! is_null($allowed)) {
|
||||
return $allowed;
|
||||
}
|
||||
|
||||
// If no policy covered this permission query, we will only grant
|
||||
// the permission if the actor's groups have it. Otherwise, we will
|
||||
// not allow the user to perform this action.
|
||||
if ($actor->isAdmin() || (! $model && $actor->hasPermission($ability))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
User::setHasher($this->app->make('hash'));
|
||||
User::setGate($this->app->make('flarum.gate'));
|
||||
|
||||
$events = $this->app->make('events');
|
||||
|
||||
$events->subscribe('Flarum\Core\Listener\SelfDemotionGuard');
|
||||
$events->subscribe('Flarum\User\EmailConfirmationMailer');
|
||||
$events->subscribe('Flarum\User\UserMetadataUpdater');
|
||||
$events->subscribe('Flarum\User\UserPolicy');
|
||||
|
||||
$events->listen(ConfigureUserPreferences::class, [$this, 'configureUserPreferences']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureUserPreferences $event
|
||||
*/
|
||||
public function configureUserPreferences(ConfigureUserPreferences $event)
|
||||
{
|
||||
$event->add('discloseOnline', 'boolval', true);
|
||||
$event->add('indexProfile', 'boolval', true);
|
||||
$event->add('locale');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user