JsonRpcMethod.php in JSON-RPC 2.x
File
src/Annotation/JsonRpcMethod.php
View source
<?php
namespace Drupal\jsonrpc\Annotation;
use Drupal\Component\Annotation\AnnotationBase;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\jsonrpc\MethodInterface;
class JsonRpcMethod extends AnnotationBase implements MethodInterface {
public $access = [];
public $call;
public $usage;
public $params = [];
public $responseHeaders = [];
public function id() {
return $this
->getId();
}
public function call() {
if (!isset($this->call)) {
$this->call = 'execute';
}
return $this->call;
}
public function getUsage() {
return $this->usage;
}
public function getParams() {
return $this->params;
}
public function areParamsPositional() {
return array_reduce(array_keys($this
->getParams()), function ($positional, $key) {
return $positional ? !is_string($key) : $positional;
}, TRUE);
}
public function get() {
return $this;
}
public function access($operation = 'execute', AccountInterface $account = NULL, $return_as_object = FALSE) {
$account = $account ?: \Drupal::currentUser();
switch ($operation) {
case 'execute':
if (is_callable($this->access)) {
return call_user_func_array($this->access, [
$operation,
$account,
$return_as_object,
]);
}
$access_result = AccessResult::allowed();
foreach ($this->access as $permission) {
$access_result = $access_result
->andIf(AccessResult::allowedIfHasPermission($account, $permission));
}
break;
case 'view':
$access_result = $this
->access('execute', $account, $return_as_object);
break;
default:
$access_result = AccessResult::neutral();
break;
}
return $return_as_object ? $access_result : $access_result
->isAllowed();
}
}
Classes
Name |
Description |
JsonRpcMethod |
Defines a JsonRpcParameterDefinition annotation object. |