@@ -377,7 +379,7 @@
diff --git a/dashboard/src/js/activation.js b/dashboard/src/js/activation.js index 1341cc87e..1ef26bd0c 100644 --- a/dashboard/src/js/activation.js +++ b/dashboard/src/js/activation.js @@ -1,6 +1,6 @@ 'use strict'; -/* global angular, window, document, localStorage */ +/* global angular, window, document, localStorage, redirectIfNeeded */ /* global $ */ // create main application module @@ -70,33 +70,6 @@ app.controller('SetupController', ['$scope', 'Client', function ($scope, Client) } }; - function redirectIfNeeded(status) { - if ('develop' in search || localStorage.getItem('develop')) { - console.warn('Cloudron develop mode on. To disable run localStorage.removeItem(\'develop\')'); - localStorage.setItem('develop', true); - return; - } - - // if we are here from https://ip/activation.html ,go to https://admin/activation.html - if (status.adminFqdn && status.adminFqdn !== window.location.hostname) { - window.location.href = 'https://' + status.adminFqdn + '/activation.html'; - return true; - } - - // if we don't have a domain yet, first go to domain setup - if (!status.adminFqdn) { - window.location.href = '/setup.html'; - return true; - } - - if (status.activated) { - window.location.href = '/'; - return true; - } - - return false; - } - function setView(view) { if (view === 'finished') { $scope.view = 'finished'; @@ -109,7 +82,8 @@ app.controller('SetupController', ['$scope', 'Client', function ($scope, Client) Client.getProvisionStatus(function (error, status) { if (error) return Client.initError(error, init); - if (redirectIfNeeded(status)) return; + if (redirectIfNeeded(status, 'activation')) return; // redirected to some other view... + setView(search.view); $scope.setupToken = search.setupToken; diff --git a/dashboard/src/js/client.js b/dashboard/src/js/client.js index 1009db9ab..fbbbf8b18 100644 --- a/dashboard/src/js/client.js +++ b/dashboard/src/js/client.js @@ -451,6 +451,70 @@ function translateFilterFactory($parse, $translate) { translateFilterFactory.displayName = 'translateFilterFactory'; angular.module('Application').filter('tr', translateFilterFactory); +// checks provision status and redirects to correct view +// { +// setup: { active, message, errorMessage } +// restore { active, message, errorMessage } +// activated +// adminFqn +// } +// returns true if redirected +function redirectIfNeeded(status, currentView) { + var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {}); + + if ('develop' in search || localStorage.getItem('develop')) { + console.warn('Cloudron develop mode on. To disable run localStorage.removeItem(\'develop\')'); + localStorage.setItem('develop', true); + return false; + } + + console.log(status, currentView); + + if (status.activated) { + console.log('Already activated'); + if (currentView === 'dashboard') { + // support local development with localhost check + if (window.location.hostname !== status.adminFqdn && window.location.hostname !== 'localhost' && !window.location.hostname.startsWith('192.')) { + // user is accessing by IP or by the old admin location (pre-migration) + window.location.href = '/setup.html' + window.location.search; + return true; + } + + return false; + } + window.location.href = '/'; + return true; + } + + if (status.setup.active) { + console.log('Setup is active'); + if (currentView === 'setup') return false; + window.location.href = '/setup.html' + window.location.search; + return true; + } + + if (status.restore.active) { + console.log('Restore is active'); + if (currentView === 'restore') return; + window.location.href = '/restore.html' + window.location.search; + return true; + } + + if (status.adminFqdn) { + console.log('adminFqdn is set'); + // if we are here from https://ip/activation.html ,go to https://admin/activation.html + if (status.adminFqdn !== window.location.hostname) { + window.location.href = 'https://' + status.adminFqdn + '/activation.html' + (window.location.search); + return true; + } + if (currentView === 'activation') return false; + window.location.href = 'https://' + status.adminFqdn + '/activation.html' + (window.location.search); + return true; + } + + // if we are here, proceed with current view + return false; +} // ---------------------------------------------- // Cloudron REST API wrapper diff --git a/dashboard/src/js/index.js b/dashboard/src/js/index.js index 6ff433cf9..e425d0446 100644 --- a/dashboard/src/js/index.js +++ b/dashboard/src/js/index.js @@ -1,6 +1,6 @@ 'use strict'; -/* global angular:false */ +/* global angular:false, window, document, localStorage, redirectIfNeeded */ /* global $:false */ /* global async */ /* global ERROR,ISTATES,HSTATES,RSTATES,APP_TYPES,NOTIFICATION_TYPES */ @@ -778,35 +778,12 @@ app.controller('MainController', ['$scope', '$route', '$timeout', '$location', ' }); } - function redirectIfNeeded(status) { - if (!status.activated) { - console.log('Not activated yet, redirecting', status); - if (status.restore.active || status.restore.errorMessage) { // show the error message in restore page - window.location.href = '/restore.html' + window.location.search; - } else if (status.adminFqdn) { - window.location.href = 'https://' + status.adminFqdn + '/activation.html' + (window.location.search); - } else { - window.location.href = '/setup.html' + window.location.search; - } - return true; - } - - // support local development with localhost check - if (window.location.hostname !== status.adminFqdn && window.location.hostname !== 'localhost' && !window.location.hostname.startsWith('192.')) { - // user is accessing by IP or by the old admin location (pre-migration) - window.location.href = '/setup.html' + window.location.search; - return true; - } - - return false; - } - // this loads the very first thing when accessing via IP or domain function init() { Client.getProvisionStatus(function (error, status) { if (error) return Client.initError(error, init); - if (redirectIfNeeded(status)) return; + if (redirectIfNeeded(status, 'dashboard')) return; // we got redirected... // check version and force reload if needed if (!localStorage.version) { diff --git a/dashboard/src/js/restore.js b/dashboard/src/js/restore.js index 3fbfc2ee9..2ea6c30a1 100644 --- a/dashboard/src/js/restore.js +++ b/dashboard/src/js/restore.js @@ -1,6 +1,6 @@ 'use strict'; -/* global $, angular, SECRET_PLACEHOLDER, STORAGE_PROVIDERS, BACKUP_FORMATS, window, FileReader, document */ +/* global $, angular, SECRET_PLACEHOLDER, STORAGE_PROVIDERS, BACKUP_FORMATS, window, FileReader, document, redirectIfNeeded */ /* global REGIONS_S3, REGIONS_WASABI, REGIONS_DIGITALOCEAN, REGIONS_EXOSCALE, REGIONS_SCALEWAY, REGIONS_LINODE, REGIONS_OVH, REGIONS_IONOS, REGIONS_UPCLOUD, REGIONS_VULTR, REGIONS_CONTABO */ // create main application module @@ -296,7 +296,7 @@ app.controller('RestoreController', ['$scope', 'Client', function ($scope, Clien $scope.busy = false; $scope.error.generic = status.restore.errorMessage; } else { // restore worked, redirect to admin page - window.location.href = '/'; + window.location.href = 'https://' + status.adminFqdn + '/'; } return; } @@ -355,14 +355,11 @@ app.controller('RestoreController', ['$scope', 'Client', function ($scope, Clien Client.getProvisionStatus(function (error, status) { if (error) return Client.initError(error, init); + if (redirectIfNeeded(status, 'restore')) return; // redirected to some other view... + if (status.restore.active) return waitForRestore(); - if (status.restore.errorMessage) $scope.error.generic = status.restore.errorMessage; - - if (status.activated) { - window.location.href = '/'; - return; - } + if (status.restore.errorMessage) $scope.error.generic = status.restore.errorMessage; // any previous restore error Client.getProvisionBlockDevices(function (error, result) { if (error) { diff --git a/dashboard/src/js/setup.js b/dashboard/src/js/setup.js index 6bdb1519b..1f1ce116d 100644 --- a/dashboard/src/js/setup.js +++ b/dashboard/src/js/setup.js @@ -1,6 +1,6 @@ 'use strict'; -/* global $, angular, Clipboard, ENDPOINTS_OVH, window, FileReader, document */ +/* global $, angular, Clipboard, ENDPOINTS_OVH, window, FileReader, document, redirectIfNeeded */ // create main application module var app = angular.module('Application', ['pascalprecht.translate', 'ngCookies', 'angular-md5', 'ui-notification', 'ui.bootstrap']); @@ -277,23 +277,22 @@ app.controller('SetupDNSController', ['$scope', '$http', '$timeout', 'Client', f return; } - $scope.message = status.setup.message; + if (!error) $scope.message = status.setup.message; setTimeout(waitForDnsSetup, 5000); }); } - function initialize() { + function init() { Client.getProvisionStatus(function (error, status) { - if (error) { - // During domain migration, the box code restarts and can result in getStatus() failing temporarily - console.error(error); - $scope.state = 'waitingForBox'; - return $timeout(initialize, 3000); - } + $scope.state = 'waitingForBox'; + if (error) return Client.initError(error, init); - // domain is currently like a lock flag - if (status.adminFqdn) return waitForDnsSetup(); + if (redirectIfNeeded(status, 'setup')) return; // redirected to some other view... + + if (status.setup.active) return waitForDnsSetup(); + + $scope.error.setup = status.setup.errorMessage; // show any previous error if (status.provider === 'digitalocean' || status.provider === 'digitalocean-mp') { $scope.dnsCredentials.provider = 'digitalocean'; @@ -329,5 +328,5 @@ app.controller('SetupDNSController', ['$scope', '$http', '$timeout', 'Client', f $timeout(function () { $scope.clipboardDone = false; }, 5000); }); - initialize(); + init(); }]); diff --git a/dashboard/src/setup.html b/dashboard/src/setup.html index 529330a0e..34e406632 100644 --- a/dashboard/src/setup.html +++ b/dashboard/src/setup.html @@ -53,7 +53,9 @@
-