对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中将操作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"
应该的.尚未测试失败案例,仅使用Google登录名进行了测试.
That should be it. Haven't tested failure case, only tested with google login.
这篇关于对Yii的OpenId支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!