1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Security\Provider\OAuth; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\OAuth; |
6
|
|
|
use AppBundle\Entity\User; |
7
|
|
|
use AppBundle\Service\OAuthService; |
8
|
|
|
use Ds\Component\Config\Service\ConfigService; |
9
|
|
|
use Exception; |
10
|
|
|
use FOS\UserBundle\Model\UserManagerInterface; |
11
|
|
|
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; |
12
|
|
|
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException; |
13
|
|
|
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider; |
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class OrganizationProvider |
18
|
|
|
*/ |
19
|
|
|
class OrganizationProvider extends FOSUBUserProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \AppBundle\Service\OAuthService |
23
|
|
|
*/ |
24
|
|
|
protected $oAuthService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Ds\Component\Config\Service\ConfigService |
28
|
|
|
*/ |
29
|
|
|
protected $configService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param \FOS\UserBundle\Model\UserManagerInterface $userManager |
35
|
|
|
* @param array $properties |
36
|
|
|
* @param \AppBundle\Service\OAuthService $oAuthService |
37
|
|
|
* @param \Ds\Component\Config\Service\ConfigService $configService |
38
|
|
|
*/ |
39
|
|
|
public function __construct(UserManagerInterface $userManager, array $properties, OAuthService $oAuthService, ConfigService $configService) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($userManager, $properties); |
42
|
|
|
$this->oAuthService = $oAuthService; |
43
|
|
|
$this->configService = $configService; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function loadUserByOAuthUserResponse(UserResponseInterface $response) |
50
|
|
|
{ |
51
|
|
|
$oAuth = $this->oAuthService->getRepository()->findOneBy([ |
52
|
|
|
'type' => $response->getResourceOwner()->getName(), |
53
|
|
|
'identifier' => $response->getUsername() |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
if ($oAuth) { |
57
|
|
|
$oAuth->setToken($response->getAccessToken()); |
58
|
|
|
$user = $oAuth->getUser(); |
59
|
|
|
} else { |
60
|
|
|
$email = $response->getEmail(); |
61
|
|
|
|
62
|
|
|
if (null === $email) { |
63
|
|
|
throw new AccountNotLinkedException('Email is not defined.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$user = $this->userManager->findUserByUsernameOrEmail($response->getEmail()); |
67
|
|
|
|
68
|
|
|
if ($user) { |
|
|
|
|
69
|
|
|
throw new AccountNotLinkedException('Username is not available.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$user = new User; |
73
|
|
|
$user |
74
|
|
|
->setUsername($response->getResourceOwner()->getName().'/'.$response->getUsername()) |
75
|
|
|
->setEmail($response->getEmail()) |
76
|
|
|
->setPlainPassword(sha1(uniqid().microtime())) |
77
|
|
|
->setRoles([]) |
78
|
|
|
->setOwner($this->configService->get('app.oauth.organization.owner.type')) |
79
|
|
|
->setOwnerUuid($this->configService->get('app.oauth.organization.owner.uuid')) |
80
|
|
|
->setIdentity('Organization') |
81
|
|
|
->setIdentityUuid('c9599da5-35a8-494c-9181-975d78be9694') |
82
|
|
|
->setEnabled($this->configService->get('app.oauth.organization.enabled')); |
83
|
|
|
$this->userManager->updateUser($user); |
84
|
|
|
$oAuth = new OAuth; |
85
|
|
|
$oAuth |
86
|
|
|
->setUser($user) |
87
|
|
|
->setType($response->getResourceOwner()->getName()) |
88
|
|
|
->setIdentifier($response->getUsername()) |
89
|
|
|
->setToken($response->getAccessToken()) |
90
|
|
|
->setOwner($this->configService->get('app.oauth.organization.owner.type')) |
91
|
|
|
->setOwnerUuid($this->configService->get('app.oauth.organization.owner.uuid')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$manager = $this->oAuthService->getManager(); |
95
|
|
|
$manager->persist($oAuth); |
96
|
|
|
$manager->flush(); |
97
|
|
|
|
98
|
|
|
return $user; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
|
|
public function connect(UserInterface $user, UserResponseInterface $response) |
105
|
|
|
{ |
106
|
|
|
throw new Exception('Not yet implemented.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function disconnect(UserInterface $user, UserResponseInterface $response) |
113
|
|
|
{ |
114
|
|
|
throw new Exception('Not yet implemented.'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function refreshUser(UserInterface $user) |
121
|
|
|
{ |
122
|
|
|
throw new Exception('Not yet implemented.'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|