【问题标题】:how to use SimpleSAMLphp in yii framework?如何在 yii 框架中使用 SimpleSAMLphp?
【发布时间】:2014-05-14 18:38:31
【问题描述】:

我在 yii 框架中有两个项目,我想使用 SimpleSAMLphp 和 SSO 来使用这两个项目。我需要的条件是,如果我从第一个项目登录,我想访问第二个项目。 提前谢谢你。

【问题讨论】:

标签: yii yii-components simplesamlphp


【解决方案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 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
        ...
    );
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    只需使用“vendors”目录即可。

    1. https://simplesamlphp.org/ 下载 PHP 库
    2. 在 Yii 框架中实现它作为供应商库。 (http://www.yiiframework.com/doc/guide/1.1/en/extension.integration)

    祝你好运:)

    【讨论】:

      【解决方案3】:

      我在 github 中发现了 SimpleSAMLphp 的 Yii 扩展

      https://github.com/asasmoyo/yii-simplesamlphp

      您可以将 simplesamlphp 作为供应商库加载,然后在扩展名中指定自动加载文件。

      除了扩展之外,您还可以将所有必要的配置和元数据复制到应用程序中,并配置 SimpleSAML 配置以从您的目录加载配置,这样您就可以保持供应商包不受影响以备将来更新。

      【讨论】:

      • 好,找到了,但这个问题是 5 年前提出的 :) 这意味着 yii1 包我还在寻找 yii1 包
      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 2017-11-16
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2015-01-26
      相关资源
      最近更新 更多