2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// create main application module
|
2017-01-04 13:23:39 +01:00
|
|
|
var app = angular.module('Application', ['angular-md5', 'ui-notification']);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
app.controller('SetupController', ['$scope', '$http', 'Client', function ($scope, $http, Client) {
|
|
|
|
|
// Stupid angular location provider either wants html5 location mode or not, do the query parsing on my own
|
|
|
|
|
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; }, {});
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
$scope.initialized = false;
|
|
|
|
|
$scope.busy = false;
|
|
|
|
|
$scope.account = {
|
|
|
|
|
email: '',
|
|
|
|
|
displayName: '',
|
|
|
|
|
requireEmail: false,
|
|
|
|
|
username: '',
|
|
|
|
|
password: ''
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
2017-01-04 13:23:39 +01:00
|
|
|
$scope.error = null;
|
|
|
|
|
$scope.provider = '';
|
|
|
|
|
$scope.apiServerOrigin = '';
|
|
|
|
|
$scope.setupToken = '';
|
|
|
|
|
$scope.createAppstoreAccount = true;
|
2017-01-04 15:06:15 +01:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
$scope.activateCloudron = function () {
|
|
|
|
|
$scope.busy = true;
|
2016-07-27 17:22:03 +02:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
function registerAppstoreAccountIfNeeded(callback) {
|
|
|
|
|
if (!$scope.createAppstoreAccount) return callback(null);
|
2017-01-05 11:02:52 +01:00
|
|
|
if ($scope.provider === 'caas') return callback(null);
|
2016-07-27 17:22:03 +02:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
$http.post($scope.apiServerOrigin + '/api/v1/users', { email: $scope.account.email, password: $scope.account.password }).success(function (data, status) {
|
|
|
|
|
if (status !== 201) return callback({ status: status, data: data });
|
2016-07-27 17:22:03 +02:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
Client.setAppstoreConfig({ userId: data.userId, token: data.accessToken }, callback);
|
|
|
|
|
}).error(function (data, status) {
|
|
|
|
|
callback({ status: status, data: data });
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
Client.createAdmin($scope.account.username, $scope.account.password, $scope.account.email, $scope.account.displayName, $scope.setupToken, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error('Internal error', error);
|
|
|
|
|
$scope.error = error;
|
|
|
|
|
$scope.busy = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
registerAppstoreAccountIfNeeded(function (error) {
|
|
|
|
|
if (error) console.error('Unable to create appstore account.', error); // this is not fatal
|
2015-10-27 12:04:27 -07:00
|
|
|
|
2017-01-05 11:02:52 +01:00
|
|
|
window.location.href = '/';
|
2015-10-27 12:04:27 -07:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
2017-01-04 13:23:39 +01:00
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-12-29 11:00:32 +01:00
|
|
|
Client.getStatus(function (error, status) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) {
|
|
|
|
|
window.location.href = '/error.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 17:25:24 +01:00
|
|
|
// if we are here from the ip first go to the real domain if already setup
|
2017-01-10 23:43:49 +01:00
|
|
|
if (status.provider !== 'caas' && status.adminFqdn && status.adminFqdn !== window.location.hostname) {
|
2017-01-11 22:54:09 +01:00
|
|
|
window.location.href = 'https://' + status.adminFqdn + '/setup.html';
|
2017-01-06 17:25:24 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-29 11:00:32 +01:00
|
|
|
if (status.activated) {
|
2015-07-20 00:09:47 -07:00
|
|
|
window.location.href = '/';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 17:25:24 +01:00
|
|
|
// if we don't have a domain yet, first go to domain setup
|
2017-01-10 23:43:49 +01:00
|
|
|
if (!status.adminFqdn) {
|
2017-01-06 17:25:24 +01:00
|
|
|
window.location.href = '/setupdns.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-29 11:04:42 +01:00
|
|
|
if (status.provider === 'caas') {
|
|
|
|
|
if (!search.setupToken) {
|
|
|
|
|
window.location.href = '/error.html?errorCode=2';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-12-29 11:00:32 +01:00
|
|
|
|
2015-12-29 11:04:42 +01:00
|
|
|
if (!search.email) {
|
|
|
|
|
window.location.href = '/error.html?errorCode=3';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-12-29 11:00:32 +01:00
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
$scope.setupToken = search.setupToken;
|
|
|
|
|
$scope.createAppstoreAccount = false;
|
2015-12-29 11:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-04 13:23:39 +01:00
|
|
|
$scope.account.email = search.email || $scope.account.email;
|
|
|
|
|
$scope.account.displayName = search.displayName || $scope.account.displayName;
|
|
|
|
|
$scope.account.requireEmail = !search.email;
|
|
|
|
|
$scope.provider = status.provider;
|
|
|
|
|
$scope.apiServerOrigin = status.apiServerOrigin;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-07-27 17:32:17 +02:00
|
|
|
$scope.initialized = true;
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}]);
|