Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / conduit / call / ConduitCall.php
blob6be49daef0598a77c7cb13dec7d488b81a979a0f
1 <?php
3 /**
4 * Run a conduit method in-process, without requiring HTTP requests. Usage:
6 * $call = new ConduitCall('method.name', array('param' => 'value'));
7 * $call->setUser($user);
8 * $result = $call->execute();
11 final class ConduitCall extends Phobject {
13 private $method;
14 private $handler;
15 private $request;
16 private $user;
18 public function __construct($method, array $params, $strictly_typed = true) {
19 $this->method = $method;
20 $this->handler = $this->buildMethodHandler($method);
22 $param_types = $this->handler->getParamTypes();
24 foreach ($param_types as $key => $spec) {
25 if (ConduitAPIMethod::getParameterMetadataKey($key) !== null) {
26 throw new ConduitException(
27 pht(
28 'API Method "%s" defines a disallowed parameter, "%s". This '.
29 'parameter name is reserved.',
30 $method,
31 $key));
35 $invalid_params = array_diff_key($params, $param_types);
36 if ($invalid_params) {
37 throw new ConduitException(
38 pht(
39 'API Method "%s" does not define these parameters: %s.',
40 $method,
41 "'".implode("', '", array_keys($invalid_params))."'"));
44 $this->request = new ConduitAPIRequest($params, $strictly_typed);
47 public function getAPIRequest() {
48 return $this->request;
51 public function setUser(PhabricatorUser $user) {
52 $this->user = $user;
53 return $this;
56 public function getUser() {
57 return $this->user;
60 public function shouldRequireAuthentication() {
61 return $this->handler->shouldRequireAuthentication();
64 public function shouldAllowUnguardedWrites() {
65 return $this->handler->shouldAllowUnguardedWrites();
68 public function getErrorDescription($code) {
69 return $this->handler->getErrorDescription($code);
72 public function execute() {
73 $profiler = PhutilServiceProfiler::getInstance();
74 $call_id = $profiler->beginServiceCall(
75 array(
76 'type' => 'conduit',
77 'method' => $this->method,
78 ));
80 try {
81 $result = $this->executeMethod();
82 } catch (Exception $ex) {
83 $profiler->endServiceCall($call_id, array());
84 throw $ex;
87 $profiler->endServiceCall($call_id, array());
88 return $result;
91 private function executeMethod() {
92 $user = $this->getUser();
93 if (!$user) {
94 $user = new PhabricatorUser();
97 $this->request->setUser($user);
99 if (!$this->shouldRequireAuthentication()) {
100 // No auth requirement here.
101 } else {
103 $allow_public = $this->handler->shouldAllowPublic() &&
104 PhabricatorEnv::getEnvConfig('policy.allow-public');
105 if (!$allow_public) {
106 if (!$user->isLoggedIn() && !$user->isOmnipotent()) {
107 // TODO: As per below, this should get centralized and cleaned up.
108 throw new ConduitException('ERR-INVALID-AUTH');
112 // TODO: This would be slightly cleaner by just using a Query, but the
113 // Conduit auth workflow requires the Call and User be built separately.
114 // Just do it this way for the moment.
115 $application = $this->handler->getApplication();
116 if ($application) {
117 $can_view = PhabricatorPolicyFilter::hasCapability(
118 $user,
119 $application,
120 PhabricatorPolicyCapability::CAN_VIEW);
122 if (!$can_view) {
123 throw new ConduitException(
124 pht(
125 'You do not have access to the application which provides this '.
126 'API method.'));
131 return $this->handler->executeMethod($this->request);
134 protected function buildMethodHandler($method_name) {
135 $method = ConduitAPIMethod::getConduitMethod($method_name);
137 if (!$method) {
138 throw new ConduitMethodDoesNotExistException($method_name);
141 $application = $method->getApplication();
142 if ($application && !$application->isInstalled()) {
143 $app_name = $application->getName();
144 throw new ConduitApplicationNotInstalledException($method, $app_name);
147 return $method;
150 public function getMethodImplementation() {
151 return $this->handler;