6

ZF2を使用してアプリケーションを開発しています。ユーザー名とパスワードでユーザー認証を行いました。しかし、認証で追加の列(例:ステータス)を確認したいと思います。

次のコードを実行しました。

public function authenticate()
{       
    $this->authAdapter = new AuthAdapter($this->dbAdapter,
            'usertable',
            'username',
            'password'
    );  

    $this->authAdapter->setIdentity($this->username)
                ->setCredential($this->password)
                ->setCredentialTreatment('MD5(?)');
    $result = $this->authAdapter->authenticate();
    return $result;
}

認証で列「ステータス」を確認するにはどうすればよいですか? 注: ステータス値は 1 にする必要があります。ありがとうございます。

4

2 に答える 2

7

zf2 と doctrine を使用して認証を構築していたとき、認証プラグインを作成し、認証用の追加の列を渡すためにこのアダプターをカスタマイズしました。おそらく、同様の方向に進む必要があります。

$adapter = new AuthAdapter($db,
                           'users',
                           'username',
                           'password',
                           'MD5(?)'
                           );

// get select object (by reference)
$select = $adapter->getDbSelect();
$select->where('active = "TRUE"');

// authenticate, this ensures that users.active = TRUE
$adapter->authenticate();

参照

変更後、コードは次のようになります。

public function authenticate()
{       
    $this->authAdapter = new AuthAdapter($this->dbAdapter,
            'usertable',
            'username',
            'password'
    );  

    $select = $this->authAdapter->getDbSelect();
    $select->where('status= "1"');
    $this->authAdapter->setIdentity($this->username)
                ->setCredential($this->password)
                ->setCredentialTreatment('MD5(?)');
    $result = $this->authAdapter->authenticate();
    return $result;
}
于 2013-01-23T11:25:15.200 に答える
1

ZF2 は、メソッドのおかげで、ID と資格情報で予測されるもの以外の列を使用して、追加のチェックを処理する別の方法を提供しますgetResultRowObject。この例の のすべての列は usertable、 によって返されるオブジェクトのプロパティとして利用できますgetResultRowObject()。したがって、これを使用してコードを拡張できます。

if ($result->isValid()) {
    $identityRowObject = $this->authAdapter->getResultRowObject();
    $status = $identityRowObject->status;
    // do whatever complex checking you need with $status...
}

よろしく、 マーク

于 2014-03-12T16:12:07.967 に答える