]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Adding new user
authorJeremy <jeremy.benoist@gmail.com>
Sun, 22 Feb 2015 08:30:25 +0000 (09:30 +0100)
committerJeremy <jeremy.benoist@gmail.com>
Sun, 22 Feb 2015 08:30:25 +0000 (09:30 +0100)
src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php
src/Wallabag/CoreBundle/Form/Type/NewUserType.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig
src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php

index b3236e3ce20d17342e206b4dd02515c13eeb29fd..aedbc999944b91ecc506c6c05f120d434b434623 100644 (file)
@@ -6,9 +6,11 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
 use Wallabag\CoreBundle\Entity\Config;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
 use Wallabag\CoreBundle\Entity\Config;
+use Wallabag\CoreBundle\Entity\User;
 use Wallabag\CoreBundle\Form\Type\ConfigType;
 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
 use Wallabag\CoreBundle\Form\Type\UserType;
 use Wallabag\CoreBundle\Form\Type\ConfigType;
 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
 use Wallabag\CoreBundle\Form\Type\UserType;
+use Wallabag\CoreBundle\Form\Type\NewUserType;
 
 class ConfigController extends Controller
 {
 
 class ConfigController extends Controller
 {
@@ -72,10 +74,28 @@ class ConfigController extends Controller
             return $this->redirect($this->generateUrl('config'));
         }
 
             return $this->redirect($this->generateUrl('config'));
         }
 
+        // handle adding new user
+        $newUser = new User();
+        $newUserForm = $this->createForm(new NewUserType(), $newUser);
+        $newUserForm->handleRequest($request);
+
+        if ($newUserForm->isValid()) {
+            $em->persist($newUser);
+            $em->flush();
+
+            $this->get('session')->getFlashBag()->add(
+                'notice',
+                sprintf('User "%s" added', $newUser->getUsername())
+            );
+
+            return $this->redirect($this->generateUrl('config'));
+        }
+
         return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
             'configForm' => $configForm->createView(),
             'pwdForm' => $pwdForm->createView(),
             'userForm' => $userForm->createView(),
         return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
             'configForm' => $configForm->createView(),
             'pwdForm' => $pwdForm->createView(),
             'userForm' => $userForm->createView(),
+            'newUserForm' => $newUserForm->createView(),
         ));
     }
 
         ));
     }
 
index de0ad537811b42c0fd6057d9215c10bc9ef2fdbb..e141789f10e913fe7ad1da9085fb3d97d1b52eae 100644 (file)
@@ -23,7 +23,7 @@ class ChangePasswordType extends AbstractType
                 'constraints' => array(
                     new Constraints\Length(array(
                         'min' => 8,
                 'constraints' => array(
                     new Constraints\Length(array(
                         'min' => 8,
-                        'minMessage' => 'Password should by at least 6 chars long',
+                        'minMessage' => 'Password should by at least 8 chars long',
                     )),
                     new Constraints\NotBlank(),
                 ),
                     )),
                     new Constraints\NotBlank(),
                 ),
diff --git a/src/Wallabag/CoreBundle/Form/Type/NewUserType.php b/src/Wallabag/CoreBundle/Form/Type/NewUserType.php
new file mode 100644 (file)
index 0000000..313a9aa
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+namespace Wallabag\CoreBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+use Symfony\Component\Validator\Constraints;
+
+class NewUserType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+            ->add('username', 'text')
+            ->add('password', 'password', array(
+                'constraints' => array(
+                    new Constraints\Length(array(
+                        'min' => 8,
+                        'minMessage' => 'Password should by at least 8 chars long',
+                    )),
+                    new Constraints\NotBlank(),
+                ),
+            ))
+            ->add('email', 'text')
+            ->add('save', 'submit')
+        ;
+    }
+
+    public function setDefaultOptions(OptionsResolverInterface $resolver)
+    {
+        $resolver->setDefaults(array(
+            'data_class' => 'Wallabag\CoreBundle\Entity\User',
+        ));
+    }
+
+    public function getName()
+    {
+        return 'new_user';
+    }
+}
index 7a45ec1f11e6029359064b54194b6db4dba3e62f..051dafd6beb9c71b54539f6998f2819736f43d50 100644 (file)
 
         {{ form_rest(pwdForm) }}
     </form>
 
         {{ form_rest(pwdForm) }}
     </form>
