Yii 的 OpenId 支持 [英] OpenId support for Yii
问题描述
我想在 Yii 中使用 OpenID 支持.
I want to play with OpenID support in Yii.
在研究了可能的插件后,我找到了这两个.一个用于 OpenidSelector,一个用于 LightOpenId
After researching for possible plugins, I found these two. One for OpenidSelector and one for LightOpenId
http://www.yiiframework.com/extension/simpleopenidselector/
http://www.yiiframework.com/extension/loid
这些是在 Yii 中用于 OpenId 支持的正确扩展吗?还要别的吗?如果这些扩展是正确的,我想就如何处理这些扩展获得一些指导.
Are these the right extensions to use in Yii for OpenId support? Anything else? And I would like to get some guide line on what to do with these extensions if they are correct.
这是我认为除了按照页面上的说明安装它们之外我还需要做的事情.
This is what I think I need to do beside installing them as per instructions on the page.
- 创建 OpenIdUserIdentity 扩展 CUserIdentity 并将 authenticate() 代码放在那里
- 创建一个登录页面并将 simpleopenidselector 代码放入视图中.
- 在 siteController 中创建 actionOpenIdLogin 方法
然后我有点迷茫,因为我不理解 Loid 中的用法示例,我不知道如何做上面的 (1) 和 (3).
then I am kind of lost as I don't understand the Usage sample in Loid and I am not sure how to do (1) and (3) above.
请让我知道我是否在正确的轨道上,并可能提供一些指导.谢谢.
Please let me know if I am on the right track and possibly provide some guidance. Thanks.
推荐答案
玩了一会儿,我来回答我自己的问题.这就是我使其工作的方式,因此您可以根据需要进行更改.
After playing with it for awhile, I am going to answer my own question. This is how I make it to work, so you can change it according to your needs.
注意:我使用 userController 而不是 siteController,请按照相应扩展页面中的所有说明进行操作.
Note: I use a userController instead of the siteController and please follow all the instructions in the respective extension page.
如果你使用了上面提到的两个插件,那么接下来你需要做的事情如下:(这是一个分步指南)但最重要的步骤是 2c 和 3,它们是两个插件的粘合剂
If you used the two plugins as indicated above, then what you need to do next to make it work are the followings: (this is a step by step guide) But the most important steps are 2c and 3, they are the glue to both plugins
1) 有一个使用 OpenidSelector 的登录页面.把它放在 views/user/login.php
1) Have a login page that uses the OpenidSelector. Place it at views/user/login.php
<?php
$this->widget('application.extensions.openidProviders.openidProviders',
array ( 'options' => array ( 'lang' => 'en',
// 'demo' => 'js:true',
'cookie_expires' => 6*30,
)));?>
2) 设置操作来处理来自 openidSelector 的选择.我把它放在 userController 中.
2) Setup actions to handle the selection from the openidSelector. I put this in the userController.
a) 在主配置文件中.
a) In main config file.
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'loginUrl' => array('/user/login'), //change the default login page
),
b) 在 userController 文件中,添加登录和验证操作
b) In userController file, add login and authenticate actions
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('login', 'authenticate'),
动作代码#1 actionLogin - 这是触发登录视图页面.
Code for action #1 actionLogin - this is to trigger the login view page.
public function actionLogin()
{
// display the login form
$this->render('login',array());
}
c) 操作 #2 actionAuthenticate 的代码 - 从 LOID 指令页面修改的代码,这是在登录页面中选择 OpenIDProvider 时处理的.
c) Code for action #2 actionAuthenticate - code modified from the LOID instruction page, this is to handle when an OpenIDProvider is selected in the login page.
public function actionAuthenticate ()
{
// Put the Simple usage: code on
// http://www.yiiframework.com/extension/loid here:
// Code from loid Simple usage page.
// START HERE
$loid = Yii::app()->loid->load();
if (!empty($_GET['openid_mode'])) {
if ($_GET['openid_mode'] == 'cancel') {
$err = Yii::t('core', 'Authorization cancelled');
} else {
try {
echo $loid->validate() ? 'Logged in.' : 'Failed';
} catch (Exception $e) {
$err = Yii::t('core', $e->getMessage());
}
}
if(!empty($err)) echo $err;
} else {
// **NOTE:Comment out this line from the loid sample page**
// $loid->identity = "http://my.openid.identifier"; //Setting identifier
// this openid_identifier is need after you click the openselector
$loid->identity = $_GET['openid_identifier']; // CHANGE HERE
$loid->required = array('namePerson/friendly', 'contact/email'); //Try to get info from openid provider
$loid->realm = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
$loid->returnUrl = $loid->realm . $_SERVER['REQUEST_URI']; //getting return URL
if (empty($err)) {
try {
$url = $loid->authUrl();
$this->redirect($url);
} catch (Exception $e) {
$err = Yii::t('core', $e->getMessage());
}
}
}
// Code from loid Simple usage page.
// END HERE
}
3) 将 openidProviders/views/main-en.php 中的 action URL 改为 Authenticate
3) Change the action URL to Authenticate in the openidProviders/views/main-en.php
改变
form action="examples/consumer/try_auth.php" method="get" id="openid_form"
到
form action="authenticate" method="get" id="openid_form"
应该是这样.没有测试失败案例,只测试了谷歌登录.
That should be it. Haven't tested failure case, only tested with google login.
这篇关于Yii 的 OpenId 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!