2011-06-08 223 views
1

我在启用ORM/Auth模块的情况下使用Kohana 3.1。我想仅将静态文件(文档,pdf等)提供给目录A_only中具有角色A的人员。如何在Kohana中设置受保护的静态文件3.1

由于.htaccess文件只提供它直接找到的URL,并且不会将它交给index.php,所以我可以通过.htaccess拒绝A_only中的所有访问,但是如何在静态文件中提供一个控制器功能?

我也可以在需要验证的A_only目录中有.htaccess。但是,即使设置为在数据库中查找用户名/密码,也需要他们再次登录。

回答

3

您将需要告诉您的Web服务器停止处理静态文件。最简单的解决方案是将静态文件移动到Web目录之外,以便Apache无法找到它们;这将强制该请求通过Kohana。

第二部分是创建一个处理权限和文件发送给你的控制器。 Kohana的userguide有东西给你工作过相当不错的例子:

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

你需要担心的主要是改变其中的Kohana查找文件:

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

其余的是样板。

+1

这工作完美,谢谢!我只是把一个.htaccess文件拒绝所有访问,然后使用一个路径将它引导到控制器,而不是从Web服务器隐藏目录。 – dualistic 2011-06-09 01:25:16

相关问题