Allow char "§" for player name & Refactor

This commit is contained in:
Pig Fang 2019-12-22 11:50:39 +08:00
parent fd61380758
commit 85d0104362
9 changed files with 76 additions and 51 deletions

View File

@ -6,6 +6,7 @@ use App\Models\Player;
use App\Models\Texture;
use App\Models\User;
use App\Notifications;
use App\Rules;
use App\Services\Filter;
use App\Services\OptionForm;
use App\Services\PluginManager;
@ -691,7 +692,12 @@ class AdminController extends Controller
return json(trans('admin.players.delete.success'), 0);
} elseif ($action == 'name') {
$name = $this->validate($request, [
'name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
'name' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
])['name'];
$player->name = $name;

View File

@ -7,7 +7,7 @@ use App\Exceptions\PrettyPageException;
use App\Mail\ForgotPassword;
use App\Models\Player;
use App\Models\User;
use App\Rules\Captcha;
use App\Rules;
use Auth;
use Cache;
use Carbon\Carbon;
@ -31,7 +31,7 @@ class AuthController extends Controller
]);
}
public function handleLogin(Request $request, Captcha $captcha)
public function handleLogin(Request $request, Rules\Captcha $captcha)
{
$this->validate($request, [
'identification' => 'required',
@ -113,14 +113,19 @@ class AuthController extends Controller
}
}
public function handleRegister(Request $request, Captcha $captcha)
public function handleRegister(Request $request, Rules\Captcha $captcha)
{
if (!option('user_can_register')) {
return json(trans('auth.register.close'), 7);
}
$rule = option('register_with_player_name') ?
['player_name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max')] :
['player_name' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
]] :
['nickname' => 'required|max:255'];
$data = $this->validate($request, array_merge([
'email' => 'required|email|unique:users',
@ -187,7 +192,7 @@ class AuthController extends Controller
}
}
public function handleForgot(Request $request, Captcha $captcha)
public function handleForgot(Request $request, Rules\Captcha $captcha)
{
$this->validate($request, [
'captcha' => ['required', $captcha],

View File

@ -11,6 +11,7 @@ use App\Http\Middleware\CheckPlayerExist;
use App\Http\Middleware\CheckPlayerOwner;
use App\Models\Player;
use App\Models\Texture;
use App\Rules;
use App\Services\Filter;
use App\Services\Rejection;
use Auth;
@ -80,7 +81,12 @@ class PlayerController extends Controller
}
$name = $this->validate($request, [
'name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
'name' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
])['name'];
event(new CheckPlayerExists($name));
@ -142,7 +148,12 @@ class PlayerController extends Controller
$pid
) {
$newName = $this->validate($request, [
'name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
'name' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
])['name'];
$player = Player::find($pid);
@ -211,7 +222,12 @@ class PlayerController extends Controller
public function bind(Request $request)
{
$name = $this->validate($request, [
'player' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
'player' => [
'required',
new Rules\PlayerName(),
'min:'.option('player_name_length_min'),
'max:'.option('player_name_length_max'),
],
])['player'];
$user = Auth::user();

View File

@ -1,39 +0,0 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Validator;
class ValidatorExtendServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('player_name', function ($a, $value, $p, $v) {
$regexp = '/^(.*)$/';
switch (option('player_name_rule')) {
case 'official':
// Mojang's official username rule
$regexp = '/^([A-Za-z0-9_]+)$/';
break;
case 'cjk':
// CJK Unified Ideographs
$regexp = '/^([A-Za-z0-9_\x{4e00}-\x{9fff}]+)$/u';
break;
case 'custom':
$regexp = option('custom_player_name_regexp') ?: $regexp;
break;
}
return preg_match($regexp, $value);
});
}
}

36
app/Rules/PlayerName.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class PlayerName implements Rule
{
public function passes($attribute, $value)
{
$regexp = '/.*/';
switch (option('player_name_rule')) {
case 'official':
// Mojang's official username rule
$regexp = '/^[A-Za-z0-9_§]+$/';
break;
case 'cjk':
// CJK Unified Ideographs
$regexp = '/^[A-Za-z0-9_§\x{4e00}-\x{9fff}]+$/u';
break;
case 'custom':
$regexp = option('custom_player_name_regexp') ?: $regexp;
break;
}
return preg_match($regexp, $value);
}
public function message()
{
return trans('user.player.player-name-rule.'.option('player_name_rule'));
}
}

View File

@ -187,7 +187,6 @@ return [
App\Providers\EventServiceProvider::class,
App\Providers\PluginServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ValidatorExtendServiceProvider::class,
App\Providers\ViewServiceProvider::class,
],

View File

@ -1,6 +1,4 @@
# Blessing Skin
username: ':attribute format is invalid.'
player_name: 'The :attribute contains invalid character.'
model: 'The :attribute must be steve, alex or cape.'
accepted: 'The :attribute must be accepted.'

View File

@ -16,6 +16,7 @@
- Added badges at texture detail page.
- Added FAQ link at error page.
- Added login with 3rd-party services. (GitHub and Microsoft Live are supported currently.)
- Added support of character "§" for player name.
## Tweaked
@ -26,6 +27,7 @@
- Load front end i18n text from `lang/front-end.yml` of a plugin automatically.
- Upgraded AdminLTE to v3.
- Lengthened `ip` field in order to support IPv6.
- Optimized performance of validating player name.
## Fixed

View File

@ -16,6 +16,7 @@
- 在材质详情页中显示上传者的 badge
- 在错误页面增加指向 FAQ 页面的链接
- 第三方登录(目前仅支持 GitHub 和 Microsoft Live
- 角色名支持字符「§」
## 调整
@ -26,6 +27,7 @@
- 插件系统:自动从 `lang/front-end.yml` 中加载前端多语言
- AdminLTE 升级到 v3
- 将 `ip` 字段的长度增加到 39 以支持 IPv6
- 优化角色名校验的性能
## 修复