forked from portrino/shopware-hybrid-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Port1HybridAuth.php
executable file
·205 lines (173 loc) · 6.47 KB
/
Port1HybridAuth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Port1HybridAuth;
use Doctrine\Common\Collections\ArrayCollection;
use Port1HybridAuth\Service\ConfigurationServiceInterface;
use Port1HybridAuth\Service\SingleSignOnServiceInterface;
use Shopware\Bundle\AttributeBundle\Service\CrudService;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Theme\LessDefinition;
use Shopware\Models\Customer\Customer;
use Exception;
/**
* Class Port1HybridAuth
* @package Port1HybridAuth
*/
class Port1HybridAuth extends Plugin
{
const PROVIDERS = [
'Amazon',
'Google',
'Facebook',
'LinkedIn',
// unfortunatly windows live login doesn`t work after hours of bugfixing against M$ oauth
// 'Live'
];
/**
* @param InstallContext $context
*/
public function install(InstallContext $context)
{
$this->addIdentityFieldsToUser();
parent::install($context);
}
/**
* @param ActivateContext $context
*/
public function activate(ActivateContext $context)
{
$this->addIdentityFieldsToUser();
parent::activate($context);
}
/**
* @param UninstallContext $context
*/
public function uninstall(UninstallContext $context)
{
parent::uninstall($context);
}
/**
*
*/
private function addIdentityFieldsToUser()
{
/** @var CrudService $service */
$service = $this->container->get('shopware_attribute.crud_service');
foreach (self::PROVIDERS as $provider) {
$service->update('s_user_attributes', strtolower($provider) . '_identity', 'string', [
'label' => 'Identity ' . $provider,
//user has the opportunity to translate the attribute field for each shop
'translatable' => false,
//attribute will be displayed in the backend module
'displayInBackend' => true,
//in case of multi_selection or single_selection type, article entities can be selected,
'entity' => Customer::class,
//numeric position for the backend view, sorted ascending
'position' => 100,
//user can modify the attribute in the free text field module
'custom' => false,
]);
}
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Front_StartDispatch' => 'onFrontStartDispatch',
'Enlight_Controller_Action_PreDispatch_Frontend_Register' => 'onFrontendPostDispatchRegister',
'Enlight_Controller_Action_PreDispatch_Frontend_Account' => 'onFrontendPostDispatchAccount',
'Theme_Compiler_Collect_Plugin_Less' => 'onCollectLessFiles'
];
}
/**
* @param \Enlight_Event_EventArgs $args
*/
public function onFrontStartDispatch(\Enlight_Event_EventArgs $args)
{
$this->registerHybridAuth();
}
/**
*
*/
public function registerHybridAuth()
{
// require_once $this->getPath() . '/vendor/autoload.php';
}
/**
* @param \Enlight_Event_EventArgs $args
*/
public function onFrontendPostDispatchRegister(\Enlight_Event_EventArgs $args)
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$view = $controller->View();
/** @var ConfigurationServiceInterface $configurationService */
$configurationService = $this->container->get('port1_hybrid_auth.configuration_service');
$enabledProviders = $configurationService->getEnabledProviders();
$view->assign('providers', $enabledProviders);
$view->addTemplateDir($this->getPath() . '/Resources/views');
}
/**
* @param \Enlight_Event_EventArgs $args
*/
public function onFrontendPostDispatchAccount(\Enlight_Event_EventArgs $args)
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$actionName = $controller->Front()->Request()->getActionName();
if ($actionName === 'profile') {
/** @var \Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$view = $controller->View();
$view->addTemplateDir($this->getPath() . '/Resources/views');
$userId = $controller->get('session')->get('sUserId');
/** @var Customer $customer */
$customer = $controller->get('models')->find(Customer::class, $userId);
/** @var ConfigurationServiceInterface $configurationService */
$configurationService = $this->container->get('port1_hybrid_auth.configuration_service');
$enabledProviders = $configurationService->getEnabledProviders();
$isSocialRegistered = false;
foreach ($enabledProviders as $enabledProvider => $label) {
$attribute = $customer->getAttribute();
$getProviderIdentity = 'get' . ucfirst(strtolower($enabledProvider)) . 'Identity';
if (method_exists($attribute, $getProviderIdentity)) {
$identity = $attribute->$getProviderIdentity();
if ($identity != '') {
$isSocialRegistered = true;
break;
}
}
}
$view->assign('isSocialRegistered', $isSocialRegistered);
}
if ($actionName === 'logout') {
/** @var SingleSignOnServiceInterface $singleSignOnService */
$singleSignOnService = $this->container->get('port1_hybrid_auth.single_sign_on_service');
$singleSignOnService->logout();
}
}
/**
* @param \Enlight_Event_EventArgs $args
*
* @return ArrayCollection
*/
public function onCollectLessFiles(\Enlight_Event_EventArgs $args)
{
$lessDir = $this->getPath() . '/Resources/views/frontend/_public/src/less/';
$lessDefinition = new LessDefinition(
// less configuration variables
[],
// less files which should be compiled
[
$lessDir . 'hybrid_auth.less'
],
//import directory for less @import commands
$lessDir
);
return new ArrayCollection([$lessDefinition]);
}
}