mirror of
https://github.com/flarum/framework.git
synced 2024-11-26 02:10:09 +08:00
User Preferences Extender and Tests (#2463)
This commit is contained in:
parent
6618a7f612
commit
073acdf7fb
|
@ -11,6 +11,9 @@ namespace Flarum\Event;
|
|||
|
||||
use Flarum\User\User;
|
||||
|
||||
/**
|
||||
* @deprecated beta 15, removed beta 16
|
||||
*/
|
||||
class ConfigureUserPreferences
|
||||
{
|
||||
public function add($key, callable $transformer = null, $default = null)
|
||||
|
|
|
@ -10,12 +10,14 @@
|
|||
namespace Flarum\Extend;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
use Flarum\User\User as FlarumUser;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class User implements ExtenderInterface
|
||||
{
|
||||
private $displayNameDrivers = [];
|
||||
private $groupProcessors = [];
|
||||
private $preferences = [];
|
||||
|
||||
/**
|
||||
* Add a display name driver.
|
||||
|
@ -51,6 +53,20 @@ class User implements ExtenderInterface
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user preference.
|
||||
*
|
||||
* @param string $key
|
||||
* @param callable $transformer
|
||||
* @param $default
|
||||
*/
|
||||
public function registerPreference(string $key, callable $transformer = null, $default = null)
|
||||
{
|
||||
$this->preferences[$key] = compact('transformer', 'default');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container, Extension $extension = null)
|
||||
{
|
||||
$container->extend('flarum.user.display_name.supported_drivers', function ($existingDrivers) {
|
||||
|
@ -60,5 +76,9 @@ class User implements ExtenderInterface
|
|||
$container->extend('flarum.user.group_processors', function ($existingRelations) {
|
||||
return array_merge($existingRelations, $this->groupProcessors);
|
||||
});
|
||||
|
||||
foreach ($this->preferences as $key => $preference) {
|
||||
FlarumUser::registerPreference($key, $preference['transformer'], $preference['default']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ class AlertNotificationDriver implements NotificationDriverInterface
|
|||
*/
|
||||
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
|
||||
{
|
||||
User::addPreference(
|
||||
User::registerPreference(
|
||||
User::getNotificationPreferenceKey($blueprintClass::getType(), 'alert'),
|
||||
'boolval',
|
||||
in_array('alert', $driversEnabledByDefault)
|
||||
|
|
|
@ -59,7 +59,7 @@ class EmailNotificationDriver implements NotificationDriverInterface
|
|||
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
|
||||
{
|
||||
if ((new ReflectionClass($blueprintClass))->implementsInterface(MailableInterface::class)) {
|
||||
User::addPreference(
|
||||
User::registerPreference(
|
||||
User::getNotificationPreferenceKey($blueprintClass::getType(), 'email'),
|
||||
'boolval',
|
||||
in_array('email', $driversEnabledByDefault)
|
||||
|
|
|
@ -143,6 +143,9 @@ class User extends AbstractModel
|
|||
Notification::whereSubject($user)->delete();
|
||||
});
|
||||
|
||||
/**
|
||||
* @deprecated beta 15, remove beta 16
|
||||
*/
|
||||
static::$dispatcher->dispatch(
|
||||
new ConfigureUserPreferences
|
||||
);
|
||||
|
@ -801,6 +804,8 @@ class User extends AbstractModel
|
|||
}
|
||||
|
||||
/**
|
||||
* @deprecated beta 15, remove beta 16. Use `registerPreference` instead.
|
||||
*
|
||||
* Register a preference with a transformer and a default value.
|
||||
*
|
||||
* @param string $key
|
||||
|
@ -808,6 +813,18 @@ class User extends AbstractModel
|
|||
* @param mixed $default
|
||||
*/
|
||||
public static function addPreference($key, callable $transformer = null, $default = null)
|
||||
{
|
||||
return static::registerPreference($key, $transformer, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a preference with a transformer and a default value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param callable $transformer
|
||||
* @param mixed $default
|
||||
*/
|
||||
public static function registerPreference($key, callable $transformer = null, $default = null)
|
||||
{
|
||||
static::$preferences[$key] = compact('transformer', 'default');
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
namespace Flarum\User;
|
||||
|
||||
use Flarum\Event\ConfigureUserPreferences;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Foundation\ContainerUtil;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
|
@ -94,16 +93,8 @@ class UserServiceProvider extends AbstractServiceProvider
|
|||
$events->subscribe(UserMetadataUpdater::class);
|
||||
$events->subscribe(UserPolicy::class);
|
||||
|
||||
$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');
|
||||
User::registerPreference('discloseOnline', 'boolval', true);
|
||||
User::registerPreference('indexProfile', 'boolval', true);
|
||||
User::registerPreference('locale');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
|||
use Flarum\Tests\integration\TestCase;
|
||||
use Flarum\User\DisplayName\DriverInterface;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class UserTest extends TestCase
|
||||
{
|
||||
|
@ -35,6 +36,14 @@ class UserTest extends TestCase
|
|||
]);
|
||||
}
|
||||
|
||||
protected function registerTestPreference()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\User())
|
||||
->registerPreference('test', 'boolval', true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
@ -104,6 +113,51 @@ class UserTest extends TestCase
|
|||
|
||||
$this->assertNotContains('viewUserList', $user->getPermissions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function can_add_user_preference()
|
||||
{
|
||||
$this->registerTestPreference();
|
||||
$this->prepDb();
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find(2);
|
||||
$this->assertEquals(true, Arr::get($user->preferences, 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function can_store_user_preference()
|
||||
{
|
||||
$this->registerTestPreference();
|
||||
$this->prepDb();
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find(2);
|
||||
|
||||
$user->setPreference('test', false);
|
||||
|
||||
$this->assertEquals(false, $user->getPreference('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function storing_user_preference_modified_by_transformer()
|
||||
{
|
||||
$this->registerTestPreference();
|
||||
$this->prepDb();
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find(2);
|
||||
|
||||
$user->setPreference('test', []);
|
||||
|
||||
$this->assertEquals(false, $user->getPreference('test'));
|
||||
}
|
||||
}
|
||||
|
||||
class CustomDisplayNameDriver implements DriverInterface
|
||||
|
|
Loading…
Reference in New Issue
Block a user