1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CoreBundle\Helper\Security; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
17
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\Client; |
18
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\User; |
19
|
|
|
use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelperInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class SecurityHelper |
23
|
|
|
* |
24
|
|
|
* @author Adam Piotrowski <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class SecurityHelper implements SecurityHelperInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var TokenStorageInterface |
30
|
|
|
*/ |
31
|
|
|
private $tokenStorage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RequestHelperInterface |
35
|
|
|
*/ |
36
|
|
|
private $requestHelper; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* SecurityHelper constructor. |
40
|
|
|
* |
41
|
|
|
* @param TokenStorageInterface $tokenStorage |
42
|
|
|
* @param RequestHelperInterface $requestHelper |
43
|
|
|
*/ |
44
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, RequestHelperInterface $requestHelper) |
45
|
|
|
{ |
46
|
|
|
$this->tokenStorage = $tokenStorage; |
47
|
|
|
$this->requestHelper = $requestHelper; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getCurrentUser() |
51
|
|
|
{ |
52
|
|
|
$token = $this->tokenStorage->getToken(); |
53
|
|
|
if (null !== $token) { |
54
|
|
|
return $token->getUser(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getCurrentClient() |
61
|
|
|
{ |
62
|
|
|
$user = $this->getCurrentUser(); |
63
|
|
|
|
64
|
|
|
return $user instanceof Client ? $user : null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getCurrentAdmin() |
68
|
|
|
{ |
69
|
|
|
$user = $this->getCurrentUser(); |
70
|
|
|
|
71
|
|
|
return $user instanceof User ? $user : null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getAuthenticatedClient(): Client |
75
|
|
|
{ |
76
|
|
|
return $this->getCurrentUser(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getAuthenticatedAdmin(): User |
80
|
|
|
{ |
81
|
|
|
return $this->getCurrentUser(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isActiveFirewall(string $name): bool |
85
|
|
|
{ |
86
|
|
|
$request = $this->requestHelper->getCurrentRequest(); |
87
|
|
|
|
88
|
|
|
return $name === $this->getFirewallNameForRequest($request); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isActiveAdminFirewall(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->isActiveFirewall('admin'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getFirewallNameForRequest(Request $request) |
97
|
|
|
{ |
98
|
|
|
list($mode,) = explode('/', ltrim($request->getPathInfo(), '/')); |
99
|
|
|
|
100
|
|
|
return ($mode === 'admin') ? 'admin' : 'client'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function generateRandomPassword(int $length = 8): string |
104
|
|
|
{ |
105
|
|
|
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; |
106
|
|
|
$password = substr(str_shuffle($chars), 0, $length); |
107
|
|
|
|
108
|
|
|
return $password; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: