From 822bdf039a126bfe1ffd7cc825e3d72ed06ee181 Mon Sep 17 00:00:00 2001 From: will Date: Sun, 25 Jun 2017 14:18:29 +0100 Subject: [PATCH] Fixed intercom user add --- .../Listener/IntercomAdminListener.php | 25 ++++++++++++------- .../Listener/IntercomCompanyListener.php | 4 +-- .../classes/Ushahidi/Repository/User.php | 9 +++++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/application/classes/Ushahidi/Listener/IntercomAdminListener.php b/application/classes/Ushahidi/Listener/IntercomAdminListener.php index 1f9ae015f3..bbf04ffa45 100644 --- a/application/classes/Ushahidi/Listener/IntercomAdminListener.php +++ b/application/classes/Ushahidi/Listener/IntercomAdminListener.php @@ -27,28 +27,35 @@ public function handle(EventInterface $event, $user = null) $intercomAppToken = getenv('INTERCOM_APP_TOKEN'); $domain = service('site'); $company = [ - "id" => $domain + "company_id" => $domain ]; - if ($intercomAppToken && empty($domain)) { + if ($intercomAppToken && !empty($domain)) { $client = new IntercomClient($intercomAppToken, null); try { - $client->users->update([ + $intercom_user = [ "email" => $user->email, "created_at" => $user->created, "user_id" => $domain . '_' . $user->id, - "realname" => $user->realname, - "last_login" => $user->last_login, - "role" => $user->role, - "language" => $user->language, + "name" => $user->realname, "companies" => [ $company + ], + "custom_attributes" => [ + "last_login" => $user->last_login, + "logins" => $user->logins, + "role" => $user->role, + "language" => $user->language, ] - ]); + ]; + + $client->users->update($intercom_user); + } catch(ClientException $e) { - Kohana::$log->add(Log::ERROR, print_r($e,true)); + $message = $e->getMessage(); + Kohana::$log->add(Log::ERROR, print_r($message,true)); } } } diff --git a/application/classes/Ushahidi/Listener/IntercomCompanyListener.php b/application/classes/Ushahidi/Listener/IntercomCompanyListener.php index ce694491fc..9afa126de4 100644 --- a/application/classes/Ushahidi/Listener/IntercomCompanyListener.php +++ b/application/classes/Ushahidi/Listener/IntercomCompanyListener.php @@ -26,7 +26,7 @@ public function handle(EventInterface $event, $data = null) $intercomAppToken = getenv('INTERCOM_APP_TOKEN'); $domain = service('site'); - if ($intercomAppToken && empty($domain)) { + if ($intercomAppToken && !empty($domain)) { try { $client = new IntercomClient($intercomAppToken, null); @@ -37,7 +37,7 @@ public function handle(EventInterface $event, $data = null) "custom_attributes" => $data ]; // Update company - $thing = $client->companies->create($company); + $client->companies->create($company); } catch(ClientException $e) { Kohana::$log->add(Log::ERROR, print_r($e,true)); diff --git a/application/classes/Ushahidi/Repository/User.php b/application/classes/Ushahidi/Repository/User.php index b9235cf2fc..cce764d7ff 100644 --- a/application/classes/Ushahidi/Repository/User.php +++ b/application/classes/Ushahidi/Repository/User.php @@ -79,7 +79,7 @@ public function createWithHash(Entity $entity) ]; $entity->setState($state); if ($entity->role === 'admin') { - $this->updateIntercomAdminUsers($state); + $this->updateIntercomAdminUsers($entity); } return parent::create($entity); @@ -96,7 +96,12 @@ public function update(Entity $entity) $state['password'] = $this->hasher->hash($entity->password); } - return parent::update($entity->setState($state)); + $entity->setState($state); + if ($entity->role === 'admin') { + $this->updateIntercomAdminUsers($entity); + } + + return parent::update($entity); } // SearchRepository