Add user id slug driver (#2787)

This commit is contained in:
Sami Mazouz 2021-04-20 23:52:53 +01:00 committed by GitHub
parent 1f2411e15e
commit b62debf031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -13,6 +13,7 @@ use Flarum\Discussion\Discussion;
use Flarum\Discussion\IdWithTransliteratedSlugDriver;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\IdSlugDriver;
use Flarum\User\User;
use Flarum\User\UsernameSlugDriver;
use Illuminate\Support\Arr;
@ -38,7 +39,8 @@ class HttpServiceProvider extends AbstractServiceProvider
'default' => IdWithTransliteratedSlugDriver::class
],
User::class => [
'default' => UsernameSlugDriver::class
'default' => UsernameSlugDriver::class,
'id' => IdSlugDriver::class
],
];
});

36
src/User/IdSlugDriver.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User;
use Flarum\Database\AbstractModel;
use Flarum\Http\SlugDriverInterface;
class IdSlugDriver implements SlugDriverInterface
{
/**
* @var UserRepository
*/
protected $users;
public function __construct(UserRepository $users)
{
$this->users = $users;
}
public function toSlug(AbstractModel $instance): string
{
return $instance->id;
}
public function fromSlug(string $slug, User $actor): AbstractModel
{
return $this->users->findOrFail($slug, $actor);
}
}