netcommons/files

NetCommons 插件文件

安装数: 89,789

依赖项: 16

建议者: 0

安全: 0

星标: 0

关注者: 15

分支: 3

开放问题: 17

类型:cakephp-plugin

3.3.7.2 2024-05-28 05:17 UTC

README

Tests Status Coverage Status Stable Version

AttachmentBehavior

当显示文件上传表单时,请将 Model::recursive 设置为 0 或更大。

如果 Model::recursive = -1,则无法获取 Model 中附加的文件信息,因此 NetCommonsForm::uploadFile() 会错误地判断没有附加文件。

认证密钥组合使用方法

通过将 认证密钥插件 与之结合,只有知道特定认证密钥的用户才能下载文件。

重定向类型

在实现下载动作的控制器中使用 AuthorizationKeyComponent。

public $components = array(
    'Files.Download',
    'AuthorizationKeys.AuthorizationKey' => [
        'operationType' => 'redirect',
        'targetAction' => 'download_pdf',
        'model' => 'BlogEntry',
    ],
);

在下载动作内设置基于认证密钥的守卫。

public function download_pdf() {
    // ここから元コンテンツを取得する処理
    $this->_prepare();
    $key = $this->params['pass'][1];

    $conditions = $this->BlogEntry->getConditions(
            Current::read('Block.id'),
            $this->Auth->user('id'),
            $this->_getPermission(),
            $this->_getCurrentDateTime()
    );

    $conditions['BlogEntry.key'] = $key;
    $options = array(
            'conditions' => $conditions,
            'recursive' => 1,
    );
    $blogEntry = $this->BlogEntry->find('first', $options);
    // ここまで元コンテンツを取得する処理

    // 認証キーによるガード
    $this->AuthorizationKey->guard('redirect', 'BlogEntry', $blogEntry);

    // ダウンロード実行
    if ($blogEntry) {
        return $this->Download->doDownload($blogEntry['BlogEntry']['id'], ['filed' => 'pdf']);
    } else {
        // 表示できない記事へのアクセスなら404
        throw new NotFoundException(__('Invalid blog entry'));
    }
}

弹出类型

在实现下载动作的控制器中使用 AuthorizationKeyComponent。

public $components = array(
    'Files.Download',
    'AuthorizationKeys.AuthorizationKey' => [
        'operationType' => 'redirect',
        'targetAction' => 'download_pdf',
        'model' => 'BlogEntry',
    ],
);

将下载动作内的守卫设置为弹出。

    $this->AuthorizationKey->guard('popup', 'BlogEntry', $blogEntry);

将下载链接重写为显示弹出窗口,认证密钥弹出窗口作为 AngularJS 的指令 authorization-keys-popup-link 实现。

<div>
    PDF :
    <?php echo $this->Html->link('PDF',
            '#',
        ['authorization-keys-popup-link',
            'url' => $this->NetCommonsHtml->url(
                [
                    'action' => 'download_pdf',
                    'key' => $blogEntry['BlogEntry']['key'],
                    'pdf',
                ]
            ),
            'frame-id' => Current::read('Frame.id')
        ]
    ); ?>
</div>

有关认证密钥的详细信息,请参阅认证密钥文档。

AuthorizationKeys

验证

在 AttachmentBehavior 中可以使用 Upload 行为的验证规则。