2011-06-08 220 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

相關問題