1
0
mirror of https://github.com/flarum/core.git synced 2025-08-08 17:36:38 +02:00

Prompt for nickname on registration (#4)

Allow users to set a nickname while registering, controlled by settings. Also, add a setting to hide the username input entirely and randomly generate the username.

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
Alexander Skvortsov
2021-11-17 11:17:36 -05:00
committed by GitHub
parent 745de66d23
commit 3c5229610f
20 changed files with 4172 additions and 2189 deletions

View File

@@ -0,0 +1,87 @@
<?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\Nicknames\Tests\integration;
use Flarum\Group\Group;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
class UpdateTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->extension('flarum-nicknames');
$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
]);
}
/**
* @test
*/
public function user_cant_edit_own_nickname_if_not_allowed()
{
$this->database()->table('group_permission')->where('permission', 'user.editOwnNickname')->where('group_id', Group::MEMBER_ID)->delete();
$response = $this->send(
$this->request('PATCH', '/api/users/2', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'attributes' => [
'nickname' => 'new nickname',
],
],
],
])
);
$this->assertEquals(403, $response->getStatusCode());
}
/**
* @test
*/
public function user_can_edit_own_nickname_if_allowed()
{
$this->prepareDatabase([
'group_permission' => [
['permission' => 'user.editOwnNickname', 'group_id' => 2],
]
]);
$response = $this->send(
$this->request('PATCH', '/api/users/2', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'attributes' => [
'nickname' => 'new nickname',
],
],
],
])
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('new nickname', User::find(2)->nickname);
}
}

View File

@@ -0,0 +1,77 @@
<?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\Nicknames\Tests\integration;
use Flarum\Extend;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
class RegisterTest extends TestCase
{
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->extension('flarum-nicknames');
$this->extend(
(new Extend\Csrf)->exemptRoute('register')
);
}
/**
* @test
*/
public function can_register_with_nickname()
{
$this->setting('flarum-nicknames.set_on_registration', true);
$response = $this->send(
$this->request('POST', '/register', [
'json' => [
'nickname' => 'фларум',
'username' => 'test',
'password' => 'too-obscure',
'email' => 'test@machine.local',
]
])
);
$this->assertEquals(201, $response->getStatusCode());
/** @var User $user */
$user = User::where('username', 'test')->firstOrFail();
$this->assertEquals(0, $user->is_email_confirmed);
$this->assertEquals('test', $user->username);
$this->assertEquals('test@machine.local', $user->email);
}
/**
* @test
*/
public function cant_register_with_nickname_if_not_allowed()
{
$this->setting('flarum-nicknames.set_on_registration', false);
$response = $this->send(
$this->request('POST', '/register', [
'json' => [
'nickname' => 'фларум',
'username' => 'test',
'password' => 'too-obscure',
'email' => 'test@machine.local',
]
])
);
$this->assertEquals(403, $response->getStatusCode());
}
}