2015-05-21 19 views
0

我有以下代碼,哪些工作,但現在下一步。Zend 2登錄後如何設置用戶

如何以及在哪裏設置會話,以便腳本「看到」用戶已經登錄?

if ($form->isValid()) { 
    $securePass = $this->getUsersTable()->getUserByUsername($this->params()->fromPost('username'));  
    if($securePass){ 
     $bcrypt = new Bcrypt(); 
     if ($bcrypt->verify($this->params()->fromPost('password') , $securePass->password)) { 

      $sm   = $this->getServiceLocator(); 
      $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
      $authAdapter = new AuthAdapter(
        $dbAdapter, 
        'users', 
        'username', 
        'password' 
        ); 
      $authAdapter 
       ->setIdentity($securePass->username) 
       ->setCredential($securePass->password);       

      $result = $authAdapter->authenticate($authAdapter); 
      echo $result->getIdentity() . "\n\n"; 
     } 
     else { 

     } 

回答

0

LoginController.php

if ($form->isValid()) { 
    $securePass = $this->getUsersTable()->getUserByUsername($this->params()->fromPost('username'));  
    if($securePass){ 
     $bcrypt = new Bcrypt(); 
     if ($bcrypt->verify($this->params()->fromPost('password') , $securePass->password)) { 

      $sm   = $this->getServiceLocator(); 
      $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
      $authAdapter = new AuthAdapter(
       $dbAdapter, 
       'users', 
       'username', 
       'password' 
      ); 
      $authAdapter->setIdentity($securePass->username) 
      ->setCredential($securePass->password);       
      $result = $authAdapter->authenticate($authAdapter); 

      $sesssionData = $authAdapter->getResultRowObject(); 

      $auth = new AuthenticationService(); 
      $storage = $auth->getStorage(); 
      $storage->write($sesssionData); 

      return $this->redirect()->toRoute('user_list'); 
     } 
    } 
} 



public function onBootstrap(MvcEvent $e) 
{ 

    $eventManager  = $e->getApplication()->getEventManager(); 
    $moduleRouteListener = new ModuleRouteListener(); 
    $moduleRouteListener->attach($eventManager); 
    $app   = $e->getParam('application'); 
    $app->getEventManager()->attach('render', array($this, 'setLayoutTitle')); 
$eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'checkLogin')); 
} 

public function checkLogin(MvcEvent $e) 
{ 
    $iden = new AuthenticationService(); 
    if($iden->getIdentity() === NULL){ 
     $matches = $e->getRouteMatch(); 
     $controller = $matches->getParam('controller'); 
     $getController = explode('\\', $controller); 

     if(isset($getController[2]) && $getController[2] != 'Login'){ 
      $controller = $e->getTarget(); 
      return $controller->plugin('redirect')->toRoute('login'); 
     } 
    } 
} 
1

這樣做的Zend方式是使用身份驗證組件爲您處理此問題。

http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html

這將允許你檢查,如果用戶登錄(你將不得不安裝認證適配器在前):

use Zend\Authentication\AuthenticationService; 
// TODO set-up authentication adapter 
$auth = new AuthenticationService() 
$identity = $auth->getIdentity(); 

對於訪問後的數據,你也應該充分利用框架,而不是直接訪問$ _POST。在你的控制器:

$this->params()->fromPost('username'); 
$this->params()->fromPost('password'); 

這將引導用戶添加驗證層到您的應用程序的全過程:

https://zf2.readthedocs.org/en/latest/modules/zend.authentication.adapter.dbtable.html

+0

這說明如何獲得一個身份,但沒有說明如何設置它擺在首位。 – srayner

+0

我已經改變了我的腳本,並且工作。這工作正常,爲什麼這麼複雜?現在我可以檢查$ container-> access是1還是0 – Bas

+1

@Bas如果你認爲這個答案很複雜,那麼我很抱歉地說zf2不適合你。這個答案顯示瞭如何使用'Zend \ Authentication'組件登錄用戶。用你的方式,所有的域邏輯被添加到控制器中,這使得控制器太臃腫。所以,這也違反了MVC結構! –

1

使用Zend公司提供的AuthenticationService,設置用戶在PHP會議被自動照顧。

好事,瞭解認證機制將是閱讀和代碼與此介紹給身份驗證一起: http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html#adapters

在自定義AuthenticationAdapter「設置用戶的會話」,或身份持久性,將通過在authenticate()方法中返回認證結果和用戶標識來返回\ Zend \ Authentication \ Result來完成。

$user = $this->userService->findByEmail($this->email); 

if($user !== false) { 
    if($this->encryption->verify($this->password, $user->getPassword()) { 
     return new Result(Result::SUCCESS, $user); 
    } 

    return new Result(Result::FAILURE, null); 
} 

$this->userService being the UserService that leads to the UserMapper 
(more about Services: http://framework.zend.com/manual/current/en/in-depth-guide/services-and-servicemanager.html) 
$user being the User entity with the encrypted password stored 
$this->encryption being your encryption method (Zend\Crypt\Password\Bcrypt for example) 
$this->email being the email/username provided by the form 
$this->password being the password provided by the form 
Result being Zend\Authentication\Result 

這是一個簡單的方法。更詳細的結果類型是:

/** 
* General Failure 
*/ 
const FAILURE      = 0; 
/** 
* Failure due to identity not being found. 
*/ 
const FAILURE_IDENTITY_NOT_FOUND  = -1; 
/** 
* Failure due to identity being ambiguous. 
*/ 
const FAILURE_IDENTITY_AMBIGUOUS  = -2; 
/** 
* Failure due to invalid credential being supplied. 
*/ 
const FAILURE_CREDENTIAL_INVALID  = -3; 
/** 
* Failure due to uncategorized reasons. 
*/ 
const FAILURE_UNCATEGORIZED   = -4; 
/** 
* Authentication success. 
*/ 
const SUCCESS      = 1;