|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Azine\HybridAuthBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
12
|
|
|
|
|
13
|
|
|
class AzineHybridAuthJsonController extends Controller { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Check if the user is connected to the requested provider. |
|
17
|
|
|
* @param Request $request |
|
18
|
|
|
* @param string $provider |
|
19
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
20
|
|
|
*/ |
|
21
|
|
|
public function isConnectedAction(Request $request, $provider){ |
|
22
|
|
|
try { |
|
23
|
|
|
$connected = $this->getAzineHybridAuthService()->isConnected($request, $provider); |
|
24
|
|
|
return new JsonResponse(array('connected' => $connected)); |
|
25
|
|
|
} catch (\Exception $e) { |
|
26
|
|
|
return new JsonResponse(array('connected' => false, 'message' => $e->getMessage()."\n\n\n".$e->getTraceAsString())); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Try to connect to the provider |
|
32
|
|
|
* @param Request $request |
|
33
|
|
|
* @param string $provider |
|
34
|
|
|
* @param null $callbackRoute |
|
35
|
|
|
* @return RedirectResponse |
|
36
|
|
|
* @throws \Exception e.g. if the api-connection is invalid |
|
37
|
|
|
*/ |
|
38
|
|
|
public function connectUserAction(Request $request, $provider, $callbackRoute = null){ |
|
39
|
|
|
$deleteSessionData = $request->query->get('force', false); |
|
40
|
|
|
$cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
|
41
|
|
|
if($deleteSessionData){ |
|
42
|
|
|
$this->getAzineHybridAuthService()->deleteSession($provider); |
|
43
|
|
|
} |
|
44
|
|
|
try { |
|
45
|
|
|
$hybridAuth = $this->getAzineHybridAuthService()->getInstance($request->cookies->get($cookieName), $provider); |
|
46
|
|
|
$connected = $hybridAuth->isConnectedWith($provider); |
|
47
|
|
|
} catch (\Exception $e) { |
|
48
|
|
|
$response = new RedirectResponse($this->generateUrl($callbackRoute)); |
|
49
|
|
|
if($deleteSessionData){ |
|
50
|
|
|
$response->headers->clearCookie($cookieName, '/', $request->getHost(), $request->isSecure(), true); |
|
51
|
|
|
} |
|
52
|
|
|
return $response; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if(!$connected || $deleteSessionData){ |
|
56
|
|
|
try { |
|
57
|
|
|
$adapter = $hybridAuth->getAdapter($provider); |
|
58
|
|
|
setcookie($cookieName, null, -1, '/', $request->getHost(), $request->isSecure(), true); |
|
59
|
|
|
$adapter->login(); |
|
60
|
|
|
} catch (\Exception $e) { |
|
61
|
|
|
throw new \Exception("Unable to create adapter for provider '$provider'. Is it configured properly?", $e->getCode(), $e); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$params = $request->query->all(); |
|
66
|
|
|
$callbackUrl = $this->generateUrl($callbackRoute, $params); |
|
67
|
|
|
if(!$callbackUrl){ |
|
68
|
|
|
throw new \Exception("Callback route not defined"); |
|
69
|
|
|
} |
|
70
|
|
|
$response = new RedirectResponse($callbackUrl); |
|
71
|
|
|
if($deleteSessionData){ |
|
72
|
|
|
$response->headers->clearCookie($cookieName, '/', $request->getHost(), $request->isSecure(), true); |
|
73
|
|
|
} |
|
74
|
|
|
return $response; |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the users Profile for the requested provider |
|
81
|
|
|
* @param Request $request |
|
82
|
|
|
* @param string $provider |
|
83
|
|
|
* @param string $userId |
|
84
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
85
|
|
|
* @throws \Exception e.g. if the api-connection is invalid |
|
86
|
|
|
*/ |
|
87
|
|
|
public function profileAction(Request $request, $provider, $userId = null){ |
|
88
|
|
|
|
|
89
|
|
|
if($userId == null){ |
|
|
|
|
|
|
90
|
|
|
$cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
|
91
|
|
|
$profile = $this->getAzineHybridAuthService()->getProvider($request->cookies->get($cookieName), $provider)->getUserProfile(); |
|
92
|
|
|
} else { |
|
93
|
|
|
$profile = $this->getBusinessNetworkProviderService()->getUserContactBasicProfile($provider, $userId); |
|
94
|
|
|
} |
|
95
|
|
|
return new JsonResponse(array('profile' => $profile)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Request $request |
|
100
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
101
|
|
|
* @throws \Exception e.g. if the api-connection is invalid |
|
102
|
|
|
*/ |
|
103
|
|
|
public function profileByUrlAction(Request $request){ |
|
104
|
|
|
$profileUrl = $request->get("searchByUrl"); |
|
105
|
|
|
$profile = $this->getBusinessNetworkProviderService()->getUserProfileByUrl($profileUrl); |
|
106
|
|
|
return new JsonResponse(array('profile' => $profile)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get all users contacts for the requested provider |
|
112
|
|
|
* @param Request $request |
|
113
|
|
|
* @param string $provider |
|
114
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
115
|
|
|
* @throws \Exception e.g. if the api-connection is invalid |
|
116
|
|
|
*/ |
|
117
|
|
|
public function contactsAction(Request $request, $provider){ |
|
118
|
|
|
$cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
|
119
|
|
|
$contacts = $this->getAzineHybridAuthService()->getProvider($request->cookies->get($cookieName), $provider)->getUserContacts(); |
|
120
|
|
|
return new JsonResponse(array('contacts' => $contacts)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get all contacts from Xing and LinkedIn |
|
125
|
|
|
* @param Request $request |
|
126
|
|
|
* @param integer $pageSize |
|
127
|
|
|
* @param integer $offset |
|
128
|
|
|
* @return JsonResponse |
|
129
|
|
|
* @throws \Exception e.g. if the api-connection is invalid |
|
130
|
|
|
*/ |
|
131
|
|
|
public function mergedContactsAction(Request $request, $pageSize, $offset){ |
|
|
|
|
|
|
132
|
|
|
$contacts = $this->getBusinessNetworkProviderService()->getContactProfiles($pageSize, $offset); |
|
133
|
|
|
return new JsonResponse(array('contacts' => $contacts)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return AzineMergedBusinessNetworksProvider |
|
138
|
|
|
*/ |
|
139
|
|
|
private function getBusinessNetworkProviderService(){ |
|
140
|
|
|
return $this->get("azine_business_networks_provider_service"); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return AzineHybridAuth |
|
145
|
|
|
*/ |
|
146
|
|
|
private function getAzineHybridAuthService(){ |
|
147
|
|
|
return $this->get("azine_hybrid_auth_service"); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|