0

こんにちは皆さん、私は Zend Framework 2 の初心者です。私のプロジェクトのルーテンシフィケーションのために、このモジュールを使用しました (( http://samsonasik.wordpress.com/2013/05/29/zend-framework-2-working-with-authenticationservice- and-db-session-save-handler/#comment-5393 )) データベースに「ロール」フィールドを追加します。

ユーザーの任意のメンバーに対して特定のルートを作成する方法を知りたいです。たとえば、ユーザーの管理者が接続した場合、ルート「管理者」に自動的にリダイレクトされ、ユーザーの「訪問者」の場合、ルート「」にリダイレクトされます。ビジター」???

どうも

4

1 に答える 1

0
/** this function called by indexAction to reduce complexity of function */
protected function authenticate($form, $viewModel)
{
    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $dataform = $form->getData();

            $this->authService->getAdapter()
                                   ->setIdentity($dataform['username'])
                                   ->setCredential($dataform['password']);
            $result = $this->authService->authenticate();
            if ($result->isValid()) {
                //authentication success
                $resultRow = $this->authService->getAdapter()->getResultRowObject();

                $this->authService->getStorage()->write(
                     array('id'          => $resultRow->id,
                            'username'   => $dataform['username'],
                            'ip_address' => $this->getRequest()->getServer('REMOTE_ADDR'),
                            'user_agent'    => $request->getServer('HTTP_USER_AGENT'))
                );

                // your userid -> select the role
                $role = $this->getRoleUser($resultRow->id);
                return $this->redirect()->toRoute('success', array('action' => 'index', 'role'=>$role));


            } else {
                $viewModel->setVariable('error', 'Login Error');
            }
        }
    }
}

次に、成功ページで、paramロールを使用していくつかのアクションを実行します

関数$role = $this->getRoleUser($resultRow->id);を作成することを忘れないでください。ユーザーの役割を取得します。

ロール機能を実装するには

このドキュメントの前に、モデル/データベースの構成および作成方法を確認してください: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html

protected function getRoleUser($userid){
  $table = $this->getServiceLocator()->get('User\Model\UserTable');
  return $table->find($userid)->current()->role;
}
于 2013-06-27T12:22:10.417 に答える