2018-01-22 13:01:38 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('Application').controller('MainController', ['$scope', '$route', '$timeout', '$location', 'Client', 'AppStore', function ($scope, $route, $timeout, $location, Client, AppStore) {
|
|
|
|
|
$scope.initialized = false;
|
|
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
$scope.installedApps = Client.getInstalledApps();
|
|
|
|
|
$scope.config = {};
|
|
|
|
|
$scope.status = {};
|
|
|
|
|
$scope.client = Client;
|
|
|
|
|
$scope.appstoreConfig = {};
|
2018-03-28 12:26:24 +02:00
|
|
|
$scope.subscription = {};
|
|
|
|
|
$scope.ready = false;
|
2018-01-22 13:01:38 -08:00
|
|
|
|
|
|
|
|
$scope.hideNavBarActions = $location.path() === '/logs';
|
|
|
|
|
|
|
|
|
|
$scope.isActive = function (url) {
|
|
|
|
|
if (!$route.current) return false;
|
|
|
|
|
return $route.current.$$route.originalPath.indexOf(url) === 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.logout = function (event) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
$scope.initialized = false;
|
|
|
|
|
Client.logout();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.error = function (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
window.location.href = '/error.html';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.waitingForPlanSelection = false;
|
|
|
|
|
$('#setupSubscriptionModal').on('hide.bs.modal', function () {
|
|
|
|
|
$scope.waitingForPlanSelection = false;
|
|
|
|
|
|
|
|
|
|
// check for updates to stay in sync
|
|
|
|
|
Client.checkForUpdates(function (error) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
Client.refreshConfig();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.waitForPlanSelection = function () {
|
|
|
|
|
if ($scope.waitingForPlanSelection) return;
|
|
|
|
|
|
|
|
|
|
$scope.waitingForPlanSelection = true;
|
|
|
|
|
|
|
|
|
|
function checkPlan() {
|
|
|
|
|
if (!$scope.waitingForPlanSelection) return;
|
|
|
|
|
|
|
|
|
|
AppStore.getSubscription($scope.appstoreConfig, function (error, result) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
// check again to give more immediate feedback once a subscription was setup
|
2018-03-14 23:28:36 +01:00
|
|
|
if (result.plan.id === 'free') {
|
2018-01-22 13:01:38 -08:00
|
|
|
$timeout(checkPlan, 5000);
|
|
|
|
|
} else {
|
|
|
|
|
$scope.waitingForPlanSelection = false;
|
|
|
|
|
$('#setupSubscriptionModal').modal('hide');
|
|
|
|
|
if ($scope.config.update && $scope.config.update.box) $('#updateModal').modal('show');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkPlan();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.showSubscriptionModal = function () {
|
|
|
|
|
$('#setupSubscriptionModal').modal('show');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function runConfigurationChecks() {
|
|
|
|
|
// warn user if dns config is not working (the 'configuring' flag detects if configureWebadmin is 'active')
|
|
|
|
|
if (!$scope.status.webadminStatus.configuring && !$scope.status.webadminStatus.dns) {
|
2018-03-30 15:12:14 +02:00
|
|
|
var dnsActionScope = $scope.$new(true);
|
|
|
|
|
dnsActionScope.action = '/#/certs';
|
|
|
|
|
Client.notify('Invalid Domain Config', 'Unable to update DNS. Click here to update it.', true, 'error', dnsActionScope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($scope.config.update && $scope.config.update.box) {
|
|
|
|
|
var updateActionScope = $scope.$new(true);
|
|
|
|
|
updateActionScope.action = '/#/settings';
|
|
|
|
|
Client.notify('Update Available', 'Update now to version ' + $scope.config.update.box.version + '.', true, 'success', updateActionScope);
|
2018-01-22 13:01:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.getBackupConfig(function (error, backupConfig) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
if (backupConfig.provider === 'noop') {
|
|
|
|
|
var actionScope = $scope.$new(true);
|
|
|
|
|
actionScope.action = '/#/settings';
|
|
|
|
|
|
|
|
|
|
Client.notify('Backup Configuration', 'Cloudron backups are disabled. Ensure the server is backed up using alternate means.', false, 'info', actionScope);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 14:19:34 +02:00
|
|
|
$scope.fetchAppstoreProfileAndSubscription = function (callback) {
|
|
|
|
|
if (!$scope.appstoreConfig.token) return callback();
|
|
|
|
|
|
|
|
|
|
AppStore.getProfile($scope.appstoreConfig.token, function (error, result) {
|
2018-03-28 14:32:21 +02:00
|
|
|
if (error) return callback(error);
|
2018-03-28 14:19:34 +02:00
|
|
|
|
|
|
|
|
$scope.appstoreConfig.profile = result;
|
|
|
|
|
|
|
|
|
|
AppStore.getSubscription($scope.appstoreConfig, function (error, result) {
|
2018-03-28 14:32:21 +02:00
|
|
|
if (error) return callback(error);
|
2018-03-28 14:19:34 +02:00
|
|
|
|
|
|
|
|
$scope.subscription = result;
|
2018-03-28 14:32:21 +02:00
|
|
|
|
|
|
|
|
callback();
|
2018-03-28 14:19:34 +02:00
|
|
|
});
|
|
|
|
|
});
|
2018-04-05 21:49:15 +02:00
|
|
|
};
|
2018-03-28 14:19:34 +02:00
|
|
|
|
2018-01-22 13:01:38 -08:00
|
|
|
Client.getStatus(function (error, status) {
|
|
|
|
|
if (error) return $scope.error(error);
|
|
|
|
|
|
|
|
|
|
// WARNING if anything about the routing is changed here test these use-cases:
|
|
|
|
|
//
|
|
|
|
|
// 1. Caas
|
|
|
|
|
// 2. selfhosted with --domain argument
|
|
|
|
|
// 3. selfhosted restore
|
|
|
|
|
// 4. local development with gulp develop
|
|
|
|
|
|
|
|
|
|
if (!status.activated) {
|
|
|
|
|
console.log('Not activated yet, redirecting', status);
|
|
|
|
|
window.location.href = status.adminFqdn ? '/setup.html' : '/setupdns.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// support local development with localhost check
|
|
|
|
|
if (window.location.hostname !== status.adminFqdn && window.location.hostname !== 'localhost') {
|
|
|
|
|
// user is accessing by IP or by the old admin location (pre-migration)
|
|
|
|
|
window.location.href = '/setupdns.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.status = status;
|
|
|
|
|
|
|
|
|
|
Client.refreshConfig(function (error) {
|
|
|
|
|
if (error) return $scope.error(error);
|
|
|
|
|
|
|
|
|
|
// check version and force reload if needed
|
|
|
|
|
if (!localStorage.version) {
|
|
|
|
|
localStorage.version = Client.getConfig().version;
|
|
|
|
|
} else if (localStorage.version !== Client.getConfig().version) {
|
|
|
|
|
localStorage.version = Client.getConfig().version;
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.refreshUserInfo(function (error) {
|
|
|
|
|
if (error) return $scope.error(error);
|
|
|
|
|
|
|
|
|
|
Client.refreshInstalledApps(function (error) {
|
|
|
|
|
if (error) return $scope.error(error);
|
|
|
|
|
|
|
|
|
|
// now mark the Client to be ready
|
|
|
|
|
Client.setReady();
|
|
|
|
|
|
|
|
|
|
$scope.config = Client.getConfig();
|
|
|
|
|
|
|
|
|
|
$scope.initialized = true;
|
|
|
|
|
|
|
|
|
|
if ($scope.user.admin) {
|
|
|
|
|
runConfigurationChecks();
|
|
|
|
|
|
2018-03-14 19:53:31 +01:00
|
|
|
Client.getAppstoreConfig(function (error, result) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
if (!result.token) return;
|
|
|
|
|
|
|
|
|
|
$scope.appstoreConfig = result;
|
|
|
|
|
|
2018-03-28 14:32:21 +02:00
|
|
|
$scope.fetchAppstoreProfileAndSubscription(function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
2018-03-28 14:19:34 +02:00
|
|
|
$scope.ready = true;
|
2018-03-14 19:53:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
2018-01-22 13:01:38 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Client.onConfig(function (config) {
|
|
|
|
|
// check if we are actually updating
|
|
|
|
|
if (config.progress.update && config.progress.update.percent !== -1) {
|
|
|
|
|
window.location.href = '/update.html';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.cloudronName) {
|
|
|
|
|
document.title = config.cloudronName;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// setup all the dialog focus handling
|
|
|
|
|
['updateModal'].forEach(function (id) {
|
|
|
|
|
$('#' + id).on('shown.bs.modal', function () {
|
|
|
|
|
$(this).find("[autofocus]:first").focus();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}]);
|