2020-10-29 13:07:56 +00:00
< ? php
namespace Appwrite\Utopia\Response\Model ;
2022-11-15 10:19:35 +00:00
use Appwrite\Auth\Auth ;
2020-10-29 13:07:56 +00:00
use Appwrite\Utopia\Response ;
use Appwrite\Utopia\Response\Model ;
use Utopia\Config\Config ;
2021-07-31 19:07:05 +00:00
use Utopia\Database\Document ;
2020-10-29 13:07:56 +00:00
class Project extends Model
{
2020-11-12 05:12:14 +00:00
/**
* @ var bool
*/
2022-08-01 10:22:04 +00:00
protected bool $public = false ;
2022-05-23 14:54:50 +00:00
2020-10-29 13:07:56 +00:00
public function __construct ()
{
$this
-> addRule ( '$id' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Project ID.' ,
2021-01-13 15:06:36 +00:00
'default' => '' ,
2020-10-29 13:07:56 +00:00
'example' => '5e5ea5c16897e' ,
])
2022-06-15 12:46:52 +00:00
-> addRule ( '$createdAt' , [
2022-07-04 09:55:11 +00:00
'type' => self :: TYPE_DATETIME ,
2022-09-04 21:26:16 +00:00
'description' => 'Project creation date in ISO 8601 format.' ,
2022-07-04 09:55:11 +00:00
'default' => '' ,
2022-08-14 10:27:07 +00:00
'example' => self :: TYPE_DATETIME_EXAMPLE ,
2022-06-15 12:46:52 +00:00
])
-> addRule ( '$updatedAt' , [
2022-07-04 09:55:11 +00:00
'type' => self :: TYPE_DATETIME ,
2022-09-04 21:26:16 +00:00
'description' => 'Project update date in ISO 8601 format.' ,
2022-07-04 09:55:11 +00:00
'default' => '' ,
2022-08-14 10:27:07 +00:00
'example' => self :: TYPE_DATETIME_EXAMPLE ,
2022-06-15 12:46:52 +00:00
])
2020-10-29 13:07:56 +00:00
-> addRule ( 'name' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Project name.' ,
'default' => '' ,
'example' => 'New Project' ,
])
-> addRule ( 'description' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Project description.' ,
'default' => '' ,
'example' => 'This is a new project.' ,
])
-> addRule ( 'teamId' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Project team ID.' ,
2021-01-13 15:06:36 +00:00
'default' => '' ,
2020-10-29 13:07:56 +00:00
'example' => '1592981250' ,
])
-> addRule ( 'logo' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Project logo file ID.' ,
'default' => '' ,
'example' => '5f5c451b403cb' ,
])
-> addRule ( 'url' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Project website URL.' ,
'default' => '' ,
'example' => '5f5c451b403cb' ,
])
-> addRule ( 'legalName' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Company legal name.' ,
'default' => '' ,
'example' => 'Company LTD.' ,
])
-> addRule ( 'legalCountry' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Country code in [ISO 3166-1](http://en.wikipedia.org/wiki/ISO_3166-1) two-character format.' ,
'default' => '' ,
'example' => 'US' ,
])
-> addRule ( 'legalState' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'State name.' ,
'default' => '' ,
'example' => 'New York' ,
])
-> addRule ( 'legalCity' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'City name.' ,
'default' => '' ,
'example' => 'New York City.' ,
])
-> addRule ( 'legalAddress' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Company Address.' ,
'default' => '' ,
'example' => '620 Eighth Avenue, New York, NY 10018' ,
])
-> addRule ( 'legalTaxId' , [
2020-11-07 22:14:48 +00:00
'type' => self :: TYPE_STRING ,
2020-10-29 13:07:56 +00:00
'description' => 'Company Tax ID.' ,
'default' => '' ,
'example' => '131102020' ,
2022-10-31 14:54:15 +00:00
])
2022-11-01 14:43:18 +00:00
-> addRule ( 'authDuration' , [
2022-11-15 10:19:35 +00:00
'type' => self :: TYPE_INTEGER ,
2022-11-04 14:48:29 +00:00
'description' => 'Session duration in seconds.' ,
2022-11-15 10:19:35 +00:00
'default' => Auth :: TOKEN_EXPIRATION_LOGIN_LONG ,
'example' => 60 ,
2020-10-29 13:07:56 +00:00
])
2021-08-06 08:34:17 +00:00
-> addRule ( 'authLimit' , [
2021-06-03 06:54:41 +00:00
'type' => self :: TYPE_INTEGER ,
2021-02-28 11:40:59 +00:00
'description' => 'Max users allowed. 0 is unlimited.' ,
'default' => 0 ,
'example' => 100 ,
])
2022-12-11 07:15:18 +00:00
-> addRule ( 'authSessionsLimit' , [
'type' => self :: TYPE_INTEGER ,
'description' => 'Max sessions allowed per user. 100 maximum.' ,
2022-12-13 06:26:25 +00:00
'default' => 10 ,
2022-12-11 07:15:18 +00:00
'example' => 10 ,
])
2022-12-18 06:27:41 +00:00
-> addRule ( 'authPasswordHistory' , [
'type' => self :: TYPE_INTEGER ,
2022-12-19 05:09:56 +00:00
'description' => 'Max allowed passwords in the history list per user. Max passwords limit allowed in history is 20. Use 0 for disabling password history.' ,
2022-12-18 06:27:41 +00:00
'default' => 0 ,
'example' => 5 ,
])
2022-08-19 09:21:51 +00:00
-> addRule ( 'providers' , [
'type' => Response :: MODEL_PROVIDER ,
'description' => 'List of Providers.' ,
'default' => [],
'example' => new \stdClass (),
'array' => true ,
])
2020-10-29 13:07:56 +00:00
-> addRule ( 'platforms' , [
'type' => Response :: MODEL_PLATFORM ,
'description' => 'List of Platforms.' ,
'default' => [],
2022-05-23 14:54:50 +00:00
'example' => new \stdClass (),
2020-10-29 13:07:56 +00:00
'array' => true ,
])
-> addRule ( 'webhooks' , [
'type' => Response :: MODEL_WEBHOOK ,
'description' => 'List of Webhooks.' ,
'default' => [],
2022-05-23 14:54:50 +00:00
'example' => new \stdClass (),
2020-10-29 13:07:56 +00:00
'array' => true ,
])
-> addRule ( 'keys' , [
'type' => Response :: MODEL_KEY ,
'description' => 'List of API Keys.' ,
'default' => [],
2022-05-23 14:54:50 +00:00
'example' => new \stdClass (),
2020-10-29 13:07:56 +00:00
'array' => true ,
])
-> addRule ( 'domains' , [
'type' => Response :: MODEL_DOMAIN ,
'description' => 'List of Domains.' ,
'default' => [],
2022-05-23 14:54:50 +00:00
'example' => new \stdClass (),
2020-10-29 13:07:56 +00:00
'array' => true ,
])
;
2021-07-31 04:58:33 +00:00
$services = Config :: getParam ( 'services' , []);
2021-02-28 11:40:59 +00:00
$auth = Config :: getParam ( 'auth' , []);
2020-10-29 13:07:56 +00:00
2021-02-28 11:40:59 +00:00
foreach ( $auth as $index => $method ) {
$name = $method [ 'name' ] ? ? '' ;
$key = $method [ 'key' ] ? ? '' ;
$this
2021-08-06 08:01:53 +00:00
-> addRule ( 'auth' . ucfirst ( $key ), [
2021-02-28 11:40:59 +00:00
'type' => self :: TYPE_BOOLEAN ,
2022-05-23 14:54:50 +00:00
'description' => $name . ' auth method status' ,
2021-02-28 11:40:59 +00:00
'example' => true ,
'default' => true ,
])
;
}
2021-07-31 04:58:33 +00:00
2021-07-31 19:07:05 +00:00
foreach ( $services as $service ) {
2022-05-23 14:54:50 +00:00
if ( ! $service [ 'optional' ]) {
2021-07-31 19:07:05 +00:00
continue ;
}
$name = $service [ 'name' ] ? ? '' ;
$key = $service [ 'key' ] ? ? '' ;
2021-07-31 04:58:33 +00:00
$this
2022-05-23 14:54:50 +00:00
-> addRule ( 'serviceStatusFor' . ucfirst ( $key ), [
2021-07-31 04:58:33 +00:00
'type' => self :: TYPE_BOOLEAN ,
2022-05-23 14:54:50 +00:00
'description' => $name . ' service status' ,
2021-07-31 04:58:33 +00:00
'example' => true ,
'default' => true ,
])
;
}
2020-10-29 13:07:56 +00:00
}
/**
* Get Name
2021-10-06 14:22:38 +00:00
*
2020-10-29 13:07:56 +00:00
* @ return string
*/
2022-05-23 14:54:50 +00:00
public function getName () : string
2020-10-29 13:07:56 +00:00
{
return 'Project' ;
}
/**
2021-12-15 10:19:29 +00:00
* Get Type
2021-10-06 14:22:38 +00:00
*
2020-10-29 13:07:56 +00:00
* @ return string
*/
2022-05-23 14:54:50 +00:00
public function getType () : string
2020-10-29 13:07:56 +00:00
{
return Response :: MODEL_PROJECT ;
}
2021-10-25 14:51:39 +00:00
/**
* Get Collection
2022-05-23 14:54:50 +00:00
*
2021-10-25 14:51:39 +00:00
* @ return string
*/
public function filter ( Document $document ) : Document
{
2022-08-19 09:21:51 +00:00
// Services
2021-10-25 14:51:39 +00:00
$values = $document -> getAttribute ( 'services' , []);
$services = Config :: getParam ( 'services' , []);
2022-05-23 14:54:50 +00:00
foreach ( $services as $service ) {
if ( ! $service [ 'optional' ]) {
2021-10-25 14:51:39 +00:00
continue ;
}
$key = $service [ 'key' ] ? ? '' ;
$value = $values [ $key ] ? ? true ;
2022-05-23 14:54:50 +00:00
$document -> setAttribute ( 'serviceStatusFor' . ucfirst ( $key ), $value );
2021-10-25 14:51:39 +00:00
}
2022-08-19 09:21:51 +00:00
// Auth
2022-05-23 14:54:50 +00:00
$authValues = $document -> getAttribute ( 'auths' , []);
2021-10-25 14:51:39 +00:00
$auth = Config :: getParam ( 'auth' , []);
$document -> setAttribute ( 'authLimit' , $authValues [ 'limit' ] ? ? 0 );
2022-11-15 10:19:35 +00:00
$document -> setAttribute ( 'authDuration' , $authValues [ 'duration' ] ? ? Auth :: TOKEN_EXPIRATION_LOGIN_LONG );
2023-01-16 09:46:53 +00:00
$document -> setAttribute ( 'authSessionsLimit' , $authValues [ 'maxSessions' ] ? ? APP_LIMIT_USER_SESSIONS_DEFAULT );
2022-12-18 06:27:41 +00:00
$document -> setAttribute ( 'authPasswordHistory' , $authValues [ 'passwordHistory' ] ? ? 0 );
2021-10-25 14:51:39 +00:00
foreach ( $auth as $index => $method ) {
$key = $method [ 'key' ];
$value = $authValues [ $key ] ? ? true ;
$document -> setAttribute ( 'auth' . ucfirst ( $key ), $value );
}
2022-08-19 09:21:51 +00:00
// Providers
2021-10-25 14:51:39 +00:00
$providers = Config :: getParam ( 'providers' , []);
2022-04-14 09:54:29 +00:00
$providerValues = $document -> getAttribute ( 'authProviders' , []);
2022-08-19 09:21:51 +00:00
$projectProviders = [];
2021-10-25 14:51:39 +00:00
foreach ( $providers as $key => $provider ) {
if ( ! $provider [ 'enabled' ]) {
2022-08-19 09:21:51 +00:00
// Disabled by Appwrite configuration, exclude from response
2021-10-25 14:51:39 +00:00
continue ;
}
2022-08-19 09:21:51 +00:00
$projectProviders [] = new Document ([
'name' => ucfirst ( $key ),
'appId' => $providerValues [ $key . 'Appid' ] ? ? '' ,
'secret' => $providerValues [ $key . 'Secret' ] ? ? '' ,
'enabled' => $providerValues [ $key . 'Enabled' ] ? ? false ,
]);
2021-10-25 14:51:39 +00:00
}
2022-08-19 09:21:51 +00:00
$document -> setAttribute ( " providers " , $projectProviders );
2021-10-25 14:51:39 +00:00
return $document ;
}
2021-10-06 14:22:38 +00:00
}