| 1 | <?php |
||
| 2 | |||
| 3 | namespace Azine\HybridAuthBundle\Controller; |
||
| 4 | |||
| 5 | use Azine\HybridAuthBundle\Services\AzineGenderGuesser; |
||
| 6 | use Azine\HybridAuthBundle\Services\AzineHybridAuth; |
||
| 7 | use Azine\HybridAuthBundle\Services\AzineMergedBusinessNetworksProvider; |
||
| 8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||
| 9 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
| 10 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
| 11 | use Symfony\Component\HttpFoundation\Request; |
||
| 12 | |||
| 13 | class AzineHybridAuthJsonController extends Controller |
||
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Check if the user is connected to the requested provider. |
||
| 17 | * |
||
| 18 | * @param Request $request |
||
| 19 | * @param string $provider |
||
| 20 | * |
||
| 21 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 22 | */ |
||
| 23 | public function isConnectedAction(Request $request, $provider) |
||
| 24 | { |
||
| 25 | try { |
||
| 26 | $connected = $this->getAzineHybridAuthService()->isConnected($request, $provider); |
||
| 27 | |||
| 28 | return new JsonResponse(array('connected' => $connected)); |
||
| 29 | } catch (\Exception $e) { |
||
| 30 | return new JsonResponse(array('connected' => false, 'message' => $e->getMessage()."\n\n\n".$e->getTraceAsString())); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Try to connect to the provider. |
||
| 36 | * |
||
| 37 | * @param Request $request |
||
| 38 | * @param string $provider |
||
| 39 | * @param null $callbackRoute |
||
|
0 ignored issues
–
show
|
|||
| 40 | * |
||
| 41 | * @return RedirectResponse |
||
| 42 | * |
||
| 43 | * @throws \Exception e.g. if the api-connection is invalid |
||
| 44 | */ |
||
| 45 | public function connectUserAction(Request $request, $provider, $callbackRoute = null) |
||
| 46 | { |
||
| 47 | $params = $request->query->all(); |
||
| 48 | $callbackUrl = $this->generateUrl($callbackRoute, $params); |
||
| 49 | |||
| 50 | $deleteSessionData = $request->query->get('force', false); |
||
| 51 | $cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
||
| 52 | if ($deleteSessionData) { |
||
| 53 | $this->getAzineHybridAuthService()->deleteSession($provider); |
||
| 54 | } |
||
| 55 | try { |
||
| 56 | $adapter = $this->getAzineHybridAuthService()->getInstance($request->cookies->get($cookieName), $provider); |
||
| 57 | $adapter->getStorage()->set("hauth_session.$provider.hauth_return_to", $callbackUrl); |
||
| 58 | $connected = $adapter->isConnected(); |
||
| 59 | } catch (\Exception $e) { |
||
| 60 | $response = new RedirectResponse($callbackUrl); |
||
| 61 | if ($deleteSessionData) { |
||
| 62 | $response->headers->clearCookie($cookieName, '/', $request->getHost(), $request->isSecure(), true); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $response; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!$connected || $deleteSessionData) { |
||
| 69 | try { |
||
| 70 | setcookie($cookieName, null, -1, '/', $request->getHost(), $request->isSecure(), true); |
||
| 71 | $adapter->authenticate(); |
||
| 72 | } catch (\Exception $e) { |
||
| 73 | throw new \Exception("Unable to create adapter for provider '$provider'. Is it configured properly?", $e->getCode(), $e); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if (!$callbackUrl) { |
||
| 78 | throw new \Exception('Callback route not defined'); |
||
| 79 | } |
||
| 80 | $response = new RedirectResponse($callbackUrl); |
||
| 81 | if ($deleteSessionData) { |
||
| 82 | $response->headers->clearCookie($cookieName, '/', $request->getHost(), $request->isSecure(), true); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $response; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the users Profile for the requested provider. |
||
| 90 | * |
||
| 91 | * @param Request $request |
||
| 92 | * @param string $provider |
||
| 93 | * @param string $userId |
||
| 94 | * |
||
| 95 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 96 | * |
||
| 97 | * @throws \Exception e.g. if the api-connection is invalid |
||
| 98 | */ |
||
| 99 | public function profileAction(Request $request, $provider, $userId = null) |
||
| 100 | { |
||
| 101 | if (null == $userId) { |
||
|
0 ignored issues
–
show
|
|||
| 102 | $cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
||
| 103 | $providerAdapter = $this->getAzineHybridAuthService()->getProvider($request->cookies->get($cookieName), $provider); |
||
| 104 | $profile = $providerAdapter->getUserProfile(); |
||
| 105 | if (!$profile->gender) { |
||
| 106 | /* @var $genderGuesser AzineGenderGuesser */ |
||
| 107 | $genderGuesser = $this->get('azine_hybrid_auth_gender_guesser'); |
||
| 108 | $gender = $genderGuesser->guess($profile->firstName); |
||
| 109 | $profile->gender = is_array($gender) ? $gender['gender'] : null; |
||
| 110 | } |
||
| 111 | if (!$profile->profileURL) { |
||
| 112 | $profile->profileURL = "LinkedIn doesn't allow to access this. :-/"; |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | $profile = $this->getBusinessNetworkProviderService()->getUserContactBasicProfile($provider, $userId); |
||
| 116 | } |
||
| 117 | |||
| 118 | return new JsonResponse(array('profile' => $profile)); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param Request $request |
||
| 123 | * |
||
| 124 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 125 | * |
||
| 126 | * @throws \Exception e.g. if the api-connection is invalid |
||
| 127 | */ |
||
| 128 | public function profileByUrlAction(Request $request) |
||
| 129 | { |
||
| 130 | $profileUrl = $request->get('searchByUrl'); |
||
| 131 | $profile = $this->getBusinessNetworkProviderService()->getUserProfileByUrl($profileUrl); |
||
| 132 | |||
| 133 | return new JsonResponse(array('profile' => $profile)); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get all users contacts for the requested provider. |
||
| 138 | * |
||
| 139 | * @param Request $request |
||
| 140 | * @param string $provider |
||
| 141 | * |
||
| 142 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 143 | * |
||
| 144 | * @throws \Exception e.g. if the api-connection is invalid |
||
| 145 | */ |
||
| 146 | public function contactsAction(Request $request, $provider) |
||
| 147 | { |
||
| 148 | $cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
||
| 149 | $contacts = $this->getAzineHybridAuthService()->getProvider($request->cookies->get($cookieName), $provider)->getUserContacts(); |
||
| 150 | |||
| 151 | return new JsonResponse(array('contacts' => $contacts)); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get all contacts from Xing and LinkedIn. |
||
| 156 | * |
||
| 157 | * @param Request $request |
||
| 158 | * @param int $pageSize |
||
| 159 | * @param int $offset |
||
| 160 | * |
||
| 161 | * @return JsonResponse |
||
| 162 | * |
||
| 163 | * @throws \Exception e.g. if the api-connection is invalid |
||
| 164 | */ |
||
| 165 | public function mergedContactsAction(Request $request, $pageSize, $offset) |
||
| 166 | { |
||
| 167 | $filterParams = $request->query->all(); |
||
| 168 | $contacts = $this->getBusinessNetworkProviderService()->getContactProfiles($pageSize, $offset, $filterParams); |
||
| 169 | |||
| 170 | return new JsonResponse(array('contacts' => $contacts)); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return AzineMergedBusinessNetworksProvider |
||
| 175 | */ |
||
| 176 | private function getBusinessNetworkProviderService() |
||
| 177 | { |
||
| 178 | return $this->get('azine_business_networks_provider_service'); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return AzineHybridAuth |
||
| 183 | */ |
||
| 184 | private function getAzineHybridAuthService() |
||
| 185 | { |
||
| 186 | return $this->get('azine_hybrid_auth_service'); |
||
| 187 | } |
||
| 188 | } |
||
| 189 |