[CORE][Nickname] Properly set nickname for existing accounts

This commit is contained in:
José Marques 2022-01-10 11:29:37 +00:00 odevzdal Diogo Peralta Cordeiro
rodič 1056bc661f
revize d713429d88
Podepsáno: diogo
ID GPG klíče: 18D2D35001FBFAB0
6 změnil soubory, kde provedl 53 přidání a 15 odebrání

Zobrazit soubor

@ -166,7 +166,7 @@ class Group extends FeedController
return [
'_template' => 'group/settings.html.twig',
'group' => $group,
'personal_info_form' => ActorForms::personalInfo($request, $group)->createView(),
'personal_info_form' => ActorForms::personalInfo($request, $actor, $group)->createView(),
'open_details_query' => $this->string('open'),
];
} else {

Zobrazit soubor

@ -21,10 +21,18 @@ declare(strict_types = 1);
namespace Component\Group\Entity;
use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Entity;
use App\Entity\Actor;
use App\Util\Exception\NicknameEmptyException;
use App\Util\Exception\NicknameException;
use App\Util\Exception\NicknameInvalidException;
use App\Util\Exception\NicknameNotAllowedException;
use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Nickname;
use DateTimeInterface;
/**
@ -114,6 +122,29 @@ class LocalGroup extends Entity
return $res === [] ? null : $res[0];
}
/**
* 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 (do not use in local user creation)
*
* @throws NicknameEmptyException
* @throws NicknameException
* @throws NicknameInvalidException
* @throws NicknameNotAllowedException
* @throws NicknameTakenException
* @throws NicknameTooLongException
*
* @return $this
*/
public function setNicknameSanitizedAndCached(string $nickname): self
{
$nickname = Nickname::normalize($nickname, check_already_used: true, which: Nickname::CHECK_LOCAL_GROUP, check_is_allowed: true);
$this->setNickname($nickname);
$this->getActor()->setNickname($nickname);
/// XXX: cache?
return $this;
}
public static function schemaDef(): array
{
return [

Zobrazit soubor

@ -77,7 +77,7 @@ class UserPanel extends Controller
$user = Common::ensureLoggedIn();
$actor = $user->getActor();
$personal_form = ActorForms::personalInfo($request, $actor);
$personal_form = ActorForms::personalInfo($request, $actor, $user);
$email_form = self::email($request);
$password_form = self::password($request);
$notifications_form_array = self::notifications($request);

Zobrazit soubor

@ -29,6 +29,7 @@ use App\Core\Entity;
use App\Core\UserRoles;
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\NicknameTakenException;
@ -349,19 +350,18 @@ class LocalUser extends Entity implements UserInterface, PasswordAuthenticatedUs
/**
* 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 string $nickname Desired NEW nickname (do not use in local user creation)
* @return $this
* @throws NicknameEmptyException
* @throws NicknameInvalidException
* @throws NicknameNotAllowedException
* @throws NicknameTakenException
* @throws NicknameTooLongException
*
* @return $this
* @throws NicknameException
*/
public function setNicknameSanitizedAndCached(string $nickname): self
{
$nickname = Nickname::normalize($nickname, check_already_used: false, which: Nickname::CHECK_LOCAL_USER, check_is_allowed: true);
$nickname = Nickname::normalize($nickname, check_already_used: true, which: Nickname::CHECK_LOCAL_USER, check_is_allowed: true);
$this->setNickname($nickname);
$this->getActor()->setNickname($nickname);
Cache::delete(self::cacheKeys($this->getId())['nickname']);

Zobrazit soubor

@ -46,6 +46,7 @@ use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\ServerException;
use Component\Group\Entity\LocalGroup;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
@ -65,7 +66,7 @@ class ActorForms
* @throws NoLoggedInUser
* @throws ServerException
*/
public static function personalInfo(Request $request, Actor $target, ?LocalUser $user = null): mixed
public static function personalInfo(Request $request, Actor $target, LocalUser|LocalGroup $user): mixed
{
// Defining the various form fields
$form_definition = [
@ -80,9 +81,7 @@ class ActorForms
// Setting nickname normalised and setting actor cache
$extra_step = function ($data, $extra_args) use ($user, $target) {
if (!\is_null($user)) {
$user->setNicknameSanitizedAndCached($data['nickname']);
}
$user->setNicknameSanitizedAndCached($data['nickname']);
$cache_keys = Actor::cacheKeys($target->getId());
foreach (['id', 'nickname', 'fullname'] as $key) {

Zobrazit soubor

@ -24,6 +24,7 @@ declare(strict_types = 1);
namespace App\Util;
use App\Core\DB\DB;
use App\Entity\LocalUser;
use App\Util\Exception\BugFoundException;
use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NicknameEmptyException;
@ -33,7 +34,7 @@ use App\Util\Exception\NicknameNotAllowedException;
use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotImplementedException;
use Component\Group\Entity\LocalGroup;
use Functional as F;
use InvalidArgumentException;
@ -150,7 +151,7 @@ class Nickname
switch ($which) {
case self::CHECK_LOCAL_USER:
try {
$lu = DB::findOneBy('local_user', ['nickname' => $nickname]);
$lu = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
throw new NicknameTakenException($lu->getActor());
} catch (NotFoundException) {
// continue
@ -158,9 +159,16 @@ class Nickname
throw new BugFoundException("Duplicate entry in `local_user` for nickname={$nickname}");
}
break;
// @codeCoverageIgnoreStart
case self::CHECK_LOCAL_GROUP:
throw new NotImplementedException();
try {
$lg = DB::findOneBy(LocalGroup::class, ['nickname' => $nickname]);
throw new NicknameTakenException($lg->getActor());
} catch (NotFoundException) {
// continue
} catch (DuplicateFoundException) {
throw new BugFoundException("Duplicate entry in `local_group` for nickname={$nickname}");
}
break; // @codeCoverageIgnoreStart
default:
throw new InvalidArgumentException();
// @codeCoverageIgnoreEnd