1

我有一个名为“文档”的实体,我使用奏鸣曲媒体来处理文件管理。因此,管理员可以添加一个新文档并附加一个文件,这个文档被分配给一个用户。

我的问题是:我希望用户能够通过“受保护的 URL”下载分配给他们的文件(因为如果您更改下载 url 末尾的数字,您可以下载分配给他们的文件给其他用户)

根据奏鸣曲媒体的文档,我需要创建一个下载策略和服务。我做到了,我称之为:PrivateDownloadStrategy.php(并制作了服务)这个文件应该基于RolesDownloadStrategy.php,因为我希望管理员能够下载所有文件,但如何让用户能够下载分配给他们的文件?

这是我实际的 PrivateDownloadStrategy.php(来自rolesDownloadStrategy.php 的副本)

<?php
namespace Application\Core\DocumentBundle\Security;

use Sonata\MediaBundle\Security\DownloadStrategyInterface;
use Sonata\MediaBundle\Model\MediaInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class PrivateDownloadStrategy implements DownloadStrategyInterface
{
    protected $roles;

    protected $security;

    protected $translator;

    /**
     * @param \Symfony\Component\Translation\TranslatorInterface        $translator
     * @param \Symfony\Component\Security\Core\SecurityContextInterface $security
     * @param array                                                     $roles
     */
    public function __construct(TranslatorInterface $translator, SecurityContextInterface $security, array $roles = array())
    {
        $this->roles      = $roles;
        $this->security   = $security;
        $this->translator = $translator;
    }

    /**
     * @param \Sonata\MediaBundle\Model\MediaInterface  $media
     * @param \Symfony\Component\HttpFoundation\Request $request
     *
     * @return bool
     */
    public function isGranted(MediaInterface $media, Request $request)
    {
        return $this->security->getToken() && $this->security->isGranted($this->roles);
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->translator->trans('description.roles_download_strategy', array('%roles%' => '<code>'.implode('</code>, <code>', $this->roles).'</code>'), 'SonataMediaBundle');
    }
}

和服务:

<service id="sonata.media.security.private_strategy" class="Application\Core\DocumentBundle\Security\PrivateDownloadStrategy" >
            <argument type="service" id="translator" />
            <argument type="service" id="security.context" />
            <argument type="collection">
                <argument>ROLE_SUPER_ADMIN</argument>
                <argument>ROLE_ADMIN</argument>
            </argument>
        </service>

注意:奏鸣曲媒体文档:链接 奏鸣曲角色下载策略:链接

4

0 回答 0