diff --git a/src/js/client.js b/src/js/client.js index 22baa9051..badb516cd 100644 --- a/src/js/client.js +++ b/src/js/client.js @@ -1731,7 +1731,7 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout }; Client.prototype.getDNSRecords = function (domain, subdomain, callback) { - get('/api/v1/domains/' + domain + '/check_dns?subdomain=' + subdomain, null, function (error, data, status) { + get('/api/v1/domains/' + domain + '/dns_check?subdomain=' + subdomain, null, function (error, data, status) { if (error) return callback(error); if (status !== 200) return callback(new ClientError(status, data)); diff --git a/src/views/app.js b/src/views/app.js index 7a72abea0..0bbc86afd 100644 --- a/src/views/app.js +++ b/src/views/app.js @@ -241,18 +241,18 @@ angular.module('Application').controller('AppController', ['$scope', '$location' if (overwriteDns) return callback(); Client.getDNSRecords(domain.domain, domain.subdomain, 'A', function (error, result) { - if (error && (error.statusCode === 424 || error.statusCode === 200)) { + if (error) return callback(error); + if (result.error) { if (data.domain === domain.domain && data.location === domain.subdomain) { - $scope.location.error.location = domain.domain + ' ' + error.message; + $scope.location.error.location = domain.domain + ' ' + result.error.message; } else { - $scope.location.error.alternateDomains = domain.domain + ' ' + error.message; + $scope.location.error.alternateDomains = domain.domain + ' ' + result.error.message; } $scope.location.busy = false; return; } - if (error) return callback(error); - if (result.length) $scope.location.domainCollisions.push(domain); + if (result.needsOverwrite) $scope.location.domainCollisions.push(domain); callback(); }); diff --git a/src/views/appstore.js b/src/views/appstore.js index f9764383b..7069e96fa 100644 --- a/src/views/appstore.js +++ b/src/views/appstore.js @@ -42,6 +42,7 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$loca subscriptionHelperPage: '', error: {}, app: {}, + overwriteDns: false, location: '', domain: null, portBindings: {}, @@ -64,6 +65,7 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$loca reset: function () { $scope.appInstall.app = {}; $scope.appInstall.error = {}; + $scope.appInstall.overwriteDns = false; $scope.appInstall.location = ''; $scope.appInstall.domain = null; $scope.appInstall.portBindings = {}; @@ -112,6 +114,7 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$loca angular.copy(app, $scope.appInstall.app); $scope.appInstall.mediaLinks = $scope.appInstall.app.manifest.mediaLinks || []; + $scope.appInstall.overwriteDns = false; $scope.appInstall.domain = $scope.domains.find(function (d) { return $scope.config.adminDomain === d.domain; }); // pre-select the adminDomain $scope.appInstall.portBindingsInfo = angular.extend({}, $scope.appInstall.app.manifest.tcpPorts, $scope.appInstall.app.manifest.udpPorts); // Portbinding map only for information $scope.appInstall.portBindings = {}; // This is the actual model holding the env:port pair @@ -164,6 +167,7 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$loca } var data = { + overwriteDns: $scope.appInstall.overwriteDns, location: $scope.appInstall.location || '', domain: $scope.appInstall.domain.domain, portBindings: finalPortBindings, @@ -173,70 +177,84 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$loca sso: !$scope.appInstall.optionalSso ? undefined : ($scope.appInstall.accessRestrictionOption !== 'nosso') }; - Client.installApp($scope.appInstall.app.id, $scope.appInstall.app.manifest, $scope.appInstall.app.title, data, function (error, newAppId) { - if (error) { - if (error.statusCode === 402) { - $scope.appInstall.state = 'subscriptionRequired'; - $scope.appInstall.subscriptionErrorMesssage = error.message; - $('#collapseMediaLinksCarousel').collapse('hide'); - $('#collapseResourceConstraint').collapse('hide'); - $('#collapseInstallForm').collapse('hide'); - $('#collapseSubscriptionRequired').collapse('show'); + Client.getDNSRecords(data.domain, data.location, function (error, result) { + if (error) return Client.error(error); - if (error.message.indexOf('Upgrade to the premium') === 0) { - $scope.appInstall.subscriptionHelperPage = 'app_install_premium_subscription_required.html'; - } else if (error.message.indexOf('The subscription for this Cloudron has expired.') === 0) { - $scope.appInstall.subscriptionHelperPage = 'app_install_subscription_expired.html'; - } else { - $scope.appInstall.subscriptionHelperPage = 'app_install_subscription_required.html'; - } - } else if (error.statusCode === 409) { - if (error.portName) { - $scope.appInstall.error.port = error.message; - } else if (error.domain) { - $scope.appInstall.error.location = error.message; - $scope.appInstallForm.location.$setPristine(); - $('#appInstallLocationInput').focus(); + if (!data.overwriteDns) { + if (result.error || result.needsOverwrite) { + $scope.appInstall.error.location = result.error ? result.error.message : 'DNS Record already exists outside of Cloudron'; + $scope.appInstall.busy = false; + $scope.appInstallForm.location.$setPristine(); + $('#appInstallLocationInput').focus(); + return; + } + } + + Client.installApp($scope.appInstall.app.id, $scope.appInstall.app.manifest, $scope.appInstall.app.title, data, function (error, newAppId) { + if (error) { + if (error.statusCode === 402) { + $scope.appInstall.state = 'subscriptionRequired'; + $scope.appInstall.subscriptionErrorMesssage = error.message; + $('#collapseMediaLinksCarousel').collapse('hide'); + $('#collapseResourceConstraint').collapse('hide'); + $('#collapseInstallForm').collapse('hide'); + $('#collapseSubscriptionRequired').collapse('show'); + + if (error.message.indexOf('Upgrade to the premium') === 0) { + $scope.appInstall.subscriptionHelperPage = 'app_install_premium_subscription_required.html'; + } else if (error.message.indexOf('The subscription for this Cloudron has expired.') === 0) { + $scope.appInstall.subscriptionHelperPage = 'app_install_subscription_expired.html'; + } else { + $scope.appInstall.subscriptionHelperPage = 'app_install_subscription_required.html'; + } + } else if (error.statusCode === 409) { + if (error.portName) { + $scope.appInstall.error.port = error.message; + } else if (error.domain) { + $scope.appInstall.error.location = error.message; + $scope.appInstallForm.location.$setPristine(); + $('#appInstallLocationInput').focus(); + } else { + $scope.appInstall.error.other = error.message; + } + } else if (error.statusCode === 400) { + if (error.field === 'cert') { + $scope.appInstall.error.cert = error.message; + $scope.appInstall.certificateFileName = ''; + $scope.appInstall.certificateFile = null; + $scope.appInstall.keyFileName = ''; + $scope.appInstall.keyFile = null; + } else { + $scope.appInstall.error.other = error.message; + } } else { $scope.appInstall.error.other = error.message; } - } else if (error.statusCode === 400) { - if (error.field === 'cert') { - $scope.appInstall.error.cert = error.message; - $scope.appInstall.certificateFileName = ''; - $scope.appInstall.certificateFile = null; - $scope.appInstall.keyFileName = ''; - $scope.appInstall.keyFile = null; - } else { - $scope.appInstall.error.other = error.message; - } - } else { - $scope.appInstall.error.other = error.message; + + $scope.appInstall.busy = false; + return; } $scope.appInstall.busy = false; - return; - } - $scope.appInstall.busy = false; + // stash new app id for later + $scope.appInstall.app.id = newAppId; - // stash new app id for later - $scope.appInstall.app.id = newAppId; + // we track the postinstall confirmation for the current user's browser + // TODO later we might want to have a notification db to track the state across admins and browsers + if ($scope.appInstall.app.manifest.postInstallMessage) { + localStorage['confirmPostInstall_' + $scope.appInstall.app.id] = true; + } - // we track the postinstall confirmation for the current user's browser - // TODO later we might want to have a notification db to track the state across admins and browsers - if ($scope.appInstall.app.manifest.postInstallMessage) { - localStorage['confirmPostInstall_' + $scope.appInstall.app.id] = true; - } - - // wait for dialog to be fully closed to avoid modal behavior breakage when moving to a different view already - $('#appInstallModal').on('hidden.bs.modal', function () { - $scope.$apply(function () { - $location.path('/apps').search({ }); + // wait for dialog to be fully closed to avoid modal behavior breakage when moving to a different view already + $('#appInstallModal').on('hidden.bs.modal', function () { + $scope.$apply(function () { + $location.path('/apps').search({ }); + }); }); - }); - $('#appInstallModal').modal('hide'); + $('#appInstallModal').modal('hide'); + }); }); } };