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

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

问题描述

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

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

在CKfinder中,有一个用于认证的配置文件如下:

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

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

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

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'];

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

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;
    }
};

感谢任何帮助.

推荐答案

您无法直接访问 $_SESSION,因为 CI 使用其自己的会话形式,这些会话形式通常以 __ci_vars<开头/code> 并且没有简单的方法可以直接访问它们,除非您独立加载会话驱动程序,这完全是另一种蠕虫.

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.

这是我的工作方式:

好吧,所以我使用的东西都和你一样——CKFinder、IonAuth 等.

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

index.php:

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

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.

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

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;

然后在您的默认路由控制器中(因此,如果您访问 localhost 或 somesite.com,则您的站点首先登陆的控制器)添加 index() 函数的顶部添加定义的 if 语句 -如果您不这样做,您的默认路由将在 CK 中呈现,并且一切都将不起作用.

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;
        }

然后在您的 ck 配置中:

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');
};

我只是检查会话 user_id 标志是否设置(登录为真,假不),因为我的前端控制器没有使用它(或会话),这就是 CK正在路由通过.如果您的整个网站都在 ion_auth 后面,或者如果您自动加载它或会话,那么您可能只需使用 return $this->ion_auth->logged_in();.

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();.

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

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.

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

I do the following in my CI config file:

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

这个解决方案并不优雅,但它是我发现唯一有效的方法!有一些库可以从 CI 中加载 CK,但对于我使用的 CK 版本来说,它们都太旧了.

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天全站免登陆