예제 #1
0
 public function testPasswordStrengthValidator()
 {
     $user = new User('*****@*****.**');
     // By default, an empty password is not allowed.
     $error = $this->userManager->validatePasswordStrength($user, '');
     $this->assertNotEmpty($error);
     // By default, any non-empty password is allowed.
     $error = $this->userManager->validatePasswordStrength($user, 'a');
     $this->assertNull($error);
     // Test setting a custom validator.
     $this->userManager->setPasswordStrengthValidator(function (User $user, $password) {
         if (strlen($password) < 2) {
             return 'Password must have at least 2 characters.';
         }
     });
     $error = $this->userManager->validatePasswordStrength($user, 'a');
     $this->assertEquals('Password must have at least 2 characters.', $error);
 }
예제 #2
0
 /**
  * Edit user action.
  *
  * @param Application $app
  * @param Request $request
  * @param int $id
  * @return Response
  * @throws NotFoundHttpException if no user is found with that ID.
  */
 public function editAction(Application $app, Request $request, $id)
 {
     $errors = array();
     $user = $this->userManager->getUser($id);
     if (!$user) {
         throw new NotFoundHttpException('No user was found with that ID.');
     }
     $customFields = $this->editCustomFields ?: array();
     if ($request->isMethod('POST')) {
         $user->setName($request->request->get('name'));
         $user->setEmail($request->request->get('email'));
         if ($request->request->has('username')) {
             $user->setUsername($request->request->get('username'));
         }
         if ($request->request->get('password')) {
             if ($request->request->get('password') != $request->request->get('confirm_password')) {
                 $errors['password'] = '******'t match.';
             } else {
                 if ($error = $this->userManager->validatePasswordStrength($user, $request->request->get('password'))) {
                     $errors['password'] = $error;
                 } else {
                     $this->userManager->setUserPassword($user, $request->request->get('password'));
                 }
             }
         }
         if ($app['security']->isGranted('ROLE_ADMIN') && $request->request->has('roles')) {
             $user->setRoles($request->request->get('roles'));
         }
         foreach (array_keys($customFields) as $customField) {
             if ($request->request->has($customField)) {
                 $user->setCustomField($customField, $request->request->get($customField));
             }
         }
         $errors += $this->userManager->validate($user);
         if (empty($errors)) {
             $this->userManager->update($user);
             $msg = 'Saved account information.' . ($request->request->get('password') ? ' Changed password.' : '');
             $app['session']->getFlashBag()->set('alert', $msg);
         }
     }
     return $app['twig']->render($this->getTemplate('edit'), array('layout_template' => $this->getTemplate('layout'), 'error' => implode("\n", $errors), 'user' => $user, 'available_roles' => array('ROLE_PEON', 'ROLE_SUPER_PEON', 'ROLE_ADMIN'), 'image_url' => $this->getGravatarUrl($user->getEmail()), 'customFields' => $customFields, 'isUsernameRequired' => $this->isUsernameRequired));
 }