/** * @param string $entity * type of entities to deal with * @param string $action * create, get, delete or some special action name. * @param array $params * array to be passed to function * @param null $extra * * @return array|int */ public function run($entity, $action, $params, $extra = NULL) { /** * @var $apiProvider \Civi\API\Provider\ProviderInterface|NULL */ $apiProvider = NULL; // TODO Define alternative calling convention makes it easier to construct $apiRequest // without the ambiguity of "data" vs "options" $apiRequest = Request::create($entity, $action, $params, $extra); try { if (!is_array($params)) { throw new \API_Exception('Input variable `params` is not an array', 2000); } $this->boot(); $errorScope = \CRM_Core_TemporaryErrorScope::useException(); list($apiProvider, $apiRequest) = $this->resolve($apiRequest); $this->authorize($apiProvider, $apiRequest); $apiRequest = $this->prepare($apiProvider, $apiRequest); $result = $apiProvider->invoke($apiRequest); $apiResponse = $this->respond($apiProvider, $apiRequest, $result); return $this->formatResult($apiRequest, $apiResponse); } catch (\Exception $e) { $this->dispatcher->dispatch(Events::EXCEPTION, new ExceptionEvent($e, $apiProvider, $apiRequest)); if ($e instanceof \PEAR_Exception) { $err = $this->formatPearException($e, $apiRequest); } elseif ($e instanceof \API_Exception) { $err = $this->formatApiException($e, $apiRequest); } else { $err = $this->formatException($e, $apiRequest); } return $this->formatResult($apiRequest, $err); } }
/** * Ensure that API parameters "is_transactional" and "force_rollback" are parsed correctly. * * @dataProvider transactionOptions * * @param $version * @param $entity * @param $action * @param array $params * @param bool $isTransactional * @param bool $isForceRollback * @param bool $isNested * * @throws \API_Exception */ public function testTransactionOptions($version, $entity, $action, $params, $isTransactional, $isForceRollback, $isNested) { $txs = new TransactionSubscriber(); $apiProvider = NULL; $params['version'] = $version; $apiRequest = \Civi\API\Request::create($entity, $action, $params, array()); $this->assertEquals($isTransactional, $txs->isTransactional($apiProvider, $apiRequest), 'check isTransactional'); $this->assertEquals($isForceRollback, $txs->isForceRollback($apiProvider, $apiRequest), 'check isForceRollback'); $this->assertEquals($isNested, $txs->isNested($apiProvider, $apiRequest), 'check isNested'); }
/** * @param $fields * * @return array|bool * @throws API_Exception */ public static function formRule($fields) { $errors = array(); require_once 'api/api.php'; /** @var \Civi\API\Kernel $apiKernel */ $apiKernel = \Civi\Core\Container::singleton()->get('civi_api_kernel'); $apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], array('version' => 3), NULL); try { $apiKernel->resolve($apiRequest); } catch (\Civi\API\Exception\NotImplementedException $e) { $errors['api_action'] = ts('Given API command is not defined.'); } if (!empty($errors)) { return $errors; } return empty($errors) ? TRUE : $errors; }
/** * @param string $entity * @param string $action * @param array $params * @param bool $throws whether we should pass any exceptions for authorization failures * * @throws API_Exception * @throws Exception * @return bool TRUE or FALSE depending on the outcome of the authorization check */ function runPermissionCheck($entity, $action, $params, $throws = FALSE) { $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); $kernel = new \Civi\API\Kernel($dispatcher); $apiRequest = \Civi\API\Request::create($entity, $action, $params, NULL); try { $kernel->authorize(NULL, $apiRequest); return TRUE; } catch (\API_Exception $e) { $extra = $e->getExtraParams(); if (!$throws && $extra['error_code'] == API_Exception::UNAUTHORIZED) { return FALSE; } else { throw $e; } } }
/** * Determine if a hypothetical API call would be authorized. * * @param string $entity * Type of entities to deal with. * @param string $action * Create, get, delete or some special action name. * @param array $params * Array to be passed to function. * @param mixed $extra * Who knows. * @return bool * TRUE if authorization would succeed. * @throws \Exception */ public function runAuthorize($entity, $action, $params, $extra = NULL) { $apiProvider = NULL; $apiRequest = Request::create($entity, $action, $params, $extra); try { $this->boot(); list($apiProvider, $apiRequest) = $this->resolve($apiRequest); $this->authorize($apiProvider, $apiRequest); return TRUE; } catch (\Civi\API\Exception\UnauthorizedException $e) { return FALSE; } }
/** * Procedural wrapper for the OO api version 4. * * @param $entity * @param $action * @param array $params * @return \Civi\API\Result */ function civicrm_api4($entity, $action, $params = array()) { $params['version'] = 4; $request = \Civi\API\Request::create($entity, $action, $params); return \Civi::service('civi_api_kernel')->runRequest($request); }