· 5 years ago · Jun 22, 2020, 10:12 AM
1public function userLogin(string $subscriptionId) {
2 $subscription = Crm_Api_Helper_Subscription::resolve($subscriptionId);
3
4 $accountActions = Crm_Account_Actions::forSubscription($subscription);
5 $useCustomDomain = $this->params->get('useCustomDomain') === 'true';
6 try {
7 $result = $accountActions->getLoginResponse($this->params->get('userId'), $useCustomDomain);
8 } catch (Exception $e) {
9 throw new ApiException(500, $e->getMessage());
10 }
11
12 if ($result === null) {
13 throw new ApiException(404, 'Could not successfully execute login call.');
14 }
15
16 return $result;
17 }
18
19 protected function getClient($useCustomDomain = false) {
20 $account = $this->subscription->getAccount();
21 $domain = $useCustomDomain ? $account->getCustomDomain() : $account->getDomain();
22
23 $baseUrl = "http://{$domain}";
24 $apiKey = $account->getAttributes()->getLoginApiKey();
25 if ($apiKey == null) {
26 throw new Exception('Login api key is not set yet.');
27 }
28
29 return new GuzzleHttp\Client([
30 'base_uri' => $baseUrl,
31 RequestOptions::ALLOW_REDIRECTS => true,
32 RequestOptions::TIMEOUT => 5,
33 RequestOptions::HTTP_ERRORS => true,
34 RequestOptions::HEADERS => [
35 'apikey' => $apiKey
36 ],
37 RequestOptions::VERIFY => false
38 ]);
39 }