Zend框架2 + Android Mobile + ZfcUser身份验证

时间:2013-03-29 10:09:02

标签: android mobile zend-framework2 sessionid zfcuser

我需要在我的网站上通过Android进行身份验证(Zend Framework2 + ZfcUser + ZfcUserDoctrineORM)。 我想调用一个验证我的URL并返回一个包含我的session_id的json对象。 我不知道这是不是正确的方法,但我不知道如何用zfcUser做到这一点。

大卫

接下来,我将能够将此session_id存储到共享首选项存储中。

1 个答案:

答案 0 :(得分: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 您需要使用您的网址参数添加路线,以便在$ matchParams = $ match-> getParams(); 如果我描述你将是auth并立即重定向到该网站。 例: http://example.com/someController/someAction/param1/param2/hash ... 结果将是auth并打开页面http://example.com/someController/someAction/param1/param2/hash ...

确定。这是我的应用程序所需要的。希望这有帮助。

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