Add phonenumber to user

This commit is contained in:
LenaertsJ 2023-11-16 15:12:12 +00:00
parent ff344dbb0c
commit be57c96a2f
13 changed files with 432 additions and 1 deletions

View File

@ -0,0 +1,6 @@
kind: Feature
body: Allow user to add a phonenumber to their profile which will be included in automatically
generated documents
time: 2023-11-16T15:09:00.369359598+01:00
custom:
Issue: "173"

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Form\UserPhonenumberType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
class UserProfileController extends AbstractController
{
public function __construct(
private readonly TranslatorInterface $translator,
) {}
/**
* User profile that allows editing of phonenumber and visualization of certain data.
*
* @Route("/{_locale}/main/user/my-profile", name="chill_main_user_profile")
*/
public function __invoke(Request $request)
{
$user = $this->getUser();
$editForm = $this->createPhonenumberEditForm($user);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$phonenumber = $editForm->get('phonenumber')->getData();
$user->setPhonenumber($phonenumber);
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', $this->translator->trans('user.profile.Phonenumber successfully updated!'));
return $this->redirectToRoute('chill_main_user_profile');
}
return $this->render('@ChillMain/User/profile.html.twig', [
'user' => $user,
'form' => $editForm->createView(),
]);
}
private function createPhonenumberEditForm(UserInterface $user): FormInterface
{
return $this->createForm(
UserPhonenumberType::class,
$user,
)
->add('submit', SubmitType::class, ['label' => $this->translator->trans('Save')]);
}
}

View File

