增加了OpenController,增加了accountController中关于Oauth交互登录的功能

This commit is contained in:
Li Jianxuan 2013-11-21 06:21:22 +00:00
parent 0e11fd37d7
commit 6ab8c24f94
7 changed files with 330 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<?php
use Helpers\View as view;
use Mail\Mail;
use Open\Client;
class AccountController extends Zend_Controller_Action
{
@ -590,5 +591,51 @@ class AccountController extends Zend_Controller_Action
} //找回密码
//OAuth2登录跳转页面
public function oauth2loginAction()
{
$type = $this->_getParam('type');
$client = new Client();
$url = $client->makeRequestCodeUrl($type);
view::Post($this,"为您转入科技网登录入口",$url);
}
//oauth2登录回调地址
public function callbackAction()
{
$type = $this->_getParam('type');
$code = $this->_getParam('code');
$client = new Client($type);
$client->initSource();
$target = $client->getSource()->getTarget($type);
$this->view->target_name = $target['name'];
//$token = $client->requestToken($code);
$token = array(
"expires_in"=>(int)432000,
"refresh_token"=>(string)"eb40e792e05a21e779aa89d71066fa61",
"userInfo"=>(string)'{"passwordType":"password_umt","securityEmail":"liujin834@live.com","cstnetIdStatus":"active","umtId":"10247308","truename":"李建轩","cstnetId":"liujin834@live.com","type":"umt"}',
"access_token"=>(string)"48415c56215f5cd26baabe403864aaf7"
);
if(is_string($token))
{
view::Post($this,$token,'/account/login');
}
$status = $client->storageTokenData($type,$token);
if($status === true)
{
echo "<script>self.location='/index'</script>";
}else{
echo $status;
}
}
}

View File

@ -0,0 +1,165 @@
<?php
use \Helpers\View as view;
use \Open\App;
use \Open\Open;
use \Open\OAuth2\Server;
class OpenController extends \Zend_Controller_Action
{
private $config = NULL;
private $db = NULL;
private $open = NULL;
function preDispatch()
{
$this->view->config = $this->config = \Zend_Registry::get('config');
$this->db = \Zend_Registry::get('db');
$this->view->theme = new Theme();
$this->_helper->layout->setLayout('layout-open');
}
function indexAction()
{
$this->view->pageID = "open-index";
}
//我的应用
function myappAction()
{
$this->view->pageID = "open-myapp";
$app = new App();
$user_state = $app->checkinfo();
if( $user_state !== true)
{
view::post($this,$user_state,"/account/edit");
}
$ac = $this->_getParam('ac');
$id = $this->_getParam('id');
$submit = $this->_getParam('submit');
if(empty($ac))
{
$this->view->myapp = $app->getUserApp();
return true;
}
if($ac == "create")
{
$this->_helper->viewRenderer('myapp-create');
$this->view->appStatus = $app->appStatus();
if(!empty($id))
{
$this->view->info = $app->getAppInfo($id);
}
if(!empty($submit))
{
if(empty($id))
{
$status = $app->appCreate();
}else{
$this->view->info = $app->getAppCreateParam();
$status = $app->appCreate($id);
}
if($status !== true && !is_numeric($status))
{
$this->view->error = view::Error($status);
}else{
if(!empty($id))
view::Post($this,"修改成功!",'/open/myapp/ac/view/id/'.$id);
else
view::Post($this,"添加成功!","/open/myapp/ac/view/id/".$status);
}
}
return true;
}
if($ac == "delete")
{
$status = $app->delete($id);
if($status !== true)
{
view::Post($this,$status,"/open/myapp");
}else{
view::Post($this,'删除成功',"/open/myapp");
}
return false;
}
if($ac == "view")
{
$this->_helper->viewRenderer('myapp-view');
$this->view->data = $app->getAppInfo($id);
$this->view->appStatus = $app->appStatus();
return true;
}
}
function authenticateAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data = array(
'client_id' => $this->_getParam('client_id'),
'redirect_uri' => $this->_getParam('redirect_uri'),
'client_secret' => $this->_getParam('client_secret'),
'host' => $_SERVER['HTTP_HOST'],
'ip' => $_SERVER['REMOTE_ADDR']
);
$server = new Server();
$status = $server->clientCredentials($data['client_id'],$data['client_secret']);
if($status !== true)
{
echo $status;
}else{
}
}
function authorizeAction()
{
$submit = $this->_getParam('submit');
if(empty($submit))
{
return true;
}
$sv = new open\server();
$server = $sv->bootstrap();
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
// validate the authorize request
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
// print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'yes');
$server->handleAuthorizeRequest($request, $response, $is_authorized);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
exit("SUCCESS! Authorization Code: $code");
}
$response->send();
}
}

