2022-04-04 18:30:07 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Event\Validator;
|
|
|
|
|
2022-04-21 01:34:55 +12:00
|
|
|
use Utopia\Config\Config;
|
2022-04-04 18:30:07 +12:00
|
|
|
use Utopia\Validator;
|
|
|
|
|
|
|
|
class Event extends Validator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get Description.
|
|
|
|
*
|
|
|
|
* Returns validator description
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDescription(): string
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
return 'Event is not valid.';
|
2022-04-04 18:30:07 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is valid.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid($value): bool
|
|
|
|
{
|
2022-04-21 01:34:55 +12:00
|
|
|
$events = Config::getParam('events', []);
|
2022-04-04 18:30:07 +12:00
|
|
|
$parts = \explode('.', $value);
|
|
|
|
$count = \count($parts);
|
|
|
|
|
|
|
|
if ($count < 2 || $count > 6) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 04:21:45 +12:00
|
|
|
* Identify all sections of the pattern.
|
2022-04-04 18:30:07 +12:00
|
|
|
*/
|
|
|
|
$type = $parts[0] ?? false;
|
|
|
|
$resource = $parts[1] ?? false;
|
2022-04-21 01:34:55 +12:00
|
|
|
$hasSubResource = $count > 3 && ($events[$type]['$resource'] ?? false) && ($events[$type][$parts[2]]['$resource'] ?? false);
|
2022-04-04 18:30:07 +12:00
|
|
|
|
|
|
|
if (!$type || !$resource) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($hasSubResource) {
|
|
|
|
$subType = $parts[2];
|
|
|
|
$subResource = $parts[3];
|
|
|
|
if ($count === 6) {
|
|
|
|
$attribute = $parts[5];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($count === 4) {
|
|
|
|
$attribute = $parts[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$subType ??= false;
|
|
|
|
$subResource ??= false;
|
|
|
|
$attribute ??= false;
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2022-04-04 18:30:07 +12:00
|
|
|
$action = match (true) {
|
|
|
|
!$hasSubResource && $count > 2 => $parts[2],
|
|
|
|
$hasSubResource && $count > 4 => $parts[4],
|
|
|
|
default => false
|
|
|
|
};
|
|
|
|
|
2022-04-21 01:34:55 +12:00
|
|
|
if (!\array_key_exists($type, $events)) {
|
2022-04-04 18:30:07 +12:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($subtype ?? false) {
|
2022-04-21 01:34:55 +12:00
|
|
|
if ($action && !\array_key_exists($action, $events[$type][$subType])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!($subResource ?? false) || !\array_key_exists($subType, $events[$type])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($action && !\array_key_exists($action, $events[$type])) {
|
2022-04-04 18:30:07 +12:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($attribute ?? false) {
|
|
|
|
if (
|
2022-04-21 01:34:55 +12:00
|
|
|
!\array_key_exists($attribute, $events[$type][$action]) ||
|
|
|
|
(($subType ?? false) && !\array_key_exists($attribute, $events[$type][$subType][$action]))
|
2022-04-04 18:30:07 +12:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is array
|
|
|
|
*
|
|
|
|
* Function will return true if object is array.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isArray(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Type
|
|
|
|
*
|
|
|
|
* Returns validator type.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType(): string
|
|
|
|
{
|
|
|
|
return self::TYPE_STRING;
|
|
|
|
}
|
|
|
|
}
|