2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-10-11 10:32:22 +02:00
|
|
|
angular.module('Application').controller('MainController', ['$scope', '$route', '$interval', 'Client', function ($scope, $route, $interval, Client) {
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.initialized = false;
|
|
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
$scope.installedApps = Client.getInstalledApps();
|
|
|
|
|
$scope.config = {};
|
2016-01-20 16:55:41 +01:00
|
|
|
$scope.client = Client;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
$scope.update = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: {},
|
|
|
|
|
password: ''
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$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.showUpdateModal = function (form) {
|
2016-11-07 12:17:55 +01:00
|
|
|
$scope.update.error.generic = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.update.error.password = null;
|
|
|
|
|
$scope.update.password = '';
|
|
|
|
|
|
|
|
|
|
form.$setPristine();
|
|
|
|
|
form.$setUntouched();
|
|
|
|
|
|
|
|
|
|
$('#updateModal').modal('show');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.doUpdate = function () {
|
2016-11-07 12:17:55 +01:00
|
|
|
$scope.update.error.generic = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.update.error.password = null;
|
|
|
|
|
|
|
|
|
|
$scope.update.busy = true;
|
|
|
|
|
Client.update($scope.update.password, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
if (error.statusCode === 403) {
|
2016-01-21 14:53:49 +01:00
|
|
|
$scope.update.error.password = true;
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.update.password = '';
|
2016-01-21 14:53:49 +01:00
|
|
|
$scope.update_form.password.$setPristine();
|
2015-07-20 00:09:47 -07:00
|
|
|
$('#inputUpdatePassword').focus();
|
2016-11-07 12:17:55 +01:00
|
|
|
} else if (error.statusCode === 409) {
|
|
|
|
|
$scope.update.error.generic = 'Please try again later. The Cloudron is creating a backup at the moment.';
|
|
|
|
|
$scope.update.password = '';
|
|
|
|
|
$scope.update_form.password.$setPristine();
|
|
|
|
|
$('#inputUpdatePassword').focus();
|
2015-07-20 00:09:47 -07:00
|
|
|
} else {
|
2016-11-07 12:17:55 +01:00
|
|
|
$scope.update.error.generic = error.message;
|
2015-07-20 00:09:47 -07:00
|
|
|
console.error('Unable to update.', error);
|
|
|
|
|
}
|
|
|
|
|
$scope.update.busy = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.location.href = '/update.html';
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-11 10:32:05 +02:00
|
|
|
function runConfigurationChecks() {
|
2017-02-02 14:43:34 -08:00
|
|
|
Client.getDnsConfig(function (error, result) {
|
2016-12-09 09:19:23 +01:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
2017-04-18 14:41:02 -07:00
|
|
|
// check if we have aws credentials if selfhosting
|
|
|
|
|
if ($scope.config.isCustomDomain) {
|
|
|
|
|
if (result.provider === 'route53' && (!result.accessKeyId || !result.secretAccessKey)) {
|
|
|
|
|
var actionScope = $scope.$new(true);
|
|
|
|
|
actionScope.action = '/#/certs';
|
|
|
|
|
Client.notify('Missing AWS credentials', 'Please provide AWS credentials, click here to add them.', true, 'error', actionScope);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 14:34:20 -08:00
|
|
|
if (result.provider === 'caas') return;
|
2016-12-19 16:22:37 +01:00
|
|
|
|
2017-04-18 14:41:02 -07:00
|
|
|
// Check if all email DNS records are set up properly only for non external DNS API
|
2017-03-09 15:43:13 -08:00
|
|
|
Client.getEmailStatus(function (error, result) {
|
2017-02-02 14:43:34 -08:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
2017-03-09 15:43:13 -08:00
|
|
|
if (!result.dns.spf.status || !result.dns.dkim.status || !result.dns.ptr.status || !result.outboundPort25.status) {
|
2017-02-02 14:43:34 -08:00
|
|
|
var actionScope = $scope.$new(true);
|
|
|
|
|
actionScope.action = '/#/settings';
|
|
|
|
|
|
2017-04-07 17:08:52 +02:00
|
|
|
Client.notify('DNS Configuration', 'Please setup all required DNS records to guarantee correct mail delivery', false, 'info', actionScope);
|
2017-02-02 14:43:34 -08:00
|
|
|
}
|
|
|
|
|
});
|
2016-12-09 09:19:23 +01:00
|
|
|
});
|
2016-10-11 10:32:05 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-29 11:00:32 +01:00
|
|
|
Client.getStatus(function (error, status) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return $scope.error(error);
|
2016-07-27 14:15:22 +02:00
|
|
|
|
2017-03-23 14:01:21 +01:00
|
|
|
// 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
|
|
|
|
|
|
2017-02-03 16:27:58 -08:00
|
|
|
if (!status.activated) {
|
2017-04-25 15:18:09 -07:00
|
|
|
console.log('Not activated yet, redirecting', status);
|
|
|
|
|
window.location.href = status.adminFqdn ? '/setup.html' : '/setupdns.html';
|
2016-07-27 14:15:22 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-10 21:23:16 +01:00
|
|
|
// support local development with localhost check
|
|
|
|
|
if (window.location.hostname !== status.adminFqdn && window.location.hostname !== 'localhost') {
|
2017-03-23 14:01:21 +01:00
|
|
|
window.location.href = '/setupdns.html';
|
2017-01-10 21:23:16 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
Client.refreshConfig(function (error) {
|
|
|
|
|
if (error) return $scope.error(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
// 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);
|
|
|
|
|
}
|
2015-09-10 13:58:24 +02:00
|
|
|
|
2016-01-18 16:35:25 +01:00
|
|
|
|
2016-05-06 14:23:21 +02:00
|
|
|
Client.refreshUserInfo(function (error) {
|
2015-09-10 14:16:59 +02:00
|
|
|
if (error) return $scope.error(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
Client.refreshInstalledApps(function (error) {
|
|
|
|
|
if (error) return $scope.error(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
// kick off installed apps and config polling
|
2016-04-02 22:47:40 +02:00
|
|
|
var refreshAppsTimer = $interval(Client.refreshInstalledApps.bind(Client), 5000);
|
2015-09-10 14:16:59 +02:00
|
|
|
var refreshConfigTimer = $interval(Client.refreshConfig.bind(Client), 5000);
|
|
|
|
|
var refreshUserInfoTimer = $interval(Client.refreshUserInfo.bind(Client), 5000);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
|
$interval.cancel(refreshAppsTimer);
|
|
|
|
|
$interval.cancel(refreshConfigTimer);
|
|
|
|
|
$interval.cancel(refreshUserInfoTimer);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
// now mark the Client to be ready
|
|
|
|
|
Client.setReady();
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
$scope.config = Client.getConfig();
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-10 14:16:59 +02:00
|
|
|
$scope.initialized = true;
|
2016-01-14 16:01:08 +01:00
|
|
|
|
2017-01-17 12:44:07 +01:00
|
|
|
if ($scope.user.admin) runConfigurationChecks();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
2015-09-10 14:16:59 +02:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// wait till the view has loaded until showing a modal dialog
|
|
|
|
|
Client.onConfig(function (config) {
|
2015-07-29 14:15:44 +02:00
|
|
|
// check if we are actually updating
|
|
|
|
|
if (config.progress.update && config.progress.update.percent !== -1) {
|
2015-07-20 00:09:47 -07:00
|
|
|
window.location.href = '/update.html';
|
|
|
|
|
}
|
2016-06-02 13:17:01 -07:00
|
|
|
|
|
|
|
|
if (config.cloudronName) {
|
|
|
|
|
document.title = config.cloudronName;
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// setup all the dialog focus handling
|
2016-08-01 13:42:28 +02:00
|
|
|
['updateModal'].forEach(function (id) {
|
2015-07-20 00:09:47 -07:00
|
|
|
$('#' + id).on('shown.bs.modal', function () {
|
|
|
|
|
$(this).find("[autofocus]:first").focus();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}]);
|