mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
95 lines
3.5 KiB
PHP
95 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Appwrite\Platform\Modules\Sites\Http\Variables;
|
|
|
|
use Appwrite\Extend\Exception;
|
|
use Appwrite\Platform\Modules\Compute\Base;
|
|
use Appwrite\Utopia\Response;
|
|
use Utopia\Database\Database;
|
|
use Utopia\Database\Document;
|
|
use Utopia\Database\Exception\Duplicate as DuplicateException;
|
|
use Utopia\Database\Helpers\ID;
|
|
use Utopia\Database\Helpers\Permission;
|
|
use Utopia\Database\Helpers\Role;
|
|
use Utopia\Database\Validator\UID;
|
|
use Utopia\Platform\Action;
|
|
use Utopia\Platform\Scope\HTTP;
|
|
use Utopia\Validator\Boolean;
|
|
use Utopia\Validator\Text;
|
|
|
|
class CreateVariable extends Base
|
|
{
|
|
use HTTP;
|
|
|
|
public static function getName()
|
|
{
|
|
return 'createVariable';
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
$this
|
|
->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
|
|
->setHttpPath('/v1/sites/:siteId/variables')
|
|
->desc('Create variable')
|
|
->groups(['api', 'sites'])
|
|
->label('scope', 'sites.write')
|
|
->label('audits.event', 'variable.create')
|
|
->label('audits.resource', 'site/{request.siteId}')
|
|
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
|
|
->label('sdk.namespace', 'sites')
|
|
->label('sdk.method', 'createVariable')
|
|
->label('sdk.description', '/docs/references/sites/create-variable.md')
|
|
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
->label('sdk.response.model', Response::MODEL_VARIABLE)
|
|
->param('siteId', '', new UID(), 'Site unique ID.', false)
|
|
->param('key', null, new Text(Database::LENGTH_KEY), 'Variable key. Max length: ' . Database::LENGTH_KEY . ' chars.', false)
|
|
->param('value', null, new Text(8192, 0), 'Variable value. Max length: 8192 chars.', false)
|
|
->param('secret', false, new Boolean(), 'Is secret? Secret variables can only be updated or deleted, they cannot be read.', true)
|
|
->inject('response')
|
|
->inject('dbForProject')
|
|
->inject('dbForConsole')
|
|
->callback([$this, 'action']);
|
|
}
|
|
|
|
public function action(string $siteId, string $key, string $value, bool $secret, Response $response, Database $dbForProject, Database $dbForConsole)
|
|
{
|
|
$site = $dbForProject->getDocument('sites', $siteId);
|
|
|
|
if ($site->isEmpty()) {
|
|
throw new Exception(Exception::SITE_NOT_FOUND);
|
|
}
|
|
|
|
$variableId = ID::unique();
|
|
|
|
$variable = new Document([
|
|
'$id' => $variableId,
|
|
'$permissions' => [
|
|
Permission::read(Role::any()),
|
|
Permission::update(Role::any()),
|
|
Permission::delete(Role::any()),
|
|
],
|
|
'resourceInternalId' => $site->getInternalId(),
|
|
'resourceId' => $site->getId(),
|
|
'resourceType' => 'site',
|
|
'key' => $key,
|
|
'value' => $value,
|
|
'secret' => $secret,
|
|
'search' => implode(' ', [$variableId, $site->getId(), $key, 'site']),
|
|
]);
|
|
|
|
try {
|
|
$variable = $dbForProject->createDocument('variables', $variable);
|
|
} catch (DuplicateException $th) {
|
|
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
|
|
}
|
|
|
|
$dbForProject->updateDocument('sites', $site->getId(), $site->setAttribute('live', false));
|
|
|
|
$response
|
|
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
->dynamic($variable, Response::MODEL_VARIABLE);
|
|
}
|
|
}
|