From: Tim Otten Date: Mon, 7 Jun 2021 00:53:49 +0000 (-0700) Subject: (REF) AuthorizeEvent - Extract AuthorizedTrait X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=63a2492e2fbd35bfbe70c3a7e9b76fd52a12ff4f;p=civicrm-core.git (REF) AuthorizeEvent - Extract AuthorizedTrait The primary purpose of this is to provide a trait (`AuthorizedTrait`) to describe the common semantics of of coarse-grained authorization check and the upcoming fine-grained authorization check. The extracted trait makes a few small changes: * Change the default value from `FALSE` to `NULL`. In grepping universe for consumers of `isAuthorized(0`, I could only find consumers that used bool-ish values. So this should be the same for them. However, for future cases, it will allow some distinction between NULL/FALSE. * Use more type-hints. The type should be nullable-boolean. * Mutators should be amenable to fluent style (e.g. `$event->authorize()->stopPropagation()`). --- diff --git a/Civi/API/Event/AuthorizeEvent.php b/Civi/API/Event/AuthorizeEvent.php index 9d146a9b40..164677a01b 100644 --- a/Civi/API/Event/AuthorizeEvent.php +++ b/Civi/API/Event/AuthorizeEvent.php @@ -22,24 +22,7 @@ namespace Civi\API\Event; * Event name: 'civi.api.authorize' */ class AuthorizeEvent extends Event { - /** - * @var bool - */ - private $authorized = FALSE; - /** - * Mark the request as authorized. - */ - public function authorize() { - $this->authorized = TRUE; - } - - /** - * @return bool - * TRUE if the request has been authorized. - */ - public function isAuthorized() { - return $this->authorized; - } + use AuthorizedTrait; } diff --git a/Civi/API/Event/AuthorizedTrait.php b/Civi/API/Event/AuthorizedTrait.php new file mode 100644 index 0000000000..af3367af1d --- /dev/null +++ b/Civi/API/Event/AuthorizedTrait.php @@ -0,0 +1,48 @@ +authorized = TRUE; + return $this; + } + + /** + * @return bool|null + * TRUE if the request has been authorized. + */ + public function isAuthorized(): ?bool { + return $this->authorized; + } + + /** + * Change the authorization status. + * + * @param bool|null $authorized + * @return static + */ + public function setAuthorized(?bool $authorized) { + $this->authorized = $authorized; + return $this; + } + +}