「ドキュメント」という名前のエンティティがあり、ソナタメディアを使用してファイル管理を処理しています。したがって、管理者は新しいドキュメントを追加してファイルを添付できます。このドキュメントはユーザーに割り当てられます。
私の問題は、ユーザーが「保護されたURL」を介して割り当てられたファイルをダウンロードできるようにしたいです(ダウンロードURLの末尾の番号を変更すると、割り当てられたファイルをダウンロードできるためです)他のユーザーに)
Sonata Media のドキュメントによると、ダウンロード戦略とサービスを作成する必要があります。私はそれをしました、私はそれを呼びました: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>
nb : sonata media doc :リンク sonata rolesDownloadStrategy :リンク