View File

@ -0,0 +1,43 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('网站接入');
$this->headTitle()->setSeparator(' - ');
?>
<div class="row">
<div class="span3">
<div class="well sidebar-nav">
<?= $this->partial('/account/left.phtml'); ?>
</div><!--/.well -->
</div><!--/span-->
<div class="span9">
<h3>应用接入</h3>
<hr />
<?php if(!empty($this->msg)) { ?>
<?= $this->msg ?>
<?php }else{?>
<?php if(!empty($this->error)) { ?>
<?= $this->error ?>
<?php } ?>
<form action="" method="post" class="form-horizontal">
<div class="control-group ">
<label class="control-label" for="name">名称</label>
<div class="controls">
<input type="text" id="name" name="name" value="<?php echo empty($this->info['name']) ? "":$this->info['name']; ?>" />
</div>
</div>
<div class="control-group ">
<label class="control-label" for="name">重定向地址</label>
<div class="controls">
<input type="text" id="uri" name="uri" value="<?php echo empty($this->info['uri']) ? "":$this->info['uri']; ?>" />
</div>
</div>
<div class="form-actions">
<input type="hidden" name="submit" value="submit" />
<button type="submit" class="btn btn-primary">确定</button>
<button type="button" class="btn">取消</button>
</div>
</form>
<?php }?>
</div><!--/span-->
</div><!--/row-->

View File

@ -0,0 +1,25 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('我的账户');
$this->headTitle()->setSeparator(' - ');
?>
<div class="row">
<div class="span3">
<div class="well sidebar-nav">
<?= $this->partial('account/left.phtml'); ?>
</div><!--/.well -->
</div><!--/span-->
<div class="span9">
<a href="/account/application/ac/add" class="btn btn-info pull-right">应用接入</a>
<h3>应用管理</h3>
<hr />
<?php if(!empty($this->msg)) { ?>
<?= $this->msg ?>
<?php }else{?>
<?php if(!empty($this->error)) { ?>
<?= $this->error ?>
<?php } ?>
<?php }?>
</div><!--/span-->
</div><!--/row-->

View File

@ -0,0 +1,46 @@
<?php
$this->headTitle($this->config->title->site);
$this->headTitle('用户登录');
$this->headTitle()->setSeparator(' - ');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/account/login">用户登录</a>');
$this->breadcrumb()->setSeparator(' > ');
?>
<style>
.login_box {
max-width: 300px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
</style>
<div class="row-fluid">
<div class="span12">
<?= empty($this->target_name) ? "":"您已经使用 <strong>".$this->target_name."</strong> 账号登录<hr />" ?>
</div>
</div>
<div class="row-fluid">
</div>
<script>
$('#captcha_img').click(function(e) {
changecaptcha();
});
function changecaptcha(){
$.ajax({
url:"/account/captcha",
data:"<?= time() ?>",
success: function(src){
document.getElementById('captcha_img').src = src;
}
});
}
</script>

View File

@ -2,6 +2,7 @@
<li class="nav-header">应用</li>
<li id="Nav-account-dataorder"><a href="/data/order">数据篮</a></li>
<!-- <li id="Nav-account-myfav"><a href="/account/myfav">我的收藏</a></li> -->
<!--<li id="Nav-account-application"><a href="/account/application">我的应用</a></li>-->
<li class="nav-header">账户</li>
<li id="Nav-account-index"><a href="/account">账户信息</a></li>
<li id="Nav-account-secure"><a href="/account/secure">安全性</a></li>

View File

@ -64,6 +64,9 @@
</div>
</div>
</form>
<hr />
<h5>其它账号登录</h5>
<a href="/account/oauth2login/type/escience">中国科技网通行证</a>
</div>
<script>
$('#captcha_img').click(function(e) {