2013-03-29 124 views
2

我需要通過我的網站上的Android進行身份驗證(Zend Framework2 + ZfcUser + ZfcUserDoctrineORM)。 我想調用一個url來驗證我並返回一個包含我的session_id的json對象。 我不知道這是否是正確的方式,但無論我不知道如何用zfcUser做到這一點。Zend framework 2 + Android Mobile + ZfcUser身份驗證

大衛

接下來,我將能夠將此SESSION_ID保存到共享偏好存儲。

+1

會話ID通常在標頭中使用值set-cookie –

回答

0

首先感謝我的英語。 在我的應用程序,我需要幾乎相同。 好的。因此,在yoursite.com/module/YourModuleName/Module.php中: 使用YourModuleName \ Model \ YourModuleName;

use YourModuleName\Model\YourModuleName; 
class Module { 
public function onBootstrap(MvcEvent $e) { 
     $app = $e->getApplication(); 
     $em = $app->getEventManager(); 
     $sm = $app->getServiceManager(); 
     $auth = $sm->get('zfcuser_auth_service'); 

     $model = new OrdersManager(); 

     if (!$auth->hasIdentity()) { 
      $em->attach(MvcEvent::EVENT_ROUTE, function($e) use ($app, $sm, $auth, $model) { 
       $match = $e->getRouteMatch(); 

       // No route, this is a 404 
       if (!$match instanceof RouteMatch) { 
        return; 
       } 

       $match = $e->getRouteMatch(); 
       $matchParams = $match->getParams(); 
       // $matchParams['hash'] == some url param 
       if (isset($matchParams['hash'])) { 

        $model->setDbAdapterColibo($sm->get('dbAdapter')); 
        $usersSqlObject = $model->getUsers(); 

        $salt = md5('caw'); 

        foreach ($usersSqlObject as $key => $user) { 
         $hash = hash('sha256', $salt.$param1.$user['user_id']); 
         if ($hash == $matchParams['hash']) { 
          $authAdapter = $sm->get('ZfcUser\Authentication\Adapter\AdapterChain'); 
          $request = $app->getRequest(); 
          $request->getPost()->set('identity', $user['email']); 
          // You may use user password to auth user 
          $request->getPost()->set('credential', $user['user_id']); 
          $result = $authAdapter->prepareForAuthentication($request); 
          $auth->authenticate($authAdapter); 

          // do your staff with session or other. 
          // after this you will be redirect to page from where query was 

          break; 
         } 
        } 
       } 
      }); 
     } 

    } 
} 

唐`噸忘記yoursite.com/module/YourModuleName/config/module.config.php 您需要使用您的網址PARAM添加路線,接受它在$ matchParams = $匹配 - > getParams (); 如果我已經描述過,您將被授權並立即重定向到該網站。 例子: http://example.com/someController/someAction/param1/param2/hash ... 結果將是權威性和開放的網頁http://example.com/someController/someAction/param1/param2/hash ...

確定。這是我需要的應用程序。希望這個幫助。

P.S.一些想法從Zend Framework 2 - Global check for authentication with ZFCUser