[CONTROLLER][UserPanel] Re-organised all settings forms. Added email, password, language forms separated from account or personal account info

[CORE][Form] Better PHPDoc and used is_null() for checks

[ENTITY][LocalUser] Add setNicknameSanitisedAndCached

[UTIL][Exception] Better NicknameNotAllowedException default message
This commit is contained in:
Eliseu Amaro 2021-12-03 02:51:41 +00:00 committed by Diogo Peralta Cordeiro
parent e16fade490
commit 4501b7e85e
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
5 changed files with 315 additions and 155 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
// {{{ License // {{{ License
@ -42,14 +42,17 @@ use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use App\Util\Nickname;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Entity\ActorLanguage; use App\Entity\ActorLanguage;
use App\Entity\Language; use App\Entity\Language;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\AuthenticationException; use App\Util\Exception\AuthenticationException;
use App\Util\Exception\NicknameEmptyException;
use App\Util\Exception\NicknameInvalidException;
use App\Util\Exception\NicknameNotAllowedException;
use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Form\ActorArrayTransformer; use App\Util\Form\ActorArrayTransformer;
@ -63,11 +66,15 @@ use Functional as F;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType; use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\SubmitButton; use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use function is_null;
// }}} Imports // }}} Imports
@ -80,71 +87,82 @@ class UserPanel extends Controller
*/ */
public function allSettings(Request $request): array public function allSettings(Request $request): array
{ {
$account_form = $this->account($request); $personal_form = $this->personalInfo($request);
$personal_form = $this->personalInfo($request); $email_form = $this->email($request);
$password_form = $this->password($request);
$language_form = $this->language($request);
$notifications_form_array = $this->notifications($request); $notifications_form_array = $this->notifications($request);
return [ return [
'_template' => 'settings/base.html.twig', '_template' => 'settings/base.html.twig',
'profile' => $personal_form->createView(), 'profile' => $personal_form->createView(),
'account' => $account_form->createView(), 'email' => $email_form->createView(),
'password' => $password_form->createView(),
'language' => $language_form->createView(),
'tabbed_forms_notify' => $notifications_form_array, 'tabbed_forms_notify' => $notifications_form_array,
'open_details_query' => $this->string('open'), 'open_details_query' => $this->string('open'),
]; ];
} }
/** /**
* Local user personal information panel * Change email settings form
*
* @param Request $request
* @return FormInterface
* @throws NoLoggedInUser
* @throws ServerException
*/ */
public function personalInfo(Request $request) public function email(Request $request): FormInterface
{
$user = Common::ensureLoggedIn();
$actor = $user->getActor();
[$_, $actor_tags] = $actor->getSelfTags();
$extra = ['self_tags' => $actor_tags];
$form_definition = [
['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
['homepage', TextType::class, ['label' => _m('Homepage'), 'required' => false, 'help' => _m('URL of your homepage, blog, or profile on another site.')]],
['bio', TextareaType::class, ['label' => _m('Bio'), 'required' => false, 'help' => _m('Describe yourself and your interests.')]],
['location', TextType::class, ['label' => _m('Location'), 'required' => false, 'help' => _m('Where you are, like "City, State (or Region), Country".')]],
['self_tags', TextType::class, ['label' => _m('Self Tags'), 'required' => false, 'help' => _m('Tags for yourself (letters, numbers, -, ., and _), comma- or space-separated.'), 'transformer' => ArrayTransformer::class]],
['save_personal_info', SubmitType::class, ['label' => _m('Save personal info')]],
];
$extra_step = function ($data, $extra_args) use ($user) {
// TODO: this isn't ideal, when the user nick is set it should be normalized and the cache updated
$nickname = Nickname::normalize($data['nickname'], check_already_used: false, which: Nickname::CHECK_LOCAL_USER, check_is_allowed: true);
if ($nickname) {
$user->setNickname($nickname);
// Updating actor cache
Cache::set('actor-nickname-id-' . $user->getActor()->getId(), $nickname);
}
};
return Form::handle($form_definition, $request, $actor, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]);
}
/**
* Local user account information panel
*/
public function account(Request $request)
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
// TODO Add support missing settings // TODO Add support missing settings
$form = Form::create([ $form = Form::create([
['outgoing_email', TextType::class, ['label' => _m('Outgoing email'), 'required' => false, 'help' => _m('Change the email we use to contact you')]], ['outgoing_email', TextType::class, ['label' => _m('Outgoing email'), 'required' => false, 'help' => _m('Change the email we use to contact you')]],
['incoming_email', TextType::class, ['label' => _m('Incoming email'), 'required' => false, 'help' => _m('Change the email you use to contact us (for posting, for instance)')]], ['incoming_email', TextType::class, ['label' => _m('Incoming email'), 'required' => false, 'help' => _m('Change the email you use to contact us (for posting, for instance)')]],
['old_password', PasswordType::class, ['label' => _m('Old password'), 'required' => false, 'help' => _m('Enter your old password for verification'), 'attr' => ['placeholder' => '********']]], ['save_email', SubmitType::class, ['label' => _m('Save email info')]],
FormFields::repeated_password(['required' => false]),
FormFields::language($user->getActor(), context_actor: null, label: _m('Languages'), help: _m('The languages you understand, so you can see primarily content in those'), multiple: true, required: false, use_short_display: false),
['phone_number', PhoneNumberType::class, ['label' => _m('Phone number'), 'required' => false, 'help' => _m('Your phone number'), 'data_class' => null]],
['save_account_info', SubmitType::class, ['label' => _m('Save account info')]],
]); ]);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData(); $data = $form->getData();
if (!\is_null($data['old_password'])) { foreach ($data as $key => $val) {
$method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key));
if (method_exists($user, $method)) {
$user->{$method}($val);
}
}
DB::flush();
}
return $form;
}
/**
* Change password form
*
* @param Request $request
* @return FormInterface
* @throws AuthenticationException
* @throws NoLoggedInUser
* @throws ServerException
*/
public function password(Request $request): FormInterface
{
$user = Common::ensureLoggedIn();
// TODO Add support missing settings
$form = Form::create([
['old_password', PasswordType::class, ['label' => _m('Old password'), 'required' => true, 'help' => _m('Enter your old password for verification'), 'attr' => ['placeholder' => '********']]],
FormFields::repeated_password(['required' => true]),
['save_password', SubmitType::class, ['label' => _m('Save new password')]],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
if (!is_null($data['old_password'])) {
$data['password'] = $form->get('password')->getData(); $data['password'] = $form->get('password')->getData();
if (!($user->changePassword($data['old_password'], $data['password']))) { if (!($user->changePassword($data['old_password'], $data['password']))) {
throw new AuthenticationException(_m('The provided password is incorrect')); throw new AuthenticationException(_m('The provided password is incorrect'));
@ -152,31 +170,6 @@ class UserPanel extends Controller
} }
unset($data['old_password'], $data['password']); unset($data['old_password'], $data['password']);
$redirect_to_language_sorting = false;
if (!\is_null($data['languages'])) {
$selected_langs = DB::findBy('language', ['locale' => $data['languages']]);
$existing_langs = DB::dql(
'select l from language l join actor_language al with l.id = al.language_id where al.actor_id = :actor_id',
['actor_id' => $user->getId()],
);
$new_langs = array_udiff($selected_langs, $existing_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
$removing_langs = array_udiff($existing_langs, $selected_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
foreach ($new_langs as $l) {
DB::persist(ActorLanguage::create(['actor_id' => $user->getId(), 'language_id' => $l->getId(), 'ordering' => 0]));
}
if (!empty($removing_langs)) {
$actor_langs_to_remove = DB::findBy('actor_language', ['actor_id' => $user->getId(), 'language_id' => F\map($removing_langs, fn ($l) => $l->getId())]);
foreach ($actor_langs_to_remove as $lang) {
DB::remove($lang);
}
}
Cache::delete(ActorLanguage::collectionCacheKey($user));
DB::flush();
ActorLanguage::normalizeOrdering($user); // In case the user doesn't submit the other page
unset($data['languages']);
$redirect_to_language_sorting = true;
}
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
$method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key)); $method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key));
if (method_exists($user, $method)) { if (method_exists($user, $method)) {
@ -184,114 +177,156 @@ class UserPanel extends Controller
} }
} }
DB::flush(); DB::flush();
if ($redirect_to_language_sorting) {
throw new RedirectException('settings_sort_languages', ['_fragment' => null]); // TODO doesn't clear fragment
}
} }
return $form; return $form;
} }
/** /**
* Controller for defining the ordering of a users's languages * @throws RedirectException
* @throws ServerException
* @throws NoLoggedInUser
*/ */
public function sortLanguages(Request $request) public function language(Request $request): FormInterface
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
// TODO Add support missing settings
$langs = DB::dql('select l.locale, l.long_display, al.ordering from language l join actor_language al with l.id = al.language_id where al.actor_id = :id order by al.ordering ASC', ['id' => $user->getId()]); $form = Form::create([
FormFields::language($user->getActor(), context_actor: null, label: _m('Languages'), help: _m('The languages you understand, so you can see primarily content in those'), multiple: true, required: false, use_short_display: false),
$form_entries = []; ['save_languages', SubmitType::class, ['label' => _m('Proceed to order selected languages')]],
foreach ($langs as $l) { ]);
$form_entries[] = [$l['locale'], IntegerType::class, ['label' => _m($l['long_display']), 'data' => $l['ordering']]];
}
$form_entries[] = ['save_language_order', SubmitType::class, []];
$form_entries[] = ['go_back', SubmitType::class, ['label' => _m('Return to settings page')]];
$form = Form::create($form_entries);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
/** @var SubmitButton $button */ $data = $form->getData();
$button = $form->get('go_back');
$go_back = $button->isClicked();
$data = $form->getData();
asort($data); // Sort by the order value
$data = array_keys($data); // This keeps the order and gives us a unique number for each
foreach ($data as $order => $locale) {
$lang = Language::getFromLocale($locale);
$actor_lang = DB::getReference('actor_language', ['actor_id' => $user->getId(), 'language_id' => $lang->getId()]);
$actor_lang->setOrdering($order + 1);
}
DB::flush();
if (!$go_back) {
// Stay on same page, but force update and prevent resubmission
throw new RedirectException('settings_sort_languages');
} else {
throw new RedirectException('settings', ['open' => 'account', '_fragment' => 'save_account_info_languages']);
}
}
return [ if (!\is_null($data['languages'])) {
'_template' => 'settings/sort_languages.html.twig', $selected_langs = DB::findBy('language', ['locale' => $data['languages']]);
'form' => $form->createView(), $existing_langs = DB::dql(
'select l from language l join actor_language al with l.id = al.language_id where al.actor_id = :actor_id',
['actor_id' => $user->getId()],
);
$new_langs = array_udiff($selected_langs, $existing_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
$removing_langs = array_udiff($existing_langs, $selected_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
foreach ($new_langs as $l) {
DB::persist(ActorLanguage::create(['actor_id' => $user->getId(), 'language_id' => $l->getId(), 'ordering' => 0]));
}
if (!empty($removing_langs)) {
$actor_langs_to_remove = DB::findBy('actor_language', ['actor_id' => $user->getId(), 'language_id' => F\map($removing_langs, fn ($l) => $l->getId())]);
foreach ($actor_langs_to_remove as $lang) {
DB::remove($lang);
}
}
Cache::delete(ActorLanguage::collectionCacheKey($user));
DB::flush();
ActorLanguage::normalizeOrdering($user); // In case the user doesn't submit the other page
unset($data['languages']);
throw new RedirectException('settings_sort_languages', ['_fragment' => null]); // TODO doesn't clear fragment
}
}
return $form;
}
/**
* Local user personal information panel
*
* @param Request $request
* @return mixed
* @throws NicknameEmptyException
* @throws NicknameInvalidException
* @throws NicknameNotAllowedException
* @throws NicknameTakenException
* @throws NicknameTooLongException
* @throws NoLoggedInUser
* @throws ServerException
*/
public function personalInfo(Request $request): mixed
{
// Ensure the user is logged in and retrieve Actor object for given user
$user = Common::ensureLoggedIn();
$actor = $user->getActor();
// Used in Form::handle as an array $extra_args
[$_, $actor_tags] = $actor->getSelfTags();
$extra = ['self_tags' => $actor_tags];
// Defining the various form fields
$form_definition = [
['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
['homepage', TextType::class, ['label' => _m('Homepage'), 'required' => false, 'help' => _m('URL of your homepage, blog, or profile on another site.')]],
['bio', TextareaType::class, ['label' => _m('Bio'), 'required' => false, 'help' => _m('Describe yourself and your interests.')]],
['phone_number', PhoneNumberType::class, ['label' => _m('Phone number'), 'required' => false, 'help' => _m('Your phone number'), 'data_class' => null]],
['location', TextType::class, ['label' => _m('Location'), 'required' => false, 'help' => _m('Where you are, like "City, State (or Region), Country".')]],
['self_tags', TextType::class, ['label' => _m('Self Tags'), 'required' => false, 'help' => _m('Tags for yourself (letters, numbers, -, ., and _), comma- or space-separated.'), 'transformer' => ArrayTransformer::class]],
['save_personal_info', SubmitType::class, ['label' => _m('Save personal info')]],
]; ];
// Setting nickname normalised and setting actor cache
$extra_step = function ($data, $extra_args) use ($user, $actor) {
$user->setNicknameSanitizedAndCached($data['nickname'], $actor->getId());
};
return Form::handle($form_definition, $request, $actor, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]);
} }
/** /**
* Local user notification settings tabbed panel * Local user notification settings tabbed panel
*/ */
public function notifications(Request $request) public function notifications(Request $request): array
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$schema = DB::getConnection()->getSchemaManager(); $schema = DB::getConnection()->getSchemaManager();
$platform = $schema->getDatabasePlatform(); $platform = $schema->getDatabasePlatform();
$columns = Common::arrayRemoveKeys($schema->listTableColumns('user_notification_prefs'), ['user_id', 'transport', 'created', 'modified']); $columns = Common::arrayRemoveKeys($schema->listTableColumns('user_notification_prefs'), ['user_id', 'transport', 'created', 'modified']);
$form_defs = ['placeholder' => []]; $form_defs = ['placeholder' => []];
foreach ($columns as $name => $col) { foreach ($columns as $name => $col) {
$type = $col->getType(); $type = $col->getType();
$val = $type->convertToPHPValue($col->getDefault(), $platform); $val = $type->convertToPHPValue($col->getDefault(), $platform);
$type_str = $type->getName(); $type_str = $type->getName();
$label = str_replace('_', ' ', ucfirst($name)); $label = str_replace('_', ' ', ucfirst($name));
$labels = [ $labels = [
'target_actor_id' => 'Target Actors', 'target_actor_id' => 'Target Actors',
'dm' => 'DM', 'dm' => 'DM',
]; ];
$help = [ $help = [
'target_actor_id' => 'If specified, these settings apply only to these profiles (comma- or space-separated list)', 'target_actor_id' => 'If specified, these settings apply only to these profiles (comma- or space-separated list)',
'activity_by_subscribed' => 'Notify me when someone I subscribed has new activity', 'activity_by_subscribed' => 'Notify me when someone I subscribed has new activity',
'mention' => 'Notify me when mentions me in a notice', 'mention' => 'Notify me when mentions me in a notice',
'reply' => 'Notify me when someone replies to a notice made by me', 'reply' => 'Notify me when someone replies to a notice made by me',
'subscription' => 'Notify me when someone subscribes to me or asks for permission to do so', 'subscription' => 'Notify me when someone subscribes to me or asks for permission to do so',
'favorite' => 'Notify me when someone favorites one of my notices', 'favorite' => 'Notify me when someone favorites one of my notices',
'nudge' => 'Notify me when someone nudges me', 'nudge' => 'Notify me when someone nudges me',
'dm' => 'Notify me when someone sends me a direct message', 'dm' => 'Notify me when someone sends me a direct message',
'post_on_status_change' => 'Post a notice when my status in this service changes', 'post_on_status_change' => 'Post a notice when my status in this service changes',
'enable_posting' => 'Enable posting from this service', 'enable_posting' => 'Enable posting from this service',
]; ];
switch ($type_str) { switch ($type_str) {
case Types::BOOLEAN: case Types::BOOLEAN:
$form_defs['placeholder'][$name] = [$name, CheckboxType::class, ['data' => $val, 'required' => false, 'label' => _m($labels[$name] ?? $label), 'help' => _m($help[$name])]]; $form_defs['placeholder'][$name] = [$name, CheckboxType::class, ['data' => $val, 'required' => false, 'label' => _m($labels[$name] ?? $label), 'help' => _m($help[$name])]];
break; break;
case Types::INTEGER: case Types::INTEGER:
if ($name == 'target_actor_id') { if ($name == 'target_actor_id') {
$form_defs['placeholder'][$name] = [$name, TextType::class, ['data' => $val, 'required' => false, 'label' => _m($labels[$name]), 'help' => _m($help[$name])], 'transformer' => ActorArrayTransformer::class]; $form_defs['placeholder'][$name] = [$name, TextType::class, ['data' => $val, 'required' => false, 'label' => _m($labels[$name]), 'help' => _m($help[$name])], 'transformer' => ActorArrayTransformer::class];
} }
break; break;
default: default:
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
Log::critical("Structure of table user_notification_prefs changed in a way not accounted to in notification settings ({$name}): " . $type_str); Log::critical("Structure of table user_notification_prefs changed in a way not accounted to in notification settings ({$name}): " . $type_str);
throw new ServerException(_m('Internal server error')); throw new ServerException(_m('Internal server error'));
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} }
} }
$form_defs['placeholder']['save'] = fn (string $transport, string $form_name) => [$form_name, SubmitType::class, $form_defs['placeholder']['save'] = fn(string $transport, string $form_name) => [$form_name, SubmitType::class,
['label' => _m('Save notification settings for {transport}', ['transport' => $transport])], ]; ['label' => _m('Save notification settings for {transport}', ['transport' => $transport])],];
Event::handle('AddNotificationTransport', [&$form_defs]); Event::handle('AddNotificationTransport', [&$form_defs]);
unset($form_defs['placeholder']); unset($form_defs['placeholder']);
@ -299,7 +334,7 @@ class UserPanel extends Controller
$tabbed_forms = []; $tabbed_forms = [];
foreach ($form_defs as $transport_name => $f) { foreach ($form_defs as $transport_name => $f) {
unset($f['save']); unset($f['save']);
$form = Form::create($f); $form = Form::create($f);
$tabbed_forms[$transport_name] = $form; $tabbed_forms[$transport_name] = $form;
$form->handleRequest($request); $form->handleRequest($request);
@ -325,7 +360,59 @@ class UserPanel extends Controller
} }
} }
$tabbed_forms = F\map($tabbed_forms, fn ($f) => $f->createView()); $tabbed_forms = F\map($tabbed_forms, fn($f) => $f->createView());
return $tabbed_forms; return $tabbed_forms;
} }
/**
* Controller for defining the ordering of a users' languages
*
* @param Request $request
* @return array
* @throws NoLoggedInUser
* @throws RedirectException
* @throws ServerException
*/
public function sortLanguages(Request $request): array
{
$user = Common::ensureLoggedIn();
$langs = DB::dql('select l.locale, l.long_display, al.ordering from language l join actor_language al with l.id = al.language_id where al.actor_id = :id order by al.ordering ASC', ['id' => $user->getId()]);
$form_entries = [];
foreach ($langs as $l) {
$form_entries[] = [$l['locale'], IntegerType::class, ['label' => _m($l['long_display']), 'data' => $l['ordering']]];
}
$form_entries[] = ['save_language_order', SubmitType::class, []];
$form_entries[] = ['go_back', SubmitType::class, ['label' => _m('Return to settings page')]];
$form = Form::create($form_entries);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var SubmitButton $button */
$button = $form->get('go_back');
$go_back = $button->isClicked();
$data = $form->getData();
asort($data); // Sort by the order value
$data = array_keys($data); // This keeps the order and gives us a unique number for each
foreach ($data as $order => $locale) {
$lang = Language::getFromLocale($locale);
$actor_lang = DB::getReference('actor_language', ['actor_id' => $user->getId(), 'language_id' => $lang->getId()]);
$actor_lang->setOrdering($order + 1);
}
DB::flush();
if (!$go_back) {
// Stay on same page, but force update and prevent resubmission
throw new RedirectException('settings_sort_languages');
} else {
throw new RedirectException('settings', ['open' => 'account', '_fragment' => 'save_account_info_languages']);
}
}
return [
'_template' => 'settings/sort_languages.html.twig',
'form' => $form->createView(),
];
}
} }

