2018-01-22 13:01:38 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
2018-04-02 11:35:02 +02:00
|
|
|
/* global asyncForEach:false */
|
2019-01-07 17:34:10 +01:00
|
|
|
/* global angular:false */
|
|
|
|
|
/* global $:false */
|
2018-04-02 11:35:02 +02:00
|
|
|
|
2019-11-07 12:08:51 +01:00
|
|
|
angular.module('Application').controller('ProfileController', ['$scope', '$location', 'Client', function ($scope, $location, Client) {
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
$scope.config = Client.getConfig();
|
|
|
|
|
|
|
|
|
|
$scope.activeClients = [];
|
|
|
|
|
$scope.webadminClient = {};
|
2018-08-27 15:26:52 -07:00
|
|
|
$scope.apiClient = {};
|
2019-11-07 14:28:52 -08:00
|
|
|
$scope.cliClient = {};
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-04-26 15:12:29 +02:00
|
|
|
$scope.twoFactorAuthentication = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: null,
|
|
|
|
|
password: '',
|
|
|
|
|
totpToken: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
qrcode: '',
|
|
|
|
|
|
|
|
|
|
reset: function () {
|
|
|
|
|
$scope.twoFactorAuthentication.busy = false;
|
|
|
|
|
$scope.twoFactorAuthentication.error = null;
|
|
|
|
|
$scope.twoFactorAuthentication.password = '';
|
|
|
|
|
$scope.twoFactorAuthentication.totpToken = '';
|
|
|
|
|
$scope.twoFactorAuthentication.secret = '';
|
|
|
|
|
$scope.twoFactorAuthentication.qrcode = '';
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
},
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.twoFactorAuthentication.reset();
|
|
|
|
|
|
|
|
|
|
if ($scope.user.twoFactorAuthenticationEnabled) {
|
|
|
|
|
$('#twoFactorAuthenticationDisableModal').modal('show');
|
|
|
|
|
} else {
|
|
|
|
|
$('#twoFactorAuthenticationEnableModal').modal('show');
|
|
|
|
|
|
|
|
|
|
Client.setTwoFactorAuthenticationSecret(function (error, result) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.twoFactorAuthentication.secret = result.secret;
|
|
|
|
|
$scope.twoFactorAuthentication.qrcode = result.qrcode;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 16:54:03 +02:00
|
|
|
Client.refreshUserInfo();
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 16:54:03 +02:00
|
|
|
Client.refreshUserInfo();
|
|
|
|
|
|
2018-04-26 15:12:29 +02:00
|
|
|
$('#twoFactorAuthenticationDisableModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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 = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: {},
|
|
|
|
|
email: '',
|
|
|
|
|
|
|
|
|
|
reset: function () {
|
|
|
|
|
$scope.emailchange.busy = false;
|
|
|
|
|
$scope.emailchange.error.email = null;
|
|
|
|
|
$scope.emailchange.email = '';
|
|
|
|
|
|
|
|
|
|
$scope.emailChangeForm.$setUntouched();
|
|
|
|
|
$scope.emailChangeForm.$setPristine();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.emailchange.reset();
|
|
|
|
|
$('#emailChangeModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.emailchange.error.email = null;
|
|
|
|
|
$scope.emailchange.busy = true;
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
email: $scope.emailchange.email
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Client.updateProfile(data, function (error) {
|
|
|
|
|
$scope.emailchange.busy = false;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
if (error.statusCode === 409) {
|
|
|
|
|
$scope.emailchange.error.email = 'Email already taken';
|
|
|
|
|
$scope.emailChangeForm.email.$setPristine();
|
|
|
|
|
$('#inputEmailChangeEmail').focus();
|
|
|
|
|
} else {
|
|
|
|
|
console.error('Unable to change email.', error);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update user info in the background
|
|
|
|
|
Client.refreshUserInfo();
|
|
|
|
|
|
|
|
|
|
$scope.emailchange.reset();
|
|
|
|
|
$('#emailChangeModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.fallbackEmailChange = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: {},
|
|
|
|
|
email: '',
|
|
|
|
|
|
|
|
|
|
reset: function () {
|
|
|
|
|
$scope.fallbackEmailChange.busy = false;
|
|
|
|
|
$scope.fallbackEmailChange.error.email = null;
|
|
|
|
|
$scope.fallbackEmailChange.email = '';
|
|
|
|
|
|
|
|
|
|
$scope.fallbackEmailChangeForm.$setUntouched();
|
|
|
|
|
$scope.fallbackEmailChangeForm.$setPristine();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.fallbackEmailChange.reset();
|
|
|
|
|
$('#fallbackEmailChangeModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.fallbackEmailChange.error.email = null;
|
|
|
|
|
$scope.fallbackEmailChange.busy = true;
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
fallbackEmail: $scope.fallbackEmailChange.email
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Client.updateProfile(data, function (error) {
|
|
|
|
|
$scope.fallbackEmailChange.busy = false;
|
|
|
|
|
|
|
|
|
|
if (error) return console.error('Unable to change fallback email.', error);
|
|
|
|
|
|
|
|
|
|
// update user info in the background
|
|
|
|
|
Client.refreshUserInfo();
|
|
|
|
|
|
|
|
|
|
$scope.fallbackEmailChange.reset();
|
|
|
|
|
$('#fallbackEmailChangeModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
|
|
var user = {
|
|
|
|
|
displayName: $scope.displayNameChange.displayName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Client.updateProfile(user, function (error) {
|
|
|
|
|
$scope.displayNameChange.busy = false;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
if (error.statusCode === 400) {
|
|
|
|
|
$scope.displayNameChange.error.displayName = 'Invalid display name';
|
|
|
|
|
$scope.displayNameChangeForm.email.$setPristine();
|
|
|
|
|
$('#inputDisplayNameChangeDisplayName').focus();
|
|
|
|
|
} else {
|
|
|
|
|
console.error('Unable to change email.', error);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update user info in the background
|
|
|
|
|
Client.refreshUserInfo();
|
|
|
|
|
|
|
|
|
|
$scope.displayNameChange.reset();
|
|
|
|
|
$('#displayNameChangeModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function revokeTokensByClient(client, callback) {
|
|
|
|
|
Client.delTokensByClientId(client.id, function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.revokeTokens = function () {
|
2019-11-08 17:24:52 +01:00
|
|
|
// first revoke all non webadmin tokens
|
|
|
|
|
asyncForEach($scope.activeClients.filter(function (c) { return c.id !== 'cid-webadmin'; }), revokeTokensByClient, function () {
|
|
|
|
|
// WARNING keep in sync with clients.js in box code
|
|
|
|
|
revokeTokensByClient('cid-webadmin', function () {
|
2019-11-08 21:33:17 +01:00
|
|
|
Client.logout(true /* destroy all OAuth sessions for this user */);
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function refreshClientTokens(client, callback) {
|
|
|
|
|
Client.getTokensByClientId(client.id, function (error, result) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
|
|
|
|
client.activeTokens = result || [];
|
|
|
|
|
|
2018-08-27 16:04:16 -07:00
|
|
|
if (callback) callback();
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.onReady(function () {
|
2019-05-14 16:55:29 +02:00
|
|
|
if (!Client.getUserInfo().admin) return;
|
|
|
|
|
|
2018-01-22 13:01:38 -08:00
|
|
|
Client.getOAuthClients(function (error, activeClients) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
asyncForEach(activeClients, refreshClientTokens, function () {
|
2018-08-27 21:06:07 -07:00
|
|
|
$scope.webadminClient = activeClients.filter(function (c) { return c.id === 'cid-webadmin'; })[0];
|
|
|
|
|
$scope.apiClient = activeClients.filter(function (c) { return c.id === 'cid-sdk'; })[0];
|
2019-11-07 14:28:52 -08:00
|
|
|
$scope.cliClient = activeClients.filter(function (c) { return c.id === 'cid-cli'; })[0];
|
2018-08-27 21:06:07 -07:00
|
|
|
|
2019-11-07 14:28:52 -08:00
|
|
|
$scope.activeClients = activeClients;
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// setup all the dialog focus handling
|
2019-11-07 14:41:28 -08:00
|
|
|
['passwordChangeModal', '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();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('.modal-backdrop').remove();
|
|
|
|
|
}]);
|