|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Security; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class SessionDownloadStrategy. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Ahmet Akbana <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class SessionDownloadStrategy implements DownloadStrategyInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ContainerInterface |
|
29
|
|
|
* |
|
30
|
|
|
* @deprecated Since version 3.x, will be removed in 4.0. |
|
31
|
|
|
* NEXT_MAJOR : remove this property |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $container; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var TranslatorInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $translator; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $times; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $sessionKey = 'sonata/media/session/times'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var SessionInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $session; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param TranslatorInterface $translator |
|
57
|
|
|
* @param ContainerInterface|SessionInterface $session |
|
58
|
|
|
* @param int $times |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(TranslatorInterface $translator, $session, $times) |
|
61
|
|
|
{ |
|
62
|
|
|
// NEXT_MAJOR : remove this block and set session from parameter. |
|
63
|
|
|
if ($session instanceof ContainerInterface) { |
|
64
|
|
|
@trigger_error( |
|
|
|
|
|
|
65
|
|
|
'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since |
|
66
|
|
|
version 3.x and will be removed in 4.0. |
|
67
|
|
|
Use Symfony\Component\HttpFoundation\Session\SessionInterface instead.', |
|
68
|
|
|
E_USER_DEPRECATED |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$this->session = $session->get('session'); |
|
72
|
|
|
} elseif ($session instanceof SessionInterface) { |
|
73
|
|
|
$this->session = $session; |
|
74
|
|
|
} else { |
|
75
|
|
|
throw new \InvalidArgumentException( |
|
76
|
|
|
'$session should be an instance of Symfony\Component\HttpFoundation\Session\SessionInterface' |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->times = $times; |
|
81
|
|
|
$this->translator = $translator; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isGranted(MediaInterface $media, Request $request) |
|
88
|
|
|
{ |
|
89
|
|
|
$times = $this->session->get($this->sessionKey, 0); |
|
90
|
|
|
|
|
91
|
|
|
if ($times >= $this->times) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
++$times; |
|
96
|
|
|
|
|
97
|
|
|
$this->session->set($this->sessionKey, $times); |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getDescription() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->translator->transChoice( |
|
108
|
|
|
'description.session_download_strategy', |
|
109
|
|
|
$this->times, |
|
110
|
|
|
array('%times%' => $this->times), |
|
111
|
|
|
'SonataMediaBundle' |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: