Codeigniter 3.x,Ion-auth,CKFinder-如何将登录状态从ion-auth传递到CKFinder的配置文件 [英] Codeigniter 3.x, Ion-auth, CKFinder - how to pass the logged in status from ion-auth to CKFinder's config file

查看:122
本文介绍了Codeigniter 3.x,Ion-auth,CKFinder-如何将登录状态从ion-auth传递到CKFinder的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将登录状态从ion-auth/codeigniter传递给CKFinder的配置文件?

在CKfinder中,有一个用于身份验证的配置文件,如下所示:

$config['authentication'] = function () {
    return false;
};

在codeigniter中,我正在使用ion-auth.在login()方法的Auth控制器中,如果用户成功登录,则添加了此代码:

$_SESSION['userloggedin'];

$ _ SESSION ['userloggedin']的确设置为当我回显到屏幕时,我得到的是"1",但该回显在login()方法中.我似乎可以在CKfinder配置中获取会话var.我该怎么做?我想做这样的事情:

$config['authentication'] = function () {
    if ($_SESSION['userloggedin'] === true) { 
        return true;
    } else {
        return false;
    }
};

任何帮助表示赞赏.

解决方案

您不能直接访问$_SESSION,因为CI使用自己的会话形式,通常以__ci_vars开头,并且没有简单的直接方法除非您单独加载会话驱动程序,否则它们将完全被蠕虫感染.

这是我的工作方式:

好的,所以我使用所有与您相同的东西-CKFinder,IonAuth等.

index.php:

执行以下$system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';$application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';这样,以便从任何地方调用这些路径都是正确的.

接下来,您的index.php文件创建一个名为CI.php的文件,并添加以下内容:

<?php
ob_start();
define('REQUEST', 'external');
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "index.php"; //or wherever the directory is relative to your path
ob_end_clean();
return $CI;

然后在默认路由控制器中(因此,如果您访问localhost或somesite.com,则站点首先登陆的控制器)将添加到index()函数的顶部,并添加已定义的if语句-如果不执行此操作您的默认路由将在CK中呈现,并且所有内容均将不起作用.

class Homepage extends MY_Frontend
{

    public function index()
    {
        // FOR SI AND CKFINDER
        if (defined('REQUEST') && REQUEST === 'external') {
            return;
        }

然后在您的ck配置中:

$CI = require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'CI.php';


/* ============================ General Settings ======================================= */
// http://docs.cksource.com/ckfinder3-php/configuration.html

$config = array();

/* ============================ Enable PHP Connector HERE ============================== */
// http://docs.cksource.com/ckfinder3-php/configuration.html#configuration_options_authentication

$config['authentication'] = function () {
    $CI = & get_instance();
    $CI->load->library('session'); //if it's not autoloaded in your CI setup
    return $CI->session->has_userdata('user_id');
};

我只是在检查会话user_id标志是否已设置(登录为TRUE,否则为FALSE),因为我的前端控制器未使用它(或会话),因此CK正在路由.如果您的整个网站都位于ion_auth后面,或者您自动加载了该网站或会话,则可能只需使用return $this->ion_auth->logged_in();.

请记住,如果您使用CSRF,则CK也会受到影响并且无法正常工作,因为它没有正确的令牌.

我在CI配置文件中执行以下操作:

if (defined('REQUEST') && REQUEST === 'external') {
    $config['csrf_protection'] = FALSE;
} else {
    $config['csrf_protection'] = TRUE;
}

此解决方案并不完美,但这是我发现唯一可行的解​​决方案!有一些库可以从 CI中加载CK,但是对于我使用的CK版本而言,它们都太旧了.

How do I pass the logged in status from ion-auth/codeigniter to CKFinder's config file?

In CKfinder, there is a config file for authentication as follows:

$config['authentication'] = function () {
    return false;
};

In codeigniter I am using ion-auth. In the Auth controller in the login() method, I added this if the user is successfully logged in:

$_SESSION['userloggedin'];

The $_SESSION['userloggedin'] does get set as when I echo to screen, I get "1" but that echo is in the login() method. I can seem to get the session var in the CKfinder config. How to I do that? I want to do something like this:

$config['authentication'] = function () {
    if ($_SESSION['userloggedin'] === true) { 
        return true;
    } else {
        return false;
    }
};

Any help appreciated.

解决方案

You can't access $_SESSION directly because CI uses its own form of sessions that are typically prepended with __ci_vars and there is no easy way of directly accessing them unless you load the session driver independently which is a whole other can of worms.

Here is how I made it work:

Alright so I use all the same things as you - CKFinder, IonAuth, .etc.

index.php:

Do the following $system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system'; and $application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application'; this makes it so that those paths are correct when called from wherever.

Next your index.php file make a file called CI.php and add the following:

<?php
ob_start();
define('REQUEST', 'external');
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "index.php"; //or wherever the directory is relative to your path
ob_end_clean();
return $CI;

Then in your default route controller (so whichever controller your site lands on first if you go to localhost or somesite.com) add the top of the index() function add the defined if statement - if you don't do this your default route will render in CK and everything won't work.

class Homepage extends MY_Frontend
{

    public function index()
    {
        // FOR SI AND CKFINDER
        if (defined('REQUEST') && REQUEST === 'external') {
            return;
        }

Then in your ck config:

$CI = require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'CI.php';


/* ============================ General Settings ======================================= */
// http://docs.cksource.com/ckfinder3-php/configuration.html

$config = array();

/* ============================ Enable PHP Connector HERE ============================== */
// http://docs.cksource.com/ckfinder3-php/configuration.html#configuration_options_authentication

$config['authentication'] = function () {
    $CI = & get_instance();
    $CI->load->library('session'); //if it's not autoloaded in your CI setup
    return $CI->session->has_userdata('user_id');
};

I am just checking if the session user_id flag is set (TRUE logged in, FALSE not) as my frontend controller doesn't make use of it (or sessions), and that is what CK is getting routed through. If your entire site is behind ion_auth or if you autoload it or sessions than you can probably just use return $this->ion_auth->logged_in();.

Please keep in mind that if you use CSRF than CK might also be affected and not work since it won't have the proper tokens.

I do the following in my CI config file:

if (defined('REQUEST') && REQUEST === 'external') {
    $config['csrf_protection'] = FALSE;
} else {
    $config['csrf_protection'] = TRUE;
}

This solution is not elegant, but it is the only thing I found that worked! There are some libraries to load CK from within CI but all were too old for the version of CK I was using.

这篇关于Codeigniter 3.x,Ion-auth,CKFinder-如何将登录状态从ion-auth传递到CKFinder的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