appwrite/src/Appwrite/Event/Audit.php

168 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Event;
use Utopia\Queue\Client;
use Utopia\Queue\Connection;
class Audit extends Event
{
protected string $resource = '';
protected string $mode = '';
protected string $userAgent = '';
protected string $ip = '';
2025-01-03 10:00:55 +00:00
protected string $hostname = '';
public function __construct(protected Connection $connection)
{
2023-06-02 03:54:34 +00:00
parent::__construct($connection);
$this
->setQueue(Event::AUDITS_QUEUE_NAME)
->setClass(Event::AUDITS_CLASS_NAME);
}
2022-04-18 16:21:45 +00:00
/**
* Set resource for this audit event.
*
* @param string $resource
* @return self
*/
public function setResource(string $resource): self
{
$this->resource = $resource;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns the set audit resource.
*
* @return string
*/
public function getResource(): string
{
return $this->resource;
}
2022-04-18 16:21:45 +00:00
/**
* Set mode for this audit event
*
* @param string $mode
* @return self
*/
public function setMode(string $mode): self
{
$this->mode = $mode;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns the set audit mode.
*
* @return string
*/
public function getMode(): string
{
return $this->mode;
}
2022-04-18 16:21:45 +00:00
/**
* Set user agent for this audit event.
*
* @param string $userAgent
* @return self
*/
public function setUserAgent(string $userAgent): self
{
$this->userAgent = $userAgent;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns the set audit user agent.
*
* @return string
*/
public function getUserAgent(): string
{
return $this->userAgent;
}
2022-04-18 16:21:45 +00:00
/**
* Set IP for this audit event.
*
2022-05-10 12:28:06 +00:00
* @param string $ip
2022-04-18 16:21:45 +00:00
* @return self
*/
public function setIP(string $ip): self
{
$this->ip = $ip;
return $this;
}
2022-04-18 16:21:45 +00:00
/**
* Returns the set audit IP.
*
* @return string
*/
public function getIP(): string
{
return $this->ip;
}
2025-01-03 10:00:55 +00:00
/**
* Set the hostname.
*
* @param string $hostname
*
* @return self
*/
public function setHostname(string $hostname): self
{
$this->hostname = $hostname;
return $this;
}
/**
* Get the hostname.
*
* @return string
*/
public function getHostname(): string
{
return $this->hostname;
}
2022-04-18 16:21:45 +00:00
/**
* Executes the event and sends it to the audit worker.
*
* @return string|bool
* @throws \InvalidArgumentException
*/
public function trigger(): string|bool
{
2024-11-08 20:55:53 +00:00
if ($this->paused) {
return false;
}
$client = new Client($this->queue, $this->connection);
return $client->enqueue([
'project' => $this->project,
'user' => $this->user,
'payload' => $this->payload,
'resource' => $this->resource,
'mode' => $this->mode,
'ip' => $this->ip,
'userAgent' => $this->userAgent,
2022-09-04 08:45:53 +00:00
'event' => $this->event,
2025-01-03 10:00:55 +00:00
'hostname' => $this->hostname
]);
}
2022-05-23 14:54:50 +00:00
}