Example #1
0
File: Cxn.php Project: kidaa30/yes
 /**
  * Get the AppMeta for an existing connection.
  *
  * @param string $cxnId
  * @return array
  * @throws \Civi\Cxn\Rpc\Exception\CxnException
  */
 public static function getAppMeta($cxnId)
 {
     $appMetaJson = CRM_Core_DAO::getFieldValue('CRM_Cxn_DAO_Cxn', $cxnId, 'app_meta', 'cxn_guid', TRUE);
     $appMeta = json_decode($appMetaJson, TRUE);
     \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
     return $appMeta;
 }
Example #2
0
/**
 * Register with a remote application and create a new connection.
 *
 * One should generally identify an application using the app_guid.
 * However, if you need to test a new/experimental application, then
 * disable CIVICRM_CXN_CA and specify app_meta_url.
 *
 * @param array $params
 *   Array with keys:
 *   - app_guid: The unique identifer of the target application.
 *   - app_meta_url: The URL for the application's metadata.
 * @return array
 * @throws Exception
 */
function civicrm_api3_cxn_register($params)
{
    if (!empty($params['app_meta_url'])) {
        list($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
        if (CRM_Utils_HttpClient::STATUS_OK != $status) {
            throw new API_Exception("Failed to download appMeta. (Bad HTTP response)");
        }
        $appMeta = json_decode($json, TRUE);
        if (empty($appMeta)) {
            throw new API_Exception("Failed to download appMeta. (Malformed)");
        }
    } elseif (!empty($params['app_guid'])) {
        $appMeta = civicrm_api3('CxnApp', 'getsingle', array('appId' => $params['app_guid']));
    }
    if (empty($appMeta) || !is_array($appMeta)) {
        throw new API_Exception("Missing expected parameter: app_guid");
    }
    \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
    try {
        /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
        $client = \Civi::service('cxn_reg_client');
        list($cxnId, $result) = $client->register($appMeta);
        CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
    } catch (Exception $e) {
        CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
        throw $e;
    }
    return $result;
}
 public function __construct($appId, $appMeta, $privateKey, $publicKey)
 {
     AppMeta::validate($appMeta);
     $this->appId = $appId;
     $this->appMeta = $appMeta;
     $this->privateKey = $privateKey;
     $this->publicKey = $publicKey;
 }
 /**
  * @param array $appMeta
  * @return array
  *   Array($cxnId, $isOk).
  */
 public function register($appMeta)
 {
     AppMeta::validate($appMeta);
     if ($this->certValidator) {
         $this->certValidator->validateCert($appMeta['appCert']);
     }
     $cxn = $this->cxnStore->getByAppId($appMeta['appId']);
     if (!$cxn) {
         $cxn = array('cxnId' => Cxn::createId(), 'secret' => AesHelper::createSecret(), 'appId' => $appMeta['appId']);
     }
     $cxn['appUrl'] = $appMeta['appUrl'];
     $cxn['siteUrl'] = $this->siteUrl;
     $cxn['perm'] = $appMeta['perm'];
     Cxn::validate($cxn);
     $this->cxnStore->add($cxn);
     list($respCode, $respData) = $this->doCall($appMeta, 'Cxn', 'register', array(), $cxn);
     $success = $respCode == 200 && $respData['is_error'] == 0;
     $this->log->info($success ? 'Registered cxnId={cxnId} ({appId}, {appUrl})' : 'Failed to register cxnId={cxnId} ({appId}, {appUrl})', array('cxnId' => $cxn['cxnId'], 'appId' => $cxn['appId'], 'appUrl' => $cxn['appUrl']));
     return array($cxn['cxnId'], $respData);
 }