Commit | Line | Data |
---|---|---|
70da3927 TO |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Event; | |
4 | ||
5 | trait ActiveUserTrait { | |
6 | ||
7 | /** | |
8 | * Contact ID of the active/target user (whose access we must check). | |
9 | * 0 for anonymous. | |
10 | * | |
11 | * @var int | |
12 | */ | |
13 | private $userID; | |
14 | ||
15 | /** | |
16 | * @param int|null $userID | |
17 | * Contact ID of the active/target user (whose access we must check). | |
18 | * 0 for anonymous. | |
19 | * @return $this | |
20 | */ | |
21 | protected function setUser(int $userID) { | |
22 | $loggedInContactID = \CRM_Core_Session::getLoggedInContactID() ?: 0; | |
23 | if ($userID !== $loggedInContactID) { | |
24 | throw new \RuntimeException("The API subsystem does not yet fully support variable user IDs."); | |
25 | // Traditionally, the API events did not emit information about the current user; it was assumed | |
26 | // that the user was the logged-in user. This may be expanded in the future to support some more edge-cases. | |
27 | // For now, the semantics are unchanged - but we've begun reporting the active userID so that | |
28 | // consumers can start adopting it. | |
29 | } | |
30 | $this->userID = $userID; | |
31 | return $this; | |
32 | } | |
33 | ||
34 | /** | |
35 | * @return int | |
36 | * Contact ID of the active/target user (whose access we must check). | |
37 | * 0 for anonymous. | |
38 | */ | |
39 | public function getUserID(): int { | |
40 | return $this->userID; | |
41 | } | |
42 | ||
43 | } |