1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** Auth controller */ |
11
|
|
|
namespace Auth\Controller; |
12
|
|
|
|
13
|
|
|
use Auth\Adapter\HybridAuth; |
14
|
|
|
use Auth\AuthenticationService; |
15
|
|
|
use Auth\Form\SocialProfiles; |
16
|
|
|
use Auth\Form\UserProfileContainer; |
17
|
|
|
use Core\Repository\RepositoryService; |
18
|
|
|
use Interop\Container\ContainerInterface; |
19
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
20
|
|
|
use Zend\Mvc\I18n\Translator; |
21
|
|
|
use Zend\View\HelperPluginManager; |
22
|
|
|
use Zend\View\Model\JsonModel; |
23
|
|
|
use Core\Form\SummaryFormInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Main Action Controller for Authentication module. |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
class ManageController extends AbstractActionController |
30
|
|
|
{ |
31
|
|
|
private $userProfileContainer; |
32
|
|
|
|
33
|
|
|
private $authService; |
34
|
|
|
|
35
|
|
|
private $socialProfileForm; |
36
|
|
|
|
37
|
|
|
private $translator; |
38
|
|
|
|
39
|
|
|
private $repositories; |
40
|
|
|
|
41
|
|
|
private $viewHelper; |
42
|
|
|
|
43
|
|
|
private $hybridAuthAdapter; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ContainerInterface $container |
47
|
|
|
* @return ManageController |
48
|
|
|
*/ |
49
|
1 |
|
static public function factory(ContainerInterface $container) |
50
|
|
|
{ |
51
|
1 |
|
$forms = $container->get('forms'); |
52
|
1 |
|
$userProfileContainer = $forms->get('Auth/UserProfileContainer'); |
53
|
1 |
|
$socialProfileForm = $forms->get('Auth/SocialProfiles'); |
54
|
1 |
|
$authService = $container->get('AuthenticationService'); |
55
|
1 |
|
$translator = $container->get('translator'); |
56
|
1 |
|
$repositories = $container->get('repositories'); |
57
|
1 |
|
$viewHelper = $container->get('ViewHelperManager'); |
58
|
1 |
|
$hybridAuthAdapter = $container->get('HybridAuthAdapter'); |
59
|
1 |
|
$controller = new ManageController( |
60
|
1 |
|
$userProfileContainer, |
61
|
1 |
|
$authService, |
62
|
1 |
|
$repositories, |
63
|
1 |
|
$socialProfileForm, |
64
|
1 |
|
$translator, |
65
|
1 |
|
$viewHelper, |
66
|
1 |
|
$hybridAuthAdapter |
67
|
|
|
); |
68
|
1 |
|
return $controller; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function __construct( |
72
|
|
|
UserProfileContainer $userProfileContainer, |
73
|
|
|
AuthenticationService $authService, |
74
|
|
|
RepositoryService $repositories, |
75
|
|
|
SocialProfiles $socialProfileForm, |
76
|
|
|
Translator $translator, |
77
|
|
|
HelperPluginManager $viewHelper, |
78
|
|
|
HybridAuth $hybridAuthAdapter |
79
|
|
|
) |
80
|
|
|
{ |
81
|
1 |
|
$this->userProfileContainer = $userProfileContainer; |
82
|
1 |
|
$this->authService = $authService; |
83
|
1 |
|
$this->socialProfileForm = $socialProfileForm; |
84
|
1 |
|
$this->repositories = $repositories; |
85
|
1 |
|
$this->translator = $translator; |
86
|
1 |
|
$this->viewHelper = $viewHelper; |
87
|
1 |
|
$this->hybridAuthAdapter = $hybridAuthAdapter; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array|JsonModel |
92
|
|
|
*/ |
93
|
1 |
|
public function profileAction() |
94
|
|
|
{ |
95
|
|
|
/* @var \Auth\Form\UserProfileContainer $userProfileContainer */ |
96
|
1 |
|
$userProfileContainer = $this->userProfileContainer; |
97
|
1 |
|
$user = $this->authService->getUser(); /* @var $user \Auth\Entity\User */ |
98
|
1 |
|
$postProfiles = (array)$this->params()->fromPost('social_profiles'); |
99
|
1 |
|
$userProfiles = $user->getProfile(); |
100
|
1 |
|
$formSocialProfiles = $this->socialProfileForm |
101
|
1 |
|
->setUseDefaultValidation(true) |
102
|
1 |
|
->setData(['social_profiles' => array_map(function ($array) |
103
|
|
|
{ |
104
|
|
|
return $array['data']; |
105
|
1 |
|
}, $userProfiles)]); |
106
|
|
|
|
107
|
1 |
|
$translator = $this->translator; |
108
|
|
|
/* @var \Auth\Form\SocialProfiles $formSocialProfiles */ |
109
|
1 |
|
$formSocialProfiles->getBaseFieldset() |
110
|
1 |
|
->setOption( |
111
|
1 |
|
'description', |
112
|
1 |
|
$translator->translate('You can connect your user profile with social networks. This allows you to log in via these networks.') |
113
|
|
|
); |
114
|
1 |
|
$userProfileContainer->setEntity($user); |
115
|
|
|
|
116
|
1 |
|
if ($this->request->isPost()) { |
|
|
|
|
117
|
|
|
$formName = $this->params()->fromQuery('form'); |
118
|
|
|
$form = $userProfileContainer->getForm($formName); |
119
|
|
|
|
120
|
|
|
if ($form) { |
121
|
|
|
$postData = $form->getOption('use_post_array') ? $_POST : array(); |
122
|
|
|
//@TODO: [ZF3] option use_files_array is false by default |
123
|
|
|
//$filesData = $form->getOption('use_files_array') ? $_FILES : array(); |
124
|
|
|
$filesData = $_FILES; |
125
|
|
|
$data = array_merge($postData, $filesData); |
126
|
|
|
$form->setData($data); |
127
|
|
|
|
128
|
|
|
if (!$form->isValid()) { |
129
|
|
|
return new JsonModel( |
130
|
|
|
array( |
131
|
|
|
'valid' => false, |
132
|
|
|
'errors' => $form->getMessages(), |
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->repositories->store($user); |
138
|
|
|
|
139
|
|
|
if ('file-uri' === $this->params()->fromPost('return')) { |
140
|
|
|
$content = $form->getHydrator()->getLastUploadedFile()->getUri(); |
|
|
|
|
141
|
|
|
} else { |
142
|
|
|
if ($form instanceof SummaryFormInterface) { |
143
|
|
|
$form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY); |
144
|
|
|
$viewHelper = 'summaryForm'; |
145
|
|
|
} else { |
146
|
|
|
$viewHelper = 'form'; |
147
|
|
|
} |
148
|
|
|
$content = $this->viewHelper->get($viewHelper)->__invoke($form); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return new JsonModel( |
152
|
|
|
array( |
153
|
|
|
'valid' => $form->isValid(), |
154
|
|
|
'content' => $content, |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
elseif ($postProfiles) { |
|
|
|
|
159
|
|
|
$formSocialProfiles->setData($this->params()->fromPost()); |
160
|
|
|
|
161
|
|
|
if ($formSocialProfiles->isValid()) { |
162
|
|
|
$dataProfiles = $formSocialProfiles->getData()['social_profiles']; |
163
|
|
|
$userRepository = $this->repositories->get('Auth/User'); /* @var $userRepository \Auth\Repository\User */ |
164
|
|
|
$hybridAuth = $this->hybridAuthAdapter->getHybridAuth(); |
165
|
|
|
|
166
|
|
|
foreach ($dataProfiles as $network => $postProfile) { |
167
|
|
|
// remove |
168
|
|
|
if (isset($userProfiles[$network]) && !$dataProfiles[$network]) { |
169
|
|
|
$user->removeProfile($network); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// add |
173
|
|
|
if (!isset($userProfiles[$network]) && $dataProfiles[$network]) { |
174
|
|
|
$authProfile = $hybridAuth->authenticate($network) |
175
|
|
|
->getUserProfile(); |
176
|
|
|
// check for existing profiles |
177
|
|
|
if ($userRepository->isProfileAssignedToAnotherUser($user->getId(), $authProfile->identifier, $network)) { |
|
|
|
|
178
|
|
|
$dataProfiles[$network] = null; |
179
|
|
|
$formSocialProfiles->setMessages(array( |
180
|
|
|
'social_profiles' => [ |
181
|
|
|
$network => [sprintf($translator->translate('Could not connect your %s profile with your user account. The profile is already connected to another user account.'), $authProfile->displayName)] |
182
|
|
|
] |
183
|
|
|
)); |
184
|
|
|
} else { |
185
|
|
|
$profile = [ |
186
|
|
|
'auth' => (array)$authProfile, |
187
|
|
|
'data' => \Zend\Json\Json::decode($dataProfiles[$network]) |
188
|
|
|
]; |
189
|
|
|
$user->addProfile($network, $profile); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// keep data in sync & properly decoded |
196
|
|
|
$formSocialProfiles->setData(['social_profiles' => array_map(function ($array) |
197
|
|
|
{ |
198
|
|
|
return \Zend\Json\Json::decode($array) ?: ''; |
199
|
|
|
}, $dataProfiles)]); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return array( |
204
|
|
|
'form' => $userProfileContainer, |
205
|
|
|
'socialProfilesForm' => $formSocialProfiles |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|