|
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 Zend\Mvc\Controller\AbstractActionController; |
|
14
|
|
|
use Zend\View\Model\JsonModel; |
|
15
|
|
|
use Core\Form\SummaryFormInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Main Action Controller for Authentication module. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
class ManageController extends AbstractActionController |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* attaches further Listeners for generating / processing the output |
|
25
|
|
|
* @return $this |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
public function attachDefaultListeners() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
parent::attachDefaultListeners(); |
|
30
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
31
|
|
|
$defaultServices = $serviceLocator->get('DefaultListeners'); |
|
32
|
|
|
$events = $this->getEventManager(); |
|
33
|
|
|
$events->attach($defaultServices); |
|
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array|JsonModel |
|
39
|
|
|
*/ |
|
40
|
|
|
public function profileAction() |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
43
|
|
|
$forms = $serviceLocator->get('forms'); |
|
44
|
|
|
$container = $forms->get('Auth/userprofilecontainer'); |
|
45
|
|
|
$user = $serviceLocator->get('AuthenticationService')->getUser(); /* @var $user \Auth\Entity\User */ |
|
46
|
|
|
$postProfiles = (array)$this->params()->fromPost('social_profiles'); |
|
47
|
|
|
$userProfiles = $user->getProfile(); |
|
48
|
|
|
$formSocialProfiles = $forms->get('Auth/SocialProfiles') |
|
49
|
|
|
->setUseDefaultValidation(true) |
|
50
|
|
|
->setData(['social_profiles' => array_map(function ($array) |
|
51
|
|
|
{ |
|
52
|
|
|
return $array['data']; |
|
53
|
|
|
}, $userProfiles)]); |
|
54
|
|
|
|
|
55
|
|
|
$translator = $serviceLocator->get('Translator'); |
|
56
|
|
|
$formSocialProfiles->getBaseFieldset() |
|
57
|
|
|
->setOption('description', $translator->translate("you can add your social profile to your application. You can preview and remove the attached profile before submitting the application.")); |
|
58
|
|
|
$container->setEntity($user); |
|
59
|
|
|
|
|
60
|
|
|
if ($this->request->isPost()) { |
|
|
|
|
|
|
61
|
|
|
$formName = $this->params()->fromQuery('form'); |
|
62
|
|
|
$form = $container->getForm($formName); |
|
63
|
|
|
|
|
64
|
|
|
if ($form) { |
|
65
|
|
|
$postData = $form->getOption('use_post_array') ? $_POST : array(); |
|
66
|
|
|
$filesData = $form->getOption('use_files_array') ? $_FILES : array(); |
|
67
|
|
|
$data = array_merge($postData, $filesData); |
|
68
|
|
|
$form->setData($data); |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
if (!$form->isValid()) { |
|
|
|
|
|
|
71
|
|
|
return new JsonModel( |
|
72
|
|
|
array( |
|
73
|
|
|
'valid' => false, |
|
74
|
|
|
'errors' => $form->getMessages(), |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$serviceLocator->get('repositories')->store($user); |
|
80
|
|
|
|
|
81
|
|
|
if ('file-uri' === $this->params()->fromPost('return')) { |
|
82
|
|
|
$content = $form->getHydrator()->getLastUploadedFile()->getUri(); |
|
83
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
84
|
|
|
if ($form instanceof SummaryFormInterface) { |
|
85
|
|
|
$form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY); |
|
86
|
|
|
$viewHelper = 'summaryform'; |
|
87
|
|
|
} else { |
|
88
|
|
|
$viewHelper = 'form'; |
|
89
|
|
|
} |
|
90
|
|
|
$content = $serviceLocator->get('ViewHelperManager')->get($viewHelper)->__invoke($form); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return new JsonModel( |
|
94
|
|
|
array( |
|
95
|
|
|
'valid' => $form->isValid(), |
|
96
|
|
|
'content' => $content, |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
elseif ($postProfiles) { |
|
|
|
|
|
|
101
|
|
|
$formSocialProfiles->setData($this->params()->fromPost()); |
|
102
|
|
|
|
|
103
|
|
|
if ($formSocialProfiles->isValid()) { |
|
104
|
|
|
$dataProfiles = $formSocialProfiles->getData()['social_profiles']; |
|
105
|
|
|
$userRepository = $serviceLocator->get('repositories')->get('Auth/User'); /* @var $userRepository \Auth\Repository\User */ |
|
106
|
|
|
$hybridAuth = $serviceLocator->get('HybridAuthAdapter') |
|
107
|
|
|
->getHybridAuth(); |
|
108
|
|
|
|
|
109
|
|
|
foreach ($dataProfiles as $network => $postProfile) { |
|
110
|
|
|
// remove |
|
111
|
|
|
if (isset($userProfiles[$network]) && !$dataProfiles[$network]) { |
|
112
|
|
|
$user->removeProfile($network); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// add |
|
116
|
|
|
if (!isset($userProfiles[$network]) && $dataProfiles[$network]) { |
|
117
|
|
|
$authProfile = $hybridAuth->authenticate($network) |
|
118
|
|
|
->getUserProfile(); |
|
119
|
|
|
// check for existing profiles |
|
120
|
|
|
if ($userRepository->isProfileAssignedToAnotherUser($user->getId(), $authProfile->identifier, $network)) { |
|
121
|
|
|
$dataProfiles[$network] = null; |
|
122
|
|
|
$formSocialProfiles->setMessages(array( |
|
123
|
|
|
'social_profiles' => [ |
|
124
|
|
|
$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)] |
|
125
|
|
|
] |
|
126
|
|
|
)); |
|
127
|
|
|
} else { |
|
128
|
|
|
$profile = [ |
|
129
|
|
|
'auth' => (array)$authProfile, |
|
130
|
|
|
'data' => \Zend\Json\Json::decode($dataProfiles[$network]) |
|
131
|
|
|
]; |
|
132
|
|
|
$user->addProfile($network, $profile); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// keep data in sync & properly decoded |
|
139
|
|
|
$formSocialProfiles->setData(['social_profiles' => array_map(function ($array) |
|
140
|
|
|
{ |
|
141
|
|
|
return \Zend\Json\Json::decode($array) ?: ''; |
|
142
|
|
|
}, $dataProfiles)]); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return array( |
|
147
|
|
|
'form' => $container, |
|
148
|
|
|
'socialProfilesForm' => $formSocialProfiles |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
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.