diff --git a/application/classes/Ushahidi/Core.php b/application/classes/Ushahidi/Core.php index 1bede426b7..6194a22e86 100644 --- a/application/classes/Ushahidi/Core.php +++ b/application/classes/Ushahidi/Core.php @@ -74,8 +74,8 @@ public static function init() }); // Intercom config settings - $di->set('site.intercomAppToken', function() use ($di) { - return Kohana::$config->load('site.intercomAppToken'); + $di->set('thirdparty.intercomAppToken', function() use ($di) { + return getenv('INTERCOM_APP_TOKEN'); }); // Roles config settings @@ -690,18 +690,17 @@ public static function init() // Add Intercom Listener to Config $di->setter['Ushahidi_Repository_Config']['setEvent'] = 'ConfigUpdateEvent'; $di->setter['Ushahidi_Repository_Config']['setListener'] = - $di->lazyNew('Ushahidi_Listener_IntercomListener'); + $di->lazyNew('Ushahidi_Listener_IntercomCompanyListener'); // Add Intercom Listener to Form $di->setter['Ushahidi_Repository_Form']['setEvent'] = 'FormUpdateEvent'; $di->setter['Ushahidi_Repository_Form']['setListener'] = - $di->lazyNew('Ushahidi_Listener_IntercomListener'); + $di->lazyNew('Ushahidi_Listener_IntercomCompanyListener'); // Add Intercom Listener to User $di->setter['Ushahidi_Repository_User']['setEvent'] = 'UserGetAllEvent'; $di->setter['Ushahidi_Repository_User']['setListener'] = - $di->lazyNew('Ushahidi_Listener_IntercomListener'); - + $di->lazyNew('Ushahidi_Listener_IntercomAdminListener'); /** * 1. Load the plugins diff --git a/application/classes/Ushahidi/Listener/IntercomAdminListener.php b/application/classes/Ushahidi/Listener/IntercomAdminListener.php new file mode 100644 index 0000000000..bbf04ffa45 --- /dev/null +++ b/application/classes/Ushahidi/Listener/IntercomAdminListener.php @@ -0,0 +1,63 @@ + + * @package Ushahidi\Application + * @copyright 2014 Ushahidi + * @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) + */ + +use League\Event\AbstractListener; +use League\Event\EventInterface; + +use Intercom\IntercomClient; + +use GuzzleHttp\Exception\ClientException; + +class Ushahidi_Listener_IntercomAdminListener extends AbstractListener +{ + + public function handle(EventInterface $event, $user = null) + { + if($user && $user->role === 'admin') { + $intercomAppToken = getenv('INTERCOM_APP_TOKEN'); + $domain = service('site'); + $company = [ + "company_id" => $domain + ]; + + if ($intercomAppToken && !empty($domain)) { + + $client = new IntercomClient($intercomAppToken, null); + + try { + $intercom_user = [ + "email" => $user->email, + "created_at" => $user->created, + "user_id" => $domain . '_' . $user->id, + "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) { + $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 new file mode 100644 index 0000000000..9afa126de4 --- /dev/null +++ b/application/classes/Ushahidi/Listener/IntercomCompanyListener.php @@ -0,0 +1,47 @@ + + * @package Ushahidi\Application + * @copyright 2014 Ushahidi + * @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) + */ + +use League\Event\AbstractListener; +use League\Event\EventInterface; + +use Intercom\IntercomClient; + +use GuzzleHttp\Exception\ClientException; + +class Ushahidi_Listener_IntercomCompanyListener extends AbstractListener +{ + + public function handle(EventInterface $event, $data = null) + { + $intercomAppToken = getenv('INTERCOM_APP_TOKEN'); + $domain = service('site'); + + if ($intercomAppToken && !empty($domain)) { + + try { + $client = new IntercomClient($intercomAppToken, null); + + + $company = [ + "company_id" => $domain, + "custom_attributes" => $data + ]; + // Update company + $client->companies->create($company); + + } catch(ClientException $e) { + Kohana::$log->add(Log::ERROR, print_r($e,true)); + } + } + } +} diff --git a/application/classes/Ushahidi/Listener/IntercomListener.php b/application/classes/Ushahidi/Listener/IntercomListener.php deleted file mode 100644 index 08a62acf1f..0000000000 --- a/application/classes/Ushahidi/Listener/IntercomListener.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @package Ushahidi\Application - * @copyright 2014 Ushahidi - * @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) - */ - -use League\Event\AbstractListener; -use League\Event\EventInterface; - -use Intercom\IntercomClient; - -use GuzzleHttp\Exception\ClientException; - -class Ushahidi_Listener_IntercomListener extends AbstractListener -{ - public function handle(EventInterface $event, $user_email = null, $data = null) - { - $intercomAppToken = service('site.intercomAppToken'); - - if ($user_email && $intercomAppToken) { - - $client = new IntercomClient($intercomAppToken, null); - try { - - $client->users->update([ - "email" => $user_email, - "custom_attributes" => $data - ]); - } catch(ClientException $e) { - Kohana::$log->add(Log::ERROR, print_r($e,true)); - } - } - } -} diff --git a/application/classes/Ushahidi/Repository/Config.php b/application/classes/Ushahidi/Repository/Config.php index 500998efb8..0fbc40a28f 100644 --- a/application/classes/Ushahidi/Repository/Config.php +++ b/application/classes/Ushahidi/Repository/Config.php @@ -97,8 +97,7 @@ public function update(Entity $entity) } if ($intercom_data) { - $user = service('session.user'); - $this->emit($this->event, $user->email, $intercom_data); + $this->emit($this->event, $intercom_data); } } @@ -160,4 +159,3 @@ public function all(Array $groups = null) return $result; } } - diff --git a/application/classes/Ushahidi/Repository/Form.php b/application/classes/Ushahidi/Repository/Form.php index 66ad76fda0..8b5f87636c 100644 --- a/application/classes/Ushahidi/Repository/Form.php +++ b/application/classes/Ushahidi/Repository/Form.php @@ -79,8 +79,7 @@ public function update(Entity $entity) // If orignal Form update Intercom if Name changed if ($entity->id === 1) { foreach ($entity->getChanged() as $key => $val) { - $user = service('session.user'); - $key === 'name' ? $this->emit($this->event, $user->email, ['primary_survey_name' => $val]) : null; + $key === 'name' ? $this->emit($this->event, ['primary_survey_name' => $val]) : null; } } $form = $entity->getChanged(); diff --git a/application/classes/Ushahidi/Repository/User.php b/application/classes/Ushahidi/Repository/User.php index a8e77c2e10..cce764d7ff 100644 --- a/application/classes/Ushahidi/Repository/User.php +++ b/application/classes/Ushahidi/Repository/User.php @@ -64,7 +64,11 @@ public function create(Entity $entity) 'created' => time(), 'password' => $this->hasher->hash($entity->password), ]; - return parent::create($entity->setState($state)); + $entity->setState($state); + if ($entity->role === 'admin') { + $this->updateIntercomAdminUsers($entity); + } + return parent::create($entity); } // CreateRepository @@ -73,8 +77,12 @@ public function createWithHash(Entity $entity) $state = [ 'created' => time() ]; + $entity->setState($state); + if ($entity->role === 'admin') { + $this->updateIntercomAdminUsers($entity); + } - return parent::create($entity->setState($state)); + return parent::create($entity); } // UpdateRepository @@ -88,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 @@ -139,8 +152,6 @@ public function isUniqueEmail($email) public function register(Entity $entity) { - $this->updateIntercomUserCount(1); - return $this->executeInsert([ 'realname' => $entity->realname, 'email' => $entity->email, @@ -213,7 +224,9 @@ public function getTotalCount(Array $where = []) // DeleteRepository public function delete(Entity $entity) { - $this->updateIntercomUserCount(-1); + if ($entity->role === 'admin') { + $this->updateIntercomAdminUsers($entity); + } return parent::delete($entity); } @@ -223,12 +236,8 @@ public function delete(Entity $entity) { * @param Integer $offset * @return void */ - protected function updateIntercomUserCount($offset) + protected function updateIntercomAdminUsers($user) { - $data = [ - 'total_users' => $this->getTotalCount() + $offset - ]; - $user = service('session.user'); - $this->emit($this->event, $user->email, $data); + $this->emit($this->event, $user); } } diff --git a/application/config/site.php b/application/config/site.php index f4de51aa9a..53972a0883 100644 --- a/application/config/site.php +++ b/application/config/site.php @@ -22,7 +22,6 @@ * - string date_format Set format in which to return dates. See http://php.net/manual/en/datetime.createfromformat.php */ -$intercomAppToken = getenv('INTERCOM_APP_TOKEN'); $clientUrl = getenv('CLIENT_URL'); @@ -46,5 +45,4 @@ 'first_login' => true, 'tier' => 'free', 'private' => false, - 'intercomAppToken' => $intercomAppToken, );