diff options
author | Jeremy <jeremy.benoist@gmail.com> | 2015-02-22 09:30:25 +0100 |
---|---|---|
committer | Jeremy <jeremy.benoist@gmail.com> | 2015-02-22 09:30:25 +0100 |
commit | e4977b8a866f84f65f08c55c050a62f40170fdbf (patch) | |
tree | f42886d6691d18ad8860b3ee2565f857204c7857 | |
parent | c0d9eba07f40a52bdfcfca3e7a926163b17d83ab (diff) | |
download | wallabag-e4977b8a866f84f65f08c55c050a62f40170fdbf.tar.gz wallabag-e4977b8a866f84f65f08c55c050a62f40170fdbf.tar.zst wallabag-e4977b8a866f84f65f08c55c050a62f40170fdbf.zip |
Adding new user
5 files changed, 181 insertions, 2 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index b3236e3c..aedbc999 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php | |||
@@ -6,9 +6,11 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |||
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7 | use Symfony\Component\HttpFoundation\Request; | 7 | use Symfony\Component\HttpFoundation\Request; |
8 | use Wallabag\CoreBundle\Entity\Config; | 8 | use Wallabag\CoreBundle\Entity\Config; |
9 | use Wallabag\CoreBundle\Entity\User; | ||
9 | use Wallabag\CoreBundle\Form\Type\ConfigType; | 10 | use Wallabag\CoreBundle\Form\Type\ConfigType; |
10 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | 11 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
11 | use Wallabag\CoreBundle\Form\Type\UserType; | 12 | use Wallabag\CoreBundle\Form\Type\UserType; |
13 | use Wallabag\CoreBundle\Form\Type\NewUserType; | ||
12 | 14 | ||
13 | class ConfigController extends Controller | 15 | class ConfigController extends Controller |
14 | { | 16 | { |
@@ -72,10 +74,28 @@ class ConfigController extends Controller | |||
72 | return $this->redirect($this->generateUrl('config')); | 74 | return $this->redirect($this->generateUrl('config')); |
73 | } | 75 | } |
74 | 76 | ||
77 | // handle adding new user | ||
78 | $newUser = new User(); | ||
79 | $newUserForm = $this->createForm(new NewUserType(), $newUser); | ||
80 | $newUserForm->handleRequest($request); | ||
81 | |||
82 | if ($newUserForm->isValid()) { | ||
83 | $em->persist($newUser); | ||
84 | $em->flush(); | ||
85 | |||
86 | $this->get('session')->getFlashBag()->add( | ||
87 | 'notice', | ||
88 | sprintf('User "%s" added', $newUser->getUsername()) | ||
89 | ); | ||
90 | |||
91 | return $this->redirect($this->generateUrl('config')); | ||
92 | } | ||
93 | |||
75 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( | 94 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( |
76 | 'configForm' => $configForm->createView(), | 95 | 'configForm' => $configForm->createView(), |
77 | 'pwdForm' => $pwdForm->createView(), | 96 | 'pwdForm' => $pwdForm->createView(), |
78 | 'userForm' => $userForm->createView(), | 97 | 'userForm' => $userForm->createView(), |
98 | 'newUserForm' => $newUserForm->createView(), | ||
79 | )); | 99 | )); |
80 | } | 100 | } |
81 | 101 | ||
diff --git a/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php b/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php index de0ad537..e141789f 100644 --- a/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php +++ b/src/Wallabag/CoreBundle/Form/Type/ChangePasswordType.php | |||
@@ -23,7 +23,7 @@ class ChangePasswordType extends AbstractType | |||
23 | 'constraints' => array( | 23 | 'constraints' => array( |
24 | new Constraints\Length(array( | 24 | new Constraints\Length(array( |
25 | 'min' => 8, | 25 | 'min' => 8, |
26 | 'minMessage' => 'Password should by at least 6 chars long', | 26 | 'minMessage' => 'Password should by at least 8 chars long', |
27 | )), | 27 | )), |
28 | new Constraints\NotBlank(), | 28 | new Constraints\NotBlank(), |
29 | ), | 29 | ), |
diff --git a/src/Wallabag/CoreBundle/Form/Type/NewUserType.php b/src/Wallabag/CoreBundle/Form/Type/NewUserType.php new file mode 100644 index 00000000..313a9aae --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/NewUserType.php | |||
@@ -0,0 +1,40 @@ | |||
1 | <?php | ||
2 | namespace Wallabag\CoreBundle\Form\Type; | ||
3 | |||
4 | use Symfony\Component\Form\AbstractType; | ||
5 | use Symfony\Component\Form\FormBuilderInterface; | ||
6 | use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
7 | use Symfony\Component\Validator\Constraints; | ||
8 | |||
9 | class NewUserType extends AbstractType | ||
10 | { | ||
11 | public function buildForm(FormBuilderInterface $builder, array $options) | ||
12 | { | ||
13 | $builder | ||
14 | ->add('username', 'text') | ||
15 | ->add('password', 'password', array( | ||
16 | 'constraints' => array( | ||
17 | new Constraints\Length(array( | ||
18 | 'min' => 8, | ||
19 | 'minMessage' => 'Password should by at least 8 chars long', | ||
20 | )), | ||
21 | new Constraints\NotBlank(), | ||
22 | ), | ||
23 | )) | ||
24 | ->add('email', 'text') | ||
25 | ->add('save', 'submit') | ||
26 | ; | ||
27 | } | ||
28 | |||
29 | public function setDefaultOptions(OptionsResolverInterface $resolver) | ||
30 | { | ||
31 | $resolver->setDefaults(array( | ||
32 | 'data_class' => 'Wallabag\CoreBundle\Entity\User', | ||
33 | )); | ||
34 | } | ||
35 | |||
36 | public function getName() | ||
37 | { | ||
38 | return 'new_user'; | ||
39 | } | ||
40 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig index 7a45ec1f..051dafd6 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Config/index.html.twig | |||
@@ -102,4 +102,36 @@ | |||
102 | 102 | ||
103 | {{ form_rest(pwdForm) }} | 103 | {{ form_rest(pwdForm) }} |
104 | </form> | 104 | </form> |
105 | |||
106 | <h2>{% trans %}Add a user{% endtrans %}</h2> | ||
107 | |||
108 | <form action="{{ path('config') }}" method="post" {{ form_enctype(newUserForm) }}> | ||
109 | {{ form_errors(newUserForm) }} | ||
110 | |||
111 | <fieldset class="w500p inline"> | ||
112 | <div class="row"> | ||
113 | {{ form_label(newUserForm.username) }} | ||
114 | {{ form_errors(newUserForm.username) }} | ||
115 | {{ form_widget(newUserForm.username) }} | ||
116 | </div> | ||
117 | </fieldset> | ||
118 | |||
119 | <fieldset class="w500p inline"> | ||
120 | <div class="row"> | ||
121 | {{ form_label(newUserForm.password) }} | ||
122 | {{ form_errors(newUserForm.password) }} | ||
123 | {{ form_widget(newUserForm.password) }} | ||
124 | </div> | ||
125 | </fieldset> | ||
126 | |||
127 | <fieldset class="w500p inline"> | ||
128 | <div class="row"> | ||
129 | {{ form_label(newUserForm.email) }} | ||
130 | {{ form_errors(newUserForm.email) }} | ||
131 | {{ form_widget(newUserForm.email) }} | ||
132 | </div> | ||
133 | </fieldset> | ||
134 | |||
135 | {{ form_rest(newUserForm) }} | ||
136 | </form> | ||
105 | {% endblock %} | 137 | {% endblock %} |
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php index 519b4ba2..9b1a0986 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php | |||
@@ -128,7 +128,7 @@ class ConfigControllerTest extends WallabagTestCase | |||
128 | 'change_passwd[new_password][first]' => 'hop', | 128 | 'change_passwd[new_password][first]' => 'hop', |
129 | 'change_passwd[new_password][second]' => 'hop', | 129 | 'change_passwd[new_password][second]' => 'hop', |
130 | ), | 130 | ), |
131 | 'Password should by at least 6 chars long', | 131 | 'Password should by at least', |
132 | ), | 132 | ), |
133 | ); | 133 | ); |
134 | } | 134 | } |
@@ -260,4 +260,91 @@ class ConfigControllerTest extends WallabagTestCase | |||
260 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text'))); | 260 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text'))); |
261 | $this->assertContains('Information updated', $alert[0]); | 261 | $this->assertContains('Information updated', $alert[0]); |
262 | } | 262 | } |
263 | |||
264 | public function dataForNewUserFailed() | ||
265 | { | ||
266 | return array( | ||
267 | array( | ||
268 | array( | ||
269 | 'new_user[username]' => '', | ||
270 | 'new_user[password]' => '', | ||
271 | 'new_user[email]' => '', | ||
272 | ), | ||
273 | 'This value should not be blank.', | ||
274 | ), | ||
275 | array( | ||
276 | array( | ||
277 | 'new_user[username]' => 'ad', | ||
278 | 'new_user[password]' => '', | ||
279 | 'new_user[email]' => '', | ||
280 | ), | ||
281 | 'This value is too short.', | ||
282 | ), | ||
283 | array( | ||
284 | array( | ||
285 | 'new_user[username]' => 'wallace', | ||
286 | 'new_user[password]' => '', | ||
287 | 'new_user[email]' => 'test', | ||
288 | ), | ||
289 | 'This value is not a valid email address.', | ||
290 | ), | ||
291 | array( | ||
292 | array( | ||
293 | 'new_user[username]' => 'wallace', | ||
294 | 'new_user[password]' => 'admin', | ||
295 | 'new_user[email]' => 'wallace@wallace.me', | ||
296 | ), | ||
297 | 'Password should by at least', | ||
298 | ), | ||
299 | ); | ||
300 | } | ||
301 | |||
302 | /** | ||
303 | * @dataProvider dataForNewUserFailed | ||
304 | */ | ||
305 | public function testNewUserFailed($data, $expectedMessage) | ||
306 | { | ||
307 | $this->logInAs('admin'); | ||
308 | $client = $this->getClient(); | ||
309 | |||
310 | $crawler = $client->request('GET', '/config'); | ||
311 | |||
312 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
313 | |||
314 | $form = $crawler->filter('button[id=new_user_save]')->form(); | ||
315 | |||
316 | $crawler = $client->submit($form, $data); | ||
317 | |||
318 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
319 | |||
320 | $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(array('_text'))); | ||
321 | $this->assertContains($expectedMessage, $alert[0]); | ||
322 | } | ||
323 | |||
324 | public function testNewUserCreated() | ||
325 | { | ||
326 | $this->logInAs('admin'); | ||
327 | $client = $this->getClient(); | ||
328 | |||
329 | $crawler = $client->request('GET', '/config'); | ||
330 | |||
331 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
332 | |||
333 | $form = $crawler->filter('button[id=new_user_save]')->form(); | ||
334 | |||
335 | $data = array( | ||
336 | 'new_user[username]' => 'wallace', | ||
337 | 'new_user[password]' => 'wallace1', | ||
338 | 'new_user[email]' => 'wallace@wallace.me', | ||
339 | ); | ||
340 | |||
341 | $client->submit($form, $data); | ||
342 | |||
343 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
344 | |||
345 | $crawler = $client->followRedirect(); | ||
346 | |||
347 | $this->assertGreaterThan(1, $alert = $crawler->filter('div.flash-notice')->extract(array('_text'))); | ||
348 | $this->assertContains('User "wallace" added', $alert[0]); | ||
349 | } | ||
263 | } | 350 | } |