1313
1414namespace CCDNMessage \MessageBundle \Component ;
1515
16+ use Symfony \Component \Security \Core \SecurityContextInterface ;
17+ use Symfony \Component \HttpFoundation \Session \Session ;
18+
1619/**
1720 *
1821 * @category CCDNMessage
@@ -29,10 +32,24 @@ class FloodControl
2932 /**
3033 *
3134 * @access protected
32- * @var $session
35+ * @var \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
36+ */
37+ protected $ securityContext ;
38+
39+ /**
40+ *
41+ * @access protected
42+ * @var \Symfony\Component\HttpFoundation\Session\Session $session
3343 */
3444 protected $ session ;
3545
46+ /**
47+ *
48+ * @access protected
49+ * @var string $kernelEnv
50+ */
51+ protected $ kernelEnv ;
52+
3653 /**
3754 *
3855 * @access protected
@@ -50,19 +67,24 @@ class FloodControl
5067 /**
5168 *
5269 * @access public
53- * @param $session
70+ * @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
71+ * @param \Symfony\Component\HttpFoundation\Session\Session $session
72+ * @param string $kernelEnv
73+ * @param int $sendLimit
74+ * @param int $blockForMinutes
5475 */
55- public function __construct ($ session , $ sendLimit , $ blockForMinutes )
76+ public function __construct (SecurityContextInterface $ securityContext , Session $ session, $ kernelEnv , $ sendLimit , $ blockForMinutes )
5677 {
78+ $ this ->securityContext = $ securityContext ;
5779 $ this ->session = $ session ;
58-
59- $ this ->sendLimit = $ sendLimit ;
60-
61- $ this ->blockForMinutes = $ blockForMinutes ;
80+ $ this ->kernelEnv = $ kernelEnv ;
6281
6382 if ( ! $ this ->session ->has ('flood_control_message_send_count ' )) {
6483 $ this ->session ->set ('flood_control_message_send_count ' , array ());
6584 }
85+
86+ $ this ->sendLimit = $ sendLimit ;
87+ $ this ->blockForMinutes = $ blockForMinutes ;
6688 }
6789
6890 /**
@@ -71,11 +93,13 @@ public function __construct($session, $sendLimit, $blockForMinutes)
7193 */
7294 public function incrementCounter ()
7395 {
74- $ sendCount = $ this ->session ->get ('flood_control_message_send_count ' );
96+ if (! $ this ->securityContext ->isGranted ('ROLE_MODERATOR ' ) || $ this ->kernelEnv != 'prod ' ) {
97+ $ sendCount = $ this ->session ->get ('flood_control_message_send_count ' );
7598
76- $ sendCount [] = new \DateTime ('now ' );
99+ $ sendCount [] = new \DateTime ('now ' );
77100
78- $ this ->session ->set ('flood_control_message_send_count ' , $ sendCount );
101+ $ this ->session ->set ('flood_control_message_send_count ' , $ sendCount );
102+ }
79103 }
80104
81105 /**
@@ -85,14 +109,17 @@ public function incrementCounter()
85109 */
86110 public function isFlooded ()
87111 {
88- $ timeLimit = new \DateTime ('- ' . $ this ->blockForMinutes . ' minutes ' );
112+ if ($ this ->sendLimit < 1 || ! $ this ->securityContext ->isGranted ('ROLE_MODERATOR ' ) || $ this ->kernelEnv != 'prod ' ) {
113+ return false ;
114+ }
89115
90116 if ($ this ->session ->has ('flood_control_message_send_count ' )) {
91117 $ attempts = $ this ->session ->get ('flood_control_message_send_count ' );
92118
93119 // Iterate over attempts and only reveal attempts that fall within the $timeLimit.
94120 $ freshenedAttempts = array ();
95121
122+ $ timeLimit = new \DateTime ('- ' . $ this ->blockForMinutes . ' minutes ' );
96123 $ limit = $ timeLimit ->getTimestamp ();
97124
98125 foreach ($ attempts as $ attempt ) {
0 commit comments