Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite minimum password lenght #987

Closed
runcom opened this issue Feb 3, 2013 · 9 comments
Closed

Overwrite minimum password lenght #987

runcom opened this issue Feb 3, 2013 · 9 comments

Comments

@runcom
Copy link

runcom commented Feb 3, 2013

How do I overwrite minimum passoword lenght in registration and set even some password requirements?

@Sharom
Copy link

Sharom commented Feb 3, 2013

You have to extend FOSUserBundle in yours UserBundle.
For example you have AcmeUserBundle. And you have class AcmeUserBundle. In this class you have to implement method getParent() like this:

namespace Acme\Bundle\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeUserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

Now. In the Resources/config subpath of your bundle create file validation.xml:

<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
        http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

    <class name="FOS\UserBundle\Model\User">

        <property name="plainPassword">
            <constraint name="NotBlank">
                <option name="message">fos_user.password.blank</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>ResetPassword</value>
                    <value>ChangePassword</value>
                </option>
            </constraint>
            <constraint name="Length">
                <option name="min">6</option> <!-- !!! change password length here !!! -->
                <option name="minMessage">fos_user.password.short</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>Profile</value>
                    <value>ResetPassword</value>
                    <value>ChangePassword</value>
                </option>
            </constraint>
        </property>
    </class>

</constraint-mapping>

You can also use yml for validation's rules.
Now yours rules will be merged with rules from FOSUserBundle.

*Read more about bundle inheritance here:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html

@kieste
Copy link

kieste commented Aug 16, 2013

Hi Sharom,

how can I replace the rules and not merge them with the validation.xml from parent bundle?
I want to disable some of the parent rules.

@Sharom
Copy link

Sharom commented Aug 16, 2013

Hi @kieste.

I am not sure that it is exclusive solution.
But you could implement FOS\UserBundle\Model\UserInterface instead of extending FOS\UserBundle\Model\User class.

In this way you may write clean validation's constraints for you entity.

@sstok
Copy link

sstok commented Aug 18, 2013

Thats one way but it will not work as the https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Security/UserProvider.php#L59 has a hard requirement for the User object.

#1172 #1178

@gondo
Copy link

gondo commented Jan 29, 2014

how to do this in validation.yml ?

@lancergr
Copy link

lancergr commented Feb 3, 2014

@gondo, Something like this will do it:

Acme\UserBundle\Entity\User:
    properties:
        plainPassword:
          - NotBlank:
              message: "not blank message"
          - Length:
              min: 5
              max: 255
              minMessage: "too short"
              maxMessage: "too large"
              groups: [Registration,Profile]

@gondo
Copy link

gondo commented Feb 3, 2014

@lancergr nope, tried exactly that and for some reason it was ignored. i even tried it with FOS\UserBundle\Model\User: and nothing. (and while i was trying validation.yml i've deleted validation.xml file) so i ended up having the only xml config file in my app for FOSUser

@addyClass
Copy link

Hi ,

I am using FOS bundle in my project. I create a new controller ChangePasswordControlle in my admin bundle. Here is my code.

namespace Pondip\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\ChangePasswordController as BaseController;

/**

  • Controller managing the change password
    *
  • @author Aman
    /
    class ChangePasswordController extends BaseController
    {
    /
    *
    • This action is used for change password for admin.

    • @author Aman

    • @return render view

    • @throws AccessDeniedException as exception

    • @
      _/
      public function changePasswordAction()
      {
      $user = $this->container->get('security.context')->getToken()->getUser();
      /_if (!is_object($user) || !$user instanceof UserInterface) {
      throw new AccessDeniedException('This user does not have access to this section.');
      }*/

      $form = $this->container->get('fos_user.change_password.form');
      $formHandler = $this->container->get('fos_user.change_password.form.handler');

      $process = $formHandler->process($user);
      if ($process) {
      $this->setFlash('fos_user_success', 'Password has been changed successfully');

      $url = $this->container->get('router')->generate('pondip_admin_changepassword');
      return new RedirectResponse($url);
      

      }

      return $this->container->get('templating')->renderResponse(
      'PondipAdminBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
      array('form' => $form->createView())
      );
      }

}

Now i want to customize error message for current password. So please help me urgent.

@runcom runcom closed this as completed May 13, 2016
@506574657220426F656C650D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants