mirror of
https://github.com/flarum/framework.git
synced 2025-01-20 16:58:32 +08:00
Finished working version
This commit is contained in:
parent
c7f0b19cfa
commit
271f5ca922
|
@ -22,6 +22,7 @@
|
|||
},
|
||||
"flarum-extension": {
|
||||
"title": "Nicknames",
|
||||
"category": "feature",
|
||||
"icon": {
|
||||
"name": "fas fa-user-tag",
|
||||
"backgroundColor": "#8E4529",
|
||||
|
|
|
@ -11,19 +11,19 @@
|
|||
|
||||
namespace Flarum\Nicknames;
|
||||
|
||||
use Flarum\Api\Serializer\UserSerializer;
|
||||
use Flarum\Event\ConfigureUserGambits;
|
||||
use Flarum\Extend;
|
||||
use Flarum\User\Event\Saving;
|
||||
use Flarum\User\User;
|
||||
use Flarum\User\UserValidator;
|
||||
|
||||
return [
|
||||
(new Extend\Frontend('forum'))
|
||||
->js(__DIR__.'/js/dist/forum.js')
|
||||
->css(__DIR__.'/resources/less/forum.less'),
|
||||
->js(__DIR__.'/js/dist/forum.js'),
|
||||
|
||||
(new Extend\Frontend('admin'))
|
||||
->js(__DIR__.'/js/dist/admin.js')
|
||||
->css(__DIR__.'/resources/less/admin.less'),
|
||||
->js(__DIR__.'/js/dist/admin.js'),
|
||||
|
||||
(new Extend\User())
|
||||
->displayNameDriver('nickname', NicknameDriver::class),
|
||||
|
@ -35,5 +35,13 @@ return [
|
|||
->listen(Saving::class, SaveNicknameToDatabase::class)
|
||||
->listen(ConfigureUserGambits::class, SetUserNicknameGambit::class),
|
||||
|
||||
new Extend\Locales(__DIR__ . '/resources/locale'),
|
||||
(new Extend\ApiSerializer(UserSerializer::class))
|
||||
->attribute('canEditOwnNickname', function($serializer, $user) {
|
||||
$actor = $serializer->getActor();
|
||||
return $actor->id === $user->id && $serializer->getActor()->can('editOwnNickname', $user);
|
||||
}),
|
||||
|
||||
(new Extend\Validator(UserValidator::class))
|
||||
->configure(AddNicknameValidation::class),
|
||||
|
||||
];
|
||||
|
|
|
@ -1,3 +1,29 @@
|
|||
app.initializers.add('flarum/nickname', () => {
|
||||
console.log('[flarum/nickname] Hello, admin!');
|
||||
});
|
||||
app.initializers.add('flarum/nicknames', () => {
|
||||
app.extensionData
|
||||
.for('flarum-nicknames')
|
||||
.registerSetting({
|
||||
setting: 'flarum-nicknames.unique',
|
||||
type: 'boolean',
|
||||
label: app.translator.trans('flarum-nicknames.admin.settings.unique_label')
|
||||
})
|
||||
.registerSetting({
|
||||
setting: 'flarum-nicknames.regex',
|
||||
type: 'text',
|
||||
label: app.translator.trans('flarum-nicknames.admin.settings.regex_label')
|
||||
})
|
||||
.registerSetting({
|
||||
setting: 'flarum-nicknames.min',
|
||||
type: 'number',
|
||||
label: app.translator.trans('flarum-nicknames.admin.settings.min_label')
|
||||
})
|
||||
.registerSetting({
|
||||
setting: 'flarum-nicknames.max',
|
||||
type: 'number',
|
||||
label: app.translator.trans('flarum-nicknames.admin.settings.max_label')
|
||||
})
|
||||
.registerPermission({
|
||||
icon: 'fas fa-user-tag',
|
||||
label: app.translator.trans('flarum-nicknames.admin.permissions.edit_own_nickname_label'),
|
||||
permission: 'user.editOwnNickname'
|
||||
}, 'start')
|
||||
});
|
||||
|
|
|
@ -13,7 +13,7 @@ export default class NicknameModal extends Modal {
|
|||
}
|
||||
|
||||
title() {
|
||||
return app.translator.trans('flarum-nicknames.forum.nickname.change');
|
||||
return app.translator.trans('flarum-nicknames.forum.change_nickname.title');
|
||||
}
|
||||
|
||||
content() {
|
||||
|
@ -34,7 +34,7 @@ export default class NicknameModal extends Modal {
|
|||
className: 'Button Button--primary Button--block',
|
||||
type: 'submit',
|
||||
loading: this.loading,
|
||||
}, app.translator.trans('flarum-nicknames.forum.nickname.submit_button'))}
|
||||
}, app.translator.trans('flarum-nicknames.forum.change_nickname.submit_button'))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -2,17 +2,23 @@ import { extend } from 'flarum/extend';
|
|||
import Button from 'flarum/components/Button';
|
||||
import EditUserModal from 'flarum/components/EditUserModal';
|
||||
import SettingsPage from 'flarum/components/SettingsPage';
|
||||
import Model from 'flarum/Model';
|
||||
import User from 'flarum/models/User';
|
||||
import extractText from 'flarum/utils/extractText';
|
||||
import Stream from 'flarum/utils/Stream';
|
||||
import NickNameModal from './components/NicknameModal';
|
||||
|
||||
app.initializers.add('flarum/nicknames', () => {
|
||||
User.prototype.canEditOwnNickname = Model.attribute('canEditOwnNickname');
|
||||
|
||||
extend(SettingsPage.prototype, 'accountItems', function (items) {
|
||||
items.add('changeNickname',
|
||||
<Button className="Button" onclick={() => app.modal.show(NickNameModal)}>
|
||||
{app.translator.trans('flarum-nicknames.forum.settings.change_nickname_button')}
|
||||
</Button>
|
||||
);
|
||||
if (this.user.canEditOwnNickname()) {
|
||||
items.add('changeNickname',
|
||||
<Button className="Button" onclick={() => app.modal.show(NickNameModal)}>
|
||||
{app.translator.trans('flarum-nicknames.forum.settings.change_nickname_button')}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
extend(EditUserModal.prototype, 'oninit', function () {
|
||||
|
@ -22,9 +28,9 @@ app.initializers.add('flarum/nicknames', () => {
|
|||
extend(EditUserModal.prototype, 'fields', function (items) {
|
||||
items.add('nickname',
|
||||
<div className="Form-group">
|
||||
<label>{app.translator.trans('flarum-nicknames.forum.edit_user.password_heading')}</label>
|
||||
<label>{app.translator.trans('flarum-nicknames.forum.edit_user.nicknames_heading')}</label>
|
||||
<input className="FormControl"
|
||||
placeholder={extractText(app.translator.trans('flarum-nicknames.forum.edit_user.password_text'))}
|
||||
placeholder={extractText(app.translator.trans('flarum-nicknames.forum.edit_user.nicknames_text'))}
|
||||
bidi={this.nickname} />
|
||||
</div>, 100);
|
||||
});
|
||||
|
|
40
extensions/nicknames/src/AddNicknameValidation.php
Normal file
40
extensions/nicknames/src/AddNicknameValidation.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Flarum\Nicknames;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
class AddNicknameValidation
|
||||
{
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
|
||||
public function __construct(SettingsRepositoryInterface $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function __invoke($flarumValidator, Validator $validator)
|
||||
{
|
||||
$unique_nickname = ($this->settings->get('flarum-nicknames.unique')) ? 'unique:users,nickname' : '';
|
||||
|
||||
$validator->setRules([
|
||||
'nickname' => [
|
||||
$unique_nickname,
|
||||
function ($attribute, $value, $fail) {
|
||||
$regex = $this->settings->get('flarum-nicknames.regex');
|
||||
if ($regex && !preg_match_all("/$regex/", $value)) {
|
||||
$fail(app('translator')->trans('flarum-nicknames.api.invalid_nickname_message'));
|
||||
}
|
||||
},
|
||||
'min:' . $this->settings->get('flarum-nicknames.min', 1),
|
||||
'max:' . $this->settings->get('flarum-nicknames.max', 150),
|
||||
],
|
||||
] + $validator->getRules());
|
||||
}
|
||||
}
|
|
@ -6,8 +6,18 @@ namespace Flarum\Nicknames;
|
|||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Http\SlugDriverInterface;
|
||||
use Flarum\User\User;
|
||||
use Flarum\User\UserRepository;
|
||||
|
||||
class IdOnlyUserSlugDriver implements SlugDriverInterface {
|
||||
/**
|
||||
* @var $users UserRepository
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
public function toSlug(AbstractModel $instance): string
|
||||
{
|
||||
|
@ -16,6 +26,6 @@ class IdOnlyUserSlugDriver implements SlugDriverInterface {
|
|||
|
||||
public function fromSlug(string $slug, User $actor): AbstractModel
|
||||
{
|
||||
return User::where('id', $slug)->whereVisibleTo($actor)->firstOrFail();
|
||||
return $this->users->findOrFail($slug, $actor);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,15 +13,15 @@ class SaveNicknameToDatabase {
|
|||
$actor = $event->actor;
|
||||
|
||||
$isSelf = $actor->id === $user->id;
|
||||
$canEdit = $actor->can('edit', $user);
|
||||
$attributes = Arr::get($data, 'attributes', []);
|
||||
|
||||
if (isset($attributes['nickname'])) {
|
||||
if (!$isSelf) {
|
||||
$actor->assertPermission($canEdit);
|
||||
if ($isSelf) {
|
||||
$actor->assertCan('editOwnNickname', $user);
|
||||
} else {
|
||||
$actor->assertCan('edit', $user);
|
||||
}
|
||||
$user->nickname = $attributes['nickname'];
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user