Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 security: account activation

Tags:

symfony

I'm using Symfony 2 security system.

When some user trying to login, I want to additionally check whether user's field "activated" is true. If not, error message appears: "You have to activate your account first".

How can i implement this feature?

like image 720
User Created Image Avatar asked Jan 23 '12 17:01

User Created Image


1 Answers

If you're using Doctrine as user provider you can implement the AdvancedUserInterface. This interface (definition visible below) provide the isEnabled() method which is equal to account activation status method. If this method return false, the user will have an error message like Your account is not enabled when he attempt to login.

I use it to provide e-mail validation on subscription.

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\User;

/**
 * AdvancedUserInterface adds status flags to a regular account.
 *
 * @author Fabien Potencier <[email protected]>
 */
interface AdvancedUserInterface extends UserInterface
{
    /**
     * Checks whether the user's account has expired.
     *
     * @return Boolean true if the user's account is non expired, false otherwise
     */
    function isAccountNonExpired();

    /**
     * Checks whether the user is locked.
     *
     * @return Boolean true if the user is not locked, false otherwise
     */
    function isAccountNonLocked();

    /**
     * Checks whether the user's credentials (password) has expired.
     *
     * @return Boolean true if the user's credentials are non expired, false otherwise
     */
    function isCredentialsNonExpired();

    /**
     * Checks whether the user is enabled.
     *
     * @return Boolean true if the user is enabled, false otherwise
     */
    function isEnabled();
}
like image 137
BlackCharly Avatar answered Sep 30 '22 18:09

BlackCharly