2019-05-09 18:54:39 +12:00
< ? php
global $utopia , $request , $response , $register , $user , $consoleDB , $projectDB , $providers ;
use Utopia\Exception ;
2019-06-09 20:51:10 +12:00
use Utopia\Response ;
2019-05-09 18:54:39 +12:00
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 ;
2019-11-30 07:26:06 +13:00
include_once 'shared/api.php' ;
2019-05-09 18:54:39 +12:00
$scopes = [ // TODO sync with console UI list
'users.read' ,
'users.write' ,
'teams.read' ,
'teams.write' ,
2019-06-09 01:13:19 +12:00
'collections.read' ,
'collections.write' ,
'documents.read' ,
'documents.write' ,
'files.read' ,
'files.write' ,
2019-05-09 18:54:39 +12:00
];
$utopia -> get ( '/v1/projects' )
-> desc ( 'List Projects' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listProjects' )
-> action (
2019-09-07 05:10:41 +12:00
function () use ( $request , $response , $providers , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$results = $consoleDB -> getCollection ([
'limit' => 20 ,
'offset' => 0 ,
'orderField' => 'name' ,
'orderType' => 'ASC' ,
'orderCast' => 'string' ,
'filters' => [
2019-09-07 05:10:41 +12:00
'$collection=' . Database :: SYSTEM_COLLECTION_PROJECTS ,
2019-05-09 18:54:39 +12:00
],
]);
foreach ( $results as $project ) {
foreach ( $providers as $provider => $node ) {
2019-09-07 05:10:41 +12:00
$secret = json_decode ( $project -> getAttribute ( 'usersOauth' . ucfirst ( $provider ) . 'Secret' , '{}' ), true );
2019-05-09 18:54:39 +12:00
2019-09-07 05:10:41 +12:00
if ( ! empty ( $secret ) && isset ( $secret [ 'version' ])) {
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V' . $secret [ 'version' ]);
$project -> setAttribute ( 'usersOauth' . ucfirst ( $provider ) . 'Secret' , OpenSSL :: decrypt ( $secret [ 'data' ], $secret [ 'method' ], $key , 0 , hex2bin ( $secret [ 'iv' ]), hex2bin ( $secret [ 'tag' ])));
2019-05-09 18:54:39 +12:00
}
}
}
$response -> json ( $results );
}
);
$utopia -> get ( '/v1/projects/:projectId' )
-> desc ( 'Get Project' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getProject' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $request , $response , $providers , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
foreach ( $providers as $provider => $node ) {
2019-09-07 05:10:41 +12:00
$secret = json_decode ( $project -> getAttribute ( 'usersOauth' . ucfirst ( $provider ) . 'Secret' , '{}' ), true );
2019-05-09 18:54:39 +12:00
2019-09-07 05:10:41 +12:00
if ( ! empty ( $secret ) && isset ( $secret [ 'version' ])) {
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V' . $secret [ 'version' ]);
$project -> setAttribute ( 'usersOauth' . ucfirst ( $provider ) . 'Secret' , OpenSSL :: decrypt ( $secret [ 'data' ], $secret [ 'method' ], $key , 0 , hex2bin ( $secret [ 'iv' ]), hex2bin ( $secret [ 'tag' ])));
2019-05-09 18:54:39 +12:00
}
}
$response -> json ( $project -> getArrayCopy ());
}
);
$utopia -> get ( '/v1/projects/:projectId/usage' )
-> desc ( 'Get Project' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getProjectUsage' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $response , $consoleDB , $projectDB , $register ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
2019-07-30 05:37:25 +12:00
$client = $register -> get ( 'influxdb' );
2019-08-07 19:35:24 +12:00
$requests = [];
2019-09-07 05:10:41 +12:00
$network = [];
2019-08-07 19:35:24 +12:00
2019-09-07 05:10:41 +12:00
if ( $client ) {
2019-10-02 07:46:57 +13:00
$start = DateTime :: createFromFormat ( 'U' , strtotime ( 'last day of last month' ));
2019-09-07 05:10:41 +12:00
$start = $start -> format ( DateTime :: RFC3339 );
$end = DateTime :: createFromFormat ( 'U' , strtotime ( 'last day of this month' ));
$end = $end -> format ( DateTime :: RFC3339 );
$database = $client -> selectDB ( 'telegraf' );
2019-07-30 05:37:25 +12:00
// Requests
2019-09-07 05:10:41 +12:00
$result = $database -> query ( 'SELECT sum(value) AS "value" FROM "appwrite_usage_requests_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $project -> getUid () . '\' GROUP BY time(1d) FILL(null)' );
$points = $result -> getPoints ();
2019-07-30 05:37:25 +12:00
foreach ( $points as $point ) {
$requests [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
2019-09-07 05:10:41 +12:00
'date' => strtotime ( $point [ 'time' ]),
2019-07-30 05:37:25 +12:00
];
}
2019-05-09 18:54:39 +12:00
2019-07-30 05:37:25 +12:00
// Network
2019-09-07 05:10:41 +12:00
$result = $database -> query ( 'SELECT sum(value) AS "value" FROM "appwrite_usage_network_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $project -> getUid () . '\' GROUP BY time(1d) FILL(null)' );
$points = $result -> getPoints ();
2019-05-09 18:54:39 +12:00
2019-07-30 05:37:25 +12:00
foreach ( $points as $point ) {
$network [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
2019-09-07 05:10:41 +12:00
'date' => strtotime ( $point [ 'time' ]),
2019-07-30 05:37:25 +12:00
];
}
2019-05-09 18:54:39 +12:00
}
2019-07-30 05:37:25 +12:00
2019-05-09 18:54:39 +12:00
// Users
$projectDB -> getCollection ([
'limit' => 0 ,
'offset' => 0 ,
'filters' => [
2019-09-07 05:10:41 +12:00
'$collection=' . Database :: SYSTEM_COLLECTION_USERS ,
2019-05-09 18:54:39 +12:00
],
]);
$usersTotal = $projectDB -> getSum ();
// Documents
$collections = $projectDB -> getCollection ([
'limit' => 100 ,
'offset' => 0 ,
'filters' => [
2019-09-07 05:10:41 +12:00
'$collection=' . Database :: SYSTEM_COLLECTION_COLLECTIONS ,
2019-05-09 18:54:39 +12:00
],
]);
$collectionsTotal = $projectDB -> getSum ();
$documents = [];
foreach ( $collections as $collection ) {
$result = $projectDB -> getCollection ([
'limit' => 0 ,
'offset' => 0 ,
'filters' => [
2019-09-07 05:10:41 +12:00
'$collection=' . $collection [ '$uid' ],
2019-05-09 18:54:39 +12:00
],
]);
$documents [] = [ 'name' => $collection [ 'name' ], 'total' => $projectDB -> getSum ()];
}
// Tasks
$tasksTotal = count ( $project -> getAttribute ( 'tasks' , []));
$response -> json ([
'requests' => [
2019-09-07 05:10:41 +12:00
'data' => $requests ,
2019-09-30 19:13:40 +13:00
'total' => array_sum ( array_map ( function ( $item ) {
return $item [ 'value' ];
}, $requests )),
2019-05-09 18:54:39 +12:00
],
'network' => [
2019-09-07 05:10:41 +12:00
'data' => $network ,
2019-09-30 19:13:40 +13:00
'total' => array_sum ( array_map ( function ( $item ) {
return $item [ 'value' ];
}, $network )),
2019-07-04 07:50:04 +12:00
],
'collections' => [
2019-09-07 05:10:41 +12:00
'data' => $collections ,
2019-07-04 07:50:04 +12:00
'total' => $collectionsTotal ,
2019-05-09 18:54:39 +12:00
],
'documents' => [
2019-09-07 05:10:41 +12:00
'data' => $documents ,
2019-09-30 19:13:40 +13:00
'total' => array_sum ( array_map ( function ( $item ) {
return $item [ 'total' ];
}, $documents )),
2019-05-09 18:54:39 +12:00
],
'users' => [
2019-09-07 05:10:41 +12:00
'data' => [],
2019-05-09 18:54:39 +12:00
'total' => $usersTotal ,
],
'tasks' => [
2019-09-07 05:10:41 +12:00
'data' => [],
2019-05-09 18:54:39 +12:00
'total' => $tasksTotal ,
],
'storage' => [
2019-09-30 19:13:40 +13:00
'total' => $projectDB -> getCount (
[
2019-05-09 18:54:39 +12:00
'filters' => [
2019-09-07 05:10:41 +12:00
'$collection=' . Database :: SYSTEM_COLLECTION_FILES ,
2019-05-09 18:54:39 +12:00
],
]
2019-09-07 05:10:41 +12:00
),
],
2019-05-09 18:54:39 +12:00
]);
}
);
$utopia -> post ( '/v1/projects' )
-> desc ( 'Create Project' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createProject' )
2019-10-07 09:10:52 +13:00
-> param ( 'name' , null , function () { return new Text ( 100 ); }, 'Project name' )
-> param ( 'teamId' , '' , function () { return new UID (); }, 'Team unique ID.' )
-> param ( 'description' , '' , function () { return new Text ( 255 ); }, 'Project description' , true )
-> param ( 'logo' , '' , function () { return new Text ( 1024 ); }, 'Project logo' , true )
-> param ( 'url' , '' , function () { return new URL (); }, 'Project URL' , true )
-> param ( 'legalName' , '' , function () { return new Text ( 256 ); }, 'Project Legal Name' , true )
-> param ( 'legalCountry' , '' , function () { return new Text ( 256 ); }, 'Project Legal Country' , true )
-> param ( 'legalState' , '' , function () { return new Text ( 256 ); }, 'Project Legal State' , true )
-> param ( 'legalCity' , '' , function () { return new Text ( 256 ); }, 'Project Legal City' , true )
-> param ( 'legalAddress' , '' , function () { return new Text ( 256 ); }, 'Project Legal Address' , true )
-> param ( 'legalTaxId' , '' , function () { return new Text ( 256 ); }, 'Project Legal Tax ID' , true )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $name , $teamId , $description , $logo , $url , $legalName , $legalCountry , $legalState , $legalCity , $legalAddress , $legalTaxId ) use ( $response , $user , $consoleDB , $projectDB ) {
2019-05-09 18:54:39 +12:00
$team = $projectDB -> getDocument ( $teamId );
2019-09-07 05:10:41 +12:00
if ( empty ( $team -> getUid ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Team not found' , 404 );
}
$project = $consoleDB -> createDocument (
[
2019-09-07 05:10:41 +12:00
'$collection' => Database :: SYSTEM_COLLECTION_PROJECTS ,
'$permissions' => [
'read' => [ 'team:' . $teamId ],
'write' => [ 'team:' . $teamId . '/owner' , 'team:' . $teamId . '/developer' ],
2019-05-09 18:54:39 +12:00
],
2019-09-07 05:10:41 +12:00
'name' => $name ,
'description' => $description ,
'logo' => $logo ,
'url' => $url ,
'legalName' => $legalName ,
'legalCountry' => $legalCountry ,
'legalState' => $legalState ,
'legalCity' => $legalCity ,
'legalAddress' => $legalAddress ,
'legalTaxId' => $legalTaxId ,
'teamId' => $team -> getUid (),
'webhooks' => [],
'keys' => [],
2019-09-30 19:13:40 +13:00
]
);
2019-05-09 18:54:39 +12:00
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
$consoleDB -> createNamespace ( $project -> getUid ());
2019-06-09 20:51:10 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> json ( $project -> getArrayCopy ())
;
2019-05-09 18:54:39 +12:00
}
);
$utopia -> patch ( '/v1/projects/:projectId' )
-> desc ( 'Update Project' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateProject' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'name' , null , function () { return new Text ( 100 ); }, 'Project name' )
-> param ( 'description' , '' , function () { return new Text ( 255 ); }, 'Project description' , true )
-> param ( 'logo' , '' , function () { return new Text ( 1024 ); }, 'Project logo' , true )
-> param ( 'url' , '' , function () { return new URL (); }, 'Project URL' , true )
-> param ( 'legalName' , '' , function () { return new Text ( 256 ); }, 'Project Legal Name' , true )
-> param ( 'legalCountry' , '' , function () { return new Text ( 256 ); }, 'Project Legal Country' , true )
-> param ( 'legalState' , '' , function () { return new Text ( 256 ); }, 'Project Legal State' , true )
-> param ( 'legalCity' , '' , function () { return new Text ( 256 ); }, 'Project Legal City' , true )
-> param ( 'legalAddress' , '' , function () { return new Text ( 256 ); }, 'Project Legal Address' , true )
-> param ( 'legalTaxId' , '' , function () { return new Text ( 256 ); }, 'Project Legal Tax ID' , true )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $name , $description , $logo , $url , $legalName , $legalCountry , $legalState , $legalCity , $legalAddress , $legalTaxId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$project = $consoleDB -> updateDocument ( array_merge ( $project -> getArrayCopy (), [
'name' => $name ,
'description' => $description ,
'logo' => $logo ,
'url' => $url ,
'legalName' => $legalName ,
'legalCountry' => $legalCountry ,
'legalState' => $legalState ,
'legalCity' => $legalCity ,
'legalAddress' => $legalAddress ,
'legalTaxId' => $legalTaxId ,
]));
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
$response -> json ( $project -> getArrayCopy ());
}
);
$utopia -> patch ( '/v1/projects/:projectId/oauth' )
-> desc ( 'Update Project OAuth' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateProjectOAuth' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'provider' , '' , function () use ( $providers ) { return new WhiteList ( array_keys ( $providers )); }, 'Provider Name' , false )
-> param ( 'appId' , '' , function () { return new Text ( 256 ); }, 'Provider App ID' , true )
-> param ( 'secret' , '' , function () { return new text ( 256 ); }, 'Provider Secret Key' , true )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $provider , $appId , $secret ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
2019-09-07 05:10:41 +12:00
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V1' );
$iv = OpenSSL :: randomPseudoBytes ( OpenSSL :: cipherIVLength ( OpenSSL :: CIPHER_AES_128_GCM ));
$tag = null ;
$secret = json_encode ([
'data' => OpenSSL :: encrypt ( $secret , OpenSSL :: CIPHER_AES_128_GCM , $key , 0 , $iv , $tag ),
'method' => OpenSSL :: CIPHER_AES_128_GCM ,
'iv' => bin2hex ( $iv ),
'tag' => bin2hex ( $tag ),
'version' => '1' ,
2019-05-09 18:54:39 +12:00
]);
$project = $consoleDB -> updateDocument ( array_merge ( $project -> getArrayCopy (), [
2019-09-07 05:10:41 +12:00
'usersOauth' . ucfirst ( $provider ) . 'Appid' => $appId ,
'usersOauth' . ucfirst ( $provider ) . 'Secret' => $secret ,
2019-05-09 18:54:39 +12:00
]));
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
$response -> json ( $project -> getArrayCopy ());
}
);
$utopia -> delete ( '/v1/projects/:projectId' )
-> desc ( 'Delete Project' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteProject' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
2019-08-23 09:00:18 +12:00
// Delete all children (keys, webhooks, tasks [stop tasks?], platforms)
2019-09-07 05:10:41 +12:00
if ( ! $consoleDB -> deleteDocument ( $projectId )) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed to remove project from DB' , 500 );
}
2019-08-23 08:53:50 +12:00
// Delete all DBs
2019-08-23 09:04:10 +12:00
// $consoleDB->deleteNamespace($project->getUid());
2019-09-07 05:10:41 +12:00
2019-08-23 09:04:10 +12:00
// Optimize DB?
// Delete all storage files
// Delete all storage cache
2019-08-23 08:53:50 +12:00
2019-05-09 18:54:39 +12:00
$response -> noContent ();
}
);
// Webhooks
$utopia -> get ( '/v1/projects/:projectId/webhooks' )
-> desc ( 'List Webhooks' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listWebhooks' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$webhooks = $project -> getAttribute ( 'webhooks' , []);
foreach ( $webhooks as $webhook ) { /* @var $webhook Document */
$httpPass = json_decode ( $webhook -> getAttribute ( 'httpPass' , '{}' ), true );
2019-09-07 05:10:41 +12:00
if ( empty ( $httpPass ) || ! isset ( $httpPass [ 'version' ])) {
2019-05-09 18:54:39 +12:00
continue ;
}
2019-09-07 05:10:41 +12:00
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V' . $httpPass [ 'version' ]);
2019-05-09 18:54:39 +12:00
$webhook -> setAttribute ( 'httpPass' , OpenSSL :: decrypt ( $httpPass [ 'data' ], $httpPass [ 'method' ], $key , 0 , hex2bin ( $httpPass [ 'iv' ]), hex2bin ( $httpPass [ 'tag' ])));
}
$response -> json ( $webhooks );
}
);
$utopia -> get ( '/v1/projects/:projectId/webhooks/:webhookId' )
-> desc ( 'Get Webhook' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getWebhook' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'webhookId' , null , function () { return new UID (); }, 'Webhook unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $webhookId ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$webhook = $project -> search ( '$uid' , $webhookId , $project -> getAttribute ( 'webhooks' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $webhook ) && $webhook instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Webhook not found' , 404 );
}
$httpPass = json_decode ( $webhook -> getAttribute ( 'httpPass' , '{}' ), true );
2019-09-07 05:10:41 +12:00
if ( ! empty ( $httpPass ) && isset ( $httpPass [ 'version' ])) {
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V' . $httpPass [ 'version' ]);
2019-05-09 18:54:39 +12:00
$webhook -> setAttribute ( 'httpPass' , OpenSSL :: decrypt ( $httpPass [ 'data' ], $httpPass [ 'method' ], $key , 0 , hex2bin ( $httpPass [ 'iv' ]), hex2bin ( $httpPass [ 'tag' ])));
}
$response -> json ( $webhook -> getArrayCopy ());
}
);
$utopia -> post ( '/v1/projects/:projectId/webhooks' )
-> desc ( 'Create Webhook' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createWebhook' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project 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 )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $name , $events , $url , $security , $httpUser , $httpPass ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
2019-09-07 05:10:41 +12:00
$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' ,
2019-05-09 18:54:39 +12:00
]);
$webhook = $consoleDB -> createDocument ([
2019-09-07 05:10:41 +12:00
'$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' ],
2019-05-09 18:54:39 +12:00
],
2019-09-07 05:10:41 +12:00
'name' => $name ,
'events' => $events ,
'url' => $url ,
'security' => ( int ) $security ,
'httpUser' => $httpUser ,
'httpPass' => $httpPass ,
2019-05-09 18:54:39 +12:00
]);
2019-09-07 05:10:41 +12:00
if ( false === $webhook ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving webhook to DB' , 500 );
}
$project -> setAttribute ( 'webhooks' , $webhook , Document :: SET_TYPE_APPEND );
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
2019-06-09 20:51:10 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> json ( $webhook -> getArrayCopy ())
;
2019-05-09 18:54:39 +12:00
}
);
$utopia -> put ( '/v1/projects/:projectId/webhooks/:webhookId' )
-> desc ( 'Update Webhook' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateWebhook' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> 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 )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $webhookId , $name , $events , $url , $security , $httpUser , $httpPass ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
2019-09-07 05:10:41 +12:00
$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' ,
2019-05-09 18:54:39 +12:00
]);
$webhook = $project -> search ( '$uid' , $webhookId , $project -> getAttribute ( 'webhooks' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $webhook ) && $webhook instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Webhook not found' , 404 );
}
$webhook
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'events' , $events )
-> setAttribute ( 'url' , $url )
2019-09-07 05:10:41 +12:00
-> setAttribute ( 'security' , ( int ) $security )
2019-05-09 18:54:39 +12:00
-> setAttribute ( 'httpUser' , $httpUser )
-> setAttribute ( 'httpPass' , $httpPass )
;
2019-09-07 05:10:41 +12:00
if ( false === $consoleDB -> updateDocument ( $webhook -> getArrayCopy ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving webhook to DB' , 500 );
}
$response -> json ( $webhook -> getArrayCopy ());
}
);
$utopia -> delete ( '/v1/projects/:projectId/webhooks/:webhookId' )
-> desc ( 'Delete Webhook' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteWebhook' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'webhookId' , null , function () { return new UID (); }, 'Webhook unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $webhookId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$webhook = $project -> search ( '$uid' , $webhookId , $project -> getAttribute ( 'webhooks' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $webhook ) && $webhook instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Webhook not found' , 404 );
}
2019-09-07 05:10:41 +12:00
if ( ! $consoleDB -> deleteDocument ( $webhook -> getUid ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed to remove webhook from DB' , 500 );
}
$response -> noContent ();
}
);
// Keys
$utopia -> get ( '/v1/projects/:projectId/keys' )
-> desc ( 'List Keys' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listKeys' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$response -> json ( $project -> getAttribute ( 'keys' , [])); //FIXME make sure array objects return correctly
}
);
$utopia -> get ( '/v1/projects/:projectId/keys/:keyId' )
-> desc ( 'Get Key' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getKey' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'keyId' , null , function () { return new UID (); }, 'Key unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $keyId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$key = $project -> search ( '$uid' , $keyId , $project -> getAttribute ( 'keys' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $key ) && $key instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Key not found' , 404 );
}
$response -> json ( $key -> getArrayCopy ());
}
);
$utopia -> post ( '/v1/projects/:projectId/keys' )
-> desc ( 'Create Key' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createKey' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project 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' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $name , $scopes ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$key = $consoleDB -> createDocument ([
2019-09-07 05:10:41 +12:00
'$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' ],
2019-05-09 18:54:39 +12:00
],
2019-09-07 05:10:41 +12:00
'name' => $name ,
'scopes' => $scopes ,
'secret' => bin2hex ( random_bytes ( 128 )),
2019-05-09 18:54:39 +12:00
]);
2019-09-07 05:10:41 +12:00
if ( false === $key ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving key to DB' , 500 );
}
$project -> setAttribute ( 'keys' , $key , Document :: SET_TYPE_APPEND );
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
2019-06-09 20:51:10 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> json ( $key -> getArrayCopy ())
;
2019-05-09 18:54:39 +12:00
}
);
$utopia -> put ( '/v1/projects/:projectId/keys/:keyId' )
-> desc ( 'Update Key' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateKey' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> 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' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $keyId , $name , $scopes ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$key = $project -> search ( '$uid' , $keyId , $project -> getAttribute ( 'keys' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $key ) && $key instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Key not found' , 404 );
}
$key
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'scopes' , $scopes )
;
2019-09-07 05:10:41 +12:00
if ( false === $consoleDB -> updateDocument ( $key -> getArrayCopy ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving key to DB' , 500 );
}
$response -> json ( $key -> getArrayCopy ());
}
);
$utopia -> delete ( '/v1/projects/:projectId/keys/:keyId' )
-> desc ( 'Delete Key' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteKey' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'keyId' , null , function () { return new UID (); }, 'Key unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $keyId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$key = $project -> search ( '$uid' , $keyId , $project -> getAttribute ( 'keys' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $key ) && $key instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Key not found' , 404 );
}
2019-09-07 05:10:41 +12:00
if ( ! $consoleDB -> deleteDocument ( $key -> getUid ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed to remove key from DB' , 500 );
}
$response -> noContent ();
}
);
// Tasks
$utopia -> get ( '/v1/projects/:projectId/tasks' )
-> desc ( 'List Tasks' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listTasks' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$tasks = $project -> getAttribute ( 'tasks' , []);
foreach ( $tasks as $task ) { /* @var $task Document */
$httpPass = json_decode ( $task -> getAttribute ( 'httpPass' , '{}' ), true );
2019-09-07 05:10:41 +12:00
if ( empty ( $httpPass ) || ! isset ( $httpPass [ 'version' ])) {
2019-05-09 18:54:39 +12:00
continue ;
}
2019-09-07 05:10:41 +12:00
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V' . $httpPass [ 'version' ]);
2019-05-09 18:54:39 +12:00
$task -> setAttribute ( 'httpPass' , OpenSSL :: decrypt ( $httpPass [ 'data' ], $httpPass [ 'method' ], $key , 0 , hex2bin ( $httpPass [ 'iv' ]), hex2bin ( $httpPass [ 'tag' ])));
}
$response -> json ( $tasks );
}
);
$utopia -> get ( '/v1/projects/:projectId/tasks/:taskId' )
-> desc ( 'Get Task' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getTask' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'taskId' , null , function () { return new UID (); }, 'Task unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $taskId ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$task = $project -> search ( '$uid' , $taskId , $project -> getAttribute ( 'tasks' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $task ) && $task instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Task not found' , 404 );
}
$httpPass = json_decode ( $task -> getAttribute ( 'httpPass' , '{}' ), true );
2019-09-07 05:10:41 +12:00
if ( ! empty ( $httpPass ) && isset ( $httpPass [ 'version' ])) {
$key = $request -> getServer ( '_APP_OPENSSL_KEY_V' . $httpPass [ 'version' ]);
2019-05-09 18:54:39 +12:00
$task -> setAttribute ( 'httpPass' , OpenSSL :: decrypt ( $httpPass [ 'data' ], $httpPass [ 'method' ], $key , 0 , hex2bin ( $httpPass [ 'iv' ]), hex2bin ( $httpPass [ 'tag' ])));
}
$response -> json ( $task -> getArrayCopy ());
}
);
$utopia -> post ( '/v1/projects/:projectId/tasks' )
-> desc ( 'Create Task' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createTask' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project 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 )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $name , $status , $schedule , $security , $httpMethod , $httpUrl , $httpHeaders , $httpUser , $httpPass ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$cron = CronExpression :: factory ( $schedule );
$next = ( $status == 'play' ) ? $cron -> getNextRunDate () -> format ( 'U' ) : null ;
2019-09-07 05:10:41 +12:00
$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' ,
2019-05-09 18:54:39 +12:00
]);
$task = $consoleDB -> createDocument ([
2019-09-07 05:10:41 +12:00
'$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' ],
2019-05-09 18:54:39 +12:00
],
2019-09-07 05:10:41 +12:00
'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 ,
2019-05-09 18:54:39 +12:00
]);
2019-09-07 05:10:41 +12:00
if ( false === $task ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving tasks to DB' , 500 );
}
$project -> setAttribute ( 'tasks' , $task , Document :: SET_TYPE_APPEND );
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
2019-09-07 05:10:41 +12:00
if ( $next ) {
2019-05-09 18:54:39 +12:00
ResqueScheduler :: enqueueAt ( $next , 'v1-tasks' , 'TasksV1' , $task -> getArrayCopy ());
}
2019-06-09 20:51:10 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> json ( $task -> getArrayCopy ())
;
2019-05-09 18:54:39 +12:00
}
);
$utopia -> put ( '/v1/projects/:projectId/tasks/:taskId' )
-> desc ( 'Update Task' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateTask' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> 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 )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $taskId , $name , $status , $schedule , $security , $httpMethod , $httpUrl , $httpHeaders , $httpUser , $httpPass ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$task = $project -> search ( '$uid' , $taskId , $project -> getAttribute ( 'tasks' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $task ) && $task instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Task not found' , 404 );
}
$cron = CronExpression :: factory ( $schedule );
$next = ( $status == 'play' ) ? $cron -> getNextRunDate () -> format ( 'U' ) : null ;
2019-09-07 05:10:41 +12:00
$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' ,
2019-05-09 18:54:39 +12:00
]);
$task
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'status' , $status )
-> setAttribute ( 'schedule' , $schedule )
-> setAttribute ( 'updated' , time ())
-> setAttribute ( 'next' , $next )
2019-09-07 05:10:41 +12:00
-> setAttribute ( 'security' , ( int ) $security )
2019-05-09 18:54:39 +12:00
-> setAttribute ( 'httpMethod' , $httpMethod )
-> setAttribute ( 'httpUrl' , $httpUrl )
-> setAttribute ( 'httpHeaders' , $httpHeaders )
-> setAttribute ( 'httpUser' , $httpUser )
-> setAttribute ( 'httpPass' , $httpPass )
;
2019-09-07 05:10:41 +12:00
if ( false === $consoleDB -> updateDocument ( $task -> getArrayCopy ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving tasks to DB' , 500 );
}
2019-09-07 05:10:41 +12:00
if ( $next ) {
2019-05-09 18:54:39 +12:00
ResqueScheduler :: enqueueAt ( $next , 'v1-tasks' , 'TasksV1' , $task -> getArrayCopy ());
}
$response -> json ( $task -> getArrayCopy ());
}
);
$utopia -> delete ( '/v1/projects/:projectId/tasks/:taskId' )
-> desc ( 'Delete Task' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteTask' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'taskId' , null , function () { return new UID (); }, 'Task unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $taskId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$task = $project -> search ( '$uid' , $taskId , $project -> getAttribute ( 'tasks' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $task ) && $task instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Task not found' , 404 );
}
2019-09-07 05:10:41 +12:00
if ( ! $consoleDB -> deleteDocument ( $task -> getUid ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed to remove tasks from DB' , 500 );
}
$response -> noContent ();
}
);
// Platforms
$utopia -> get ( '/v1/projects/:projectId/platforms' )
-> desc ( 'List Platforms' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listPlatforms' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , '' , function () { return new UID (); }, 'Project unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$platforms = $project -> getAttribute ( 'platforms' , []);
$response -> json ( $platforms );
}
);
$utopia -> get ( '/v1/projects/:projectId/platforms/:platformId' )
-> desc ( 'Get Platform' )
-> label ( 'scope' , 'projects.read' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getPlatform' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'platformId' , null , function () { return new UID (); }, 'Platform unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $platformId ) use ( $request , $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$platform = $project -> search ( '$uid' , $platformId , $project -> getAttribute ( 'platforms' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $platform ) && $platform instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Platform not found' , 404 );
}
$response -> json ( $platform -> getArrayCopy ());
}
);
$utopia -> post ( '/v1/projects/:projectId/platforms' )
-> desc ( 'Create Platform' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createPlatform' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> 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 )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $type , $name , $key , $store , $url ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$platform = $consoleDB -> createDocument ([
2019-09-07 05:10:41 +12:00
'$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' ],
2019-05-09 18:54:39 +12:00
],
2019-09-07 05:10:41 +12:00
'type' => $type ,
'name' => $name ,
'key' => $key ,
'store' => $store ,
'url' => $url ,
'dateCreated' => time (),
'dateUpdated' => time (),
2019-05-09 18:54:39 +12:00
]);
2019-09-07 05:10:41 +12:00
if ( false === $platform ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving platform to DB' , 500 );
}
$project -> setAttribute ( 'platforms' , $platform , Document :: SET_TYPE_APPEND );
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-09-07 05:10:41 +12:00
if ( false === $project ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving project to DB' , 500 );
}
2019-06-09 20:51:10 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> json ( $platform -> getArrayCopy ())
;
2019-05-09 18:54:39 +12:00
}
);
$utopia -> put ( '/v1/projects/:projectId/platforms/:platformId' )
-> desc ( 'Update Platform' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updatePlatform' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> 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 )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $platformId , $name , $key , $store , $url ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$platform = $project -> search ( '$uid' , $platformId , $project -> getAttribute ( 'platforms' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $platform ) && $platform instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Platform not found' , 404 );
}
$platform
-> setAttribute ( 'name' , $name )
2019-08-15 10:01:27 +12:00
-> setAttribute ( 'dateUpdated' , time ())
2019-05-09 18:54:39 +12:00
-> setAttribute ( 'key' , $key )
-> setAttribute ( 'store' , $store )
2019-08-10 23:38:09 +12:00
-> setAttribute ( 'url' , $url )
2019-05-09 18:54:39 +12:00
;
2019-09-07 05:10:41 +12:00
if ( false === $consoleDB -> updateDocument ( $platform -> getArrayCopy ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed saving platform to DB' , 500 );
}
$response -> json ( $platform -> getArrayCopy ());
}
);
$utopia -> delete ( '/v1/projects/:projectId/platforms/:platformId' )
-> desc ( 'Delete Platform' )
-> label ( 'scope' , 'projects.write' )
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deletePlatform' )
2019-10-07 09:10:52 +13:00
-> param ( 'projectId' , null , function () { return new UID (); }, 'Project unique ID.' )
-> param ( 'platformId' , null , function () { return new UID (); }, 'Platform unique ID.' )
2019-05-09 18:54:39 +12:00
-> action (
2019-09-07 05:10:41 +12:00
function ( $projectId , $platformId ) use ( $response , $consoleDB ) {
2019-05-09 18:54:39 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-09-07 05:10:41 +12:00
if ( empty ( $project -> getUid ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Project not found' , 404 );
}
$platform = $project -> search ( '$uid' , $platformId , $project -> getAttribute ( 'platforms' , []));
2019-09-07 05:10:41 +12:00
if ( empty ( $platform ) && $platform instanceof Document ) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Platform not found' , 404 );
}
2019-09-07 05:10:41 +12:00
if ( ! $consoleDB -> deleteDocument ( $platform -> getUid ())) {
2019-05-09 18:54:39 +12:00
throw new Exception ( 'Failed to remove platform from DB' , 500 );
}
$response -> noContent ();
}
2019-09-07 05:10:41 +12:00
);