2

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

大卫

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

4

1 回答 1

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 您需要使用您的 URL 参数添加路由,以便在 $matchParams = $match->getParams(); 中接收它 如果我描述过您将获得身份验证并立即重定向到该站点。示例: http ://example.com/someController/someAction/param1/param2/hash ...结果将是身份验证并打开页面http://example.com/someController/someAction/param1/param2/hash ...

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

PS 一些想法来自Zend Framework 2 - Global check for authentication with ZFCUser

于 2013-04-01T19:33:21.600 回答