2019-12-15 08:33:29 +13:00
< ? php
2020-06-29 05:31:21 +12:00
use Utopia\App ;
2019-12-15 08:33:29 +13:00
use Utopia\Exception ;
2019-12-18 23:24:54 +13:00
use Utopia\Validator\ArrayList ;
2020-06-22 00:18:00 +12:00
use Utopia\Validator\Boolean ;
2019-12-15 08:33:29 +13:00
use Utopia\Validator\Text ;
use Utopia\Validator\WhiteList ;
2021-03-25 06:47:43 +13:00
use Appwrite\Network\Validator\URL ;
2020-07-09 03:07:52 +12:00
use Utopia\Validator\Range ;
2021-03-01 00:40:59 +13:00
use Utopia\Validator\Integer ;
2020-03-29 01:42:16 +13:00
use Utopia\Config\Config ;
2020-02-24 06:05:42 +13:00
use Utopia\Domains\Domain ;
2020-03-25 06:56:32 +13:00
use Appwrite\Auth\Auth ;
use Appwrite\Task\Validator\Cron ;
use Appwrite\Database\Database ;
use Appwrite\Database\Document ;
use Appwrite\Database\Validator\UID ;
2020-06-12 07:36:10 +12:00
use Appwrite\Network\Validator\CNAME ;
2020-07-08 10:11:02 +12:00
use Appwrite\Network\Validator\Domain as DomainValidator ;
2020-08-15 09:56:50 +12:00
use Appwrite\Utopia\Response ;
2019-12-18 23:24:54 +13:00
use Cron\CronExpression ;
2019-12-15 08:33:29 +13:00
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/projects' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Create Project' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'create' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT )
2020-09-11 02:40:14 +12:00
-> param ( 'name' , null , new Text ( 128 ), 'Project name. Max length: 128 chars.' )
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
-> param ( 'description' , '' , new Text ( 256 ), 'Project description. Max length: 256 chars.' , true )
-> param ( 'logo' , '' , new Text ( 1024 ), 'Project logo.' , true )
-> param ( 'url' , '' , new URL (), 'Project URL.' , true )
-> param ( 'legalName' , '' , new Text ( 256 ), 'Project legal Name. Max length: 256 chars.' , true )
-> param ( 'legalCountry' , '' , new Text ( 256 ), 'Project legal Country. Max length: 256 chars.' , true )
-> param ( 'legalState' , '' , new Text ( 256 ), 'Project legal State. Max length: 256 chars.' , true )
-> param ( 'legalCity' , '' , new Text ( 256 ), 'Project legal City. Max length: 256 chars.' , true )
-> param ( 'legalAddress' , '' , new Text ( 256 ), 'Project legal Address. Max length: 256 chars.' , true )
-> param ( 'legalTaxId' , '' , new Text ( 256 ), 'Project legal Tax ID. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
-> inject ( 'projectDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $name , $teamId , $description , $logo , $url , $legalName , $legalCountry , $legalState , $legalCity , $legalAddress , $legalTaxId , $response , $consoleDB , $projectDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
/** @var Appwrite\Database\Database $projectDB */
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$team = $projectDB -> getDocument ( $teamId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> createDocument (
[
'$collection' => Database :: SYSTEM_COLLECTION_PROJECTS ,
'$permissions' => [
'read' => [ 'team:' . $teamId ],
'write' => [ 'team:' . $teamId . '/owner' , 'team:' . $teamId . '/developer' ],
],
'name' => $name ,
'description' => $description ,
'logo' => $logo ,
'url' => $url ,
'legalName' => $legalName ,
'legalCountry' => $legalCountry ,
'legalState' => $legalState ,
'legalCity' => $legalCity ,
'legalAddress' => $legalAddress ,
'legalTaxId' => $legalTaxId ,
'teamId' => $team -> getId (),
'platforms' => [],
'webhooks' => [],
'keys' => [],
'tasks' => [],
2020-09-12 20:07:28 +12:00
'domains' => [],
2020-07-01 03:46:42 +12:00
]
);
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2020-02-01 11:34:07 +13:00
}
2020-07-01 03:46:42 +12:00
$consoleDB -> createNamespace ( $project -> getId ());
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $project , Response :: MODEL_PROJECT )
;
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects' )
2019-12-15 08:33:29 +13:00
-> desc ( 'List Projects' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-15 08:33:29 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-15 08:33:29 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'list' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
-> param ( 'limit' , 25 , new Range ( 0 , 100 ), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.' , true )
-> param ( 'offset' , 0 , new Range ( 0 , 2000 ), 'Results offset. The default value is 0. Use this param to manage pagination.' , true )
-> param ( 'orderType' , 'ASC' , new WhiteList ([ 'ASC' , 'DESC' ], true ), 'Order result by ASC or DESC order.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-09 03:07:52 +12:00
-> action ( function ( $search , $limit , $offset , $orderType , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-07-09 03:07:52 +12:00
2020-07-01 03:46:42 +12:00
$results = $consoleDB -> getCollection ([
2020-07-09 03:07:52 +12:00
'limit' => $limit ,
'offset' => $offset ,
2020-12-30 08:30:49 +13:00
'orderType' => $orderType ,
2020-07-09 03:07:52 +12:00
'search' => $search ,
2020-07-01 03:46:42 +12:00
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_PROJECTS ,
],
]);
2020-09-12 20:07:28 +12:00
$response -> dynamic ( new Document ([
'sum' => $consoleDB -> getSum (),
'projects' => $results
]), Response :: MODEL_PROJECT_LIST );
2020-12-27 05:33:36 +13:00
});
2019-12-15 08:33:29 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId' )
2019-12-15 08:33:29 +13:00
-> desc ( 'Get Project' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-15 08:33:29 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-15 08:33:29 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'get' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-15 08:33:29 +13:00
2020-09-12 20:07:28 +12:00
$response -> dynamic ( $project , Response :: MODEL_PROJECT );
2020-12-27 05:33:36 +13:00
});
2019-12-15 08:33:29 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/usage' )
2019-12-15 08:33:29 +13:00
-> desc ( 'Get Project' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-15 08:33:29 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-15 08:33:29 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'getUsage' )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2020-09-12 06:08:41 +12:00
-> param ( 'range' , '30d' , new WhiteList ([ '24h' , '7d' , '30d' , '90d' ], true ), 'Date range.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
-> inject ( 'projectDB' )
-> inject ( 'register' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $range , $response , $consoleDB , $projectDB , $register ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Registry\Registry $register */
$project = $consoleDB -> getDocument ( $projectId );
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-15 08:33:29 +13:00
2021-01-13 20:07:28 +13:00
if ( App :: getEnv ( '_APP_USAGE_STATS' , 'enabled' ) == 'enabled' ) {
2020-07-01 03:46:42 +12:00
2021-01-13 02:35:17 +13:00
$period = [
'24h' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-24 hours' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( '+1 hour' )),
'group' => '30m' ,
],
'7d' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-7 days' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( 'now' )),
'group' => '1d' ,
],
'30d' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-30 days' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( 'now' )),
'group' => '1d' ,
],
'90d' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-90 days' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( 'now' )),
'group' => '1d' ,
],
];
$client = $register -> get ( 'influxdb' );
$requests = [];
$network = [];
$functions = [];
if ( $client ) {
$start = $period [ $range ][ 'start' ] -> format ( DateTime :: RFC3339 );
$end = $period [ $range ][ 'end' ] -> format ( DateTime :: RFC3339 );
$database = $client -> selectDB ( 'telegraf' );
// Requests
$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 -> getId () . '\' GROUP BY time(' . $period [ $range ][ 'group' ] . ') FILL(null)' );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$requests [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
'date' => \strtotime ( $point [ 'time' ]),
];
}
// Network
$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 -> getId () . '\' GROUP BY time(' . $period [ $range ][ 'group' ] . ') FILL(null)' );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$network [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
'date' => \strtotime ( $point [ 'time' ]),
];
}
// Functions
$result = $database -> query ( 'SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $project -> getId () . '\' GROUP BY time(' . $period [ $range ][ 'group' ] . ') FILL(null)' );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$functions [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
'date' => \strtotime ( $point [ 'time' ]),
];
}
2020-07-21 22:33:23 +12:00
}
2021-01-13 02:35:17 +13:00
} else {
$requests = [];
$network = [];
$functions = [];
2020-07-01 03:46:42 +12:00
}
2019-12-15 08:33:29 +13:00
2021-01-13 02:35:17 +13:00
2020-07-01 03:46:42 +12:00
// Users
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$projectDB -> getCollection ([
'limit' => 0 ,
'offset' => 0 ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_USERS ,
],
]);
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$usersTotal = $projectDB -> getSum ();
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
// Documents
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$collections = $projectDB -> getCollection ([
'limit' => 100 ,
'offset' => 0 ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_COLLECTIONS ,
],
]);
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$collectionsTotal = $projectDB -> getSum ();
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$documents = [];
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
foreach ( $collections as $collection ) {
$result = $projectDB -> getCollection ([
'limit' => 0 ,
2019-12-15 08:33:29 +13:00
'offset' => 0 ,
'filters' => [
2020-07-01 03:46:42 +12:00
'$collection=' . $collection [ '$id' ],
2019-12-15 08:33:29 +13:00
],
]);
2020-07-01 03:46:42 +12:00
$documents [] = [ 'name' => $collection [ 'name' ], 'total' => $projectDB -> getSum ()];
2019-12-15 08:33:29 +13:00
}
2020-07-01 03:46:42 +12:00
// Tasks
$tasksTotal = \count ( $project -> getAttribute ( 'tasks' , []));
$response -> json ([
2020-07-24 02:59:44 +12:00
'range' => $range ,
2020-07-01 03:46:42 +12:00
'requests' => [
'data' => $requests ,
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'value' ];
}, $requests )),
],
'network' => [
2020-07-13 08:43:03 +12:00
'data' => \array_map ( function ( $value ) { return [ 'value' => \round ( $value [ 'value' ] / 1000000 , 2 ), 'date' => $value [ 'date' ]];}, $network ), // convert bytes to mb
2020-07-01 03:46:42 +12:00
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'value' ];
}, $network )),
],
2020-07-21 22:33:23 +12:00
'functions' => [
'data' => $functions ,
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'value' ];
}, $functions )),
],
2020-07-01 03:46:42 +12:00
'collections' => [
'data' => $collections ,
'total' => $collectionsTotal ,
],
'documents' => [
'data' => $documents ,
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'total' ];
}, $documents )),
],
'users' => [
'data' => [],
'total' => $usersTotal ,
],
'tasks' => [
'data' => [],
'total' => $tasksTotal ,
],
'storage' => [
'total' => $projectDB -> getCount (
[
2020-07-17 21:48:43 +12:00
'attribute' => 'sizeOriginal' ,
2020-07-01 03:46:42 +12:00
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_FILES ,
],
]
2020-07-17 21:48:43 +12:00
) +
$projectDB -> getCount (
[
2020-08-08 22:41:17 +12:00
'attribute' => 'size' ,
2020-07-17 21:48:43 +12:00
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_TAGS ,
],
]
2020-07-01 03:46:42 +12:00
),
],
]);
2020-12-27 05:33:36 +13:00
});
2019-12-15 08:33:29 +13:00
2020-06-29 05:31:21 +12:00
App :: patch ( '/v1/projects/:projectId' )
2019-12-15 08:33:29 +13:00
-> desc ( 'Update Project' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-15 08:33:29 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-15 08:33:29 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'update' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Project name. Max length: 128 chars.' )
-> param ( 'description' , '' , new Text ( 256 ), 'Project description. Max length: 256 chars.' , true )
-> param ( 'logo' , '' , new Text ( 1024 ), 'Project logo.' , true )
-> param ( 'url' , '' , new URL (), 'Project URL.' , true )
-> param ( 'legalName' , '' , new Text ( 256 ), 'Project legal name. Max length: 256 chars.' , true )
-> param ( 'legalCountry' , '' , new Text ( 256 ), 'Project legal country. Max length: 256 chars.' , true )
-> param ( 'legalState' , '' , new Text ( 256 ), 'Project legal state. Max length: 256 chars.' , true )
-> param ( 'legalCity' , '' , new Text ( 256 ), 'Project legal city. Max length: 256 chars.' , true )
-> param ( 'legalAddress' , '' , new Text ( 256 ), 'Project legal address. Max length: 256 chars.' , true )
-> param ( 'legalTaxId' , '' , new Text ( 256 ), 'Project legal tax ID. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $name , $description , $logo , $url , $legalName , $legalCountry , $legalState , $legalCity , $legalAddress , $legalTaxId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$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 ,
]));
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2019-12-15 08:33:29 +13:00
}
2020-07-01 03:46:42 +12:00
2020-09-12 20:07:28 +12:00
$response -> dynamic ( $project , Response :: MODEL_PROJECT );
2020-12-27 05:33:36 +13:00
});
2019-12-15 08:33:29 +13:00
2020-06-29 05:31:21 +12:00
App :: patch ( '/v1/projects/:projectId/oauth2' )
2020-02-17 00:41:03 +13:00
-> desc ( 'Update Project OAuth2' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-15 08:33:29 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-15 08:33:29 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-02-17 00:41:03 +13:00
-> label ( 'sdk.method' , 'updateOAuth2' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
-> param ( 'provider' , '' , new WhiteList ( \array_keys ( Config :: getParam ( 'providers' )), true ), 'Provider Name' , false )
-> param ( 'appId' , '' , new Text ( 256 ), 'Provider app ID. Max length: 256 chars.' , true )
-> param ( 'secret' , '' , new text ( 512 ), 'Provider secret key. Max length: 512 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $provider , $appId , $secret , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> updateDocument ( \array_merge ( $project -> getArrayCopy (), [
'usersOauth2' . \ucfirst ( $provider ) . 'Appid' => $appId ,
'usersOauth2' . \ucfirst ( $provider ) . 'Secret' => $secret ,
]));
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2019-12-15 08:33:29 +13:00
}
2020-07-01 03:46:42 +12:00
2020-09-12 20:07:28 +12:00
$response -> dynamic ( $project , Response :: MODEL_PROJECT );
2020-12-27 05:33:36 +13:00
});
2019-12-15 08:33:29 +13:00
2021-03-01 04:00:27 +13:00
App :: patch ( '/v1/projects/:projectId/auth/limit' )
-> desc ( 'Update Project users limit' )
2021-03-01 00:40:59 +13:00
-> groups ([ 'api' , 'projects' ])
-> label ( 'scope' , 'projects.write' )
2021-04-23 18:31:22 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2021-03-01 00:40:59 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2021-03-01 04:00:27 +13:00
-> label ( 'sdk.method' , 'updateAuthLimit' )
2021-03-01 00:40:59 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT )
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2021-03-01 04:00:27 +13:00
-> param ( 'limit' , false , new Integer ( true ), 'Set the max number of users allowed in this project. Use 0 for unlimited.' )
2021-03-01 00:40:59 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2021-03-01 04:00:27 +13:00
-> action ( function ( $projectId , $limit , $response , $consoleDB ) {
2021-03-01 00:40:59 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $consoleDB */
$project = $consoleDB -> getDocument ( $projectId );
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
if ( false === $consoleDB -> updateDocument (
\array_merge ( $project -> getArrayCopy (), [
2021-03-01 04:00:27 +13:00
'usersAuthLimit' => $limit ,
2021-03-01 00:40:59 +13:00
]))
) {
throw new Exception ( 'Failed saving project to DB' , 500 );
};
$response -> dynamic ( $project , Response :: MODEL_PROJECT );
});
2021-03-01 04:00:27 +13:00
App :: patch ( '/v1/projects/:projectId/auth/:method' )
-> desc ( 'Update Project auth method status. Use this endpoint to enable or disable a given auth method for this project.' )
2021-03-01 00:40:59 +13:00
-> groups ([ 'api' , 'projects' ])
-> label ( 'scope' , 'projects.write' )
2021-04-23 18:31:22 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2021-03-01 00:40:59 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2021-03-01 04:00:27 +13:00
-> label ( 'sdk.method' , 'updateAuthStatus' )
2021-03-01 00:40:59 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROJECT )
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2021-03-01 04:00:27 +13:00
-> param ( 'method' , '' , new WhiteList ( \array_keys ( Config :: getParam ( 'auth' )), true ), 'Auth Method. Possible values: ' . implode ( ',' , \array_keys ( Config :: getParam ( 'auth' ))), false )
-> param ( 'status' , false , new Boolean ( true ), 'Set the status of this auth method.' )
2021-03-01 00:40:59 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2021-03-01 04:00:27 +13:00
-> action ( function ( $projectId , $method , $status , $response , $consoleDB ) {
2021-03-01 00:40:59 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $consoleDB */
$project = $consoleDB -> getDocument ( $projectId );
2021-03-01 04:00:27 +13:00
$auth = Config :: getParam ( 'auth' )[ $method ] ? ? [];
$authKey = $auth [ 'key' ] ? ? '' ;
$status = ( $status === '1' || $status === 'true' || $status === 1 || $status === true );
2021-03-01 00:40:59 +13:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
if ( false === $consoleDB -> updateDocument (
\array_merge ( $project -> getArrayCopy (), [
2021-03-01 04:00:27 +13:00
$authKey => $status ,
2021-03-01 00:40:59 +13:00
]))
) {
throw new Exception ( 'Failed saving project to DB' , 500 );
};
$response -> dynamic ( $project , Response :: MODEL_PROJECT );
});
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/projects/:projectId' )
2019-12-15 08:33:29 +13:00
-> desc ( 'Delete Project' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-15 08:33:29 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-15 08:33:29 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'delete' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
-> param ( 'password' , '' , new UID (), 'Your user password for confirmation. Must be between 6 to 32 chars.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'user' )
-> inject ( 'consoleDB' )
-> inject ( 'deletes' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $password , $response , $user , $consoleDB , $deletes ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Document $user */
/** @var Appwrite\Database\Database $consoleDB */
/** @var Appwrite\Event\Event $deletes */
if ( ! Auth :: passwordVerify ( $password , $user -> getAttribute ( 'password' ))) { // Double check user password
throw new Exception ( 'Invalid credentials' , 401 );
}
2020-01-31 12:50:17 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-15 08:33:29 +13:00
2020-12-19 00:08:58 +13:00
$deletes
2020-12-19 03:05:15 +13:00
-> setParam ( 'type' , DELETE_TYPE_DOCUMENT )
2020-12-22 07:15:52 +13:00
-> setParam ( 'document' , $project -> getArrayCopy ())
2020-12-19 00:08:58 +13:00
;
2020-05-27 05:11:20 +12:00
2020-07-01 03:46:42 +12:00
foreach ([ 'keys' , 'webhooks' , 'tasks' , 'platforms' , 'domains' ] as $key ) { // Delete all children (keys, webhooks, tasks [stop tasks?], platforms)
2021-02-08 22:01:52 +13:00
$list = $project -> getAttribute ( $key , []);
foreach ( $list as $document ) {
/** @var Document $document */
if ( $consoleDB -> deleteDocument ( $document -> getId ())) {
if ( $document -> getCollection () == Database :: SYSTEM_COLLECTION_DOMAINS ) {
$deletes
-> setParam ( 'type' , DELETE_TYPE_CERTIFICATES )
-> setParam ( 'document' , $document )
;
}
} else {
2020-07-01 03:46:42 +12:00
throw new Exception ( 'Failed to remove project document (' . $key . ')] from DB' , 500 );
2020-05-26 17:09:06 +12:00
}
}
2020-07-01 03:46:42 +12:00
}
2021-02-08 22:01:52 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $consoleDB -> deleteDocument ( $project -> getAttribute ( 'teamId' , null ))) {
throw new Exception ( 'Failed to remove project team from DB' , 500 );
}
2019-12-15 08:33:29 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $consoleDB -> deleteDocument ( $projectId )) {
throw new Exception ( 'Failed to remove project from DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
$response -> noContent ();
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
// Webhooks
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/projects/:projectId/webhooks' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Create Webhook' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createWebhook' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_WEBHOOK )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Webhook name. Max length: 128 chars.' )
-> param ( 'events' , null , new ArrayList ( new WhiteList ( array_keys ( Config :: getParam ( 'events' ), true ), true )), 'Events list.' )
-> param ( 'url' , null , new URL (), 'Webhook URL.' )
-> param ( 'security' , false , new Boolean ( true ), 'Certificate verification, false for disabled or true for enabled.' )
-> param ( 'httpUser' , '' , new Text ( 256 ), 'Webhook HTTP user. Max length: 256 chars.' , true )
-> param ( 'httpPass' , '' , new Text ( 256 ), 'Webhook HTTP password. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $name , $events , $url , $security , $httpUser , $httpPass , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$security = ( $security === '1' || $security === 'true' || $security === 1 || $security === true );
$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 ,
2020-07-09 21:11:42 +12:00
'security' => $security ,
2020-07-01 03:46:42 +12:00
'httpUser' => $httpUser ,
'httpPass' => $httpPass ,
]);
if ( false === $webhook ) {
throw new Exception ( 'Failed saving webhook to DB' , 500 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project -> setAttribute ( 'webhooks' , $webhook , Document :: SET_TYPE_APPEND );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2020-02-01 11:34:07 +13:00
}
2020-07-01 03:46:42 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $webhook , Response :: MODEL_WEBHOOK )
;
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/webhooks' )
2019-12-18 23:24:54 +13:00
-> desc ( 'List Webhooks' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listWebhooks' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_WEBHOOK_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$webhooks = $project -> getAttribute ( 'webhooks' , []);
2019-12-18 23:24:54 +13:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( new Document ([
'sum' => count ( $webhooks ),
'webhooks' => $webhooks
]), Response :: MODEL_WEBHOOK_LIST );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/webhooks/:webhookId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Get Webhook' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getWebhook' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_WEBHOOK )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'webhookId' , null , new UID (), 'Webhook unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $webhookId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$webhook = $project -> search ( '$id' , $webhookId , $project -> getAttribute ( 'webhooks' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $webhook ) || ! $webhook instanceof Document ) {
throw new Exception ( 'Webhook not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $webhook , Response :: MODEL_WEBHOOK );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/projects/:projectId/webhooks/:webhookId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Update Webhook' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateWebhook' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_WEBHOOK )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'webhookId' , null , new UID (), 'Webhook unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Webhook name. Max length: 128 chars.' )
-> param ( 'events' , null , new ArrayList ( new WhiteList ( array_keys ( Config :: getParam ( 'events' ), true ), true )), 'Events list.' )
-> param ( 'url' , null , new URL (), 'Webhook URL.' )
-> param ( 'security' , false , new Boolean ( true ), 'Certificate verification, false for disabled or true for enabled.' )
-> param ( 'httpUser' , '' , new Text ( 256 ), 'Webhook HTTP user. Max length: 256 chars.' , true )
-> param ( 'httpPass' , '' , new Text ( 256 ), 'Webhook HTTP password. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $webhookId , $name , $events , $url , $security , $httpUser , $httpPass , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$security = ( $security === '1' || $security === 'true' || $security === 1 || $security === true );
$webhook = $project -> search ( '$id' , $webhookId , $project -> getAttribute ( 'webhooks' , []));
if ( empty ( $webhook ) || ! $webhook instanceof Document ) {
throw new Exception ( 'Webhook not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$webhook
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'events' , $events )
-> setAttribute ( 'url' , $url )
2020-07-09 21:11:42 +12:00
-> setAttribute ( 'security' , $security )
2020-07-01 03:46:42 +12:00
-> setAttribute ( 'httpUser' , $httpUser )
-> setAttribute ( 'httpPass' , $httpPass )
;
if ( false === $consoleDB -> updateDocument ( $webhook -> getArrayCopy ())) {
throw new Exception ( 'Failed saving webhook to DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $webhook , Response :: MODEL_WEBHOOK );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/projects/:projectId/webhooks/:webhookId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Delete Webhook' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteWebhook' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'webhookId' , null , new UID (), 'Webhook unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $webhookId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$webhook = $project -> search ( '$id' , $webhookId , $project -> getAttribute ( 'webhooks' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $webhook ) || ! $webhook instanceof Document ) {
throw new Exception ( 'Webhook not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $consoleDB -> deleteDocument ( $webhook -> getId ())) {
throw new Exception ( 'Failed to remove webhook from DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
$response -> noContent ();
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
// Keys
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/projects/:projectId/keys' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Create Key' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.method' , 'createKey' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_KEY )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Key name. Max length: 128 chars.' )
2021-01-01 19:42:50 +13:00
-> param ( 'scopes' , null , new ArrayList ( new WhiteList ( array_keys ( Config :: getParam ( 'scopes' )), true )), 'Key scopes list.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $name , $scopes , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$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 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project -> setAttribute ( 'keys' , $key , Document :: SET_TYPE_APPEND );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $key , Response :: MODEL_KEY )
;
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/keys' )
2020-02-01 11:34:07 +13:00
-> desc ( 'List Keys' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listKeys' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_KEY_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
$project = $consoleDB -> getDocument ( $projectId );
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
2020-02-01 11:34:07 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$keys = $project -> getAttribute ( 'keys' , []);
$response -> dynamic ( new Document ([
'sum' => count ( $keys ),
'keys' => $keys
]), Response :: MODEL_KEY_LIST );
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/keys/:keyId' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Get Key' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getKey' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_KEY )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'keyId' , null , new UID (), 'Key unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $keyId , $response , $consoleDB ) {
$project = $consoleDB -> getDocument ( $projectId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$key = $project -> search ( '$id' , $keyId , $project -> getAttribute ( 'keys' , []));
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $key ) || ! $key instanceof Document ) {
throw new Exception ( 'Key not found' , 404 );
2020-02-01 11:34:07 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $key , Response :: MODEL_KEY );
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/projects/:projectId/keys/:keyId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Update Key' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateKey' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_KEY )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'keyId' , null , new UID (), 'Key unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Key name. Max length: 128 chars.' )
2021-01-01 19:42:50 +13:00
-> param ( 'scopes' , null , new ArrayList ( new WhiteList ( array_keys ( Config :: getParam ( 'scopes' )), true )), 'Key scopes list' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $keyId , $name , $scopes , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$key = $project -> search ( '$id' , $keyId , $project -> getAttribute ( 'keys' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $key ) || ! $key instanceof Document ) {
throw new Exception ( 'Key not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$key
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'scopes' , $scopes )
;
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $consoleDB -> updateDocument ( $key -> getArrayCopy ())) {
throw new Exception ( 'Failed saving key to DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $key , Response :: MODEL_KEY );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/projects/:projectId/keys/:keyId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Delete Key' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteKey' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'keyId' , null , new UID (), 'Key unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $keyId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$key = $project -> search ( '$id' , $keyId , $project -> getAttribute ( 'keys' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $key ) || ! $key instanceof Document ) {
throw new Exception ( 'Key not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $consoleDB -> deleteDocument ( $key -> getId ())) {
throw new Exception ( 'Failed to remove key from DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
$response -> noContent ();
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
// Tasks
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/projects/:projectId/tasks' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Create Task' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createTask' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TASK )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Task name. Max length: 128 chars.' )
-> param ( 'status' , null , new WhiteList ([ 'play' , 'pause' ], true ), 'Task status.' )
-> param ( 'schedule' , null , new Cron (), 'Task schedule CRON syntax.' )
-> param ( 'security' , false , new Boolean ( true ), 'Certificate verification, false for disabled or true for enabled.' )
-> param ( 'httpMethod' , '' , new WhiteList ([ 'GET' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' , 'HEAD' , 'OPTIONS' , 'TRACE' , 'CONNECT' ], true ), 'Task HTTP method.' )
-> param ( 'httpUrl' , '' , new URL (), 'Task HTTP URL' )
-> param ( 'httpHeaders' , null , new ArrayList ( new Text ( 256 )), 'Task HTTP headers list.' , true )
-> param ( 'httpUser' , '' , new Text ( 256 ), 'Task HTTP user. Max length: 256 chars.' , true )
-> param ( 'httpPass' , '' , new Text ( 256 ), 'Task HTTP password. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $name , $status , $schedule , $security , $httpMethod , $httpUrl , $httpHeaders , $httpUser , $httpPass , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2021-02-22 10:37:22 +13:00
$cron = new CronExpression ( $schedule );
2020-07-01 03:46:42 +12:00
$next = ( $status == 'play' ) ? $cron -> getNextRunDate () -> format ( 'U' ) : null ;
$security = ( $security === '1' || $security === 'true' || $security === 1 || $security === true );
$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 ,
2020-07-09 21:11:42 +12:00
'security' => $security ,
2020-07-01 03:46:42 +12:00
'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 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project -> setAttribute ( 'tasks' , $task , Document :: SET_TYPE_APPEND );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( $next ) {
ResqueScheduler :: enqueueAt ( $next , 'v1-tasks' , 'TasksV1' , $task -> getArrayCopy ());
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $task , Response :: MODEL_TASK )
;
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/tasks' )
2020-02-01 11:34:07 +13:00
-> desc ( 'List Tasks' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listTasks' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TASK_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$tasks = $project -> getAttribute ( 'tasks' , []);
2020-02-01 11:34:07 +13:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( new Document ([
'sum' => count ( $tasks ),
'tasks' => $tasks
]), Response :: MODEL_TASK_LIST );
2020-09-13 02:36:28 +12:00
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/tasks/:taskId' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Get Task' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getTask' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TASK )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'taskId' , null , new UID (), 'Task unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $taskId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$task = $project -> search ( '$id' , $taskId , $project -> getAttribute ( 'tasks' , []));
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $task ) || ! $task instanceof Document ) {
throw new Exception ( 'Task not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $task , Response :: MODEL_TASK );
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/projects/:projectId/tasks/:taskId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Update Task' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updateTask' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TASK )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'taskId' , null , new UID (), 'Task unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Task name. Max length: 128 chars.' )
-> param ( 'status' , null , new WhiteList ([ 'play' , 'pause' ], true ), 'Task status.' )
-> param ( 'schedule' , null , new Cron (), 'Task schedule CRON syntax.' )
-> param ( 'security' , false , new Boolean ( true ), 'Certificate verification, false for disabled or true for enabled.' )
-> param ( 'httpMethod' , '' , new WhiteList ([ 'GET' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' , 'HEAD' , 'OPTIONS' , 'TRACE' , 'CONNECT' ], true ), 'Task HTTP method.' )
-> param ( 'httpUrl' , '' , new URL (), 'Task HTTP URL.' )
-> param ( 'httpHeaders' , null , new ArrayList ( new Text ( 256 )), 'Task HTTP headers list.' , true )
-> param ( 'httpUser' , '' , new Text ( 256 ), 'Task HTTP user. Max length: 256 chars.' , true )
-> param ( 'httpPass' , '' , new Text ( 256 ), 'Task HTTP password. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $taskId , $name , $status , $schedule , $security , $httpMethod , $httpUrl , $httpHeaders , $httpUser , $httpPass , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$task = $project -> search ( '$id' , $taskId , $project -> getAttribute ( 'tasks' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $task ) || ! $task instanceof Document ) {
throw new Exception ( 'Task not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2021-02-22 10:37:22 +13:00
$cron = new CronExpression ( $schedule );
2020-07-01 03:46:42 +12:00
$next = ( $status == 'play' ) ? $cron -> getNextRunDate () -> format ( 'U' ) : null ;
$security = ( $security === '1' || $security === 'true' || $security === 1 || $security === true );
$task
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'status' , $status )
-> setAttribute ( 'schedule' , $schedule )
-> setAttribute ( 'updated' , \time ())
-> setAttribute ( 'next' , $next )
2020-07-09 21:11:42 +12:00
-> setAttribute ( 'security' , $security )
2020-07-01 03:46:42 +12:00
-> 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 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( $next ) {
ResqueScheduler :: enqueueAt ( $next , 'v1-tasks' , 'TasksV1' , $task -> getArrayCopy ());
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $task , Response :: MODEL_TASK );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/projects/:projectId/tasks/:taskId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Delete Task' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteTask' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'taskId' , null , new UID (), 'Task unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $taskId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$task = $project -> search ( '$id' , $taskId , $project -> getAttribute ( 'tasks' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $task ) || ! $task instanceof Document ) {
throw new Exception ( 'Task not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $consoleDB -> deleteDocument ( $task -> getId ())) {
throw new Exception ( 'Failed to remove tasks from DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
$response -> noContent ();
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
// Platforms
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/projects/:projectId/platforms' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Create Platform' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createPlatform' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PLATFORM )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'type' , null , new WhiteList ([ 'web' , 'flutter-ios' , 'flutter-android' , 'ios' , 'android' , 'unity' ], true ), 'Platform type.' )
-> param ( 'name' , null , new Text ( 128 ), 'Platform name. Max length: 128 chars.' )
-> param ( 'key' , '' , new Text ( 256 ), 'Package name for android or bundle ID for iOS. Max length: 256 chars.' , true )
-> param ( 'store' , '' , new Text ( 256 ), 'App store or Google Play store ID. Max length: 256 chars.' , true )
-> param ( 'hostname' , '' , new Text ( 256 ), 'Platform client hostname. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $type , $name , $key , $store , $hostname , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$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 ,
'hostname' => $hostname ,
'dateCreated' => \time (),
'dateUpdated' => \time (),
]);
if ( false === $platform ) {
throw new Exception ( 'Failed saving platform to DB' , 500 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project -> setAttribute ( 'platforms' , $platform , Document :: SET_TYPE_APPEND );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $platform , Response :: MODEL_PLATFORM )
;
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/platforms' )
2020-02-01 11:34:07 +13:00
-> desc ( 'List Platforms' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listPlatforms' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PLATFORM_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
2020-02-01 11:34:07 +13:00
}
2020-07-01 03:46:42 +12:00
$platforms = $project -> getAttribute ( 'platforms' , []);
2020-08-15 09:56:50 +12:00
$response -> dynamic ( new Document ([
'sum' => count ( $platforms ),
'platforms' => $platforms
]), Response :: MODEL_PLATFORM_LIST );
2020-12-27 05:33:36 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/platforms/:platformId' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Get Platform' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getPlatform' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PLATFORM )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'platformId' , null , new UID (), 'Platform unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $platformId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
$platform = $project -> search ( '$id' , $platformId , $project -> getAttribute ( 'platforms' , []));
2020-02-01 11:34:07 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $platform ) || ! $platform instanceof Document ) {
throw new Exception ( 'Platform not found' , 404 );
2020-02-01 11:34:07 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $platform , Response :: MODEL_PLATFORM );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/projects/:projectId/platforms/:platformId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Update Platform' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'updatePlatform' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PLATFORM )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'platformId' , null , new UID (), 'Platform unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Platform name. Max length: 128 chars.' )
-> param ( 'key' , '' , new Text ( 256 ), 'Package name for android or bundle ID for iOS. Max length: 256 chars.' , true )
-> param ( 'store' , '' , new Text ( 256 ), 'App store or Google Play store ID. Max length: 256 chars.' , true )
-> param ( 'hostname' , '' , new Text ( 256 ), 'Platform client URL. Max length: 256 chars.' , true )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $platformId , $name , $key , $store , $hostname , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$platform = $project -> search ( '$id' , $platformId , $project -> getAttribute ( 'platforms' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $platform ) || ! $platform instanceof Document ) {
throw new Exception ( 'Platform not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$platform
-> setAttribute ( 'name' , $name )
-> setAttribute ( 'dateUpdated' , \time ())
-> setAttribute ( 'key' , $key )
-> setAttribute ( 'store' , $store )
-> setAttribute ( 'hostname' , $hostname )
;
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $consoleDB -> updateDocument ( $platform -> getArrayCopy ())) {
throw new Exception ( 'Failed saving platform to DB' , 500 );
2019-12-18 23:24:54 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $platform , Response :: MODEL_PLATFORM );
2020-12-27 05:33:36 +13:00
});
2019-12-18 23:24:54 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/projects/:projectId/platforms/:platformId' )
2019-12-18 23:24:54 +13:00
-> desc ( 'Delete Platform' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2019-12-18 23:24:54 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2019-12-18 23:24:54 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deletePlatform' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'platformId' , null , new UID (), 'Platform unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $platformId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
$platform = $project -> search ( '$id' , $platformId , $project -> getAttribute ( 'platforms' , []));
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $platform ) || ! $platform instanceof Document ) {
throw new Exception ( 'Platform not found' , 404 );
}
2019-12-18 23:24:54 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $consoleDB -> deleteDocument ( $platform -> getId ())) {
throw new Exception ( 'Failed to remove platform from DB' , 500 );
2019-12-15 08:33:29 +13:00
}
2020-07-01 03:46:42 +12:00
$response -> noContent ();
2020-12-27 05:33:36 +13:00
});
2020-02-22 21:10:30 +13:00
// Domains
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/projects/:projectId/domains' )
2020-02-22 21:10:30 +13:00
-> desc ( 'Create Domain' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-22 21:10:30 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-22 21:10:30 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'createDomain' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_DOMAIN )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'domain' , null , new DomainValidator (), 'Domain name.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $domain , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-24 06:05:42 +13:00
2020-07-01 03:46:42 +12:00
$document = $project -> search ( 'domain' , $domain , $project -> getAttribute ( 'domains' , []));
2020-02-24 06:05:42 +13:00
2020-07-01 03:46:42 +12:00
if ( ! empty ( $document )) {
throw new Exception ( 'Domain already exists' , 409 );
}
2020-02-23 21:59:29 +13:00
2020-07-01 03:46:42 +12:00
$target = new Domain ( App :: getEnv ( '_APP_DOMAIN_TARGET' , '' ));
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $target -> isKnown () || $target -> isTest ()) {
2021-02-03 03:48:07 +13:00
throw new Exception ( 'Unreachable CNAME target (' . $target -> get () . '), please use a domain with a public suffix.' , 500 );
2020-07-01 03:46:42 +12:00
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$domain = new Domain ( $domain );
$domain = $consoleDB -> createDocument ([
'$collection' => Database :: SYSTEM_COLLECTION_DOMAINS ,
'$permissions' => [
'read' => [ 'team:' . $project -> getAttribute ( 'teamId' , null )],
'write' => [ 'team:' . $project -> getAttribute ( 'teamId' , null ) . '/owner' , 'team:' . $project -> getAttribute ( 'teamId' , null ) . '/developer' ],
],
'updated' => \time (),
'domain' => $domain -> get (),
'tld' => $domain -> getSuffix (),
'registerable' => $domain -> getRegisterable (),
'verification' => false ,
'certificateId' => null ,
]);
if ( false === $domain ) {
throw new Exception ( 'Failed saving domain to DB' , 500 );
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project -> setAttribute ( 'domains' , $domain , Document :: SET_TYPE_APPEND );
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> updateDocument ( $project -> getArrayCopy ());
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $project ) {
throw new Exception ( 'Failed saving project to DB' , 500 );
2020-02-22 21:10:30 +13:00
}
2020-07-01 03:46:42 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $domain , Response :: MODEL_DOMAIN )
;
2020-12-27 05:33:36 +13:00
});
2020-02-22 21:10:30 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/domains' )
2020-02-22 21:10:30 +13:00
-> desc ( 'List Domains' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-22 21:10:30 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-22 21:10:30 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'listDomains' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_DOMAIN_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , '' , new UID (), 'Project unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
2020-02-22 21:10:30 +13:00
}
2020-07-01 03:46:42 +12:00
$domains = $project -> getAttribute ( 'domains' , []);
2020-08-15 09:56:50 +12:00
$response -> dynamic ( new Document ([
'sum' => count ( $domains ),
'domains' => $domains
]), Response :: MODEL_DOMAIN_LIST );
2020-12-27 05:33:36 +13:00
});
2020-02-22 21:10:30 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/projects/:projectId/domains/:domainId' )
2020-02-22 21:10:30 +13:00
-> desc ( 'Get Domain' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-22 21:10:30 +13:00
-> label ( 'scope' , 'projects.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-22 21:10:30 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'getDomain' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_DOMAIN )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'domainId' , null , new UID (), 'Domain unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $domainId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$domain = $project -> search ( '$id' , $domainId , $project -> getAttribute ( 'domains' , []));
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $domain ) || ! $domain instanceof Document ) {
throw new Exception ( 'Domain not found' , 404 );
2020-02-22 21:10:30 +13:00
}
2020-07-01 03:46:42 +12:00
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $domain , Response :: MODEL_DOMAIN );
2020-12-27 05:33:36 +13:00
});
2020-02-22 21:10:30 +13:00
2020-06-29 05:31:21 +12:00
App :: patch ( '/v1/projects/:projectId/domains/:domainId/verification' )
2020-02-22 21:10:30 +13:00
-> desc ( 'Update Domain Verification Status' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-22 21:10:30 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-22 21:10:30 +13:00
-> label ( 'sdk.namespace' , 'projects' )
2020-02-23 21:59:29 +13:00
-> label ( 'sdk.method' , 'updateDomainVerification' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_DOMAIN )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'domainId' , null , new UID (), 'Domain unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2020-07-01 03:46:42 +12:00
-> action ( function ( $projectId , $domainId , $response , $consoleDB ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$domain = $project -> search ( '$id' , $domainId , $project -> getAttribute ( 'domains' , []));
2020-02-23 21:59:29 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $domain ) || ! $domain instanceof Document ) {
throw new Exception ( 'Domain not found' , 404 );
}
2020-02-23 21:59:29 +13:00
2020-07-01 03:46:42 +12:00
$target = new Domain ( App :: getEnv ( '_APP_DOMAIN_TARGET' , '' ));
2020-02-23 22:10:32 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $target -> isKnown () || $target -> isTest ()) {
2021-02-03 03:48:07 +13:00
throw new Exception ( 'Unreachable CNAME target (' . $target -> get () . '), please use a domain with a public suffix.' , 500 );
2020-07-01 03:46:42 +12:00
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( $domain -> getAttribute ( 'verification' ) === true ) {
2020-08-15 09:56:50 +12:00
return $response -> dynamic ( $domain , Response :: MODEL_DOMAIN );
2020-07-01 03:46:42 +12:00
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
// Verify Domain with DNS records
$validator = new CNAME ( $target -> get ());
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( ! $validator -> isValid ( $domain -> getAttribute ( 'domain' , '' ))) {
throw new Exception ( 'Failed to verify domain' , 401 );
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$domain
-> setAttribute ( 'verification' , true )
;
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( false === $consoleDB -> updateDocument ( $domain -> getArrayCopy ())) {
throw new Exception ( 'Failed saving domains to DB' , 500 );
2020-02-22 21:10:30 +13:00
}
2020-07-01 03:46:42 +12:00
// Issue a TLS certificate when domain is verified
Resque :: enqueue ( 'v1-certificates' , 'CertificatesV1' , [
'document' => $domain -> getArrayCopy (),
'domain' => $domain -> getAttribute ( 'domain' ),
]);
2020-08-15 09:56:50 +12:00
$response -> dynamic ( $domain , Response :: MODEL_DOMAIN );
2020-12-27 05:33:36 +13:00
});
2020-02-22 21:10:30 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/projects/:projectId/domains/:domainId' )
2020-02-22 21:10:30 +13:00
-> desc ( 'Delete Domain' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'projects' ])
2020-02-22 21:10:30 +13:00
-> label ( 'scope' , 'projects.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
2020-02-22 21:10:30 +13:00
-> label ( 'sdk.namespace' , 'projects' )
-> label ( 'sdk.method' , 'deleteDomain' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'projectId' , null , new UID (), 'Project unique ID.' )
-> param ( 'domainId' , null , new UID (), 'Domain unique ID.' )
2020-12-27 05:33:36 +13:00
-> inject ( 'response' )
-> inject ( 'consoleDB' )
2021-02-05 22:05:46 +13:00
-> inject ( 'deletes' )
-> action ( function ( $projectId , $domainId , $response , $consoleDB , $deletes ) {
2020-08-15 09:56:50 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 03:46:42 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$project = $consoleDB -> getDocument ( $projectId );
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $project -> getId ()) || Database :: SYSTEM_COLLECTION_PROJECTS != $project -> getCollection ()) {
throw new Exception ( 'Project not found' , 404 );
}
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
$domain = $project -> search ( '$id' , $domainId , $project -> getAttribute ( 'domains' , []));
2020-02-22 21:10:30 +13:00
2020-07-01 03:46:42 +12:00
if ( empty ( $domain ) || ! $domain instanceof Document ) {
throw new Exception ( 'Domain not found' , 404 );
}
2020-02-22 21:10:30 +13:00
2021-02-05 22:05:46 +13:00
if ( $consoleDB -> deleteDocument ( $domain -> getId ())) {
$deletes
2021-02-05 23:57:43 +13:00
-> setParam ( 'type' , DELETE_TYPE_CERTIFICATES )
2021-02-05 22:05:46 +13:00
-> setParam ( 'document' , $domain )
;
} else {
2020-07-01 03:46:42 +12:00
throw new Exception ( 'Failed to remove domains from DB' , 500 );
2020-02-22 21:10:30 +13:00
}
2020-07-01 03:46:42 +12:00
$response -> noContent ();
2020-12-27 05:33:36 +13:00
});