View File

@ -134,20 +134,33 @@ abstract class Form
return $form[$field][2]['required'] ?? true; return $form[$field][2]['required'] ?? true;
} }
/** /**
* Handle the full life cycle of a form. Creates it with @see * Handle the full life cycle of a form. Creates it with @see
* self::create and inserts the submitted values into the database * self::create and inserts the submitted values into the database
*
* @param array $form_definition
* @param Request $request
* @param object|null $target
* @param array $extra_args
* @param callable|null $extra_step
* @param array $create_args
* @param SymfForm|null $testing_only_form
* @return mixed
* @throws ServerException
*/ */
public static function handle(array $form_definition, Request $request, ?object $target, array $extra_args = [], ?callable $extra_step = null, array $create_args = [], ?SymfForm $testing_only_form = null) public static function handle(array $form_definition, Request $request, ?object $target, array $extra_args = [], ?callable $extra_step = null, array $create_args = [], ?SymfForm $testing_only_form = null): mixed
{ {
$form = $testing_only_form ?? self::create($form_definition, $target, ...$create_args); $form = $testing_only_form ?? self::create($form_definition, $target, ...$create_args);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData(); $data = $form->getData();
if ($target == null) { if (is_null($target)) {
return $data; return $data;
} }
unset($data['translation_domain'], $data['save']); unset($data['translation_domain'], $data['save']);
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
$method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key)); $method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key));
@ -155,19 +168,22 @@ abstract class Form
if (isset($extra_args[$key])) { if (isset($extra_args[$key])) {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
$target->{$method}($val, $extra_args[$key]); $target->{$method}($val, $extra_args[$key]);
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} else { } else {
$target->{$method}($val); $target->{$method}($val);
} }
} }
} }
if (isset($extra_step)) { if (isset($extra_step)) {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
$extra_step($data, $extra_args); $extra_step($data, $extra_args);
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} }
DB::flush(); DB::flush();
} }
return $form; return $form;
} }
} }