@ -18,9 +18,11 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\Mapping as ORM;
use libphonenumber\PhoneNumber;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Validation\Constraint\PhonenumberConstraint;
/**
* User.
@ -161,6 +163,15 @@ class User implements UserInterface, \Stringable
*/
private ?string $usernameCanonical = null;
/**
* The user's mobile phone number.
*
* @ORM\Column(type="phone_number", nullable=true)
*
* @PhonenumberConstraint()
*/
private ?PhoneNumber $phonenumber = null;
/**
* User constructor.
*/
@ -419,6 +430,11 @@ class User implements UserInterface, \Stringable
}
}
public function getPhonenumber(): ?PhoneNumber
{
return $this->phonenumber;
}
/**
* @throws \RuntimeException if the groupCenter is not in the collection
*/
@ -639,4 +655,11 @@ class User implements UserInterface, \Stringable
return $this;
}
public function setPhonenumber(?PhoneNumber $phonenumber): self
{
$this->phonenumber = $phonenumber;
return $this;
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Form;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserPhonenumberType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('phonenumber', ChillPhoneNumberType::class, [
'required' => false,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}

View File

@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
use Chill\MainBundle\Form\Type\PickCivilityType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\EntityRepository;
@ -44,6 +45,9 @@ class UserType extends AbstractType
->add('email', EmailType::class, [
'required' => true,
])
->add('phonenumber', ChillPhoneNumberType::class, [
'required' => false,
])
->add('label', TextType::class)
->add('civility', PickCivilityType::class, [
'required' => false,

View File

@ -0,0 +1,58 @@
{#
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
<info@champs-libres.coop> / <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{% extends "@ChillMain/layout.html.twig" %}
{% block title %}{{"My profile"|trans}}{% endblock %}
{% block content %}
<div class="justify-content-center col-10">
<h1>{{ 'user.profile.title'|trans }}</h1>
<dl>
<dt>{{ 'Job'|trans }}</dt>
{% if user.getUserJob is not null %}
<dd>{{ user.getUserJob.label|localize_translatable_string }}</dd>
{% else %}
<dd class="chill-no-data-statement">{{ 'user.profile.no job'|trans }}</dd>
{% endif %}
<dt>{{ 'Scope'|trans }}</dt>
{% if user.getMainScope is not null %}
<dd>{{ user.getMainScope.name|localize_translatable_string }}</dd>
{% else %}
<dd class="chill-no-data-statement">{{ 'user.profile.no scope'|trans }}</dd>
{% endif %}
</dl>
<div>
{{ form_start(form) }}
{{ form_row(form.phonenumber) }}
<ul class="record_actions">
<li>
{{ form_widget(form.submit, { 'attr': { 'class': 'btn btn-save' } } ) }}
</li>
</ul>
{{ form_end(form) }}
</div>
</div>
{% endblock %}

View File

@ -29,6 +29,14 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
$user = $this->security->getUser();
if ($user instanceof User) {
$menu->addChild($this->translator->trans('user.profile.title'), [
'route' => 'chill_main_user_profile',
])
->setExtras([
'order' => -11_111_111,
'icon' => 'user',
]);
if (null !== $user->getCurrentLocation()) {
$locationTextMenu = $user->getCurrentLocation()->getName();
} else {

View File

@ -18,6 +18,7 @@ use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Templating\Entity\UserRender;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
@ -34,6 +35,7 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware
'text_without_absent' => '',
'label' => '',
'email' => '',
'isAbsent' => false,
];
public function __construct(private readonly UserRender $userRender) {}
@ -61,9 +63,13 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware
$context,
['docgen:expects' => Civility::class, 'groups' => 'docgen:read']
);
$phonenumberContext = array_merge(
$context,
['docgen:expects' => PhoneNumber::class, 'groups' => 'docgen:read']
);
if (null === $object && 'docgen' === $format) {
return [...self::NULL_USER, 'civility' => $this->normalizer->normalize(null, $format, $civilityContext), 'user_job' => $this->normalizer->normalize(null, $format, $userJobContext), 'main_center' => $this->normalizer->normalize(null, $format, $centerContext), 'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext), 'current_location' => $this->normalizer->normalize(null, $format, $locationContext), 'main_location' => $this->normalizer->normalize(null, $format, $locationContext)];
return [...self::NULL_USER, 'phonenumber' => $this->normalizer->normalize(null, $format, $phonenumberContext), 'civility' => $this->normalizer->normalize(null, $format, $civilityContext), 'user_job' => $this->normalizer->normalize(null, $format, $userJobContext), 'main_center' => $this->normalizer->normalize(null, $format, $centerContext), 'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext), 'current_location' => $this->normalizer->normalize(null, $format, $locationContext), 'main_location' => $this->normalizer->normalize(null, $format, $locationContext)];
}
$data = [
@ -74,6 +80,7 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware
'text_without_absent' => $this->userRender->renderString($object, ['absence' => false]),
'label' => $object->getLabel(),
'email' => (string) $object->getEmail(),
'phonenumber' => $this->normalizer->normalize($object->getPhonenumber(), $format, $phonenumberContext),
'user_job' => $this->normalizer->normalize($object->getUserJob(), $format, $userJobContext),
'main_center' => $this->normalizer->normalize($object->getMainCenter(), $format, $centerContext),
'main_scope' => $this->normalizer->normalize($object->getMainScope(), $format, $scopeContext),

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Controller;
use Chill\MainBundle\Test\PrepareClientTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
*
* @coversNothing
*/
final class UserProfileControllerTest extends WebTestCase
{
use PrepareClientTrait;
public function testPage()
{
$client = $this->getClientAuthenticated();
$client->request('GET', '/fr/main/user/my-profile');
$this->assertResponseIsSuccessful('Request GET /main/user/my-profile was successful');
}
}

View File

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Serializer\Normalizer;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Civility;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Serializer\Normalizer\UserNormalizer;
use Chill\MainBundle\Templating\Entity\UserRender;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberUtil;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
*
* @coversNothing
*/
final class UserNormalizerTest extends TestCase
{
use ProphecyTrait;
/**
* @throws NumberParseException
*/
public function dataProviderUserNormalizer()
{
$user = new User();
$userNoPhone = new User();
$user
->setUsername('SomeUser')
->setLabel('SomeUser')
->setPhonenumber(PhoneNumberUtil::getInstance()->parse('+32475928635'))
->setEmail('some.user@chill.com');
$userNoPhone
->setUsername('AnotherUser')
->setLabel('AnotherUser');
yield [$user, 'docgen', ['docgen:expects' => User::class],
[
'id' => $user->getId(), // id
'type' => 'user', // type
'username' => 'SomeUser', // username
'email' => 'some.user@chill.com', // email
'text' => 'SomeUser', // text
'label' => 'SomeUser', // label
'phonenumber' => ['context' => PhoneNumber::class], // phonenumber
'main_scope' => ['context' => Scope::class], // scope
'user_job' => ['context' => UserJob::class], // user job
'current_location' => ['context' => Location::class], // curent location
'main_location' => ['context' => Location::class], // main location
'civility' => ['context' => Civility::class], // civility
'text_without_absent' => 'SomeUser',
'isAbsent' => false,
'main_center' => ['context' => Center::class],
]];
yield [$userNoPhone, 'docgen', ['docgen:expects' => User::class],
[
'id' => $user->getId(), // id
'type' => 'user', // type
'username' => 'AnotherUser', // username
'email' => '', // email
'text' => 'AnotherUser', // text
'label' => 'AnotherUser', // label
'phonenumber' => ['context' => PhoneNumber::class], // phonenumber
'main_scope' => ['context' => Scope::class], // scope
'user_job' => ['context' => UserJob::class], // user job
'current_location' => ['context' => Location::class], // curent location
'main_location' => ['context' => Location::class], // main location
'civility' => ['context' => Civility::class], // civility
'text_without_absent' => 'AnotherUser',
'isAbsent' => false,
'main_center' => ['context' => Center::class],
]];
yield [null, 'docgen', ['docgen:expects' => User::class], [
'id' => '', // id
'type' => 'user', // type
'username' => '', // username
'email' => '', // email
'text' => '', // text
'label' => '', // label
'phonenumber' => ['context' => PhoneNumber::class], // phonenumber
'main_scope' => ['context' => Scope::class], // scope
'user_job' => ['context' => UserJob::class], // user job
'current_location' => ['context' => Location::class], // curent location
'main_location' => ['context' => Location::class], // main location
'civility' => ['context' => Civility::class], // civility
'text_without_absent' => '',
'isAbsent' => false,
'main_center' => ['context' => Center::class],
]];
}
/**
* @dataProvider dataProviderUserNormalizer
*
* @throws ExceptionInterface
*/
public function testNormalize(null|User $user, mixed $format, mixed $context, mixed $expected)
{
$userRender = $this->prophesize(UserRender::class);
$userRender->renderString(Argument::type(User::class), Argument::type('array'))->willReturn($user ? $user->getLabel() : '');
$normalizer = new UserNormalizer($userRender->reveal());
$normalizer->setNormalizer(new class () implements NormalizerInterface {
public function normalize($object, string $format = null, array $context = [])
{
return ['context' => $context['docgen:expects'] ?? null];
}
public function supportsNormalization($data, string $format = null)
{
return true;
}
});
$this->assertEquals($expected, $normalizer->normalize($user, $format, $context));
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add phonenumber to user profile.
*/
final class Version20231020075524 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add phonenumber to user profile';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE users ADD phonenumber VARCHAR(35) DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN users.phonenumber IS \'(DC2Type:phone_number)\'');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE users DROP phonenumber');
}
}

View File

@ -44,6 +44,13 @@ address_fields: Données liées à l'adresse
Datas: Données
No title: Aucun titre
user:
profile:
title: Mon profil
Phonenumber successfully updated!: Numéro de téléphone mis à jour!
no job: Pas de métier assigné
no scope: Pas de cercle assigné
inactive: inactif
Edit: Modifier

View File

@ -39,6 +39,13 @@ Last updated by: Laatste update door
on: "op "
Last updated on: Laatste update op
by_user: "door "
lifecycleUpdate: Updates en creatie gebeurtenissen
address_fields: Gegevens gelinked aan het adres
Datas: Gegevens
No title: Geen titel
User profile: Mijn gebruikersprofiel
Phonenumber successfully updated!: Telefoonnummer bijgewerkt!
Edit: Bewerken
Update: Updaten