1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Helper\Security\OAuth; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use FOS\UserBundle\Model\GroupInterface; |
7
|
|
|
use Kunstmaan\AdminBundle\Entity\Group; |
8
|
|
|
use Kunstmaan\AdminBundle\Entity\User; |
9
|
|
|
|
10
|
|
|
class OAuthUserCreator implements OAuthUserCreatorInterface |
11
|
|
|
{ |
12
|
|
|
/** @var EntityManagerInterface */ |
13
|
|
|
private $em; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
private $hostedDomains; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $userClass; |
20
|
|
|
|
21
|
|
|
/** @var OAuthUserFinderInterface */ |
22
|
|
|
private $userFinder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* OAuthUserCreator constructor. |
26
|
|
|
* |
27
|
|
|
* @param EntityManagerInterface $em |
28
|
|
|
* @param array $hostedDomains |
29
|
|
|
* @param string $userClass |
30
|
|
|
* @param OAuthUserFinderInterface $userFinder |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct(EntityManagerInterface $em, $hostedDomains, $userClass, OAuthUserFinderInterface $userFinder) |
33
|
|
|
{ |
34
|
4 |
|
$this->em = $em; |
35
|
4 |
|
$this->hostedDomains = $hostedDomains; |
36
|
4 |
|
$this->userClass = $userClass; |
37
|
4 |
|
$this->userFinder = $userFinder; |
38
|
4 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
3 |
|
public function getOrCreateUser($email, $googleId) |
44
|
|
|
{ |
45
|
3 |
|
if ($this->isConfiguredDomain($email)) { |
46
|
2 |
|
$user = $this->userFinder->findUserByGoogleSignInData($email, $googleId); |
47
|
|
|
|
48
|
2 |
|
if (!$user instanceof $this->userClass) { |
49
|
|
|
//User not present in database, create new one |
50
|
|
|
/** @var User $user */ |
51
|
1 |
|
$user = new $this->userClass(); |
52
|
1 |
|
$user->setUsername($email); |
53
|
1 |
|
$user->setEmail($email); |
54
|
1 |
|
$user->setPlainPassword($googleId.$email.time()); |
55
|
1 |
|
$user->setEnabled(true); |
56
|
1 |
|
$user->setAdminLocale('en'); |
57
|
1 |
|
$user->setPasswordChanged(true); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
foreach ($this->getAccessLevels($email) as $accessLevel) { |
61
|
|
|
/** @var GroupInterface $group */ |
62
|
2 |
|
$group = $this->em->getRepository(Group::class)->findOneBy(['name' => $accessLevel]); |
63
|
2 |
|
if (null !== $group) { |
64
|
2 |
|
$user->addGroup($group); |
65
|
|
|
} |
66
|
|
|
} |
67
|
2 |
|
$user->setGoogleId($googleId); |
68
|
|
|
|
69
|
|
|
// Persist |
70
|
2 |
|
$this->em->persist($user); |
71
|
2 |
|
$this->em->flush(); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return isset($user) ? $user : null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* This method returns the access level coupled with the domain of the given email |
79
|
|
|
* If the given domain name has not been configured this function will return null |
80
|
|
|
* |
81
|
|
|
* @param string $email |
82
|
|
|
* |
83
|
|
|
* @return string[]|null |
84
|
|
|
*/ |
85
|
3 |
View Code Duplication |
private function getAccessLevels($email) |
|
|
|
|
86
|
|
|
{ |
87
|
3 |
|
foreach ($this->hostedDomains as $hostedDomain) { |
88
|
3 |
|
if (preg_match('/'.$hostedDomain['domain_name'].'$/', $email)) { |
89
|
2 |
|
return $hostedDomain['access_levels']; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* This method returns wether a domain for the given email has been configured |
98
|
|
|
* |
99
|
|
|
* @param string $email |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
3 |
View Code Duplication |
private function isConfiguredDomain($email) |
|
|
|
|
104
|
|
|
{ |
105
|
3 |
|
foreach ($this->hostedDomains as $hostedDomain) { |
106
|
3 |
|
if (preg_match('/'.$hostedDomain['domain_name'].'$/', $email)) { |
107
|
2 |
|
return true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return false; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.