1
0
Fork 0
mirror of synced 2024-05-20 04:32:37 +12:00

Added config manager

This commit is contained in:
Eldad Fux 2020-03-28 15:42:16 +03:00
parent 6fb6c8ebac
commit 4548622386
21 changed files with 213 additions and 140 deletions

View file

@ -3,12 +3,13 @@
// Init
require_once __DIR__.'/init.php';
global $env, $utopia, $request, $response, $register, $consoleDB, $project, $domain, $version, $service, $protocol, $domainVerification;
global $utopia, $request, $response, $register, $consoleDB, $project, $service;
use Utopia\App;
use Utopia\Request;
use Utopia\View;
use Utopia\Exception;
use Utopia\Config\Config;
use Utopia\Domains\Domain;
use Appwrite\Auth\Auth;
use Appwrite\Database\Database;
@ -50,7 +51,7 @@ $clients = array_unique(array_merge($clientsConsole, array_map(function ($node)
return false;
}))));
$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $roles, $webhook, $audit, $usage, $domain, $clients, &$domainVerification) {
$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $roles, $webhook, $audit, $usage, $clients) {
$route = $utopia->match($request);
@ -62,10 +63,10 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
$refDomain = $protocol.'://'.((in_array($origin, $clients))
? $origin : 'localhost') . (!empty($port) ? ':'.$port : '');
$selfDomain = new Domain($domain);
$selfDomain = new Domain(Config::getParam('domain'));
$endDomain = new Domain($origin);
$domainVerification = ($selfDomain->getRegisterable() === $endDomain->getRegisterable());
Config::setParam('domainVerification', ($selfDomain->getRegisterable() === $endDomain->getRegisterable()));
/*
* Security Headers
@ -138,7 +139,7 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
$user = new Document([
'$id' => 0,
'status' => Auth::USER_STATUS_ACTIVATED,
'email' => 'app.'.$project->getId().'@service.'.$domain,
'email' => 'app.'.$project->getId().'@service.'.Config::getParam('domain'),
'password' => '',
'name' => $project->getAttribute('name', 'Untitled'),
]);
@ -247,7 +248,10 @@ $utopia->options(function () use ($request, $response) {
->send();
});
$utopia->error(function ($error /* @var $error Exception */) use ($request, $response, $utopia, $project, $env, $version) {
$utopia->error(function ($error /* @var $error Exception */) use ($request, $response, $utopia, $project) {
$env = Config::getParam('env');
$version = Config::getParam('version');
switch ($error->getCode()) {
case 400: // Error allowed publicly
case 401: // Error allowed publicly

View file

@ -1,9 +1,12 @@
<?php
global $providers, $request;
global $request;
use Utopia\Config\Config;
use Appwrite\Database\Database;
$providers = Config::getParam('providers');
$collections = [
'console' => [
'$id' => 'console',
@ -1189,15 +1192,15 @@ $collections = [
/*
* Add enabled OAuth2 providers to default data rules
*/
foreach ($providers as $key => $provider) {
foreach ($providers as $index => $provider) {
if (!$provider['enabled']) {
continue;
}
$collections[Database::SYSTEM_COLLECTION_PROJECTS]['rules'][] = [
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'OAuth2 '.ucfirst($key).' ID',
'key' => 'usersOauth2'.ucfirst($key).'Appid',
'label' => 'OAuth2 '.ucfirst($index).' ID',
'key' => 'usersOauth2'.ucfirst($index).'Appid',
'type' => 'text',
'default' => '',
'required' => false,
@ -1206,8 +1209,8 @@ foreach ($providers as $key => $provider) {
$collections[Database::SYSTEM_COLLECTION_PROJECTS]['rules'][] = [
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'OAuth2 '.ucfirst($key).' Secret',
'key' => 'usersOauth2'.ucfirst($key).'Secret',
'label' => 'OAuth2 '.ucfirst($index).' Secret',
'key' => 'usersOauth2'.ucfirst($index).'Secret',
'type' => 'text',
'default' => '',
'required' => false,
@ -1216,8 +1219,8 @@ foreach ($providers as $key => $provider) {
$collections[Database::SYSTEM_COLLECTION_USERS]['rules'][] = [
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'OAuth2 '.ucfirst($key).' ID',
'key' => 'oauth2'.ucfirst($key),
'label' => 'OAuth2 '.ucfirst($index).' ID',
'key' => 'oauth2'.ucfirst($index),
'type' => 'text',
'default' => '',
'required' => false,
@ -1226,8 +1229,8 @@ foreach ($providers as $key => $provider) {
$collections[Database::SYSTEM_COLLECTION_USERS]['rules'][] = [
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'OAuth2 '.ucfirst($key).' Access Token',
'key' => 'oauth2'.ucfirst($key).'AccessToken',
'label' => 'OAuth2 '.ucfirst($index).' Access Token',
'key' => 'oauth2'.ucfirst($index).'AccessToken',
'type' => 'text',
'default' => '',
'required' => false,

View file

@ -1,10 +1,11 @@
<?php
global $utopia, $register, $request, $response, $user, $audit,
$webhook, $project, $domain, $projectDB, $providers, $clients, $protocol;
$webhook, $project, $projectDB, $clients;
use Utopia\Exception;
use Utopia\Response;
use Utopia\Config\Config;
use Utopia\Validator\Assoc;
use Utopia\Validator\Text;
use Utopia\Validator\Email;
@ -30,8 +31,8 @@ include_once __DIR__ . '/../shared/api.php';
$oauth2Keys = [];
$utopia->init(function() use ($providers, &$oauth2Keys) {
foreach ($providers as $key => $provider) {
$utopia->init(function() use (&$oauth2Keys) {
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
@ -155,7 +156,8 @@ $utopia->post('/v1/account/sessions')
->param('email', '', function () { return new Email(); }, 'User email.')
->param('password', '', function () { return new Password(); }, 'User password.')
->action(
function ($email, $password) use ($response, $request, $projectDB, $audit, $webhook, $protocol, $domainVerification) {
function ($email, $password) use ($response, $request, $projectDB, $audit, $webhook) {
$protocol = Config::getParam('protocol');
$profile = $projectDB->getCollection([ // Get user by email address
'limit' => 1,
'first' => true,
@ -216,7 +218,7 @@ $utopia->post('/v1/account/sessions')
->setParam('resource', 'users/'.$profile->getId())
;
if(!$domainVerification) {
if(!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', json_encode([Auth::$cookieName => Auth::encodeSession($profile->getId(), $secret)]))
;
@ -244,11 +246,12 @@ $utopia->get('/v1/account/sessions/oauth2/:provider')
->label('sdk.location', true)
->label('abuse-limit', 50)
->label('abuse-key', 'ip:{ip}')
->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth2 Provider. Currently, supported providers are: ' . implode(', ', array_keys(array_filter($providers, function($node) {return (!$node['mock']);}))).'.')
->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'OAuth2 Provider. Currently, supported providers are: ' . implode(', ', array_keys(array_filter(Config::getParam('providers'), function($node) {return (!$node['mock']);}))).'.')
->param('success', '', function () use ($clients) { return new Host($clients); }, 'URL to redirect back to your app after a successful login attempt.')
->param('failure', '', function () use ($clients) { return new Host($clients); }, 'URL to redirect back to your app after a failed login attempt.')
->action(
function ($provider, $success, $failure) use ($response, $request, $project, $protocol) {
function ($provider, $success, $failure) use ($response, $request, $project) {
$protocol = Config::getParam('protocol');
$callback = $protocol.'://'.$request->getServer('HTTP_HOST').'/v1/account/sessions/oauth2/callback/'.$provider.'/'.$project->getId();
$appId = $project->getAttribute('usersOauth2'.ucfirst($provider).'Appid', '');
$appSecret = $project->getAttribute('usersOauth2'.ucfirst($provider).'Secret', '{}');
@ -282,11 +285,13 @@ $utopia->get('/v1/account/sessions/oauth2/callback/:provider/:projectId')
->label('scope', 'public')
->label('docs', false)
->param('projectId', '', function () { return new Text(1024); }, 'Project unique ID.')
->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth2 provider.')
->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'OAuth2 provider.')
->param('code', '', function () { return new Text(1024); }, 'OAuth2 code.')
->param('state', '', function () { return new Text(2048); }, 'Login state params.', true)
->action(
function ($projectId, $provider, $code, $state) use ($response, $request, $domain, $protocol) {
function ($projectId, $provider, $code, $state) use ($response) {
$domain = Config::getParam('domain');
$protocol = Config::getParam('protocol');
$response->redirect($protocol.'://'.$domain.'/v1/account/sessions/oauth2/'.$provider.'/redirect?'
.http_build_query(['project' => $projectId, 'code' => $code, 'state' => $state]));
}
@ -300,11 +305,12 @@ $utopia->get('/v1/account/sessions/oauth2/:provider/redirect')
->label('abuse-limit', 50)
->label('abuse-key', 'ip:{ip}')
->label('docs', false)
->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth2 provider.')
->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'OAuth2 provider.')
->param('code', '', function () { return new Text(1024); }, 'OAuth2 code.')
->param('state', '', function () { return new Text(2048); }, 'OAuth2 state params.', true)
->action(
function ($provider, $code, $state) use ($response, $request, $user, $projectDB, $project, $audit, $protocol, $domainVerification) {
function ($provider, $code, $state) use ($response, $request, $user, $projectDB, $project, $audit) {
$protocol = Config::getParam('protocol');
$callback = $protocol.'://'.$request->getServer('HTTP_HOST').'/v1/account/sessions/oauth2/callback/'.$provider.'/'.$project->getId();
$defaultState = ['success' => $project->getAttribute('url', ''), 'failure' => ''];
$validateURL = new URL();
@ -457,7 +463,7 @@ $utopia->get('/v1/account/sessions/oauth2/:provider/redirect')
->setParam('data', ['provider' => $provider])
;
if(!$domainVerification) {
if(!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', json_encode([Auth::$cookieName => Auth::encodeSession($user->getId(), $secret)]))
;
@ -832,7 +838,8 @@ $utopia->delete('/v1/account')
->label('sdk.method', 'delete')
->label('sdk.description', '/docs/references/account/delete.md')
->action(
function () use ($response, $user, $projectDB, $audit, $webhook, $protocol, $domainVerification) {
function () use ($response, $user, $projectDB, $audit, $webhook) {
$protocol = Config::getParam('protocol');
$user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [
'status' => Auth::USER_STATUS_BLOCKED,
]));
@ -863,7 +870,7 @@ $utopia->delete('/v1/account')
])
;
if(!$domainVerification) {
if(!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', json_encode([]))
;
@ -888,7 +895,8 @@ $utopia->delete('/v1/account/sessions/:sessionId')
->label('abuse-limit', 100)
->param('sessionId', null, function () { return new UID(); }, 'Session unique ID. Use the string \'current\' to delete the current device session.')
->action(
function ($sessionId) use ($response, $user, $projectDB, $webhook, $audit, $protocol, $domainVerification) {
function ($sessionId) use ($response, $user, $projectDB, $webhook, $audit) {
$protocol = Config::getParam('protocol');
$sessionId = ($sessionId === 'current')
? Auth::tokenVerify($user->getAttribute('tokens'), Auth::TOKEN_TYPE_LOGIN, Auth::$secret)
: $sessionId;
@ -914,7 +922,7 @@ $utopia->delete('/v1/account/sessions/:sessionId')
])
;
if(!$domainVerification) {
if(!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', json_encode([]))
;
@ -945,7 +953,8 @@ $utopia->delete('/v1/account/sessions')
->label('sdk.description', '/docs/references/account/delete-sessions.md')
->label('abuse-limit', 100)
->action(
function () use ($response, $user, $projectDB, $audit, $webhook, $protocol, $domainVerification) {
function () use ($response, $user, $projectDB, $audit, $webhook) {
$protocol = Config::getParam('protocol');
$tokens = $user->getAttribute('tokens', []);
foreach ($tokens as $token) { /* @var $token Document */
@ -966,7 +975,7 @@ $utopia->delete('/v1/account/sessions')
])
;
if(!$domainVerification) {
if(!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', json_encode([]))
;

View file

@ -1,6 +1,6 @@
<?php
global $utopia, $request, $response, $version;
global $utopia, $request, $response;
use Utopia\Exception;
use Utopia\Validator\Text;
@ -14,6 +14,7 @@ use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use Utopia\Config\Config;
include_once __DIR__ . '/../shared/api.php';
@ -206,7 +207,7 @@ $utopia->get('/v1/avatars/favicon')
->label('sdk.method', 'getFavicon')
->label('sdk.description', '/docs/references/avatars/get-favicon.md')
->action(
function ($url) use ($response, $request, $version) {
function ($url) use ($response, $request) {
$width = 56;
$height = 56;
$quality = 80;
@ -238,7 +239,7 @@ $utopia->get('/v1/avatars/favicon')
CURLOPT_MAXREDIRS => 3,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => sprintf(APP_USERAGENT,
$version,
Config::getParam('version'),
$request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
),
]);

View file

@ -1,6 +1,6 @@
<?php
global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB, $providers;
global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB;
use Utopia\Exception;
use Utopia\Response;
@ -10,6 +10,7 @@ use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;
use Utopia\Validator\Range;
use Utopia\Validator\URL;
use Utopia\Config\Config;
use Utopia\Domains\Domain;
use Appwrite\Auth\Auth;
use Appwrite\Task\Validator\Cron;
@ -92,7 +93,7 @@ $utopia->get('/v1/projects')
->label('sdk.namespace', 'projects')
->label('sdk.method', 'list')
->action(
function () use ($request, $response, $providers, $consoleDB) {
function () use ($request, $response, $consoleDB) {
$results = $consoleDB->getCollection([
'limit' => 20,
'offset' => 0,
@ -105,7 +106,7 @@ $utopia->get('/v1/projects')
]);
foreach ($results as $project) {
foreach ($providers as $provider => $node) {
foreach (Config::getParam('providers') as $provider => $node) {
$secret = json_decode($project->getAttribute('usersOauth2'.ucfirst($provider).'Secret', '{}'), true);
if (!empty($secret) && isset($secret['version'])) {
@ -126,14 +127,14 @@ $utopia->get('/v1/projects/:projectId')
->label('sdk.method', 'get')
->param('projectId', '', function () { return new UID(); }, 'Project unique ID.')
->action(
function ($projectId) use ($request, $response, $providers, $consoleDB) {
function ($projectId) use ($request, $response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {
throw new Exception('Project not found', 404);
}
foreach ($providers as $provider => $node) {
foreach (Config::getParam('providers') as $provider => $node) {
$secret = json_decode($project->getAttribute('usersOauth2'.ucfirst($provider).'Secret', '{}'), true);
if (!empty($secret) && isset($secret['version'])) {
@ -331,7 +332,7 @@ $utopia->patch('/v1/projects/:projectId/oauth2')
->label('sdk.namespace', 'projects')
->label('sdk.method', 'updateOAuth2')
->param('projectId', '', function () { return new UID(); }, 'Project unique ID.')
->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'Provider Name', false)
->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'Provider Name', false)
->param('appId', '', function () { return new Text(256); }, 'Provider app ID.', true)
->param('secret', '', function () { return new text(256); }, 'Provider secret key.', true)
->action(
@ -1201,8 +1202,9 @@ $utopia->post('/v1/projects/:projectId/domains')
->param('projectId', null, function () { return new UID(); }, 'Project unique ID.')
->param('domain', null, function () { return new DomainValidator(); }, 'Domain name.')
->action(
function ($projectId, $domain) use ($request, $response, $consoleDB) {
function ($projectId) use ($request, $response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
$domain = Config::getParam('domain');
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {
throw new Exception('Project not found', 404);
@ -1283,7 +1285,7 @@ $utopia->get('/v1/projects/:projectId/domains/:domainId')
->param('projectId', null, function () { return new UID(); }, 'Project unique ID.')
->param('domainId', null, function () { return new UID(); }, 'Domain unique ID.')
->action(
function ($projectId, $domainId) use ($request, $response, $consoleDB) {
function ($projectId, $domainId) use ($response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {

View file

@ -1,6 +1,6 @@
<?php
global $utopia, $request, $response, $register, $user, $audit, $usage, $project, $projectDB, $version;
global $utopia, $request, $response, $register, $user, $audit, $usage, $project, $projectDB;
use Utopia\Exception;
use Utopia\Response;
@ -338,7 +338,7 @@ $utopia->get('/v1/storage/files/:fileId/preview')
//->param('storage', 'local', function () {return new WhiteList(array('local'));}, 'Selected storage device. defaults to local')
//->param('token', '', function () {return new Text(128);}, 'Preview token', true)
->action(
function ($fileId, $width, $height, $quality, $background, $output) use ($request, $response, $projectDB, $project, $inputs, $outputs, $fileLogos, $version) {
function ($fileId, $width, $height, $quality, $background, $output) use ($request, $response, $projectDB, $project, $inputs, $outputs, $fileLogos) {
$storage = 'local';
if (!extension_loaded('imagick')) {
@ -354,7 +354,7 @@ $utopia->get('/v1/storage/files/:fileId/preview')
}
$date = date('D, d M Y H:i:s', time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache
$key = md5($version.$fileId.$width.$height.$quality.$background.$storage.$output);
$key = md5($fileId.$width.$height.$quality.$background.$storage.$output);
$file = $projectDB->getDocument($fileId);
@ -374,7 +374,7 @@ $utopia->get('/v1/storage/files/:fileId/preview')
$cipher = null;
$background = (empty($background)) ? 'eceff1' : $background;
$type = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$key = md5($version.$path.$width.$height.$quality.$background.$storage.$output);
$key = md5($path.$width.$height.$quality.$background.$storage.$output);
}
$compressor = new GZIP();

View file

@ -4,6 +4,7 @@ global $utopia, $register, $request, $response, $projectDB, $project, $user, $au
use Utopia\Exception;
use Utopia\Response;
use Utopia\Config\Config;
use Utopia\Validator\Email;
use Utopia\Validator\Text;
use Utopia\Validator\Host;
@ -431,7 +432,8 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('secret', '', function () { return new Text(256); }, 'Secret key.')
->action(
function ($teamId, $inviteId, $userId, $secret) use ($response, $request, $user, $audit, $projectDB, $protocol, $domainVerification) {
function ($teamId, $inviteId, $userId, $secret) use ($response, $request, $user, $audit, $projectDB) {
$protocol = Config::getParam('protocol');
$membership = $projectDB->getDocument($inviteId);
if (empty($membership->getId()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $membership->getCollection()) {
@ -525,7 +527,7 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status')
->setParam('resource', 'teams/'.$teamId)
;
if(!$domainVerification) {
if(!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', json_encode([Auth::$cookieName => Auth::encodeSession($user->getId(), $secret)]))
;

View file

@ -1,6 +1,6 @@
<?php
global $utopia, $response, $projectDB, $providers;
global $utopia, $response, $projectDB;
use Utopia\Exception;
use Utopia\Response;
@ -11,6 +11,7 @@ use Utopia\Validator\Text;
use Utopia\Validator\Range;
use Utopia\Audit\Audit;
use Utopia\Audit\Adapters\MySQL as AuditAdapter;
use Utopia\Config\Config;
use Utopia\Locale\Locale;
use Appwrite\Auth\Auth;
use Appwrite\Auth\Validator\Password;
@ -33,7 +34,7 @@ $utopia->post('/v1/users')
->param('password', '', function () { return new Password(); }, 'User password.')
->param('name', '', function () { return new Text(100); }, 'User name.', true)
->action(
function ($email, $password, $name) use ($response, $register, $projectDB, $providers) {
function ($email, $password, $name) use ($response, $projectDB) {
$profile = $projectDB->getCollection([ // Get user by email address
'limit' => 1,
'first' => true,
@ -69,7 +70,7 @@ $utopia->post('/v1/users')
$oauth2Keys = [];
foreach ($providers as $key => $provider) {
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
@ -103,7 +104,7 @@ $utopia->get('/v1/users')
->param('offset', 0, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true)
->action(
function ($search, $limit, $offset, $orderType) use ($response, $projectDB, $providers) {
function ($search, $limit, $offset, $orderType) use ($response, $projectDB) {
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
@ -118,7 +119,7 @@ $utopia->get('/v1/users')
$oauth2Keys = [];
foreach ($providers as $key => $provider) {
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
@ -154,7 +155,7 @@ $utopia->get('/v1/users/:userId')
->label('sdk.description', '/docs/references/users/get-user.md')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->action(
function ($userId) use ($response, $projectDB, $providers) {
function ($userId) use ($response, $projectDB) {
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
@ -163,7 +164,7 @@ $utopia->get('/v1/users/:userId')
$oauth2Keys = [];
foreach ($providers as $key => $provider) {
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
@ -352,7 +353,7 @@ $utopia->patch('/v1/users/:userId/status')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('status', '', function () { return new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED]); }, 'User Status code. To activate the user pass '.Auth::USER_STATUS_ACTIVATED.', to block the user pass '.Auth::USER_STATUS_BLOCKED.' and for disabling the user pass '.Auth::USER_STATUS_UNACTIVATED)
->action(
function ($userId, $status) use ($response, $projectDB, $providers) {
function ($userId, $status) use ($response, $projectDB) {
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
@ -369,7 +370,7 @@ $utopia->patch('/v1/users/:userId/status')
$oauth2Keys = [];
foreach ($providers as $key => $provider) {
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
@ -400,7 +401,7 @@ $utopia->patch('/v1/users/:userId/prefs')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('prefs', '', function () { return new Assoc();}, 'Prefs key-value JSON object.')
->action(
function ($userId, $prefs) use ($response, $projectDB, $providers) {
function ($userId, $prefs) use ($response, $projectDB) {
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {

View file

@ -1,10 +1,9 @@
<?php
use Utopia\View;
use Utopia\Config\Config;
use Utopia\Locale\Locale;
global $protocol;
Locale::$exceptions = false;
$roles = [
@ -22,7 +21,7 @@ if (!empty($request->getQuery('version', ''))) {
$layout
->setParam('title', APP_NAME)
->setParam('protocol', $protocol)
->setParam('protocol', Config::getParam('protocol'))
->setParam('domain', $domain)
->setParam('home', $request->getServer('_APP_HOME'))
->setParam('setup', $request->getServer('_APP_SETUP'))
@ -32,9 +31,9 @@ $layout
->setParam('env', $utopia->getEnv())
;
$utopia->shutdown(function () use ($utopia, $response, $request, $layout, $version, $env) {
$utopia->shutdown(function () use ($utopia, $response, $request, $layout) {
$time = (60 * 60 * 24 * 45); // 45 days cache
$isDev = (\Utopia\App::ENV_TYPE_DEVELOPMENT == $env);
$isDev = (\Utopia\App::ENV_TYPE_DEVELOPMENT == Config::getParam('env'));
$response
->addHeader('Cache-Control', 'public, max-age='.$time)
@ -44,7 +43,7 @@ $utopia->shutdown(function () use ($utopia, $response, $request, $layout, $versi
$route = $utopia->match($request);
$scope = $route->getLabel('scope', '');
$layout
->setParam('version', $version)
->setParam('version', Config::getParam('version'))
->setParam('isDev', $isDev)
->setParam('class', $scope)
;

View file

@ -2,9 +2,10 @@
include_once __DIR__ . '/../shared/web.php';
global $utopia, $response, $request, $layout, $version, $providers, $projectDB;
global $utopia, $response, $request, $layout, $projectDB;
use Utopia\View;
use Utopia\Config\Config;
use Utopia\Domains\Domain;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\UID;
@ -16,25 +17,18 @@ $utopia->init(function () use ($layout, $utopia) {
;
});
$utopia->shutdown(function () use ($utopia, $response, $request, $layout, $version) {
$utopia->shutdown(function () use ($response, $request, $layout) {
$header = new View(__DIR__.'/../../views/console/comps/header.phtml');
$footer = new View(__DIR__.'/../../views/console/comps/footer.phtml');
$footer
->setParam('home', $request->getServer('_APP_HOME', ''))
->setParam('version', $version)
->setParam('version', Config::getParam('version'))
;
$layout
->setParam('header', [$header])
->setParam('footer', [$footer])
->setParam('prefetch', [
//'/console/database?version=' . $version,
//'/console/storage?version=' . $version,
//'/console/users?version=' . $version,
//'/console/settings?version=' . $version,
//'/console/account?version=' . $version,
])
;
$response->send($layout->render());
@ -228,10 +222,10 @@ $utopia->get('/console/users')
->desc('Platform console project settings')
->label('permission', 'public')
->label('scope', 'console')
->action(function () use ($layout, $providers) {
->action(function () use ($layout) {
$page = new View(__DIR__.'/../../views/console/users/index.phtml');
$page->setParam('providers', $providers);
$page->setParam('providers', Config::getParam('providers'));
$layout
->setParam('title', APP_NAME.' - Users')
@ -242,7 +236,7 @@ $utopia->get('/console/users/view')
->desc('Platform console project user')
->label('permission', 'public')
->label('scope', 'console')
->action(function () use ($layout, $providers) {
->action(function () use ($layout) {
$page = new View(__DIR__.'/../../views/console/users/view.phtml');
$layout

View file

@ -2,22 +2,23 @@
include_once __DIR__ . '/../shared/web.php';
global $utopia, $response, $request, $layout, $version, $providers, $platforms;
global $utopia, $response, $request, $layout;
use Utopia\View;
use Utopia\Config\Config;
$header = new View(__DIR__.'/../../views/home/comps/header.phtml');
$footer = new View(__DIR__.'/../../views/home/comps/footer.phtml');
$footer
->setParam('version', $version)
->setParam('version', Config::getParam('version'))
;
$layout
->setParam('title', APP_NAME)
->setParam('description', '')
->setParam('class', 'home')
->setParam('platforms', $platforms)
->setParam('platforms', Config::getParam('platforms'))
->setParam('header', [$header])
->setParam('footer', [$footer])
;

View file

@ -14,6 +14,7 @@ if (file_exists(__DIR__.'/../vendor/autoload.php')) {
use Utopia\App;
use Utopia\Request;
use Utopia\Response;
use Utopia\Config\Config;
use Utopia\Locale\Locale;
use Utopia\Registry\Registry;
use Appwrite\Auth\Auth;
@ -52,30 +53,31 @@ $response = new Response();
/*
* ENV vars
*/
$env = $request->getServer('_APP_ENV', App::ENV_TYPE_PRODUCTION);
$domain = $request->getServer('HTTP_HOST', '');
$domainVerification = false;
$version = $request->getServer('_APP_VERSION', 'UNKNOWN');
$providers = include __DIR__.'/../app/config/providers.php'; // OAuth2 providers list
$platforms = include __DIR__.'/../app/config/platforms.php';
$locales = include __DIR__.'/../app/config/locales.php'; // Locales list
$collections = include __DIR__.'/../app/config/collections.php'; // Collections list
$redisHost = $request->getServer('_APP_REDIS_HOST', '');
$redisPort = $request->getServer('_APP_REDIS_PORT', '');
$utopia = new App('Asia/Tel_Aviv', $env);
$protocol = $request->getServer('HTTP_X_FORWARDED_PROTO', $request->getServer('REQUEST_SCHEME', 'https'));
$port = (string) parse_url($protocol.'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_PORT);
Config::load('providers', __DIR__.'/../app/config/providers.php');
Config::load('platforms', __DIR__.'/../app/config/platforms.php');
Config::load('locales', __DIR__.'/../app/config/locales.php');
Config::load('collections', __DIR__.'/../app/config/collections.php');
Resque::setBackend($redisHost.':'.$redisPort);
Config::setParam('env', $request->getServer('_APP_ENV', App::ENV_TYPE_PRODUCTION));
Config::setParam('domain', $request->getServer('HTTP_HOST', ''));
Config::setParam('domainVerification', false);
Config::setParam('version', $request->getServer('_APP_VERSION', 'UNKNOWN'));
Config::setParam('protocol', $request->getServer('HTTP_X_FORWARDED_PROTO', $request->getServer('REQUEST_SCHEME', 'https')));
Config::setParam('port', (string) parse_url(Config::getParam('protocol').'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_PORT));
$utopia = new App('Asia/Tel_Aviv', Config::getParam('env'));
Resque::setBackend($request->getServer('_APP_REDIS_HOST', '')
.':'.$request->getServer('_APP_REDIS_PORT', ''));
define('COOKIE_DOMAIN',
(
$request->getServer('HTTP_HOST', null) === 'localhost' ||
$request->getServer('HTTP_HOST', null) === 'localhost:'.$port ||
$request->getServer('HTTP_HOST', null) === 'localhost:'.Config::getParam('port') ||
(filter_var($request->getServer('HTTP_HOST', null), FILTER_VALIDATE_IP) !== false)
)
? null
: '.'.parse_url($protocol.'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_HOST));
: '.'.parse_url(Config::getParam('protocol').'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_HOST));
define('COOKIE_SAMESITE', Response::COOKIE_SAMESITE_NONE);
/*
@ -119,10 +121,11 @@ $register->set('statsd', function () use ($request) { // Register DB connection
return $statsd;
});
$register->set('cache', function () use ($redisHost, $redisPort) { // Register cache connection
$register->set('cache', function () use ($request) { // Register cache connection
$redis = new Redis();
$redis->connect($redisHost, $redisPort);
$redis->connect($request->getServer('_APP_REDIS_HOST', ''),
$request->getServer('_APP_REDIS_PORT', ''));
return $redis;
});
@ -209,7 +212,7 @@ Locale::setLanguage('zh-tw', include __DIR__.'/config/locales/zh-tw.php');
Locale::setDefault('en');
if (in_array($locale, $locales)) {
if (in_array($locale, Config::getParam('locales'))) {
Locale::setDefault($locale);
}
@ -217,7 +220,7 @@ stream_context_set_default([ // Set global user agent and http settings
'http' => [
'method' => 'GET',
'user_agent' => sprintf(APP_USERAGENT,
$version,
Config::getParam('version'),
$request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)),
'timeout' => 2,
],
@ -229,8 +232,8 @@ stream_context_set_default([ // Set global user agent and http settings
$consoleDB = new Database();
$consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
$consoleDB->setNamespace('app_console'); // Should be replaced with param if we want to have parent projects
$consoleDB->setMocks($collections);
$consoleDB->setMocks(Config::getParam('collections', []));
Authorization::disable();
$project = $consoleDB->getDocument($request->getParam('project', $request->getHeader('X-Appwrite-Project', null)));
@ -268,7 +271,7 @@ Auth::$secret = $session['secret'];
$projectDB = new Database();
$projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
$projectDB->setNamespace('app_'.$project->getId());
$projectDB->setMocks($collections);
$projectDB->setMocks(Config::getParam('collections', []));
$user = $projectDB->getDocument(Auth::$unique);

View file

@ -4,6 +4,7 @@
require_once __DIR__.'/../../vendor/autoload.php';
require_once __DIR__.'/../../app/init.php';
use Utopia\Config\Config;
use Utopia\CLI\CLI;
use Utopia\CLI\Console;
use Appwrite\Spec\Swagger2;
@ -39,7 +40,7 @@ $cli
return $result;
}
$platforms = include __DIR__ . '/../config/platforms.php';
$platforms = Config::getParam('platforms');
$message = Console::confirm('Please enter your commit message:');
$production = (Console::confirm('Type "Appwrite" to deploy for production') == 'Appwrite');

View file

@ -3,8 +3,9 @@
require_once __DIR__.'/../init.php';
global $register, $projectDB, $console, $providers, $request;
global $register, $projectDB, $console, $request;
use Utopia\Config\Config;
use Utopia\CLI\CLI;
use Utopia\CLI\Console;
use Appwrite\Database\Database;
@ -93,7 +94,7 @@ $callbacks = [
];
function fixDocument(Document $document) {
global $providers;
$providers = Config::getParam('providers');
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_PROJECTS){
foreach($providers as $key => $provider) {

View file

@ -1,6 +1,7 @@
<?php
use Utopia\App;
use Utopia\Config\Config;
use Utopia\Domains\Domain;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\Authorization;
@ -22,7 +23,7 @@ class CertificatesV1
public function perform()
{
global $request, $consoleDB, $env;
global $request, $consoleDB;
/**
* 1. Get new domain document - DONE
@ -103,7 +104,7 @@ class CertificatesV1
throw new Exception('Renew isn\'t required');
}
$staging = ($env === App::ENV_TYPE_PRODUCTION) ? '' : ' --dry-run';
$staging = (Config::getParam('env') === App::ENV_TYPE_PRODUCTION) ? '' : ' --dry-run';
$response = shell_exec("certbot certonly --webroot --noninteractive --agree-tos{$staging} --email security@appwrite.io \
-w ".APP_STORAGE_CERTIFICATES." \

View file

@ -2,6 +2,7 @@
require_once __DIR__.'/../init.php';
use Utopia\Config\Config;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\Authorization;
use Cron\CronExpression;
@ -23,7 +24,7 @@ class TasksV1
public function perform()
{
global $consoleDB, $version, $request;
global $consoleDB, $request;
/*
* 1. Get Original Task
@ -94,7 +95,7 @@ class TasksV1
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, sprintf(APP_USERAGENT,
$version,
Config::getParam('version'),
$request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
));
curl_setopt(

View file

@ -1,5 +1,7 @@
<?php
use Utopia\Config\Config;
require_once __DIR__.'/../init.php';
cli_set_process_title('Usage V1 Worker');
@ -19,7 +21,7 @@ class UsageV1
public function perform()
{
global $register, $version;
global $register;
$projectId = $this->args['projectId'];
$method = $this->args['method'];
@ -29,7 +31,7 @@ class UsageV1
$statsd = $register->get('statsd', true);
$tags = ",project={$projectId},version=".$version.'';
$tags = ",project={$projectId},version=".Config::getParam('version').'';
// the global namespace is prepended to every key (optional)
$statsd->setNamespace('appwrite.usage');

View file

@ -6,6 +6,7 @@ cli_set_process_title('Webhooks V1 Worker');
echo APP_NAME.' webhooks worker v1 has started';
use Utopia\Config\Config;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\Authorization;
@ -19,7 +20,7 @@ class WebhooksV1
public function perform()
{
global $consoleDB, $version, $request;
global $consoleDB, $request;
$errors = [];
@ -59,7 +60,7 @@ class WebhooksV1
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, sprintf(APP_USERAGENT,
$version,
Config::getParam('version'),
$request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
));
curl_setopt(

View file

@ -37,6 +37,7 @@
"utopia-php/audit": "0.2.*",
"utopia-php/cache": "0.2.*",
"utopia-php/cli": "0.4.*",
"utopia-php/config": "0.2.*",
"utopia-php/locale": "0.2.*",
"utopia-php/registry": "0.2.*",
"utopia-php/domains": "0.2.*",

70
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "54701e5067d9b94ae765f3f0e560b531",
"content-hash": "d19ba7d1c39970839f6f191664e9498d",
"packages": [
{
"name": "appwrite/php-clamav",
@ -1381,6 +1381,52 @@
],
"time": "2020-03-25T04:46:43+00:00"
},
{
"name": "utopia-php/config",
"version": "0.2.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/config.git",
"reference": "f1f41d3863eb00bd2837b45c2e17e8b5da1cf46d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/config/zipball/f1f41d3863eb00bd2837b45c2e17e8b5da1cf46d",
"reference": "f1f41d3863eb00bd2837b45c2e17e8b5da1cf46d",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Utopia\\Config\\": "src/Config"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Eldad Fux",
"email": "eldad@appwrite.io"
}
],
"description": "A simple Config library to managing application config variables",
"keywords": [
"config",
"framework",
"php",
"upf",
"utopia"
],
"time": "2020-03-28T10:24:18+00:00"
},
{
"name": "utopia-php/domains",
"version": "0.2.0",
@ -1576,7 +1622,7 @@
"source": {
"type": "git",
"url": "https://github.com/appwrite/sdk-generator",
"reference": "7a43413e650705843672ebe930fe6ecd77693393"
"reference": "97cb12c9c187a42a545060c324b0960c99582893"
},
"require": {
"ext-curl": "*",
@ -1606,7 +1652,7 @@
}
],
"description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms",
"time": "2020-03-01T12:19:20+00:00"
"time": "2020-03-27T15:12:28+00:00"
},
{
"name": "doctrine/instantiator",
@ -2395,16 +2441,16 @@
},
{
"name": "phpunit/phpunit",
"version": "7.5.20",
"version": "7.5.x-dev",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "9467db479d1b0487c99733bb1e7944d32deded2c"
"reference": "6ef9ac810e494e7793013f8f5004d5d268852412"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9467db479d1b0487c99733bb1e7944d32deded2c",
"reference": "9467db479d1b0487c99733bb1e7944d32deded2c",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6ef9ac810e494e7793013f8f5004d5d268852412",
"reference": "6ef9ac810e494e7793013f8f5004d5d268852412",
"shasum": ""
},
"require": {
@ -2475,7 +2521,7 @@
"testing",
"xunit"
],
"time": "2020-01-08T08:45:45+00:00"
"time": "2019-11-27T08:45:03+00:00"
},
{
"name": "sebastian/code-unit-reverse-lookup",
@ -3206,12 +3252,12 @@
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "27af7d9cd1b77fcb13b4ed452c291d9defad088b"
"reference": "6df2e2c3181e6d0b961fc341fbe8269fefc933c6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/27af7d9cd1b77fcb13b4ed452c291d9defad088b",
"reference": "27af7d9cd1b77fcb13b4ed452c291d9defad088b",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/6df2e2c3181e6d0b961fc341fbe8269fefc933c6",
"reference": "6df2e2c3181e6d0b961fc341fbe8269fefc933c6",
"shasum": ""
},
"require": {
@ -3263,7 +3309,7 @@
"keywords": [
"templating"
],
"time": "2020-03-24T12:35:30+00:00"
"time": "2020-03-27T16:41:01+00:00"
},
{
"name": "webmozart/assert",

View file

@ -148,18 +148,18 @@ services:
# ports:
# - "8081:8081"
# resque:
# image: registry.gitlab.com/appwrite/appwrite/resque-web:v1.0.2
# restart: unless-stopped
# networks:
# - appwrite
# ports:
# - "5678:5678"
# environment:
# - RESQUE_WEB_HOST=redis
# - RESQUE_WEB_PORT=6379
# - RESQUE_WEB_HTTP_BASIC_AUTH_USER=user
# - RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=password
resque:
image: registry.gitlab.com/appwrite/appwrite/resque-web:v1.0.2
restart: unless-stopped
networks:
- appwrite
ports:
- "5678:5678"
environment:
- RESQUE_WEB_HOST=redis
- RESQUE_WEB_PORT=6379
- RESQUE_WEB_HTTP_BASIC_AUTH_USER=user
- RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=password
maildev:
image: djfarrelly/maildev