Keep entities under projects
This commit is contained in:
parent
16362e85e5
commit
a1023a683d
4 changed files with 0 additions and 702 deletions
|
@ -1,135 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
global $utopia, $response, $consoleDB, $project;
|
|
||||||
|
|
||||||
use Utopia\Exception;
|
|
||||||
use Utopia\Response;
|
|
||||||
use Utopia\Validator\ArrayList;
|
|
||||||
use Utopia\Validator\Text;
|
|
||||||
use Utopia\Validator\WhiteList;
|
|
||||||
use Database\Database;
|
|
||||||
use Database\Document;
|
|
||||||
use Database\Validator\UID;
|
|
||||||
|
|
||||||
include_once __DIR__ . '/../shared/api.php';
|
|
||||||
|
|
||||||
$scopes = include __DIR__.'/../../../app/config/scopes.php';
|
|
||||||
|
|
||||||
$utopia->get('/v1/keys')
|
|
||||||
->desc('List Keys')
|
|
||||||
->label('scope', 'keys.read')
|
|
||||||
->label('sdk.namespace', 'keys')
|
|
||||||
->label('sdk.method', 'listKeys')
|
|
||||||
->action(
|
|
||||||
function () use ($response, $consoleDB, $project) {
|
|
||||||
$response->json($project->getAttribute('keys', [])); //FIXME make sure array objects return correctly
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->get('/v1/keys/:keyId')
|
|
||||||
->desc('Get Key')
|
|
||||||
->label('scope', 'keys.read')
|
|
||||||
->label('sdk.namespace', 'keys')
|
|
||||||
->label('sdk.method', 'getKey')
|
|
||||||
->param('keyId', null, function () { return new UID(); }, 'Key unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($keyId) use ($response, $consoleDB, $project) {
|
|
||||||
$key = $project->search('$uid', $keyId, $project->getAttribute('keys', []));
|
|
||||||
|
|
||||||
if (empty($key) && $key instanceof Document) {
|
|
||||||
throw new Exception('Key not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($key->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->post('/v1/keys')
|
|
||||||
->desc('Create Key')
|
|
||||||
->label('scope', 'keys.write')
|
|
||||||
->label('sdk.namespace', 'keys')
|
|
||||||
->label('sdk.method', 'createKey')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Key name')
|
|
||||||
->param('scopes', null, function () use ($scopes) { return new ArrayList(new WhiteList($scopes)); }, 'Key scopes list')
|
|
||||||
->action(
|
|
||||||
function ($name, $scopes) use ($response, $consoleDB, $project) {
|
|
||||||
$key = $consoleDB->createDocument([
|
|
||||||
'$collection' => Database::SYSTEM_COLLECTION_KEYS,
|
|
||||||
'$permissions' => [
|
|
||||||
'read' => ['team:'.$project->getAttribute('teamId', null)],
|
|
||||||
'write' => ['team:'.$project->getAttribute('teamId', null).'/owner', 'team:'.$project->getAttribute('teamId', null).'/developer'],
|
|
||||||
],
|
|
||||||
'name' => $name,
|
|
||||||
'scopes' => $scopes,
|
|
||||||
'secret' => bin2hex(random_bytes(128)),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (false === $key) {
|
|
||||||
throw new Exception('Failed saving key to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$project->setAttribute('keys', $key, Document::SET_TYPE_APPEND);
|
|
||||||
|
|
||||||
$project = $consoleDB->updateDocument($project->getArrayCopy());
|
|
||||||
|
|
||||||
if (false === $project) {
|
|
||||||
throw new Exception('Failed saving project to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response
|
|
||||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
||||||
->json($key->getArrayCopy())
|
|
||||||
;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->put('/v1/keys/:keyId')
|
|
||||||
->desc('Update Key')
|
|
||||||
->label('scope', 'keys.write')
|
|
||||||
->label('sdk.namespace', 'keys')
|
|
||||||
->label('sdk.method', 'updateKey')
|
|
||||||
->param('keyId', null, function () { return new UID(); }, 'Key unique ID.')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Key name')
|
|
||||||
->param('scopes', null, function () use ($scopes) { return new ArrayList(new WhiteList($scopes)); }, 'Key scopes list')
|
|
||||||
->action(
|
|
||||||
function ($keyId, $name, $scopes) use ($response, $consoleDB, $project) {
|
|
||||||
$key = $project->search('$uid', $keyId, $project->getAttribute('keys', []));
|
|
||||||
|
|
||||||
if (empty($key) && $key instanceof Document) {
|
|
||||||
throw new Exception('Key not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$key
|
|
||||||
->setAttribute('name', $name)
|
|
||||||
->setAttribute('scopes', $scopes)
|
|
||||||
;
|
|
||||||
|
|
||||||
if (false === $consoleDB->updateDocument($key->getArrayCopy())) {
|
|
||||||
throw new Exception('Failed saving key to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($key->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->delete('/v1/keys/:keyId')
|
|
||||||
->desc('Delete Key')
|
|
||||||
->label('scope', 'keys.write')
|
|
||||||
->label('sdk.namespace', 'keys')
|
|
||||||
->label('sdk.method', 'deleteKey')
|
|
||||||
->param('keyId', null, function () { return new UID(); }, 'Key unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($keyId) use ($response, $consoleDB, $project) {
|
|
||||||
$key = $project->search('$uid', $keyId, $project->getAttribute('keys', []));
|
|
||||||
|
|
||||||
if (empty($key) && $key instanceof Document) {
|
|
||||||
throw new Exception('Key not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$consoleDB->deleteDocument($key->getUid())) {
|
|
||||||
throw new Exception('Failed to remove key from DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->noContent();
|
|
||||||
}
|
|
||||||
);
|
|
|
@ -1,145 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
global $utopia, $request, $response, $consoleDB, $project;
|
|
||||||
|
|
||||||
use Utopia\Exception;
|
|
||||||
use Utopia\Response;
|
|
||||||
use Utopia\Validator\Text;
|
|
||||||
use Utopia\Validator\WhiteList;
|
|
||||||
use Utopia\Validator\URL;
|
|
||||||
use Database\Database;
|
|
||||||
use Database\Document;
|
|
||||||
use Database\Validator\UID;
|
|
||||||
|
|
||||||
include_once __DIR__ . '/../shared/api.php';
|
|
||||||
|
|
||||||
$utopia->get('/v1/platforms')
|
|
||||||
->desc('List Platforms')
|
|
||||||
->label('scope', 'platforms.read')
|
|
||||||
->label('sdk.namespace', 'platforms')
|
|
||||||
->label('sdk.method', 'listPlatforms')
|
|
||||||
->action(
|
|
||||||
function () use ($request, $response, $consoleDB, $project) {
|
|
||||||
$response->json($project->getAttribute('platforms', []));
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->get('/v1/platforms/:platformId')
|
|
||||||
->desc('Get Platform')
|
|
||||||
->label('scope', 'platforms.read')
|
|
||||||
->label('sdk.namespace', 'platforms')
|
|
||||||
->label('sdk.method', 'getPlatform')
|
|
||||||
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($platformId) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$platform = $project->search('$uid', $platformId, $project->getAttribute('platforms', []));
|
|
||||||
|
|
||||||
if (empty($platform) && $platform instanceof Document) {
|
|
||||||
throw new Exception('Platform not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($platform->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->post('/v1/platforms')
|
|
||||||
->desc('Create Platform')
|
|
||||||
->label('scope', 'platforms.write')
|
|
||||||
->label('sdk.namespace', 'platforms')
|
|
||||||
->label('sdk.method', 'createPlatform')
|
|
||||||
->param('type', null, function () { return new WhiteList(['web', 'ios', 'android', 'unity']); }, 'Platform name')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Platform name')
|
|
||||||
->param('key', '', function () { return new Text(256); }, 'Package name for android or bundle ID for iOS', true)
|
|
||||||
->param('store', '', function () { return new Text(256); }, 'App store or Google Play store ID', true)
|
|
||||||
->param('url', '', function () { return new URL(); }, 'Platform client URL', true)
|
|
||||||
->action(
|
|
||||||
function ($type, $name, $key, $store, $url) use ($response, $consoleDB, $project) {
|
|
||||||
$platform = $consoleDB->createDocument([
|
|
||||||
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
|
|
||||||
'$permissions' => [
|
|
||||||
'read' => ['team:'.$project->getAttribute('teamId', null)],
|
|
||||||
'write' => ['team:'.$project->getAttribute('teamId', null).'/owner', 'team:'.$project->getAttribute('teamId', null).'/developer'],
|
|
||||||
],
|
|
||||||
'type' => $type,
|
|
||||||
'name' => $name,
|
|
||||||
'key' => $key,
|
|
||||||
'store' => $store,
|
|
||||||
'url' => $url,
|
|
||||||
'dateCreated' => time(),
|
|
||||||
'dateUpdated' => time(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (false === $platform) {
|
|
||||||
throw new Exception('Failed saving platform to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$project->setAttribute('platforms', $platform, Document::SET_TYPE_APPEND);
|
|
||||||
|
|
||||||
$project = $consoleDB->updateDocument($project->getArrayCopy());
|
|
||||||
|
|
||||||
if (false === $project) {
|
|
||||||
throw new Exception('Failed saving project to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response
|
|
||||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
||||||
->json($platform->getArrayCopy())
|
|
||||||
;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->put('/v1/platforms/:platformId')
|
|
||||||
->desc('Update Platform')
|
|
||||||
->label('scope', 'platforms.write')
|
|
||||||
->label('sdk.namespace', 'platforms')
|
|
||||||
->label('sdk.method', 'updatePlatform')
|
|
||||||
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Platform name')
|
|
||||||
->param('key', '', function () { return new Text(256); }, 'Package name for android or bundle ID for iOS', true)
|
|
||||||
->param('store', '', function () { return new Text(256); }, 'App store or Google Play store ID', true)
|
|
||||||
->param('url', '', function () { return new URL(); }, 'Platform client URL', true)
|
|
||||||
->action(
|
|
||||||
function ($platformId, $name, $key, $store, $url) use ($response, $consoleDB, $project) {
|
|
||||||
$platform = $project->search('$uid', $platformId, $project->getAttribute('platforms', []));
|
|
||||||
|
|
||||||
if (empty($platform) && $platform instanceof Document) {
|
|
||||||
throw new Exception('Platform not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$platform
|
|
||||||
->setAttribute('name', $name)
|
|
||||||
->setAttribute('dateUpdated', time())
|
|
||||||
->setAttribute('key', $key)
|
|
||||||
->setAttribute('store', $store)
|
|
||||||
->setAttribute('url', $url)
|
|
||||||
;
|
|
||||||
|
|
||||||
if (false === $consoleDB->updateDocument($platform->getArrayCopy())) {
|
|
||||||
throw new Exception('Failed saving platform to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($platform->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->delete('/v1/platforms/:platformId')
|
|
||||||
->desc('Delete Platform')
|
|
||||||
->label('scope', 'platforms.write')
|
|
||||||
->label('sdk.namespace', 'platforms')
|
|
||||||
->label('sdk.method', 'deletePlatform')
|
|
||||||
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($platformId) use ($response, $consoleDB, $project) {
|
|
||||||
$platform = $project->search('$uid', $platformId, $project->getAttribute('platforms', []));
|
|
||||||
|
|
||||||
if (empty($platform) && $platform instanceof Document) {
|
|
||||||
throw new Exception('Platform not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$consoleDB->deleteDocument($platform->getUid())) {
|
|
||||||
throw new Exception('Failed to remove platform from DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->noContent();
|
|
||||||
}
|
|
||||||
);
|
|
|
@ -1,229 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
global $utopia, $request, $response, $consoleDB, $project;
|
|
||||||
|
|
||||||
use Utopia\Exception;
|
|
||||||
use Utopia\Response;
|
|
||||||
use Utopia\Validator\ArrayList;
|
|
||||||
use Utopia\Validator\Text;
|
|
||||||
use Utopia\Validator\WhiteList;
|
|
||||||
use Utopia\Validator\Range;
|
|
||||||
use Utopia\Validator\URL;
|
|
||||||
use Task\Validator\Cron;
|
|
||||||
use Database\Database;
|
|
||||||
use Database\Document;
|
|
||||||
use Database\Validator\UID;
|
|
||||||
use OpenSSL\OpenSSL;
|
|
||||||
use Cron\CronExpression;
|
|
||||||
|
|
||||||
include_once __DIR__ . '/../shared/api.php';
|
|
||||||
|
|
||||||
$utopia->get('/v1/tasks')
|
|
||||||
->desc('List Tasks')
|
|
||||||
->label('scope', 'tasks.read')
|
|
||||||
->label('sdk.namespace', 'tasks')
|
|
||||||
->label('sdk.method', 'listTasks')
|
|
||||||
->action(
|
|
||||||
function () use ($request, $response, $consoleDB, $project) {
|
|
||||||
$tasks = $project->getAttribute('tasks', []);
|
|
||||||
|
|
||||||
foreach ($tasks as $task) { /* @var $task Document */
|
|
||||||
$httpPass = json_decode($task->getAttribute('httpPass', '{}'), true);
|
|
||||||
|
|
||||||
if (empty($httpPass) || !isset($httpPass['version'])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V'.$httpPass['version']);
|
|
||||||
|
|
||||||
$task->setAttribute('httpPass', OpenSSL::decrypt($httpPass['data'], $httpPass['method'], $key, 0, hex2bin($httpPass['iv']), hex2bin($httpPass['tag'])));
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($tasks);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->get('/v1/tasks/:taskId')
|
|
||||||
->desc('Get Task')
|
|
||||||
->label('scope', 'tasks.read')
|
|
||||||
->label('sdk.namespace', 'tasks')
|
|
||||||
->label('sdk.method', 'getTask')
|
|
||||||
->param('taskId', null, function () { return new UID(); }, 'Task unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($taskId) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$task = $project->search('$uid', $taskId, $project->getAttribute('tasks', []));
|
|
||||||
|
|
||||||
if (empty($task) && $task instanceof Document) {
|
|
||||||
throw new Exception('Task not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$httpPass = json_decode($task->getAttribute('httpPass', '{}'), true);
|
|
||||||
|
|
||||||
if (!empty($httpPass) && isset($httpPass['version'])) {
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V'.$httpPass['version']);
|
|
||||||
$task->setAttribute('httpPass', OpenSSL::decrypt($httpPass['data'], $httpPass['method'], $key, 0, hex2bin($httpPass['iv']), hex2bin($httpPass['tag'])));
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($task->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->post('/v1/tasks')
|
|
||||||
->desc('Create Task')
|
|
||||||
->label('scope', 'tasks.write')
|
|
||||||
->label('sdk.namespace', 'tasks')
|
|
||||||
->label('sdk.method', 'createTask')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Task name')
|
|
||||||
->param('status', null, function () { return new WhiteList(['play', 'pause']); }, 'Task status')
|
|
||||||
->param('schedule', null, function () { return new Cron(); }, 'Task schedule syntax')
|
|
||||||
->param('security', null, function () { return new Range(0, 1); }, 'Certificate verification, 0 for disabled or 1 for enabled')
|
|
||||||
->param('httpMethod', '', function () { return new WhiteList(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']); }, 'Task HTTP method')
|
|
||||||
->param('httpUrl', '', function () { return new URL(); }, 'Task HTTP URL')
|
|
||||||
->param('httpHeaders', null, function () { return new ArrayList(new Text(256)); }, 'Task HTTP headers list', true)
|
|
||||||
->param('httpUser', '', function () { return new Text(256); }, 'Task HTTP user', true)
|
|
||||||
->param('httpPass', '', function () { return new Text(256); }, 'Task HTTP password', true)
|
|
||||||
->action(
|
|
||||||
function ($name, $status, $schedule, $security, $httpMethod, $httpUrl, $httpHeaders, $httpUser, $httpPass) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$cron = CronExpression::factory($schedule);
|
|
||||||
$next = ($status == 'play') ? $cron->getNextRunDate()->format('U') : null;
|
|
||||||
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V1');
|
|
||||||
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
|
||||||
$tag = null;
|
|
||||||
$httpPass = json_encode([
|
|
||||||
'data' => OpenSSL::encrypt($httpPass, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
|
||||||
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
|
||||||
'iv' => bin2hex($iv),
|
|
||||||
'tag' => bin2hex($tag),
|
|
||||||
'version' => '1',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$task = $consoleDB->createDocument([
|
|
||||||
'$collection' => Database::SYSTEM_COLLECTION_TASKS,
|
|
||||||
'$permissions' => [
|
|
||||||
'read' => ['team:'.$project->getAttribute('teamId', null)],
|
|
||||||
'write' => ['team:'.$project->getAttribute('teamId', null).'/owner', 'team:'.$project->getAttribute('teamId', null).'/developer'],
|
|
||||||
],
|
|
||||||
'name' => $name,
|
|
||||||
'status' => $status,
|
|
||||||
'schedule' => $schedule,
|
|
||||||
'updated' => time(),
|
|
||||||
'previous' => null,
|
|
||||||
'next' => $next,
|
|
||||||
'security' => (int) $security,
|
|
||||||
'httpMethod' => $httpMethod,
|
|
||||||
'httpUrl' => $httpUrl,
|
|
||||||
'httpHeaders' => $httpHeaders,
|
|
||||||
'httpUser' => $httpUser,
|
|
||||||
'httpPass' => $httpPass,
|
|
||||||
'log' => '{}',
|
|
||||||
'failures' => 0,
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (false === $task) {
|
|
||||||
throw new Exception('Failed saving tasks to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$project->setAttribute('tasks', $task, Document::SET_TYPE_APPEND);
|
|
||||||
|
|
||||||
$project = $consoleDB->updateDocument($project->getArrayCopy());
|
|
||||||
|
|
||||||
if (false === $project) {
|
|
||||||
throw new Exception('Failed saving project to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($next) {
|
|
||||||
ResqueScheduler::enqueueAt($next, 'v1-tasks', 'TasksV1', $task->getArrayCopy());
|
|
||||||
}
|
|
||||||
|
|
||||||
$response
|
|
||||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
||||||
->json($task->getArrayCopy())
|
|
||||||
;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->put('/v1/tasks/:taskId')
|
|
||||||
->desc('Update Task')
|
|
||||||
->label('scope', 'tasks.write')
|
|
||||||
->label('sdk.namespace', 'tasks')
|
|
||||||
->label('sdk.method', 'updateTask')
|
|
||||||
->param('taskId', null, function () { return new UID(); }, 'Task unique ID.')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Task name')
|
|
||||||
->param('status', null, function () { return new WhiteList(['play', 'pause']); }, 'Task status')
|
|
||||||
->param('schedule', null, function () { return new Cron(); }, 'Task schedule syntax')
|
|
||||||
->param('security', null, function () { return new Range(0, 1); }, 'Certificate verification, 0 for disabled or 1 for enabled')
|
|
||||||
->param('httpMethod', '', function () { return new WhiteList(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']); }, 'Task HTTP method')
|
|
||||||
->param('httpUrl', '', function () { return new URL(); }, 'Task HTTP URL')
|
|
||||||
->param('httpHeaders', null, function () { return new ArrayList(new Text(256)); }, 'Task HTTP headers list', true)
|
|
||||||
->param('httpUser', '', function () { return new Text(256); }, 'Task HTTP user', true)
|
|
||||||
->param('httpPass', '', function () { return new Text(256); }, 'Task HTTP password', true)
|
|
||||||
->action(
|
|
||||||
function ($taskId, $name, $status, $schedule, $security, $httpMethod, $httpUrl, $httpHeaders, $httpUser, $httpPass) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$task = $project->search('$uid', $taskId, $project->getAttribute('tasks', []));
|
|
||||||
|
|
||||||
if (empty($task) && $task instanceof Document) {
|
|
||||||
throw new Exception('Task not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$cron = CronExpression::factory($schedule);
|
|
||||||
$next = ($status == 'play') ? $cron->getNextRunDate()->format('U') : null;
|
|
||||||
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V1');
|
|
||||||
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
|
||||||
$tag = null;
|
|
||||||
$httpPass = json_encode([
|
|
||||||
'data' => OpenSSL::encrypt($httpPass, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
|
||||||
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
|
||||||
'iv' => bin2hex($iv),
|
|
||||||
'tag' => bin2hex($tag),
|
|
||||||
'version' => '1',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$task
|
|
||||||
->setAttribute('name', $name)
|
|
||||||
->setAttribute('status', $status)
|
|
||||||
->setAttribute('schedule', $schedule)
|
|
||||||
->setAttribute('updated', time())
|
|
||||||
->setAttribute('next', $next)
|
|
||||||
->setAttribute('security', (int) $security)
|
|
||||||
->setAttribute('httpMethod', $httpMethod)
|
|
||||||
->setAttribute('httpUrl', $httpUrl)
|
|
||||||
->setAttribute('httpHeaders', $httpHeaders)
|
|
||||||
->setAttribute('httpUser', $httpUser)
|
|
||||||
->setAttribute('httpPass', $httpPass)
|
|
||||||
;
|
|
||||||
|
|
||||||
if (false === $consoleDB->updateDocument($task->getArrayCopy())) {
|
|
||||||
throw new Exception('Failed saving tasks to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($next) {
|
|
||||||
ResqueScheduler::enqueueAt($next, 'v1-tasks', 'TasksV1', $task->getArrayCopy());
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($task->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->delete('/v1/tasks/:taskId')
|
|
||||||
->desc('Delete Task')
|
|
||||||
->label('scope', 'tasks.write')
|
|
||||||
->label('sdk.namespace', 'tasks')
|
|
||||||
->label('sdk.method', 'deleteTask')
|
|
||||||
->param('taskId', null, function () { return new UID(); }, 'Task unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($taskId) use ($response, $consoleDB, $project) {
|
|
||||||
$task = $project->search('$uid', $taskId, $project->getAttribute('tasks', []));
|
|
||||||
|
|
||||||
if (empty($task) && $task instanceof Document) {
|
|
||||||
throw new Exception('Task not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$consoleDB->deleteDocument($task->getUid())) {
|
|
||||||
throw new Exception('Failed to remove tasks from DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->noContent();
|
|
||||||
}
|
|
||||||
);
|
|
|
@ -1,193 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
global $utopia, $request, $response, $consoleDB, $project;
|
|
||||||
|
|
||||||
use Utopia\Exception;
|
|
||||||
use Utopia\Response;
|
|
||||||
use Utopia\Validator\ArrayList;
|
|
||||||
use Utopia\Validator\Text;
|
|
||||||
use Utopia\Validator\Range;
|
|
||||||
use Utopia\Validator\URL;
|
|
||||||
use Database\Database;
|
|
||||||
use Database\Document;
|
|
||||||
use Database\Validator\UID;
|
|
||||||
use OpenSSL\OpenSSL;
|
|
||||||
|
|
||||||
include_once __DIR__ . '/../shared/api.php';
|
|
||||||
|
|
||||||
$utopia->get('/v1/webhooks')
|
|
||||||
->desc('List Webhooks')
|
|
||||||
->label('scope', 'webhooks.read')
|
|
||||||
->label('sdk.namespace', 'webhooks')
|
|
||||||
->label('sdk.method', 'listWebhooks')
|
|
||||||
->action(
|
|
||||||
function () use ($request, $response, $consoleDB, $project) {
|
|
||||||
$webhooks = $project->getAttribute('webhooks', []);
|
|
||||||
|
|
||||||
foreach ($webhooks as $webhook) { /* @var $webhook Document */
|
|
||||||
$httpPass = json_decode($webhook->getAttribute('httpPass', '{}'), true);
|
|
||||||
|
|
||||||
if (empty($httpPass) || !isset($httpPass['version'])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V'.$httpPass['version']);
|
|
||||||
|
|
||||||
$webhook->setAttribute('httpPass', OpenSSL::decrypt($httpPass['data'], $httpPass['method'], $key, 0, hex2bin($httpPass['iv']), hex2bin($httpPass['tag'])));
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($webhooks);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->get('/v1/webhooks/:webhookId')
|
|
||||||
->desc('Get Webhook')
|
|
||||||
->label('scope', 'webhooks.read')
|
|
||||||
->label('sdk.namespace', 'webhooks')
|
|
||||||
->label('sdk.method', 'getWebhook')
|
|
||||||
->param('webhookId', null, function () { return new UID(); }, 'Webhook unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($webhookId) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$webhook = $project->search('$uid', $webhookId, $project->getAttribute('webhooks', []));
|
|
||||||
|
|
||||||
if (empty($webhook) && $webhook instanceof Document) {
|
|
||||||
throw new Exception('Webhook not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$httpPass = json_decode($webhook->getAttribute('httpPass', '{}'), true);
|
|
||||||
|
|
||||||
if (!empty($httpPass) && isset($httpPass['version'])) {
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V'.$httpPass['version']);
|
|
||||||
$webhook->setAttribute('httpPass', OpenSSL::decrypt($httpPass['data'], $httpPass['method'], $key, 0, hex2bin($httpPass['iv']), hex2bin($httpPass['tag'])));
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($webhook->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->post('/v1/webhooks')
|
|
||||||
->desc('Create Webhook')
|
|
||||||
->label('scope', 'webhooks.write')
|
|
||||||
->label('sdk.namespace', 'webhooks')
|
|
||||||
->label('sdk.method', 'createWebhook')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Webhook name')
|
|
||||||
->param('events', null, function () { return new ArrayList(new Text(256)); }, 'Webhook events list')
|
|
||||||
->param('url', null, function () { return new Text(2000); }, 'Webhook URL')
|
|
||||||
->param('security', null, function () { return new Range(0, 1); }, 'Certificate verification, 0 for disabled or 1 for enabled')
|
|
||||||
->param('httpUser', '', function () { return new Text(256); }, 'Webhook HTTP user', true)
|
|
||||||
->param('httpPass', '', function () { return new Text(256); }, 'Webhook HTTP password', true)
|
|
||||||
->action(
|
|
||||||
function ($name, $events, $url, $security, $httpUser, $httpPass) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V1');
|
|
||||||
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
|
||||||
$tag = null;
|
|
||||||
$httpPass = json_encode([
|
|
||||||
'data' => OpenSSL::encrypt($httpPass, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
|
||||||
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
|
||||||
'iv' => bin2hex($iv),
|
|
||||||
'tag' => bin2hex($tag),
|
|
||||||
'version' => '1',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$webhook = $consoleDB->createDocument([
|
|
||||||
'$collection' => Database::SYSTEM_COLLECTION_WEBHOOKS,
|
|
||||||
'$permissions' => [
|
|
||||||
'read' => ['team:'.$project->getAttribute('teamId', null)],
|
|
||||||
'write' => ['team:'.$project->getAttribute('teamId', null).'/owner', 'team:'.$project->getAttribute('teamId', null).'/developer'],
|
|
||||||
],
|
|
||||||
'name' => $name,
|
|
||||||
'events' => $events,
|
|
||||||
'url' => $url,
|
|
||||||
'security' => (int) $security,
|
|
||||||
'httpUser' => $httpUser,
|
|
||||||
'httpPass' => $httpPass,
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (false === $webhook) {
|
|
||||||
throw new Exception('Failed saving webhook to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$project->setAttribute('webhooks', $webhook, Document::SET_TYPE_APPEND);
|
|
||||||
|
|
||||||
$project = $consoleDB->updateDocument($project->getArrayCopy());
|
|
||||||
|
|
||||||
if (false === $project) {
|
|
||||||
throw new Exception('Failed saving project to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response
|
|
||||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
||||||
->json($webhook->getArrayCopy())
|
|
||||||
;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->put('/v1/webhooks/:webhookId')
|
|
||||||
->desc('Update Webhook')
|
|
||||||
->label('scope', 'webhooks.write')
|
|
||||||
->label('sdk.namespace', 'webhooks')
|
|
||||||
->label('sdk.method', 'updateWebhook')
|
|
||||||
->param('webhookId', null, function () { return new UID(); }, 'Webhook unique ID.')
|
|
||||||
->param('name', null, function () { return new Text(256); }, 'Webhook name')
|
|
||||||
->param('events', null, function () { return new ArrayList(new Text(256)); }, 'Webhook events list')
|
|
||||||
->param('url', null, function () { return new Text(2000); }, 'Webhook URL')
|
|
||||||
->param('security', null, function () { return new Range(0, 1); }, 'Certificate verification, 0 for disabled or 1 for enabled')
|
|
||||||
->param('httpUser', '', function () { return new Text(256); }, 'Webhook HTTP user', true)
|
|
||||||
->param('httpPass', '', function () { return new Text(256); }, 'Webhook HTTP password', true)
|
|
||||||
->action(
|
|
||||||
function ($webhookId, $name, $events, $url, $security, $httpUser, $httpPass) use ($request, $response, $consoleDB, $project) {
|
|
||||||
$key = $request->getServer('_APP_OPENSSL_KEY_V1');
|
|
||||||
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
|
||||||
$tag = null;
|
|
||||||
$httpPass = json_encode([
|
|
||||||
'data' => OpenSSL::encrypt($httpPass, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
|
||||||
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
|
||||||
'iv' => bin2hex($iv),
|
|
||||||
'tag' => bin2hex($tag),
|
|
||||||
'version' => '1',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$webhook = $project->search('$uid', $webhookId, $project->getAttribute('webhooks', []));
|
|
||||||
|
|
||||||
if (empty($webhook) && $webhook instanceof Document) {
|
|
||||||
throw new Exception('Webhook not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$webhook
|
|
||||||
->setAttribute('name', $name)
|
|
||||||
->setAttribute('events', $events)
|
|
||||||
->setAttribute('url', $url)
|
|
||||||
->setAttribute('security', (int) $security)
|
|
||||||
->setAttribute('httpUser', $httpUser)
|
|
||||||
->setAttribute('httpPass', $httpPass)
|
|
||||||
;
|
|
||||||
|
|
||||||
if (false === $consoleDB->updateDocument($webhook->getArrayCopy())) {
|
|
||||||
throw new Exception('Failed saving webhook to DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->json($webhook->getArrayCopy());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$utopia->delete('/v1/webhooks/:webhookId')
|
|
||||||
->desc('Delete Webhook')
|
|
||||||
->label('scope', 'webhooks.write')
|
|
||||||
->label('sdk.namespace', 'webhooks')
|
|
||||||
->label('sdk.method', 'deleteWebhook')
|
|
||||||
->param('webhookId', null, function () { return new UID(); }, 'Webhook unique ID.')
|
|
||||||
->action(
|
|
||||||
function ($webhookId) use ($response, $consoleDB, $project) {
|
|
||||||
$webhook = $project->search('$uid', $webhookId, $project->getAttribute('webhooks', []));
|
|
||||||
|
|
||||||
if (empty($webhook) && $webhook instanceof Document) {
|
|
||||||
throw new Exception('Webhook not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$consoleDB->deleteDocument($webhook->getUid())) {
|
|
||||||
throw new Exception('Failed to remove webhook from DB', 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->noContent();
|
|
||||||
}
|
|
||||||
);
|
|
Loading…
Reference in a new issue