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 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)