+
+    <h2>{% trans %}Add a user{% endtrans %}</h2>
+
+    <form action="{{ path('config') }}" method="post" {{ form_enctype(newUserForm) }}>
+        {{ form_errors(newUserForm) }}
+
+        <fieldset class="w500p inline">
+            <div class="row">
+                {{ form_label(newUserForm.username) }}
+                {{ form_errors(newUserForm.username) }}
+                {{ form_widget(newUserForm.username) }}
+            </div>
+        </fieldset>
+
+        <fieldset class="w500p inline">
+            <div class="row">
+                {{ form_label(newUserForm.password) }}
+                {{ form_errors(newUserForm.password) }}
+                {{ form_widget(newUserForm.password) }}
+            </div>
+        </fieldset>
+
+        <fieldset class="w500p inline">
+            <div class="row">
+                {{ form_label(newUserForm.email) }}
+                {{ form_errors(newUserForm.email) }}
+                {{ form_widget(newUserForm.email) }}
+            </div>
+        </fieldset>
+
+        {{ form_rest(newUserForm) }}
+    </form>
 {% endblock %}
 {% endblock %}
index 519b4ba25ddd37a0cfa921bb20e474e831c876ed..9b1a0986e9067b87dd5b7839d78cc0580f023465 100644 (file)
@@ -128,7 +128,7 @@ class ConfigControllerTest extends WallabagTestCase
                     'change_passwd[new_password][first]' => 'hop',
                     'change_passwd[new_password][second]' => 'hop',
                 ),
                     'change_passwd[new_password][first]' => 'hop',
                     'change_passwd[new_password][second]' => 'hop',
                 ),
-                'Password should by at least 6 chars long',
+                'Password should by at least',
             ),
         );
     }
             ),
         );
     }
@@ -260,4 +260,91 @@ class ConfigControllerTest extends WallabagTestCase
         $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text')));
         $this->assertContains('Information updated', $alert[0]);
     }
         $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text')));
         $this->assertContains('Information updated', $alert[0]);
     }
+
+    public function dataForNewUserFailed()
+    {
+        return array(
+            array(
+                array(
+                    'new_user[username]' => '',
+                    'new_user[password]' => '',
+                    'new_user[email]' => '',
+                ),
+                'This value should not be blank.',
+            ),
+            array(
+                array(
+                    'new_user[username]' => 'ad',
+                    'new_user[password]' => '',
+                    'new_user[email]' => '',
+                ),
+                'This value is too short.',
+            ),
+            array(
+                array(
+                    'new_user[username]' => 'wallace',
+                    'new_user[password]' => '',
+                    'new_user[email]' => 'test',
+                ),
+                'This value is not a valid email address.',
+            ),
+            array(
+                array(
+                    'new_user[username]' => 'wallace',
+                    'new_user[password]' => 'admin',
+                    'new_user[email]' => 'wallace@wallace.me',
+                ),
+                'Password should by at least',
+            ),
+        );
+    }
+
+    /**
+     * @dataProvider dataForNewUserFailed
+     */
+    public function testNewUserFailed($data, $expectedMessage)
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $crawler = $client->request('GET', '/config');
+
+        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+        $form = $crawler->filter('button[id=new_user_save]')->form();
+
+        $crawler = $client->submit($form, $data);
+
+        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+        $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text')));
+        $this->assertContains($expectedMessage, $alert[0]);
+    }
+
+    public function testNewUserCreated()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $crawler = $client->request('GET', '/config');
+
+        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+        $form = $crawler->filter('button[id=new_user_save]')->form();
+
+        $data = array(
+            'new_user[username]' => 'wallace',
+            'new_user[password]' => 'wallace1',
+            'new_user[email]' => 'wallace@wallace.me',
+        );
+
+        $client->submit($form, $data);
+
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+
+        $crawler = $client->followRedirect();
+
+        $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text')));
+        $this->assertContains('User "wallace" added', $alert[0]);
+    }
 }
 }