Files
cloudron-box/src/views/apps.js
2019-08-29 11:33:22 -07:00

726 lines
28 KiB
JavaScript

'use strict';
/* global angular:false */
/* global $:false */
angular.module('Application').controller('AppsController', ['$scope', '$location', '$timeout', '$interval', 'Client', function ($scope, $location, $timeout, $interval, Client) {
var ALL_DOMAINS_DOMAIN = { _alldomains: true, domain: 'All Domains' }; // dummy record for the single select filter
$scope.HOST_PORT_MIN = 1024;
$scope.HOST_PORT_MAX = 65535;
$scope.installedApps = Client.getInstalledApps();
$scope.tags = Client.getAppTags();
$scope.selectedTags = [];
$scope.selectedDomain = ALL_DOMAINS_DOMAIN;
$scope.filterDomains = [ ALL_DOMAINS_DOMAIN ];
$scope.config = Client.getConfig();
$scope.user = Client.getUserInfo();
$scope.domains = [];
$scope.groups = [];
$scope.users = [];
$scope.backupsEnabled = true;
$scope.disableIndexingTemplate = '# Disable search engine indexing\n\nUser-agent: *\nDisallow: /';
$scope.appConfigure = {
busy: false,
error: {},
app: {},
domain: null,
location: '',
advancedVisible: false,
portBindings: {},
portBindingsEnabled: {},
portBindingsInfo: {},
robotsTxt: '',
certificateFile: null,
certificateFileName: '',
keyFile: null,
keyFileName: '',
memoryLimit: 0,
memoryTicks: [],
mailboxName: '',
accessRestrictionOption: 'any',
accessRestriction: { users: [], groups: [] },
dataDir: null,
alternateDomains: [],
mailboxNameEnabled: false,
dataDirEnabled: false,
ssoAuth: false,
ftp: false,
tags: '',
label: '',
icon: { data: null },
action: 'location',
iconUrl: function () {
if ($scope.appConfigure.icon.data === '__original__') { // user clicked reset
return $scope.appConfigure.app.iconUrl + '&original=true';
} else if ($scope.appConfigure.icon.data) { // user uploaded icon
return $scope.appConfigure.icon.data;
} else { // current icon
return $scope.appConfigure.app.iconUrl;
}
},
isAccessRestrictionValid: function () {
var tmp = $scope.appConfigure.accessRestriction;
return !!(tmp.users.length || tmp.groups.length);
},
addAlternateDomain: function (event) {
event.preventDefault();
$scope.appConfigure.alternateDomains.push({
domain: $scope.domains[0],
subdomain: ''
});
},
delAlternateDomain: function (event, index) {
event.preventDefault();
$scope.appConfigure.alternateDomains.splice(index, 1);
},
show: function (app) {
$scope.reset();
// fill relevant info from the app
$scope.appConfigure.app = app;
$scope.appConfigure.location = app.location;
$scope.appConfigure.domain = $scope.domains.filter(function (d) { return d.domain === app.domain; })[0];
$scope.appConfigure.portBindingsInfo = angular.extend({}, app.manifest.tcpPorts, app.manifest.udpPorts); // Portbinding map only for information
$scope.appConfigure.memoryLimit = app.memoryLimit || app.manifest.memoryLimit || (256 * 1024 * 1024);
$scope.appConfigure.dataDirEnabled = !!app.dataDir;
$scope.appConfigure.dataDir = app.dataDir;
$scope.appConfigure.alternateDomains = app.alternateDomains.map(function (a) { return { subdomain: a.subdomain, domain: $scope.domains.filter(function (d) { return d.domain === a.domain; })[0] };});
$scope.appConfigure.robotsTxt = app.robotsTxt;
$scope.appConfigure.enableBackup = app.enableBackup;
$scope.appConfigure.enableAutomaticUpdate = app.enableAutomaticUpdate;
$scope.appConfigure.mailboxNameEnabled = app.mailboxName && (app.mailboxName.match(/\.app$/) === null);
$scope.appConfigure.mailboxName = app.mailboxName || '';
$scope.appConfigure.label = app.label || '';
$scope.appConfigure.ssoAuth = (app.manifest.addons['ldap'] || app.manifest.addons['oauth']) && app.sso;
$scope.appConfigure.ftp = app.manifest.addons.localstorage && app.manifest.addons.localstorage.ftp;
// create ticks starting from manifest memory limit. the memory limit here is currently split into ram+swap (and thus *2 below)
// TODO: the *2 will overallocate since 4GB is max swap that cloudron itself allocates
$scope.appConfigure.memoryTicks = [ ];
var npow2 = Math.pow(2, Math.ceil(Math.log($scope.config.memory)/Math.log(2)));
for (var i = 256; i <= (npow2*2/1024/1024); i *= 2) {
if (i >= (app.manifest.memoryLimit/1024/1024 || 0)) $scope.appConfigure.memoryTicks.push(i * 1024 * 1024);
}
if (app.manifest.memoryLimit && $scope.appConfigure.memoryTicks[0] !== app.manifest.memoryLimit) {
$scope.appConfigure.memoryTicks.unshift(app.manifest.memoryLimit);
}
$scope.appConfigure.accessRestrictionOption = app.accessRestriction ? 'groups' : 'any';
$scope.appConfigure.accessRestriction = { users: [], groups: [] };
if (app.accessRestriction) {
var userSet = { };
app.accessRestriction.users.forEach(function (uid) { userSet[uid] = true; });
$scope.users.forEach(function (u) { if (userSet[u.id] === true) $scope.appConfigure.accessRestriction.users.push(u); });
var groupSet = { };
app.accessRestriction.groups.forEach(function (gid) { groupSet[gid] = true; });
$scope.groups.forEach(function (g) { if (groupSet[g.id] === true) $scope.appConfigure.accessRestriction.groups.push(g); });
}
// fill the portBinding structures. There might be holes in the app.portBindings, which signalizes a disabled port
for (var env in $scope.appConfigure.portBindingsInfo) {
if (app.portBindings && app.portBindings[env]) {
$scope.appConfigure.portBindings[env] = app.portBindings[env];
$scope.appConfigure.portBindingsEnabled[env] = true;
} else {
$scope.appConfigure.portBindings[env] = $scope.appConfigure.portBindingsInfo[env].defaultValue || 0;
$scope.appConfigure.portBindingsEnabled[env] = false;
}
}
// translate for tag-input
$scope.appConfigure.tags = app.tags ? app.tags.join(',') : '';
$scope.appConfigure.icon = { data: null };
$('#iconFileInput').get(0).onchange = function (event) {
var fr = new FileReader();
fr.onload = function () {
$scope.$apply(function () {
// var file = event.target.files[0];
$scope.appConfigure.icon.data = fr.result;
});
};
fr.readAsDataURL(event.target.files[0]);
};
$('#appConfigureModal').modal('show');
},
submit: function () {
$scope.appConfigure.busy = true;
$scope.appConfigure.error.other = null;
$scope.appConfigure.error.location = null;
$scope.appConfigure.error.label = null;
$scope.appConfigure.error.dataDir = null;
$scope.appConfigure.error.alternateDomains = null;
$scope.appConfigure.error.mailboxName = null;
// only use enabled ports from portBindings
var finalPortBindings = {};
for (var env in $scope.appConfigure.portBindings) {
if ($scope.appConfigure.portBindingsEnabled[env]) {
finalPortBindings[env] = $scope.appConfigure.portBindings[env];
}
}
var finalAccessRestriction = null;
if ($scope.appConfigure.accessRestrictionOption === 'groups') {
finalAccessRestriction = { users: [], groups: [] };
finalAccessRestriction.users = $scope.appConfigure.accessRestriction.users.map(function (u) { return u.id; });
finalAccessRestriction.groups = $scope.appConfigure.accessRestriction.groups.map(function (g) { return g.id; });
}
var data = {
location: $scope.appConfigure.location,
domain: $scope.appConfigure.domain.domain,
portBindings: finalPortBindings,
accessRestriction: finalAccessRestriction,
cert: $scope.appConfigure.certificateFile,
key: $scope.appConfigure.keyFile,
memoryLimit: $scope.appConfigure.memoryLimit === $scope.appConfigure.memoryTicks[0] ? 0 : $scope.appConfigure.memoryLimit,
robotsTxt: $scope.appConfigure.robotsTxt,
enableBackup: $scope.appConfigure.enableBackup,
enableAutomaticUpdate: $scope.appConfigure.enableAutomaticUpdate,
alternateDomains: $scope.appConfigure.alternateDomains.map(function (a) { return { subdomain: a.subdomain, domain: a.domain.domain };}),
label: $scope.appConfigure.label,
tags: $scope.appConfigure.tags.split(',').map(function (t) { return t.trim(); }).filter(function (t) { return !!t; }),
dataDir: $scope.appConfigure.dataDirEnabled ? $scope.appConfigure.dataDir : ''
};
if ($scope.appConfigure.mailboxNameEnabled) {
data.mailboxName = $scope.appConfigure.mailboxName;
// add mailbox automatically for convenience
if ($scope.appConfigure.app.manifest.addons.recvmail) {
Client.addMailbox(data.domain, data.mailboxName, $scope.user.id, function (error) {
if (error && error.statusCode !== 409) console.error(error); // it's fine if it already exists
});
}
} else { // setting to empty will reset to .app name
data.mailboxName = '';
}
if ($scope.appConfigure.icon.data === '__original__') { // user reset the icon
data.icon = '';
} else if ($scope.appConfigure.icon.data) { // user loaded custom icon
data.icon = $scope.appConfigure.icon.data.replace(/^data:image\/[a-z]+;base64,/, '');
}
Client.configureApp($scope.appConfigure.app.id, data, function (error) {
let tab = 'advanced'; // the tab to switch to
if (error) {
if (error.statusCode === 409 && (error.message.indexOf('Port') !== -1)) {
$scope.appConfigure.error.port = error.message;
tab = 'location';
} else if (error.statusCode === 400 && error.message.indexOf('mailbox') !== -1 ) {
$scope.appConfigure.error.mailboxName = error.message;
$scope.appConfigureForm.mailboxName.$setPristine();
$('#appConfigureMailboxNameInput').focus();
} else if (error.statusCode === 409 && error.message.indexOf('Domain') === 0) {
$scope.appConfigure.error.location = error.message;
$scope.appConfigureForm.location.$setPristine();
tab = 'location';
$('#appConfigureLocationInput').focus();
} else if (error.statusCode === 409 && error.message.indexOf('Alternate domain') === 0 ) {
$scope.appConfigure.error.alternateDomains = error.message;
tab = 'location';
} else if (error.statusCode === 400 && error.message.indexOf('cert') !== -1 ) {
$scope.appConfigure.error.cert = error.message;
$scope.appConfigure.certificateFileName = '';
$scope.appConfigure.certificateFile = null;
$scope.appConfigure.keyFileName = '';
$scope.appConfigure.keyFile = null;
} else if (error.message.indexOf('dataDir') !== -1 ) { // can be 400 or 409
$scope.appConfigure.error.dataDir = error.message;
$scope.appConfigureForm.dataDir.$setPristine();
$('#appConfigureDataDirInput').focus();
} else {
$scope.appConfigure.error.other = error.message;
tab = 'location';
}
$scope.appConfigure.action = tab;
$scope.appConfigure.busy = false;
return;
}
$scope.appConfigure.busy = false;
Client.refreshAppCache($scope.appConfigure.app.id); // reflect the new app state immediately
$('#appConfigureModal').modal('hide');
$scope.reset();
});
},
resetCustomIcon: function () {
$scope.appConfigure.icon.data = '__original__';
},
showCustomIconSelector: function () {
$('#iconFileInput').click();
}
};
$scope.appUninstall = {
busy: false,
error: {},
app: {},
show: function (app) {
$scope.reset();
$scope.appUninstall.app = app;
$('#appUninstallModal').modal('show');
},
submit: function () {
$scope.appUninstall.busy = true;
Client.uninstallApp($scope.appUninstall.app.id, function (error) {
if (error && error.statusCode === 402) { // unpurchase failed
Client.error('Relogin to Cloudron App Store');
} else if (error) {
Client.error(error);
} else {
$('#appUninstallModal').modal('hide');
Client.refreshAppCache($scope.appUninstall.app.id); // reflect the new app state immediately
$scope.reset();
}
$scope.appUninstall.busy = false;
});
}
};
$scope.appClone = {
busy: false,
error: {},
app: {},
backup: {},
// form
location: '',
domain: null,
portBindings: {},
portBindingsInfo: {},
portBindingsEnabled: {},
show: function (app, backup) {
// hide restore modal if open
$('#appRestoreModal').modal('hide');
$scope.appClone.busy = false;
$scope.appClone.error = {};
$scope.appClone.app = app;
$scope.appClone.backup = backup;
$scope.appClone.location = '';
$scope.appClone.domain = $scope.domains.find(function (d) { return app.domain === d.domain; }); // pre-select the app's domain
$scope.appClone.portBindingsInfo = angular.extend({}, $scope.appClone.app.manifest.tcpPorts, $scope.appClone.app.manifest.udpPorts); // Portbinding map only for information
// set default ports
for (var env in $scope.appClone.portBindingsInfo) {
$scope.appClone.portBindings[env] = $scope.appClone.portBindingsInfo[env].defaultValue || 0;
$scope.appClone.portBindingsEnabled[env] = true;
}
$('#appCloneModal').modal('show');
},
submit: function () {
$scope.appClone.busy = true;
// only use enabled ports from portBindings
var finalPortBindings = {};
for (var env in $scope.appClone.portBindings) {
if ($scope.appClone.portBindingsEnabled[env]) {
finalPortBindings[env] = $scope.appClone.portBindings[env];
}
}
var data = {
location: $scope.appClone.location,
domain: $scope.appClone.domain.domain,
portBindings: finalPortBindings,
backupId: $scope.appClone.backup.id
};
Client.cloneApp($scope.appClone.app.id, data, function (error, clonedApp) {
$scope.appClone.busy = false;
if (error) {
if (error.statusCode === 409 && (error.message.indexOf('is reserved') !== -1 || error.message.indexOf('is already in use') !== -1)) {
$scope.appClone.error.port = error.message;
} else if (error.statusCode === 409) {
$scope.appClone.error.location = 'This location is already taken.';
$('#appCloneLocationInput').focus();
} else {
Client.error(error);
}
return;
}
$('#appCloneModal').modal('hide');
Client.refreshAppCache(clonedApp.id); // reflect the new app state immediately
});
}
};
$scope.appRestore = {
busy: false,
busyFetching: false,
error: {},
app: {},
backups: [],
copyBackupIdDone: false,
copyBackupId: function (backup) {
var copyText = document.getElementById('appRestoreBackupIdHelper');
copyText.value = backup.id;
copyText.select();
document.execCommand('copy');
$scope.appRestore.copyBackupIdDone = true;
// reset after 2.5sec
$timeout(function () { $scope.appRestore.copyBackupIdDone = false; }, 2500);
},
createBackup: function () {
Client.backupApp($scope.appRestore.app.id, function (error) {
if (error) Client.error(error);
function waitForBackupFinish() {
if ($scope.appRestore.app.installationState === 'pending_backup') return $timeout(waitForBackupFinish, 1000);
// we are done, refresh the backup list
Client.getAppBackups($scope.appRestore.app.id, function (error, backups) {
if (error) return Client.error(error);
$scope.appRestore.backups = backups;
});
}
// reflect the new app state immediately
Client.refreshAppCache($scope.appRestore.app.id, waitForBackupFinish);
});
},
show: function (app) {
$scope.reset();
$scope.appRestore.app = app;
$scope.appRestore.busyFetching = true;
$('#appRestoreModal').modal('show');
Client.getAppBackups(app.id, function (error, backups) {
if (error) {
Client.error(error);
} else {
$scope.appRestore.backups = backups;
$scope.appRestore.busyFetching = false;
}
});
return false; // prevent propagation and default
},
restore: function (backup) {
$scope.appRestore.busy = true;
Client.restoreApp($scope.appRestore.app.id, backup.id, function (error) {
if (error) {
Client.error(error);
} else {
$('#appRestoreModal').modal('hide');
}
$scope.appRestore.busy = false;
Client.refreshAppCache($scope.appRestore.app.id); // reflect the new app state immediately
});
}
};
$scope.appInfo = {
app: {},
message: ''
};
$scope.appPostInstallConfirm = {
app: {},
message: '',
confirmed: false,
show: function (app) {
$scope.reset();
$scope.appPostInstallConfirm.app = app;
$scope.appPostInstallConfirm.message = app.manifest.postInstallMessage;
$('#appPostInstallConfirmModal').modal('show');
return false; // prevent propagation and default
},
submit: function () {
if (!$scope.appPostInstallConfirm.confirmed) return;
$scope.appPostInstallConfirm.app.pendingPostInstallConfirmation = false;
delete localStorage['confirmPostInstall_' + $scope.appPostInstallConfirm.app.id];
$('#appPostInstallConfirmModal').modal('hide');
}
};
$scope.appError = {
app: {}
};
$scope.appUpdate = {
busy: false,
error: {},
app: {},
manifest: {},
portBindings: {},
show: function (app, updateManifest) {
$scope.reset();
$scope.appUpdate.app = app;
$scope.appUpdate.manifest = angular.copy(updateManifest);
$('#appUpdateModal').modal('show');
},
submit: function () {
$scope.appUpdate.busy = true;
Client.updateApp($scope.appUpdate.app.id, $scope.appUpdate.manifest, function (error) {
if (error) {
Client.error(error);
} else {
$scope.appUpdate.app = {};
$('#appUpdateModal').modal('hide');
}
$scope.appUpdate.busy = false;
Client.refreshAppCache($scope.appUpdate.app.id); // reflect the new app state immediately
});
}
};
$scope.reset = function () {
// close all dialogs
$('#appErrorModal').modal('hide');
$('#appConfigureModal').modal('hide');
$('#appRestoreModal').modal('hide');
$('#appUpdateModal').modal('hide');
$('#appInfoModal').modal('hide');
$('#appUninstallModal').modal('hide');
$('#appPostInstallConfirmModal').modal('hide');
// reset configure dialog
$scope.appConfigure.error = {};
$scope.appConfigure.app = {};
$scope.appConfigure.domain = null;
$scope.appConfigure.location = '';
$scope.appConfigure.advancedVisible = false;
$scope.appConfigure.portBindings = {}; // This is the actual model holding the env:port pair
$scope.appConfigure.portBindingsEnabled = {}; // This is the actual model holding the enabled/disabled flag
$scope.appConfigure.certificateFile = null;
$scope.appConfigure.certificateFileName = '';
$scope.appConfigure.keyFile = null;
$scope.appConfigure.keyFileName = '';
$scope.appConfigure.memoryLimit = 0;
$scope.appConfigure.memoryTicks = [];
$scope.appConfigure.accessRestrictionOption = 'any';
$scope.appConfigure.accessRestriction = { users: [], groups: [] };
$scope.appConfigure.ssoAuth = false;
$scope.appConfigure.ftp = false;
$scope.appConfigure.robotsTxt = '';
$scope.appConfigure.enableBackup = true;
$scope.appConfigure.enableAutomaticUpdate = true;
$scope.appConfigureForm.$setPristine();
$scope.appConfigureForm.$setUntouched();
// reset uninstall dialog
$scope.appUninstall.app = {};
$scope.appUninstall.error = {};
// reset update dialog
$scope.appUpdate.error = {};
$scope.appUpdate.app = {};
$scope.appUpdate.manifest = {};
// reset restore dialog
$scope.appRestore.error = {};
$scope.appRestore.app = {};
$scope.appRestore.backups = [];
$scope.appRestore.location = '';
$scope.appRestore.domain = null;
$scope.appRestore.portBindings = {};
$scope.appRestore.portBindingsInfo = {};
$scope.appRestore.portBindingsEnabled = {};
$scope.appRestore.action = 'restore';
// post install confirmation dialog
$scope.appPostInstallConfirm.app = {};
$scope.appPostInstallConfirm.message = '';
$scope.appPostInstallConfirm.confirmed = false;
};
$scope.readCertificate = function (event) {
$scope.$apply(function () {
$scope.appConfigure.certificateFile = null;
$scope.appConfigure.certificateFileName = event.target.files[0].name;
var reader = new FileReader();
reader.onload = function (result) {
if (!result.target || !result.target.result) return console.error('Unable to read local file');
$scope.appConfigure.certificateFile = result.target.result;
};
reader.readAsText(event.target.files[0]);
});
};
$scope.readKey = function (event) {
$scope.$apply(function () {
$scope.appConfigure.keyFile = null;
$scope.appConfigure.keyFileName = event.target.files[0].name;
var reader = new FileReader();
reader.onload = function (result) {
if (!result.target || !result.target.result) return console.error('Unable to read local file');
$scope.appConfigure.keyFile = result.target.result;
};
reader.readAsText(event.target.files[0]);
});
};
$scope.showInformation = function (app) {
$scope.reset();
$scope.appInfo.app = app;
$scope.appInfo.message = app.manifest.postInstallMessage;
$('#appInfoModal').modal('show');
return false; // prevent propagation and default
};
$scope.showError = function (app) {
$scope.reset();
$scope.appError.app = app;
$('#appErrorModal').modal('show');
return false; // prevent propagation and default
};
$scope.renderAccessRestrictionUser = function (userId) {
var user = $scope.users.filter(function (u) { return u.id === userId; })[0];
// user not found
if (!user) return userId;
return user.username ? user.username : user.email;
};
$scope.cancel = function () {
window.history.back();
};
function fetchUsers() {
Client.getUsers(function (error, users) {
if (error) {
console.error(error);
return $timeout(fetchUsers, 5000);
}
// ensure we have something to work with in the access restriction dropdowns
users.forEach(function (user) { user.display = user.username || user.email; });
$scope.users = users;
});
}
function fetchGroups() {
Client.getGroups(function (error, groups) {
if (error) {
console.error(error);
return $timeout(fetchUsers, 5000);
}
$scope.groups = groups;
});
}
function getDomains() {
Client.getDomains(function (error, result) {
if (error) {
console.error(error);
return $timeout(getDomains, 5000);
}
$scope.domains = result;
$scope.filterDomains = [ALL_DOMAINS_DOMAIN].concat(result);
});
}
function getBackupConfig() {
Client.getBackupConfig(function (error, backupConfig) {
if (error) return console.error(error);
$scope.backupEnabled = backupConfig.provider !== 'noop';
});
}
function refreshInstalledApps() {
Client.refreshInstalledApps();
}
Client.onReady(function () {
refreshInstalledApps(); // refresh the new list immediately when switching from another view (appstore)
if ($scope.user.admin) {
fetchUsers();
fetchGroups();
getDomains();
getBackupConfig();
}
var refreshAppsTimer = $interval(refreshInstalledApps, 5000);
$scope.$on('$destroy', function () {
$interval.cancel(refreshAppsTimer);
});
});
// setup all the dialog focus handling
['appConfigureModal', 'appUninstallModal', 'appUpdateModal', 'appRestoreModal', 'appInfoModal', 'appErrorModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
$(this).find("[autofocus]:first").focus();
});
});
$('.modal-backdrop').remove();
}]);