Zf2 + Doctrine 2 Authentication Adapter restrict to fetch delete flag is equal to true row

150 Views Asked by At

I have an issue like, in my account table delete flag column is there. So if I delete a user it will set as 1. And i can create new user with same mail id. At that time this code fails, there are two records with same mail id. Is there any way to give delete flag also like email. My module.config.php:

'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
            )
        )
    ),
    'authentication' => array(
        'orm_default' => array(
            'object_manager' => 'Doctrine\ORM\EntityManager',
            'identity_class' => 'Authentication\Entity\Account',
            'identity_property' => 'email',
            'credential_property' => 'password',
            'credential_callable' => function(Account $account, $passwordGiven) {
                $userPassword = new UserPassword();

                $res = $userPassword->verify($account->getPassword(), $passwordGiven);

                return $res && !$account->getDeleteFlag() && $account->getStatus();
            },
        ),
    )        
)
1

There are 1 best solutions below

0
fkupper On

I don't know if this works on ZF2, i'm currently working with ZF1, but I had kind of the same problem.

The solution was to write a custom Auth Adapter. This class implements Zend_Auth_Adapter_Interface and requires the authenticate method.

In my implementation of authenticate I have included the custom verification for a flag plus username and password.

So, when I have to check an user:

$authAdapter = new MyAuthAdapter();
// I need only to set these values since the flag is always considered
// to be false so the user can authenticate
$authAdapter->setCredential($password)
            ->setIdentity($username);
          //->setDeleteFlag($yourFlag) you could also implement this
Zend_Auth::getInstance()->authenticate($authAdapter)->isValid();

Hope it helps since it's on ZF1.