Skip to content

Commit 8452443

Browse files
authored
Badgeuse : être plus libre dans le choix des IP + refactoring (#1082)
* Move isLocationOk to new helper * Use startswith instead of equals * fix import
1 parent b258ab8 commit 8452443

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

src/AppBundle/Helper/PlaceIP.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace AppBundle\Helper;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
6+
7+
class PlaceIP {
8+
9+
private $container;
10+
11+
public function __construct(Container $container) {
12+
$this->container = $container;
13+
}
14+
15+
/**
16+
* If enable_place_local_ip_address_check is true & place_local_ip_address is set
17+
* Then we check the client's IP against the IP list
18+
*/
19+
public function isLocationOk()
20+
{
21+
$checkIps = $this->container->getParameter('enable_place_local_ip_address_check');
22+
23+
if (isset($checkIps) and $checkIps) {
24+
$whitelist_ips = $this->container->getParameter('place_local_ip_address');
25+
$whitelist_ips = explode(',', $whitelist_ips);
26+
$current_ip = $this->container->get('request_stack')->getCurrentRequest()->getClientIp();
27+
28+
$current_ip_in_whitelist_ips = array_filter($whitelist_ips, function($whitelist_ip) use ($current_ip) {
29+
return str_starts_with($current_ip, $whitelist_ip);
30+
});
31+
if (count($current_ip_in_whitelist_ips)) {
32+
return True;
33+
}
34+
}
35+
36+
return False;
37+
}
38+
}

src/AppBundle/Security/CodeVoter.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AppBundle\Entity\Code;
66
use AppBundle\Entity\User;
7+
use AppBundle\Helper\PlaceIP;
78
use Symfony\Component\DependencyInjection\ContainerInterface;
89
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
910
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@@ -99,7 +100,7 @@ private function canAdd(Code $code, User $user)
99100
$now = new \DateTime('now');
100101
if ($this->canView($code, $user)) { //can add only if last code can be seen
101102
if ($code->getRegistrar() != $user || $code->getCreatedAt()->format('Y m d') != ($now->format('Y m d'))) { // on ne change pas son propre code
102-
if ($this->isLocationOk()) { // et si l'utilisateur est physiquement à l'épicerie
103+
if ($this->container->get(PlaceIP::class)->isLocationOk()) { // et si l'utilisateur est physiquement à l'épicerie
103104
return true;
104105
}
105106
}
@@ -145,14 +146,4 @@ private function canDelete(Code $code, User $user)
145146
{
146147
return false;
147148
}
148-
149-
//\AppBundle\Security\UserVoter::isLocationOk DUPLICATED
150-
private function isLocationOk()
151-
{
152-
$ip = $this->container->get('request_stack')->getCurrentRequest()->getClientIp();
153-
$checkIps = $this->container->getParameter('enable_place_local_ip_address_check');
154-
$ips = $this->container->getParameter('place_local_ip_address');
155-
$ips = explode(',', $ips);
156-
return (isset($checkIps) and !$checkIps) or (isset($ip) and in_array($ip, $ips));
157-
}
158149
}

src/AppBundle/Security/MembershipVoter.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AppBundle\Entity\Membership;
66
use AppBundle\Entity\User;
7+
use AppBundle\Helper\PlaceIP;
78
use Symfony\Component\DependencyInjection\ContainerInterface;
89
use Symfony\Component\HttpFoundation\Session\Session;
910
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -96,7 +97,7 @@ protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
9697
case self::ACCESS_TOOLS:
9798
case self::BENEFICIARY_ADD:
9899
case self::CREATE:
99-
return $this->isLocationOk();
100+
return $this->container->get(PlaceIP::class)->isLocationOk();
100101
case self::VIEW:
101102
case self::ANNOTATE:
102103
return $this->canView($subject, $token);
@@ -156,15 +157,5 @@ private function canEdit(Membership $subject, TokenInterface $token)
156157
}
157158

158159
return false;
159-
160-
}
161-
162-
private function isLocationOk()
163-
{
164-
$ip = $this->container->get('request_stack')->getCurrentRequest()->getClientIp();
165-
$checkIps = $this->container->getParameter('enable_place_local_ip_address_check');
166-
$ips = $this->container->getParameter('place_local_ip_address');
167-
$ips = explode(',', $ips);
168-
return (isset($checkIps) and !$checkIps) or (isset($ip) and in_array($ip, $ips));
169160
}
170161
}

src/AppBundle/Security/UserVoter.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AppBundle\Security;
44

55
use AppBundle\Entity\User;
6+
use AppBundle\Helper\PlaceIP;
67
use Symfony\Component\DependencyInjection\ContainerInterface;
78
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
89
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@@ -51,8 +52,8 @@ protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
5152
$user = $token->getUser();
5253

5354
if (!$user instanceof User) {
54-
if ($attribute == self::CARD_READER){
55-
return $this->isLocationOk();
55+
if ($attribute == self::CARD_READER) {
56+
return $this->container->get(PlaceIP::class)->isLocationOk();
5657
}
5758
// the user must be logged in; if not, deny access
5859
return false;
@@ -82,7 +83,7 @@ protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
8283
return true;
8384
case self::ACCESS_TOOLS:
8485
case self::CREATE:
85-
return $this->isLocationOk();
86+
return $this->container->get(PlaceIP::class)->isLocationOk();
8687
case self::VIEW:
8788
case self::ANNOTATE:
8889
return $this->canView($subject, $token);
@@ -119,7 +120,7 @@ private function canEdit(User $subject, TokenInterface $token)
119120
{
120121
$user = $token->getUser();
121122

122-
if ($this->isLocationOk()) {
123+
if ($this->container->get(PlaceIP::class)->isLocationOk()) {
123124
if ($this->decisionManager->decide($token, ['ROLE_USER_MANAGER'])) {
124125
return true;
125126
}
@@ -131,13 +132,4 @@ private function canEdit(User $subject, TokenInterface $token)
131132
return false;
132133

133134
}
134-
135-
private function isLocationOk()
136-
{
137-
$ip = $this->container->get('request_stack')->getCurrentRequest()->getClientIp();
138-
$checkIps = $this->container->getParameter('enable_place_local_ip_address_check');
139-
$ips = $this->container->getParameter('place_local_ip_address');
140-
$ips = explode(',', $ips);
141-
return (isset($checkIps) and !$checkIps) or (isset($ip) and in_array($ip, $ips));
142-
}
143-
}
135+
}

0 commit comments

Comments
 (0)