我在yii框架中有两个项目,我想使用SimpleSAMLphp和SSO两个项目。我需要的条件是,如果我从第一个项目登录,我想访问第二个项目。 提前谢谢。
答案 0 :(得分:1)
首先通过暂时禁用Yii自动加载器来加载SAML库。这只是为了让您使用SAML类和方法:
<?php
class YiiSAML extends CComponent {
private $_yiiSAML = null;
static private function pre() {
require_once (Yii::app()->params['simpleSAML'] . '/lib/_autoload.php');
// temporary disable Yii autoloader
spl_autoload_unregister(array(
'YiiBase',
'autoload'
));
}
static private function post() {
// enable Yii autoloader
spl_autoload_register(array(
'YiiBase',
'autoload'
));
}
public function __construct() {
self::pre();
//We select our authentication source:
$this->_yiiSAML = new SimpleSAML_Auth_Simple(Yii::app()->params['authSource']);
self::post();
}
static public function loggedOut($param, $stage) {
self::pre();
$state = SimpleSAML_Auth_State::loadState($param, $stage);
self::post();
if (isset($state['saml:sp:LogoutStatus'])) $ls = $state['saml:sp:LogoutStatus']; /* Only works for SAML SP */
else return true;
return $ls['Code'] === 'urn:oasis:names:tc:SAML:2.0:status:Success' && !isset($ls['SubCode']);
}
public function __call($method, $args) {
$params = (is_array($args) and !empty($args)) ? $args[0] : $args;
if (method_exists($this->_yiiSAML, $method)) return $this->_yiiSAML->$method($params);
else throw new YiiSAMLException(Yii::t('app', 'The method {method} does not exist in the SAML class', array(
'{method}' => $method
)));
}
}
class YiiSAMLException extends CException {
}
然后定义一个扩展CFilter
Yii类的过滤器:
<?php
Yii::import('lib.YiiSAML');
class SAMLControl extends CFilter {
protected function preFilter($filterChain) {
$msg = Yii::t('yii', 'You are not authorized to perform this action.');
$saml = new YiiSAML();
if (Yii::app()->user->isGuest) {
Yii::app()->user->loginRequired();
return false;
} else {
$saml_attributes = $saml->getAttributes();
if (!$saml->isAuthenticated() or Yii::app()->user->id != $saml_attributes['User.id'][0]) {
Yii::app()->user->logout();
Yii::app()->user->loginRequired();
return false;
}
return true;
}
}
}
最后,在您有兴趣限制的控制器中,您可以覆盖filters()
方法:
public function filters() {
return array(
array(
'lib.SAMLControl'
) , // perform access control for CRUD operations
...
);
}
希望它有所帮助。
答案 1 :(得分:0)
可以简单地使用&#34;供应商&#34;目录。
祝你好运:)
答案 2 :(得分:0)
我在github中遇到了SimpleSAMLphp的Yii扩展
https://github.com/asasmoyo/yii-simplesamlphp
您可以将simplesamlphp作为供应商库加载,然后在扩展名中指定自动加载文件。
除了扩展,您可以将所有必要的配置和元数据复制到应用程序中,并配置SimpleSAML配置以从目录中加载配置,这样您就可以保持供应商包不受影响以便将来更新。