appwrite/src/Appwrite/Utopia/Response/Model/Project.php

360 lines
13 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Utopia\Response\Model;
2022-11-15 10:19:35 +00:00
use Appwrite\Auth\Auth;
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;
class Project extends Model
{
/**
* @var bool
*/
2022-08-01 10:22:04 +00:00
protected bool $public = false;
2022-05-23 14:54:50 +00:00
public function __construct()
{
$this
->addRule('$id', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Project ID.',
2021-01-13 15:06:36 +00:00
'default' => '',
'example' => '5e5ea5c16897e',
])
->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,
])
->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,
])
->addRule('name', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Project name.',
'default' => '',
'example' => 'New Project',
])
->addRule('description', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Project description.',
'default' => '',
'example' => 'This is a new project.',
])
->addRule('teamId', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Project team ID.',
2021-01-13 15:06:36 +00:00
'default' => '',
'example' => '1592981250',
])
->addRule('logo', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Project logo file ID.',
'default' => '',
'example' => '5f5c451b403cb',
])
->addRule('url', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Project website URL.',
'default' => '',
'example' => '5f5c451b403cb',
])
->addRule('legalName', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'Company legal name.',
'default' => '',
'example' => 'Company LTD.',
])
->addRule('legalCountry', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'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,
'description' => 'State name.',
'default' => '',
'example' => 'New York',
])
->addRule('legalCity', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'description' => 'City name.',
'default' => '',
'example' => 'New York City.',
])
->addRule('legalAddress', [
2020-11-07 22:14:48 +00:00
'type' => self::TYPE_STRING,
'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,
'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,
])
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,
'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-12-26 05:58:16 +00:00
->addRule('authPasswordDictionary', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether or not to check user\'s password against most commonly used passwords.',
'default' => false,
'example' => true,
])
2023-07-19 22:24:32 +00:00
->addRule('authPersonalDataCheck', [
2023-04-11 23:01:50 +00:00
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether or not to check the user password for similarity with their personal data.',
'default' => false,
'example' => true,
])
2024-06-21 18:21:05 +00:00
->addRule('authSessionEmails', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether or not to send session emails to users.',
'default' => false,
'example' => true,
])
2023-10-25 17:33:23 +00:00
->addRule('oAuthProviders', [
2023-08-07 10:49:55 +00:00
'type' => Response::MODEL_AUTH_PROVIDER,
'description' => 'List of Auth Providers.',
'default' => [],
'example' => [new \stdClass()],
'array' => true,
])
->addRule('platforms', [
'type' => Response::MODEL_PLATFORM,
'description' => 'List of Platforms.',
'default' => [],
2022-05-23 14:54:50 +00:00
'example' => new \stdClass(),
'array' => true,
])
->addRule('webhooks', [
'type' => Response::MODEL_WEBHOOK,
'description' => 'List of Webhooks.',
'default' => [],
2022-05-23 14:54:50 +00:00
'example' => new \stdClass(),
'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(),
'array' => true,
])
2023-03-09 01:59:17 +00:00
->addRule('smtpEnabled', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Status for custom SMTP',
'default' => false,
'example' => false,
'array' => false
])
->addRule('smtpSenderName', [
'type' => self::TYPE_STRING,
'description' => 'SMTP sender name',
'default' => '',
'example' => 'John Appwrite',
])
->addRule('smtpSenderEmail', [
2023-03-09 01:59:17 +00:00
'type' => self::TYPE_STRING,
'description' => 'SMTP sender email',
'default' => '',
'example' => 'john@appwrite.io',
])
->addRule('smtpReplyTo', [
'type' => self::TYPE_STRING,
'description' => 'SMTP reply to email',
'default' => '',
'example' => 'support@appwrite.io',
])
2023-03-09 01:59:17 +00:00
->addRule('smtpHost', [
'type' => self::TYPE_STRING,
'description' => 'SMTP server host name',
'default' => '',
'example' => 'mail.appwrite.io',
])
->addRule('smtpPort', [
'type' => self::TYPE_INTEGER,
'description' => 'SMTP server port',
'default' => '',
'example' => 25,
])
->addRule('smtpUsername', [
'type' => self::TYPE_STRING,
'description' => 'SMTP server username',
'default' => '',
'example' => 'emailuser',
])
->addRule('smtpPassword', [
'type' => self::TYPE_STRING,
'description' => 'SMTP server password',
'default' => '',
'example' => 'securepassword',
])
->addRule('smtpSecure', [
'type' => self::TYPE_STRING,
'description' => 'SMTP server secure protocol',
'default' => '',
'example' => 'tls',
2024-06-21 18:21:05 +00:00
]);
2021-07-31 04:58:33 +00:00
$services = Config::getParam('services', []);
2021-02-28 11:40:59 +00:00
$auth = Config::getParam('auth', []);
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,
2024-06-21 18:21:05 +00:00
]);
2021-02-28 11:40:59 +00:00
}
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,
2024-06-21 18:21:05 +00:00
]);
2021-07-31 04:58:33 +00:00
}
}
/**
* Get Name
*
* @return string
*/
2022-05-23 14:54:50 +00:00
public function getName(): string
{
return 'Project';
}
/**
2021-12-15 10:19:29 +00:00
* Get Type
*
* @return string
*/
2022-05-23 14:54:50 +00:00
public function getType(): string
{
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
{
2023-03-09 01:59:17 +00:00
// SMTP
$smtp = $document->getAttribute('smtp', []);
$document->setAttribute('smtpEnabled', $smtp['enabled'] ?? false);
$document->setAttribute('smtpSenderEmail', $smtp['senderEmail'] ?? '');
$document->setAttribute('smtpSenderName', $smtp['senderName'] ?? '');
$document->setAttribute('smtpReplyTo', $smtp['replyTo'] ?? '');
2023-03-09 01:59:17 +00:00
$document->setAttribute('smtpHost', $smtp['host'] ?? '');
$document->setAttribute('smtpPort', $smtp['port'] ?? '');
$document->setAttribute('smtpUsername', $smtp['username'] ?? '');
$document->setAttribute('smtpPassword', $smtp['password'] ?? '');
$document->setAttribute('smtpSecure', $smtp['secure'] ?? '');
// 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
}
// 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);
$document->setAttribute('authPasswordDictionary', $authValues['passwordDictionary'] ?? false);
2023-07-19 22:24:32 +00:00
$document->setAttribute('authPersonalDataCheck', $authValues['personalDataCheck'] ?? false);
2024-06-21 18:21:05 +00:00
$document->setAttribute('authSessionEmails', $authValues['sessionEmails'] ?? false);
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);
}
2023-10-25 17:33:23 +00:00
// OAuth Providers
$providers = Config::getParam('oAuthProviders', []);
$providerValues = $document->getAttribute('oAuthProviders', []);
$projectProviders = [];
2021-10-25 14:51:39 +00:00
foreach ($providers as $key => $provider) {
if (!$provider['enabled']) {
// Disabled by Appwrite configuration, exclude from response
2021-10-25 14:51:39 +00:00
continue;
}
$projectProviders[] = new Document([
'key' => $key,
'name' => $provider['name'] ?? '',
'appId' => $providerValues[$key . 'Appid'] ?? '',
'secret' => $providerValues[$key . 'Secret'] ?? '',
'enabled' => $providerValues[$key . 'Enabled'] ?? false,
]);
2021-10-25 14:51:39 +00:00
}
2023-10-25 17:33:23 +00:00
$document->setAttribute('oAuthProviders', $projectProviders);
2021-10-25 14:51:39 +00:00
return $document;
}
}