Exemplo n.º 1
0
 /**
  * Checks the user credentials for validty
  * Unfortunately i have not found a proper way to check for validity other than calling a function
  * that requires authorisation. So we do some basic checks by calling several methods to figure out
  * what we can do and what not.
  * I probably need to check on submodule, but seems your API user needs quite a high authentication
  * level to actually do anything at all.
  *
  * @param $data
  *
  * @return bool
  */
 protected function checkConnection($data)
 {
     if ($data['client_environment'] && $data['client_account'] && $data['client_username'] && $data['client_password']) {
         try {
             $tp = new TripolisProvider($data['client_account'], $data['client_username'], $data['client_password'], $data['client_environment']);
             // Check if server is working
             $infoResponse = $tp->contact()->info();
             // Check the user
             $user = $tp->user()->getByAuthInfo();
             if (!$user->hasRole(GetByAuthInfoResponse::ROLE_MODULE_CONTACT)) {
                 add_settings_error('client_account', 'unauthorized', __('Your user does not have acces to the Contact Module'));
                 return false;
             }
             // Check if we have access to the required components
             $databases = $tp->ContactDatabase()->all();
             $db = $databases->first();
             if ($db) {
                 // Test the COntact Group Service
                 $groups = $tp->ContactGroup()->all($db->id);
                 // Test the Contact service
                 $result = $tp->Contact()->countByContactDatabaseId($db->id);
                 add_settings_error('client_account', 'nodatabases', __('Settings ok, and validated'), 'updated');
                 return true;
             } else {
                 add_settings_error('client_account', 'nodatabases', __('You do not have access to any databases in this environment'));
             }
         } catch (UnauthorizedException $e) {
             add_settings_error('client_username', 'unauthorized', __('Wrong credentials, access was denied by the API'));
         } catch (\SoapFault $f) {
             add_settings_error('client_environment', $f->getCode(), $f->getMessage());
         } catch (\Exception $e) {
             add_settings_error('client_environment', $e->getCode(), $e->getMessage());
         }
         return false;
     }
     return false;
 }