dounao5856 2011-06-08 02:28
浏览 25
已采纳

如何在Kohana 3.1中设置受保护的静态文件

I'm using Kohana 3.1 with the ORM/Auth module enabled. I would like to serve static files (docs, pdfs, etc.) only to people with role A, in directory A_only.

Since the .htaccess files just serves URLs that it finds directly and doesn't hand it off to index.php, I could deny all access in A_only through a .htaccess, but then how would I serve the static files in a controller function?

I could also have an .htaccess in the A_only directory that requires authentication. However, this would require them to log in again even if I set it up to look in the database for user/passwords.

  • 写回答

1条回答 默认 最新

  • dscw1223 2011-06-08 09:58
    关注

    You're going to need to tell your web server to stop handling static files. The easiest solution would be to move the static files outside of the web directory so Apache can't find them; This will force that request to go through Kohana.

    The second part is creating a controller which handles the permissions and file sending for you. The Kohana userguide has a fairly good example of something for to you work off:

    Line 247 of Controller_Userguide

    // Get the file path from the request
    $file = $this->request->param('file');
    
    // Find the file extension
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    
    // Remove the extension from the filename
    $file = substr($file, 0, -(strlen($ext) + 1));
    
    if ($file = Kohana::find_file('media/guide', $file, $ext))
    {
        // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
        $this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);
    
        // Send the file content as the response
        $this->response->body(file_get_contents($file));
    
        // Set the proper headers to allow caching
        $this->response->headers('content-type',  File::mime_by_ext($ext));
        $this->response->headers('last-modified', date('r', filemtime($file)));
    }
    else
    {
        // Return a 404 status
        $this->response->status(404);
    }
    

    The main thing you need to worry about is changing where Kohana looks for the files:

    if ($file = Kohana::find_file('media/guide', $file, $ext))
    

    The rest is boilerplate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路