View File

@ -28,11 +28,20 @@ use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\UserRoles; use App\Core\UserRoles;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\NicknameEmptyException;
use App\Util\Exception\NicknameException;
use App\Util\Exception\NicknameInvalidException;
use App\Util\Exception\NicknameNotAllowedException;
use App\Util\Exception\NicknameNotFoundException;
use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Nickname;
use DateTimeInterface; use DateTimeInterface;
use Exception; use Exception;
use libphonenumber\PhoneNumber; use libphonenumber\PhoneNumber;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use function App\Core\I18n\_m;
/** /**
* Entity for users * Entity for users
@ -343,6 +352,40 @@ class LocalUser extends Entity implements UserInterface, PasswordAuthenticatedUs
} }
// }}} Authentication // }}} Authentication
/**
* Checks if desired nickname is allowed, and in case it is, it sets Actor's nickname cache to newly set nickname
*
* @param string $nickname Desired new nickname
* @param int $actor_id Used to cache Actor nickname
* @return $this
* @throws NicknameEmptyException
* @throws NicknameInvalidException
* @throws NicknameNotAllowedException
* @throws NicknameTakenException
* @throws NicknameTooLongException
*/
public function setNicknameSanitizedAndCached(string $nickname, int $actor_id): self
{
try {
$nickname = Nickname::normalize($nickname, check_already_used: false, which: Nickname::CHECK_LOCAL_USER, check_is_allowed: true);
$this->nickname = $nickname;
Cache::set('actor-nickname-id-' . $actor_id, $nickname);
} catch (NicknameEmptyException $e) {
throw new NicknameEmptyException();
} catch (NicknameInvalidException $e) {
throw new NicknameInvalidException();
} catch (NicknameNotAllowedException $e) {
throw new NicknameNotAllowedException();
} catch (NicknameTakenException $e) {
throw new NicknameTakenException();
} catch (NicknameTooLongException $e) {
throw new NicknameTooLongException();
} catch (NicknameException $e) {
}
return $this;
}
public function getActor() public function getActor()
{ {
return DB::find('actor', ['id' => $this->id]); return DB::find('actor', ['id' => $this->id]);

View File

@ -48,6 +48,6 @@ class NicknameNotAllowedException extends NicknameException
protected function defaultMessage(): string protected function defaultMessage(): string
{ {
// TRANS: Validation error in form for registration, profile and group settings, etc. // TRANS: Validation error in form for registration, profile and group settings, etc.
return _m('This nickname is not allowed.'); return _m('Nickname not allowed! Only alphanumeric characters allowed.');
} }
} }

View File

@ -65,9 +65,17 @@
</ul> </ul>
</nav> </nav>
</li> </li>
{% elseif title == 'Account' %} {% elseif title == 'Email' %}
<li> <li>
{{ form(context.account) }} {{ form(context.email) }}
</li>
{% elseif title == 'Password' %}
<li>
{{ form(context.password) }}
</li>
{% elseif title == 'Language' %}
<li>
{{ form(context.language) }}
</li> </li>
{% elseif title == 'Notifications' %} {% elseif title == 'Notifications' %}
<li> <li>
@ -105,7 +113,13 @@
</li> </li>
<hr> <hr>
<li> <li>
{{ _self.settings_details_container('Account', 'Email, Password and Language', ['account'], _context) }} {{ _self.settings_details_container('Email', 'Set incoming and outgoing email settings', ['email'], _context) }}
</li>
<li>
{{ _self.settings_details_container('Password', 'Change current password', ['password'], _context) }}
</li>
<li>
{{ _self.settings_details_container('Language', 'Select and set language preferences', ['language'], _context) }}
</li> </li>
<hr> <hr>
<li> <li>