1
votes

I am implementing my own profile settings page. This will allow the user to change their name, email, password etc.

I understand FOSUserBundle has prebuilt /profile/edit and /profile/change-password. I would like to use the same functionality here except in my own twig bundles. I am not using FormBuilder.

This is my controller for changing user settings.

public function applySettings(Request $request)
{ 
    $user = $this->getUser();

    $dm = $this->get('doctrine_mongodb')->getManager();
    $repository = $dm->getRepository('AppBundle:User');

    $name = $request->get('_username');
    $email = $request->get('_email');
    $password = $request->get('_password');
    $confirm = $request->get('_confirm');

    if ($name != null) {
        if ($name == ($repository->findOneBy(array('username' => $name )))) { 
            throw $this->createNotFoundException('Username already in use');
        } else { 
            // username changed
            $user->setUsername($name); 
        }         
    }

    if ($email != null) {
        if ($email == ($repository->findOneBy(array('email' => $email )))) { 
            throw $this->createNotFoundException('Email already in use');
        } else { 
            // email changed
            $user->setEmail($email);    
        }      
    }

    if (strcmp($password, $confirm) == 0) {
        // password = confirm here
        // change password functionality done here
    } else {
        throw $this->createNotFoundException(
            'Passwords do not match '
        );
    }

    $dm->flush();
    return $this->redirectToRoute('settings');
}

Is there a way I can do password validation in my own controller, salt and store the password if valid?

Also is there a way to apply the same validation methods that exist in /register/ and /profile/change-password where the alert comes up if the passwords do not match before submitting?

Any help would be greatly appreciated.

I am using Symfony 2.6.1 and the latest version of FOSUserBundle.

1
just check the documentation on how to override a given controller / action. symfony.com/doc/current/bundles/FOSUserBundle/… - MouradK

1 Answers

0
votes

This is the function I used. Works but doesn't do the live validation check.

    if (strcmp($password, $confirm) == 0) {
        $encoder_service = $this->get('security.encoder_factory');
        $encoder = $encoder_service->getEncoder($user);

        $newpass = $encoder->encodePassword($password, $user->getSalt());
        $user->setPassword($newpass);
    } else {
        throw $this->createNotFoundException(
            'Passwords do not match '
        );
    }