public function testAccountController()
 {
     $this->_instance = Sipgate_Controller_Account::getInstance();
     $newRecord = $this->_instance->create(new Sipgate_Model_Account($this->_testConfig), false);
     $this->assertEquals($newRecord->password, NULL);
     $record = $this->_instance->get($newRecord->getId());
     $this->assertEquals($record->__get('password'), NULL);
 }
 /**
  * Takes the config from config.inc.php and creates an account with the associated lines
  * @param Zend_Console_Getopt $_opts 
  */
 public function take_config($_opts)
 {
     // resolve arguments
     $args = $this->_parseArgs($_opts, array());
     $type = in_array('shared', $args['other']) ? 'shared' : 'private';
     if (@isset(Tinebase_Core::getConfig()->sipgate)) {
         $conf = Tinebase_Core::getConfig()->sipgate;
         if (@isset($conf->api_username) && @isset($conf->api_password)) {
             echo 'Validate configuration from config.inc.php...' . PHP_EOL;
             $accountData = array('data' => array('accounttype' => @isset($conf->api_url) && $conf->api_url != 'https://samurai.sipgate.net/RPC2' ? 'team' : 'plus', 'description' => 'Created by update', 'username' => $conf->api_username, 'password' => $conf->api_password, 'type' => $type));
             if (Sipgate_Controller_Account::getInstance()->validateAccount($accountData)) {
                 echo 'Data from config.inc.php could be validated, creating account...' . PHP_EOL;
                 try {
                     $account = Sipgate_Controller_Account::getInstance()->create(new Sipgate_Model_Account($accountData['data']));
                 } catch (Tinebase_Exception_Duplicate $e) {
                     echo 'An account with this credentials exists already! Did you use this script twice?' . PHP_EOL;
                     die;
                 }
                 if ($account) {
                     echo 'Account created. Trying to synchronize the lines..' . PHP_EOL;
                     if (Sipgate_Controller_Line::getInstance()->syncAccount($account->getId())) {
                         $opts = new Zend_Console_Getopt('abp:');
                         $args = array('initial', 'verbose');
                         if ($type == 'shared') {
                             $args[] = 'shared';
                         }
                         $opts->setArguments($args);
                         echo 'Lines have been synchronized. Now syncing connections from the last two months, day per day. This could take some time.' . PHP_EOL;
                         $this->sync_connections($opts);
                         echo 'Connections has been synchronized. Now assign users to the line(s) to allow them to use the line(s)' . PHP_EOL;
                         echo 'READY!' . PHP_EOL;
                     } else {
                         echo 'The lines for the account could not be created!' . PHP_EOL;
                     }
                 } else {
                     echo 'The account could not be created!' . PHP_EOL;
                 }
             } else {
                 echo 'The credentials found in config.inc.php could not be validated!' . PHP_EOL;
             }
         } else {
             echo 'No username or password could be found in config.php.inc!' . PHP_EOL;
         }
     } else {
         echo 'No sipgate config could be found in config.php.inc!' . PHP_EOL;
     }
 }
 /**
  * returns multiple records prepared for json transport
  *
  * @param Tinebase_Record_RecordSet $_records Tinebase_Record_Abstract
  * @param Tinebase_Model_Filter_FilterGroup $_filter
  * @param Tinebase_Model_Pagination $_pagination
  * @return array data
  */
 protected function _multipleRecordsToJson(Tinebase_Record_RecordSet $_records, $_filter = NULL, $_pagination = null)
 {
     switch ($_records->getRecordClassName()) {
         case 'Sipgate_Model_Connection':
             Sipgate_Controller_Line::getInstance()->resolveMultipleLines($_records);
             $this->_resolveMultipleContacts($_records);
             break;
         case 'Sipgate_Model_Line':
             Sipgate_Controller_Account::getInstance()->resolveMultipleAccounts($_records);
             Tinebase_User::getInstance()->resolveMultipleUsers($_records, $this->_resolveUserFields['Sipgate_Model_Line']);
             break;
     }
     return parent::_multipleRecordsToJson($_records);
 }
 /**
  * (non-PHPdoc)
  * @see Tinebase_Controller_Record_Abstract::_addDefaultFilter()
  */
 protected function _addDefaultFilter(Tinebase_Model_Filter_FilterGroup $_filter = NULL)
 {
     $manageShared = Tinebase_Core::getUser()->hasRight('Sipgate', Sipgate_Acl_Rights::MANAGE_SHARED_ACCOUNTS);
     $managePrivate = Tinebase_Core::getUser()->hasRight('Sipgate', Sipgate_Acl_Rights::MANAGE_PRIVATE_ACCOUNTS);
     $manage = Tinebase_Core::getUser()->hasRight('Sipgate', Sipgate_Acl_Rights::MANAGE_ACCOUNTS);
     $fg = new Sipgate_Model_LineFilter(array(), 'OR');
     // fetch all assignes lines
     $my = new Tinebase_Model_Filter_Id(array('field' => 'user_id', 'operator' => 'equals', 'value' => Tinebase_Core::getUser()->getId()));
     $my->setIsImplicit(true);
     $fg->addFilter($my);
     if ($manage) {
         // fetch all private accounts of the user himself
         if ($managePrivate) {
             $accountFilter = new Sipgate_Model_AccountFilter(array(), 'AND');
             $accountFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'type', 'operator' => 'equals', 'value' => 'private')));
             $r = Sipgate_Controller_Account::getInstance()->search($accountFilter);
             if ($r->count()) {
                 $f = new Tinebase_Model_Filter_Id(array('field' => 'account_id', 'operator' => 'in', 'value' => $r->id));
                 $f->setIsImplicit(true);
                 $fg->addFilter($f);
             }
         }
         // fetch shared accounts
         if ($manageShared) {
             $accountFilter = new Sipgate_Model_AccountFilter(array(), 'AND');
             $accountFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'type', 'operator' => 'equals', 'value' => 'shared')));
             $r = Sipgate_Controller_Account::getInstance()->searchRightsLess($accountFilter);
             if ($r->count()) {
                 $f = new Tinebase_Model_Filter_Id(array('field' => 'account_id', 'operator' => 'in', 'value' => $r->id));
                 $f->setIsImplicit(true);
                 $fg->addFilter($f);
             }
         }
     }
     $_filter->addFiltergroup($fg);
 }
 /**
  * Syncs all lines for the current user
  */
 public function syncLines(Tinebase_DateTime $_from = NULL, Tinebase_DateTime $_to = NULL, $_shared = false, $verbose = false)
 {
     $filter = new Sipgate_Model_AccountFilter(array('field' => 'created_by', 'operator' => 'equals', 'value' => $_shared ? NULL : Tinebase_Core::getUser()->getId()));
     $pag = new Tinebase_Model_Pagination();
     $accounts = Sipgate_Controller_Account::getInstance()->search($filter, $pag);
     foreach ($accounts->getIterator() as $account) {
         $filter = new Sipgate_Model_ConnectionFilter(array('field' => 'account_id', 'operator' => 'equals', 'value' => $account->getId()));
         $lines = Sipgate_Controller_Line::getInstance()->search($filter, $pag);
         foreach ($lines->getIterator() as $line) {
             $count = $count + $this->syncLine($line, $_from, $_to, $verbose);
         }
     }
 }
 /**
  * the singleton pattern
  *
  * @return Sipgate_Controller_Account
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Sipgate_Controller_Account();
     }
     return self::$_instance;
 }
 /**
  * connect
  * @param String $_accountId     ID of the account if configured already or resolved Sipgate_Model_Account
  * @param String $_username      Username if no account-id is given
  * @param String $_password      Password if no account-id is given
  * @param String $_accounttype   Accounttype if no account-id is given
  */
 public function connect($_accountId = NULL, $_username = NULL, $_password = NULL, $_accounttype = NULL)
 {
     if ($_accountId) {
         if ($_accountId instanceof Sipgate_Model_Account) {
             $this->_account = $_accountId;
         } else {
             $this->_account = Sipgate_Controller_Account::getInstance()->getResolved($_accountId);
         }
         $cfg = $this->_account->toArray();
         if (!$cfg) {
             throw new Sipgate_Exception_ResolveCredentials();
         }
     } else {
         if ($_username && $_password && $_accounttype) {
             $cfg['username'] = $_username;
             $cfg['password'] = $_password;
             $cfg['accounttype'] = $_accounttype;
         } else {
             throw new Sipgate_Exception_MissingConfig();
         }
     }
     if ($cfg['accounttype'] == 'team') {
         $url = 'https://api.sipgate.net/RPC2';
     } else {
         $url = 'https://samurai.sipgate.net/RPC2';
     }
     $this->_http = new Zend_Http_Client($url);
     $this->_http->setMethod('post');
     $this->_http->setAuth($cfg['username'], $cfg['password']);
     $this->_rpc = new Zend_XmlRpc_Client($url, $this->_http);
     try {
         $this->identify();
     } catch (Zend_XmlRpc_Client_HttpException $e) {
         switch ($e->getCode()) {
             case 401:
                 // Set Account Invalid on auth error
                 if ($_accountId) {
                     $this->_account->is_valid = false;
                     $ac->update($this->_account);
                 }
                 throw new Sipgate_Exception_Authorization();
                 break;
             default:
                 $this->_throwUnknownExeption($e);
         }
     } catch (Zend_Http_Client_Adapter_Exception $e) {
         switch ($e->getCode()) {
             case 0:
                 if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " throws connection error exception: " . get_class($e));
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Code:    " . $e->getCode());
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Message: " . $e->getMessage());
                 }
                 throw new Sipgate_Exception_NoConnection();
                 break;
             default:
                 $this->_throwUnknownExeption($e);
         }
     } catch (Exception $e) {
         $this->_throwUnknownExeption($e);
     }
     return $this;
 }