LoginSignup
1
0

More than 5 years have passed since last update.

concrete5のファイルダウンロードをログイン必須にする

Last updated at Posted at 2017-09-05

背景

concrete5で作ったサイトにお問い合わせフォームを作って、そこにファイル添付機能もつけた。
お問い合わせが来るとメールで通知するようにしてるけど、その中に書いてあるファイルダウンロードリンクが知られると第三者に添付ファイルが見られるので不安。

やること

ファイルのダウンロードリンク(http://example.com/download_file/0000/000みたいな形式のやつ)をログインユーザーしかアクセスできないようにする。

やりかた

applicationフォルダ内にdownload_fileコントローラのコピーを作って、オーバーライドする。

application/controllers/single_page/download_file.php
<?php
namespace Application\Controller\SinglePage; // ← 忘れずnamespaceをApplicationに上書き

use PageController;
use Core;
use Page;
use Permissions;
use Concrete\Core\Entity\File\File as FileEntity;
use Concrete\Core\File\File;
use User; // ← ログインチェックのため、Userクラスをインポート

// 省略...


    protected function download(\Concrete\Core\Entity\File\File $file, $rcID = null)
    {
        // ↓以下4行を追加
        $loggedIn = User::isLoggedIn();
        if ($loggedIn == false) {
            $this->redirect('/login'); // ←好きなところにリダイレクト
        }

        $filename = $file->getFilename();
        $file->trackDownload($rcID);
        $fsl = $file->getFileStorageLocationObject();
        $configuration = $fsl->getConfigurationObject();
        $fv = $file->getVersion();
        if ($configuration->hasPublicURL()) {
            return \Redirect::url($fv->getURL())->send();
        } else {
            return $fv->forceDownload();
        }
    }

// 以下省略

こんな感じでどうでしょうか。もっといけてるやり方あったら教えてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0