Files
cloudron-box/dashboard/public/views/profile.js

813 lines
30 KiB
JavaScript
Raw Normal View History

2018-01-22 13:01:38 -08:00
'use strict';
/* global async, Clipboard */
2020-06-03 23:17:06 +02:00
/* global angular */
/* global $ */
/* global TOKEN_TYPES */
angular.module('Application').controller('ProfileController', ['$scope', '$translate', '$location', 'Client', '$timeout', function ($scope, $translate, $location, Client, $timeout) {
2018-01-22 13:01:38 -08:00
$scope.user = Client.getUserInfo();
$scope.config = Client.getConfig();
2020-02-01 10:04:09 -08:00
$scope.apps = Client.getInstalledApps();
2018-01-22 13:01:38 -08:00
$scope.language = '';
$scope.languages = [];
$scope.$watch('language', function (newVal, oldVal) {
if (newVal === oldVal) return;
Client.setProfileLanguage(newVal.id, function (error) {
if (error) return console.error('Failed to reset password:', error);
});
$translate.use(newVal.id); // this switches the language and saves locally in localStorage['NG_TRANSLATE_LANG_KEY']
});
$scope.logout = function (event) {
event.stopPropagation();
Client.logout();
};
$scope.sendPasswordReset = function () {
Client.sendSelfPasswordReset($scope.user.email, function (error) {
if (error) return console.error('Failed to reset password:', error);
Client.notify($translate.instant('profile.passwordResetNotification.title'), $translate.instant('profile.passwordResetNotification.body', { email: $scope.user.fallbackEmail || $scope.user.email }), false, 'success');
});
};
2018-04-26 15:12:29 +02:00
$scope.twoFactorAuthentication = {
busy: false,
error: null,
2022-04-27 14:09:07 +02:00
notSupportedError: null,
2018-04-26 15:12:29 +02:00
password: '',
totpToken: '',
secret: '',
qrcode: '',
2020-07-10 10:43:08 -07:00
mandatory2FA: false,
mandatory2FAHelp: false, // show the initial help text when mandatory 2fa forces modal popup
2018-04-26 15:12:29 +02:00
reset: function () {
$scope.twoFactorAuthentication.busy = false;
$scope.twoFactorAuthentication.error = null;
2022-04-27 14:09:07 +02:00
$scope.twoFactorAuthentication.notSupportedError = null;
2018-04-26 15:12:29 +02:00
$scope.twoFactorAuthentication.password = '';
$scope.twoFactorAuthentication.totpToken = '';
$scope.twoFactorAuthentication.secret = '';
$scope.twoFactorAuthentication.qrcode = '';
2020-07-10 10:43:08 -07:00
$scope.twoFactorAuthentication.mandatory2FAHelp = false;
2018-04-26 15:12:29 +02:00
$scope.twoFactorAuthenticationEnableForm.$setUntouched();
$scope.twoFactorAuthenticationEnableForm.$setPristine();
2018-04-26 16:38:26 +02:00
$scope.twoFactorAuthenticationDisableForm.$setUntouched();
$scope.twoFactorAuthenticationDisableForm.$setPristine();
2018-04-26 15:12:29 +02:00
},
2020-07-10 10:43:08 -07:00
getSecret: function () {
$scope.twoFactorAuthentication.mandatory2FAHelp = false;
Client.setTwoFactorAuthenticationSecret(function (error, result) {
2022-04-27 14:09:07 +02:00
if (error && error.statusCode === 400) return $scope.twoFactorAuthentication.notSupportedError = error.message;
else if (error) return console.error(error);
2020-07-10 10:43:08 -07:00
$scope.twoFactorAuthentication.secret = result.secret;
$scope.twoFactorAuthentication.qrcode = result.qrcode;
});
},
showMandatory2FA: function () {
$scope.twoFactorAuthentication.reset();
$scope.twoFactorAuthentication.mandatory2FA = true;
$scope.twoFactorAuthentication.mandatory2FAHelp = true;
$('#twoFactorAuthenticationEnableModal').modal({ backdrop: 'static', keyboard: false }); // undimissable dialog
},
2018-04-26 15:12:29 +02:00
show: function () {
$scope.twoFactorAuthentication.reset();
if ($scope.user.twoFactorAuthenticationEnabled) {
$('#twoFactorAuthenticationDisableModal').modal('show');
} else {
$('#twoFactorAuthenticationEnableModal').modal('show');
2020-07-10 10:43:08 -07:00
$scope.twoFactorAuthentication.getSecret();
2018-04-26 15:12:29 +02:00
}
},
enable: function() {
$scope.twoFactorAuthentication.busy = true;
Client.enableTwoFactorAuthentication($scope.twoFactorAuthentication.totpToken, function (error) {
$scope.twoFactorAuthentication.busy = false;
if (error) {
$scope.twoFactorAuthentication.error = error.message;
$scope.twoFactorAuthentication.totpToken = '';
$scope.twoFactorAuthenticationEnableForm.totpToken.$setPristine();
$('#twoFactorAuthenticationTotpTokenInput').focus();
return;
}
Client.refreshProfile();
2018-04-26 15:12:29 +02:00
$('#twoFactorAuthenticationEnableModal').modal('hide');
});
},
disable: function () {
$scope.twoFactorAuthentication.busy = true;
2018-04-26 16:38:26 +02:00
Client.disableTwoFactorAuthentication($scope.twoFactorAuthentication.password, function (error) {
2018-04-26 15:12:29 +02:00
$scope.twoFactorAuthentication.busy = false;
if (error) {
$scope.twoFactorAuthentication.error = error.message;
2018-04-26 16:38:26 +02:00
$scope.twoFactorAuthentication.password = '';
$scope.twoFactorAuthenticationDisableForm.password.$setPristine();
$('#twoFactorAuthenticationPasswordInput').focus();
2018-04-26 15:12:29 +02:00
return;
}
Client.refreshProfile();
2018-04-26 15:12:29 +02:00
$('#twoFactorAuthenticationDisableModal').modal('hide');
});
}
};
$scope.avatarChange = {
busy: false,
error: {},
avatar: null,
type: '',
typeOrig: '',
2019-12-12 15:34:26 +01:00
pictureChanged: false,
getBlobFromImg: function (img, callback) {
var size = 256;
var canvas = document.createElement('canvas');
canvas.width = size;
canvas.height = size;
var imageDimensionRatio = img.width / img.height;
var canvasDimensionRatio = canvas.width / canvas.height;
var renderableHeight, renderableWidth, xStart, yStart;
if (imageDimensionRatio > canvasDimensionRatio) {
renderableHeight = canvas.height;
renderableWidth = img.width * (renderableHeight / img.height);
xStart = (canvas.width - renderableWidth) / 2;
yStart = 0;
} else if (imageDimensionRatio < canvasDimensionRatio) {
renderableWidth = canvas.width;
renderableHeight = img.height * (renderableWidth / img.width);
xStart = 0;
yStart = (canvas.height - renderableHeight) / 2;
} else {
renderableHeight = canvas.height;
renderableWidth = canvas.width;
xStart = 0;
yStart = 0;
}
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, xStart, yStart, renderableWidth, renderableHeight);
canvas.toBlob(callback);
},
2019-12-12 15:34:26 +01:00
doChangeAvatar: function () {
$scope.avatarChange.error.avatar = null;
$scope.avatarChange.busy = true;
function done(error) {
if (error) return console.error('Unable to change avatar.', error);
Client.refreshProfile(function (error) {
if (error) return console.error(error);
$('#avatarChangeModal').modal('hide');
$scope.avatarChange.avatarChangeReset();
});
2019-12-12 15:34:26 +01:00
}
if ($scope.avatarChange.type === 'custom') {
2019-12-12 15:34:26 +01:00
var img = document.getElementById('previewAvatar');
$scope.avatarChange.getBlobFromImg(img, function (blob) {
Client.changeAvatar(blob, done);
});
} else {
Client.changeAvatar($scope.avatarChange.type, done);
2019-12-12 15:34:26 +01:00
}
},
setPreviewAvatar: function (avatar) {
2019-12-12 15:34:26 +01:00
$scope.avatarChange.pictureChanged = true;
$scope.avatarChange.avatar = avatar;
document.getElementById('previewAvatar').src = avatar.data;
},
avatarChangeReset: function () {
$scope.avatarChange.error.avatar = null;
2024-01-29 13:35:54 +01:00
console.log($scope.user)
$scope.avatarChange.type = $scope.user.avatarType;
$scope.avatarChange.typeOrig = $scope.avatarChange.type;
2024-01-29 13:35:54 +01:00
document.getElementById('previewAvatar').src = $scope.avatarChange.type === 'custom' ? $scope.user.avatarUrl : '';
2019-12-12 15:34:26 +01:00
$scope.avatarChange.pictureChanged = false;
$scope.avatarChange.avatar = null;
$scope.avatarChange.busy = false;
},
showChangeAvatar: function () {
$scope.avatarChange.avatarChangeReset();
$('#avatarChangeModal').modal('show');
},
showCustomAvatarSelector: function () {
$('#avatarFileInput').click();
}
};
$scope.backgroundImageChange = {
busy: false,
error: {},
pictureChanged: false,
submit: function () {
$scope.backgroundImageChange.error.backgroundImage = null;
$scope.backgroundImageChange.busy = true;
var imageFile = document.getElementById('backgroundImageFileInput').files[0];
if (!imageFile) return;
Client.setBackgroundImage(imageFile, function (error) {
if (error) return console.error('Unable to change backgroundImage.', error);
document.getElementById('mainContentContainer').style.backgroundImage = 'url("' + Client.getBackgroundImageUrl() + '")';
document.getElementById('mainContentContainer').classList.add('has-background');
$scope.user.hasBackgroundImage = true;
$('#backgroundImageChangeModal').modal('hide');
$scope.backgroundImageChange.reset();
});
},
2022-05-15 12:14:32 +02:00
unset: function () {
Client.setBackgroundImage(null, function (error) {
if (error) return console.error('Unable to change backgroundImage.', error);
document.getElementById('mainContentContainer').style.backgroundImage = '';
document.getElementById('mainContentContainer').classList.remove('has-background');
$scope.user.hasBackgroundImage = false;
$('#backgroundImageChangeModal').modal('hide');
$scope.backgroundImageChange.reset();
2022-05-15 12:14:32 +02:00
});
},
setPreviewBackgroundImage: function (backgroundImageData) {
$scope.backgroundImageChange.pictureChanged = true;
document.getElementById('previewBackgroundImage').src = backgroundImageData;
},
reset: function () {
$scope.backgroundImageChange.error.avatar = null;
if ($scope.user.hasBackgroundImage) document.getElementById('previewBackgroundImage').src = Client.getBackgroundImageUrl();
else document.getElementById('previewBackgroundImage').src = '/img/background-image-placeholder.svg';
$scope.backgroundImageChange.pictureChanged = false;
$scope.backgroundImageChange.busy = false;
},
show: function () {
$scope.backgroundImageChange.reset();
$('#backgroundImageChangeModal').modal('show');
},
showCustomBackgroundImageSelector: function () {
$('#backgroundImageFileInput').click();
}
};
2018-01-22 13:01:38 -08:00
$scope.passwordchange = {
busy: false,
error: {},
password: '',
newPassword: '',
newPasswordRepeat: '',
reset: function () {
$scope.passwordchange.error.password = null;
$scope.passwordchange.error.newPassword = null;
$scope.passwordchange.error.newPasswordRepeat = null;
$scope.passwordchange.password = '';
$scope.passwordchange.newPassword = '';
$scope.passwordchange.newPasswordRepeat = '';
$scope.passwordChangeForm.$setUntouched();
$scope.passwordChangeForm.$setPristine();
},
show: function () {
$scope.passwordchange.reset();
$('#passwordChangeModal').modal('show');
},
submit: function () {
$scope.passwordchange.error.password = null;
$scope.passwordchange.error.newPassword = null;
$scope.passwordchange.error.newPasswordRepeat = null;
$scope.passwordchange.busy = true;
Client.changePassword($scope.passwordchange.password, $scope.passwordchange.newPassword, function (error) {
$scope.passwordchange.busy = false;
if (error) {
2019-10-29 12:39:39 +01:00
if (error.statusCode === 412) {
2018-01-22 13:01:38 -08:00
$scope.passwordchange.error.password = true;
$scope.passwordchange.password = '';
$('#inputPasswordChangePassword').focus();
$scope.passwordChangeForm.password.$setPristine();
} else if (error.statusCode === 400) {
$scope.passwordchange.error.newPassword = error.message;
$scope.passwordchange.newPassword = '';
$scope.passwordchange.newPasswordRepeat = '';
$scope.passwordChangeForm.newPassword.$setPristine();
$scope.passwordChangeForm.newPasswordRepeat.$setPristine();
$('#inputPasswordChangeNewPassword').focus();
} else {
console.error('Unable to change password.', error);
}
return;
}
$scope.passwordchange.reset();
$('#passwordChangeModal').modal('hide');
});
}
};
$scope.emailChange = {
2018-01-22 13:01:38 -08:00
busy: false,
error: {},
email: '',
password: '',
2018-01-22 13:01:38 -08:00
reset: function () {
$scope.emailChange.busy = false;
$scope.emailChange.error = {};
$scope.emailChange.email = '';
$scope.emailChange.password = '';
2018-01-22 13:01:38 -08:00
$scope.emailChangeForm.$setUntouched();
$scope.emailChangeForm.$setPristine();
},
show: function () {
$scope.emailChange.reset();
2018-01-22 13:01:38 -08:00
$('#emailChangeModal').modal('show');
},
submit: function () {
$scope.emailChange.error.email = null;
$scope.emailChange.busy = true;
2018-01-22 13:01:38 -08:00
Client.setProfileEmail($scope.emailChange.email, $scope.emailChange.password, function (error) {
$scope.emailChange.busy = false;
2018-01-22 13:01:38 -08:00
if (error) {
if (error.statusCode === 412) {
$scope.emailChange.error.password = true;
$scope.emailChange.password = '';
$scope.emailChangeForm.password.$setPristine();
$('#inputFallbackEmailChangePassword').focus();
} else {
$scope.emailChange.error.email = error.message;
$('#inputEmailChangeEmail').focus();
}
$scope.emailChangeForm.$setUntouched();
$scope.emailChangeForm.$setPristine();
2018-01-22 13:01:38 -08:00
return;
}
Client.refreshProfile();
2018-01-22 13:01:38 -08:00
$scope.emailChange.reset();
2018-01-22 13:01:38 -08:00
$('#emailChangeModal').modal('hide');
});
}
};
$scope.fallbackEmailChange = {
busy: false,
error: {
email: false,
password: false
},
2018-01-22 13:01:38 -08:00
email: '',
password: '',
2018-01-22 13:01:38 -08:00
reset: function () {
$scope.fallbackEmailChange.busy = false;
$scope.fallbackEmailChange.error.email = null;
$scope.fallbackEmailChange.error.password = null;
2018-01-22 13:01:38 -08:00
$scope.fallbackEmailChange.email = '';
$scope.fallbackEmailChange.password = '';
2018-01-22 13:01:38 -08:00
$scope.fallbackEmailChangeForm.$setUntouched();
$scope.fallbackEmailChangeForm.$setPristine();
},
show: function () {
$scope.fallbackEmailChange.reset();
$('#fallbackEmailChangeModal').modal('show');
},
submit: function () {
$scope.fallbackEmailChange.error.email = null;
$scope.fallbackEmailChange.error.password = null;
$scope.fallbackEmailChange.error.generic = null;
2018-01-22 13:01:38 -08:00
$scope.fallbackEmailChange.busy = true;
Client.setProfileFallbackEmail($scope.fallbackEmailChange.email, $scope.fallbackEmailChange.password, function (error) {
2018-01-22 13:01:38 -08:00
$scope.fallbackEmailChange.busy = false;
if (error) {
if (error.statusCode === 412) {
$scope.fallbackEmailChange.error.password = true;
$scope.fallbackEmailChange.password = '';
$scope.fallbackEmailChangeForm.password.$setPristine();
$('#inputFallbackEmailChangePassword').focus();
} else if (error.statusCode === 400) {
$scope.fallbackEmailChange.error.generic = error.message;
} else {
console.error('Unable to change fallback email.', error);
}
return;
}
2018-01-22 13:01:38 -08:00
// update user info in the background
Client.refreshProfile();
2018-01-22 13:01:38 -08:00
$scope.fallbackEmailChange.reset();
$('#fallbackEmailChangeModal').modal('hide');
});
}
};
2020-02-01 10:04:09 -08:00
$scope.appPasswordAdd = {
password: null,
name: '',
identifier: '',
busy: false,
error: {},
reset: function () {
$scope.appPasswordAdd.busy = false;
$scope.appPasswordAdd.password = null;
$scope.appPasswordAdd.error.name = null;
$scope.appPasswordAdd.name = '';
$scope.appPasswordAdd.identifier = '';
$scope.appPasswordAddForm.$setUntouched();
$scope.appPasswordAddForm.$setPristine();
},
show: function () {
$scope.appPasswordAdd.reset();
$('#appPasswordAddModal').modal('show');
},
submit: function () {
$scope.appPasswordAdd.busy = true;
Client.addAppPassword($scope.appPasswordAdd.identifier, $scope.appPasswordAdd.name, function (error, result) {
$scope.appPasswordAdd.busy = false;
2020-02-01 10:04:09 -08:00
if (error) {
if (error.statusCode === 400 || error.statusCode === 409) {
2020-02-01 10:04:09 -08:00
$scope.appPasswordAdd.error.name = error.message;
$scope.appPasswordAddForm.name.$setPristine();
$('#inputAppPasswordName').focus();
} else {
console.error('Unable to create password.', error);
}
return;
}
$scope.appPasswordAdd.password = result;
$scope.appPassword.refresh();
});
}
};
$scope.appPassword = {
busy: false,
error: {},
passwords: [],
identifiers: [],
refresh: function () {
Client.getAppPasswords(function (error, result) {
if (error) console.error(error);
$scope.appPassword.passwords = result.appPasswords || [];
2020-03-05 20:02:27 -08:00
$scope.appPassword.identifiers = [];
2020-02-01 10:04:09 -08:00
var appsById = {};
$scope.apps.forEach(function (app) {
if (!app.manifest.addons) return;
2021-01-17 18:17:15 -08:00
if (app.manifest.addons.email) return;
var ftp = app.manifest.addons.localstorage && app.manifest.addons.localstorage.ftp;
var sso = app.sso && (app.manifest.addons.ldap || app.manifest.addons.proxyAuth);
if (!ftp && !sso) return;
2020-02-01 10:04:09 -08:00
appsById[app.id] = app;
var labelSuffix = '';
if (ftp && sso) labelSuffix = ' - SFTP & App Login';
else if (ftp) labelSuffix = ' - SFTP Only';
var label = app.label ? app.label + ' (' + app.fqdn + ')' + labelSuffix : app.fqdn + labelSuffix;
$scope.appPassword.identifiers.push({ id: app.id, label: label });
2020-02-01 10:04:09 -08:00
});
2020-03-05 20:02:27 -08:00
$scope.appPassword.identifiers.push({ id: 'mail', label: 'Mail client' });
2020-02-01 10:04:09 -08:00
2020-03-05 20:02:27 -08:00
// setup label for the table UI
2020-02-01 10:04:09 -08:00
$scope.appPassword.passwords.forEach(function (password) {
2020-03-05 20:02:27 -08:00
if (password.identifier === 'mail') return password.label = password.identifier;
2020-02-01 10:04:09 -08:00
var app = appsById[password.identifier];
2020-03-05 20:02:27 -08:00
if (!app) return password.label = password.identifier + ' (App not found)';
2020-02-01 10:04:09 -08:00
var ftp = app.manifest.addons && app.manifest.addons.localstorage && app.manifest.addons.localstorage.ftp;
var labelSuffix = ftp ? ' - SFTP' : '';
password.label = app.label ? app.label + ' (' + app.fqdn + ')' + labelSuffix : app.fqdn + labelSuffix;
2020-02-01 10:04:09 -08:00
});
});
},
del: function (id) {
Client.delAppPassword(id, function (error) {
if (error) console.error(error);
$scope.appPassword.refresh();
});
}
};
2018-01-22 13:01:38 -08:00
$scope.displayNameChange = {
busy: false,
error: {},
displayName: '',
reset: function () {
$scope.displayNameChange.busy = false;
$scope.displayNameChange.error.displayName = null;
$scope.displayNameChange.displayName = '';
$scope.displayNameChangeForm.$setUntouched();
$scope.displayNameChangeForm.$setPristine();
},
show: function () {
$scope.displayNameChange.reset();
$scope.displayNameChange.displayName = $scope.user.displayName;
$('#displayNameChangeModal').modal('show');
},
submit: function () {
$scope.displayNameChange.error.displayName = null;
$scope.displayNameChange.busy = true;
Client.setProfileDisplayName($scope.displayNameChange.displayName, function (error) {
2018-01-22 13:01:38 -08:00
$scope.displayNameChange.busy = false;
if (error) {
if (error.statusCode === 400) $scope.displayNameChange.error.displayName = error.message;
else console.error('Unable to change email.', error);
$('#inputDisplayNameChangeDisplayName').focus();
$scope.displayNameChangeForm.$setUntouched();
$scope.displayNameChangeForm.$setPristine();
2018-01-22 13:01:38 -08:00
return;
}
// update user info in the background
Client.refreshProfile();
2018-01-22 13:01:38 -08:00
$scope.displayNameChange.reset();
$('#displayNameChangeModal').modal('hide');
});
}
};
2020-02-07 17:03:14 +01:00
$scope.tokens = {
busy: false,
error: {},
allTokens: [],
webadminTokens: [],
cliTokens: [],
apiTokens: [],
refresh: function () {
$scope.tokens.busy = true;
Client.getTokens(function (error, result) {
if (error) return console.error(error);
$scope.tokens.busy = false;
$scope.tokens.allTokens = result;
// dashboard and development clientIds were issued with 7.5.0
$scope.tokens.webadminTokens = result.filter(function (c) { return c.clientId === TOKEN_TYPES.ID_WEBADMIN || c.clientId === TOKEN_TYPES.ID_DEVELOPMENT || c.clientId === 'dashboard' || c.clientId === 'development'; });
$scope.tokens.cliTokens = result.filter(function (c) { return c.clientId === TOKEN_TYPES.ID_CLI; });
$scope.tokens.apiTokens = result.filter(function (c) { return c.clientId === TOKEN_TYPES.ID_SDK; });
2018-01-22 13:01:38 -08:00
});
2020-02-07 17:03:14 +01:00
},
2018-01-22 13:01:38 -08:00
revokeAllWebAndCliTokens: function () {
2020-02-07 19:59:56 +01:00
$scope.tokens.busy = true;
2020-06-03 23:17:06 +02:00
async.eachSeries($scope.tokens.webadminTokens.concat($scope.tokens.cliTokens), function (token, callback) {
2020-02-07 19:59:56 +01:00
// do not revoke token for this session, will do at the end with logout
if (token.accessToken === Client.getToken()) return callback();
2018-01-22 13:01:38 -08:00
2020-02-07 19:59:56 +01:00
Client.delToken(token.id, callback);
}, function (error) {
if (error) console.error(error);
Client.logout();
});
2020-02-07 17:03:14 +01:00
},
2018-01-22 13:01:38 -08:00
2020-02-07 17:03:14 +01:00
add: {
busy: false,
error: null,
name: '',
accessToken: '',
scope: 'rw',
2018-01-22 13:01:38 -08:00
2020-02-07 17:03:14 +01:00
show: function () {
$scope.tokens.add.name = '';
$scope.tokens.add.accessToken = '';
$scope.tokens.add.scope = 'rw';
$scope.tokens.add.busy = false;
$scope.tokens.add.error = null;
$scope.apiTokenAddForm.name.$setPristine();
2020-02-07 17:03:14 +01:00
$('#apiTokenAddModal').modal('show');
2020-02-07 17:03:14 +01:00
},
2020-02-07 17:03:14 +01:00
submit: function () {
$scope.tokens.add.busy = true;
var scope = { '*': $scope.tokens.add.scope };
2022-09-22 21:59:10 +02:00
Client.createToken($scope.tokens.add.name, scope, function (error, result) {
if (error) {
if (error.statusCode === 400) {
$scope.tokens.add.error = error.message;
$scope.apiTokenAddForm.name.$setPristine();
$('#inputApiTokenName').focus();
} else {
console.error('Unable to create password.', error);
}
return;
}
2018-01-22 13:01:38 -08:00
$scope.tokens.add.busy = false;
$scope.tokens.add.accessToken = result.accessToken;
$scope.tokens.refresh();
});
2020-02-07 17:03:14 +01:00
}
2020-02-07 19:59:56 +01:00
},
revokeToken: function (token) {
Client.delToken(token.id, function (error) {
if (error) console.error(error);
$scope.tokens.refresh();
});
2020-02-07 17:03:14 +01:00
}
};
2020-02-01 10:04:09 -08:00
2020-02-07 17:03:14 +01:00
Client.onReady(function () {
$scope.appPassword.refresh();
$scope.tokens.refresh();
Client.refreshProfile(); // 2fa status might have changed by admin
2020-11-20 18:09:09 +01:00
$translate.onReady(function () {
var usedLang = $translate.use() || $translate.fallbackLanguage();
$scope.languages = Client.getAvailableLanguages().map(function (l) {
return {
display: $translate.instant('lang.'+l, {}, undefined, 'en'),
id: l
};
}).sort(function (a, b) { return a.display.localeCompare(b.display); });
2020-11-20 18:09:09 +01:00
$scope.language = $scope.languages.find(function (l) { return l.id === usedLang; });
});
2018-01-22 13:01:38 -08:00
});
$('#avatarFileInput').get(0).onchange = function (event) {
var fr = new FileReader();
fr.onload = function () {
$scope.$apply(function () {
var tmp = {
file: event.target.files[0],
data: fr.result,
url: null
};
$scope.avatarChange.avatar = tmp;
$scope.avatarChange.setPreviewAvatar(tmp);
});
};
fr.readAsDataURL(event.target.files[0]);
};
$('#backgroundImageFileInput').get(0).onchange = function (event) {
var fr = new FileReader();
fr.onload = function () {
$scope.$apply(function () {
$scope.backgroundImageChange.setPreviewBackgroundImage(fr.result);
});
};
fr.readAsDataURL(event.target.files[0]);
};
2018-01-22 13:01:38 -08:00
// setup all the dialog focus handling
['passwordChangeModal', 'apiTokenAddModal', 'appPasswordAddModal', 'emailChangeModal', 'fallbackEmailChangeModal', 'displayNameChangeModal', 'twoFactorAuthenticationEnableModal', 'twoFactorAuthenticationDisableModal'].forEach(function (id) {
2018-01-22 13:01:38 -08:00
$('#' + id).on('shown.bs.modal', function () {
$(this).find("[autofocus]:first").focus();
});
});
new Clipboard('#newAccessTokenClipboardButton').on('success', function(e) {
$('#newAccessTokenClipboardButton').tooltip({
title: 'Copied!',
trigger: 'manual'
}).tooltip('show');
$timeout(function () { $('#newAccessTokenClipboardButton').tooltip('hide'); }, 2000);
e.clearSelection();
}).on('error', function(/*e*/) {
$('#newAccessTokenClipboardButton').tooltip({
title: 'Press Ctrl+C to copy',
trigger: 'manual'
}).tooltip('show');
$timeout(function () { $('#newAccessTokenClipboardButton').tooltip('hide'); }, 2000);
});
new Clipboard('#newAppPasswordClipboardButton').on('success', function(e) {
$('#newAppPasswordClipboardButton').tooltip({
title: 'Copied!',
trigger: 'manual'
}).tooltip('show');
$timeout(function () { $('#newAppPasswordClipboardButton').tooltip('hide'); }, 2000);
e.clearSelection();
}).on('error', function(/*e*/) {
$('#newAppPasswordClipboardButton').tooltip({
title: 'Press Ctrl+C to copy',
trigger: 'manual'
}).tooltip('show');
$timeout(function () { $('#newAppPasswordClipboardButton').tooltip('hide'); }, 2000);
});
2018-01-22 13:01:38 -08:00
$('.modal-backdrop').remove();
2020-07-10 10:43:08 -07:00
if ($location.search().setup2fa) {
// the form elements of the FormController won't appear in scope yet
$timeout(function () { $scope.twoFactorAuthentication.showMandatory2FA(); }, 1000);
} else {
// don't let the user bypass 2FA by removing the 'setup2FA' in the url
if (Client.getConfig().mandatory2FA && !Client.getUserInfo().twoFactorAuthenticationEnabled) {
$location.path('/profile').search({ setup2fa: true });
return;
}
2020-07-10 10:43:08 -07:00
}
2018-01-22 13:01:38 -08:00
}]);