Skip to content

Commit

Permalink
Simplify method-based allow checkers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Feb 6, 2024
1 parent 88d55c4 commit b2674f2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
26 changes: 1 addition & 25 deletions src/conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -5342,35 +5342,11 @@ function api_map() {
}
return $this->_api_map;
}
/** @param object $xt
* @param XtParams $xtp
* @param string $method
* @return ?bool */
private function api_method_checker($xt, $xtp, $method) {
if (!$method || isset($xt->alias)) {
return null;
}
$k = strtolower($method);
$methodx = $xt->$k ?? null;
if ($methodx === null
&& ($method === "POST" || $method === "HEAD")) {
$methodx = $xt->get ?? false;
}
return $methodx ?? false;
}
/** @param string $method
* @return callable(object,XtParams):bool */
function make_api_method_checker($method) {
return function ($xt, $xtp) use ($method) {
return $this->api_method_checker($xt, $xtp, $method);
};
}
function has_api($fn, Contact $user = null, $method = null) {
return !!$this->api($fn, $user, $method);
}
function api($fn, Contact $user = null, $method = null) {
$xtp = new XtParams($this, $user);
$xtp->allow_checkers[] = $this->make_api_method_checker($method);
$xtp = (new XtParams($this, $user))->add_allow_checker_method($method);
$uf = $xtp->search_name($this->api_map(), $fn);
return self::xt_enabled($uf) ? $uf : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/listaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static private function do_call($name, Contact $user, Qrequest $qreq, $selection
}
$conf = $user->conf;
$gex = self::grouped_extensions($user);
$gex->xtp->allow_checkers = [$conf->make_api_method_checker($qreq->method())];
$gex->xtp->add_allow_checker_method($qreq->method());
$uf = $gex->get($name);
if (!$uf && ($slash = strpos($name, "/"))) {
$uf = $gex->get(substr($name, 0, $slash));
Expand Down
37 changes: 36 additions & 1 deletion src/xtparams.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ function __construct($conf, $user) {
$this->user = $user;
}

/** @param object $xt
* @param XtParams $xtp
* @return ?bool */
static function allow_checker_GET($xt, $xtp) {
return isset($xt->alias) ? null : $xt->get ?? false;
}

/** @param object $xt
* @param XtParams $xtp
* @return ?bool */
static function allow_checker_HEAD($xt, $xtp) {
return isset($xt->alias) ? null : $xt->head ?? $xt->get ?? false;
}

/** @param object $xt
* @param XtParams $xtp
* @return ?bool */
static function allow_checker_POST($xt, $xtp) {
return isset($xt->alias) ? null : $xt->post ?? $xt->get ?? false;
}

/** @param ?string $method
* @return $this */
function add_allow_checker_method($method) {
if ($method === "GET" || $method === "HEAD" || $method === "POST") {
$this->allow_checkers[] = "XtParams::allow_checker_{$method}";
} else if ($method !== null && $method !== "") {
$method = strtolower($method);
$this->allow_checkers[] = function ($xt, $xtp) use ($method) {
return isset($xt->alias) ? null : $xt->$method ?? false;
};
}
return $this;
}

/** @param object $xt
* @return bool */
function checkf($xt) {
Expand All @@ -39,7 +74,7 @@ function checkf($xt) {
}
if ($this->allow_checkers !== null) {
foreach ($this->allow_checkers as $checker) {
if (($x = $checker($xt, $this)) !== nulL)
if (($x = $checker($xt, $this)) !== null)
return $x;
}
}
Expand Down

0 comments on commit b2674f2

Please sign in to comment.