From 136df3724f3d9f1f03a38f5ae157c0a2d0047a39 Mon Sep 17 00:00:00 2001 From: Simone Gasparini Date: Tue, 16 Apr 2019 17:30:51 +0200 Subject: [PATCH] removed active flag in user preferences form for own profile (#696) --- src/Controller/ProfileController.php | 3 +- src/Controller/UserController.php | 3 +- src/Form/UserEditType.php | 14 ++++++++-- tests/Controller/ProfileControllerTest.php | 32 ++++++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index bcec5b10..918bc7b9 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -320,7 +320,8 @@ class ProfileController extends AbstractController $user, [ 'action' => $this->generateUrl('user_profile_edit', ['username' => $user->getUsername()]), - 'method' => 'POST' + 'method' => 'POST', + 'include_active_flag' => ($user->getId() !== $this->getUser()->getId()) ] ); } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index d291959f..5c6d9797 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -189,7 +189,8 @@ class UserController extends AbstractController { return $this->createForm(UserCreateType::class, $user, [ 'action' => $this->generateUrl('admin_user_create'), - 'method' => 'POST' + 'method' => 'POST', + 'include_active_flag' => true ]); } } diff --git a/src/Form/UserEditType.php b/src/Form/UserEditType.php index 3ac759ae..1ef8cdbb 100644 --- a/src/Form/UserEditType.php +++ b/src/Form/UserEditType.php @@ -16,12 +16,14 @@ use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; /** * Defines the form used to edit the profile of a User. */ class UserEditType extends AbstractType { + /** * {@inheritdoc} */ @@ -43,10 +45,15 @@ class UserEditType extends AbstractType ->add('email', EmailType::class, [ 'label' => 'label.email', ]) - ->add('enabled', YesNoType::class, [ - 'label' => 'label.active', - ]) ; + + if ($options['include_active_flag']) { + $builder + ->add('enabled', YesNoType::class, [ + 'label' => 'label.active', + ]) + ; + } } /** @@ -59,6 +66,7 @@ class UserEditType extends AbstractType 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_token_id' => 'edit_user_profile', + 'include_active_flag' => false, ]); } } diff --git a/tests/Controller/ProfileControllerTest.php b/tests/Controller/ProfileControllerTest.php index ea28d9f3..2965c77b 100644 --- a/tests/Controller/ProfileControllerTest.php +++ b/tests/Controller/ProfileControllerTest.php @@ -76,6 +76,38 @@ class ProfileControllerTest extends ControllerBaseTest $this->assertEquals('john_user@example.com', $user->getEmail()); $this->assertTrue($user->isEnabled()); + $form = $client->getCrawler()->filter('form[name=user_edit]')->form(); + $client->submit($form, [ + 'user_edit' => [ + 'alias' => 'Johnny', + 'title' => 'Code Monkey', + 'avatar' => '/fake/image.jpg', + 'email' => 'updated@example.com', + ] + ]); + + $this->assertIsRedirect($client, $this->createUrl('/profile/' . urlencode(UserFixtures::USERNAME_USER) . '/edit')); + $client->followRedirect(); + $this->assertTrue($client->getResponse()->isSuccessful()); + + $this->assertHasFlashSuccess($client); + + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + $user = $this->getUserByRole($em, User::ROLE_USER); + + $this->assertEquals(UserFixtures::USERNAME_USER, $user->getUsername()); + $this->assertEquals('Johnny', $user->getAlias()); + $this->assertEquals('Code Monkey', $user->getTitle()); + $this->assertEquals('/fake/image.jpg', $user->getAvatar()); + $this->assertEquals('updated@example.com', $user->getEmail()); + $this->assertTrue($user->isEnabled()); + } + + public function testEditActionWithActiveFlag() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN); + $this->request($client, '/profile/' . UserFixtures::USERNAME_USER . '/edit'); + $form = $client->getCrawler()->filter('form[name=user_edit]')->form(); $client->submit($form, [ 'user_edit' => [