1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BWC\Share\Symfony\Security; |
4
|
|
|
|
5
|
|
|
use FOS\UserBundle\Model\UserInterface; |
6
|
|
|
use FOS\UserBundle\Model\UserManagerInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
8
|
|
|
|
9
|
|
|
class FosUserProvider |
10
|
|
|
{ |
11
|
|
|
/** @var \FOS\UserBundle\Model\UserManagerInterface */ |
12
|
|
|
private $_fosUserManager; |
13
|
|
|
|
14
|
|
|
/** @var \Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface */ |
15
|
|
|
private $_encoderFactory; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
function __construct( |
|
|
|
|
20
|
|
|
UserManagerInterface $fosUserManager, // @fos_user.user_manager |
21
|
|
|
EncoderFactoryInterface $encoderFactory // @security.encoder_factory |
22
|
|
|
) { |
23
|
|
|
$this->_fosUserManager = $fosUserManager; |
24
|
|
|
$this->_encoderFactory = $encoderFactory; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $un |
30
|
|
|
* @param string $pw |
31
|
|
|
* @return \FOS\UserBundle\Model\UserInterface|null |
32
|
|
|
*/ |
33
|
|
|
function getUserByUnPw($un, $pw) { |
|
|
|
|
34
|
|
|
$result = null; |
35
|
|
|
if ($un && $pw) { |
36
|
|
|
$user = $this->_fosUserManager->findUserByUsername($un); |
37
|
|
|
if ($user && $this->isPasswordValid($user, $pw)) { |
38
|
|
|
$result = $user; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
return $result; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $email |
47
|
|
|
* @param string $pw |
48
|
|
|
* @return \FOS\UserBundle\Model\UserInterface|null |
49
|
|
|
*/ |
50
|
|
|
function getUserByEmailPw($email, $pw) { |
|
|
|
|
51
|
|
|
$result = null; |
52
|
|
|
if ($email && $pw) { |
53
|
|
|
$user = $this->_fosUserManager->findUserByEmail($email); |
54
|
|
|
if ($user && $this->isPasswordValid($user, $pw)) { |
55
|
|
|
$result = $user; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param UserInterface $user |
64
|
|
|
* @param string $pw |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
private function isPasswordValid(UserInterface $user, $pw) { |
68
|
|
|
$pwdEncoded = $this->_encoderFactory->getEncoder($user)->encodePassword($pw, $user->getSalt()); |
69
|
|
|
$result = $pwdEncoded == $user->getPassword(); |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.