2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-06-07 22:53:36 +02:00
|
|
|
angular.module('Application').controller('AccountController', ['$scope', 'Client', function ($scope, Client) {
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
$scope.config = Client.getConfig();
|
|
|
|
|
|
2016-06-08 12:56:48 +02:00
|
|
|
$scope.activeTokens = 0;
|
|
|
|
|
$scope.activeClients = [];
|
|
|
|
|
$scope.webadminClient = {};
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
$scope.passwordchange = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: {},
|
|
|
|
|
password: '',
|
|
|
|
|
newPassword: '',
|
2016-02-25 14:46:53 +01:00
|
|
|
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();
|
2016-02-25 14:58:26 +01:00
|
|
|
$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) {
|
|
|
|
|
if (error.statusCode === 403) {
|
|
|
|
|
$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');
|
|
|
|
|
});
|
2016-02-25 14:46:53 +01:00
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.emailchange = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: {},
|
2016-02-25 14:46:53 +01:00
|
|
|
email: '',
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-02-25 14:46:53 +01:00
|
|
|
reset: function () {
|
|
|
|
|
$scope.emailchange.busy = false;
|
|
|
|
|
$scope.emailchange.error.email = null;
|
|
|
|
|
$scope.emailchange.email = '';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-02-25 14:46:53 +01:00
|
|
|
$scope.emailChangeForm.$setUntouched();
|
2016-02-25 14:58:26 +01:00
|
|
|
$scope.emailChangeForm.$setPristine();
|
|
|
|
|
},
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-02-25 14:58:26 +01:00
|
|
|
show: function () {
|
|
|
|
|
$scope.emailchange.reset();
|
|
|
|
|
$('#emailChangeModal').modal('show');
|
|
|
|
|
},
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-02-25 14:58:26 +01:00
|
|
|
submit: function () {
|
|
|
|
|
$scope.emailchange.error.email = null;
|
|
|
|
|
$scope.emailchange.busy = true;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-09-28 15:49:41 -07:00
|
|
|
var data = {
|
2016-02-25 14:58:26 +01:00
|
|
|
email: $scope.emailchange.email
|
|
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-09-28 15:49:41 -07:00
|
|
|
Client.updateProfile(data, function (error) {
|
2016-02-25 14:58:26 +01:00
|
|
|
$scope.emailchange.busy = false;
|
2016-02-25 14:34:16 +01:00
|
|
|
|
2016-02-25 14:58:26 +01:00
|
|
|
if (error) {
|
2016-09-28 16:05:39 -07:00
|
|
|
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);
|
|
|
|
|
}
|
2016-02-25 14:58:26 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-02-25 14:58:26 +01:00
|
|
|
// update user info in the background
|
|
|
|
|
Client.refreshUserInfo();
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-02-25 14:58:26 +01:00
|
|
|
$scope.emailchange.reset();
|
|
|
|
|
$('#emailChangeModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2016-02-25 15:09:52 +01: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;
|
|
|
|
|
|
|
|
|
|
var user = {
|
|
|
|
|
displayName: $scope.displayNameChange.displayName
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-28 15:50:15 -07:00
|
|
|
Client.updateProfile(user, function (error) {
|
2016-02-25 15:09:52 +01:00
|
|
|
$scope.displayNameChange.busy = false;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
2016-09-28 16:05:39 -07:00
|
|
|
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);
|
|
|
|
|
}
|
2016-02-25 15:09:52 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update user info in the background
|
|
|
|
|
Client.refreshUserInfo();
|
|
|
|
|
|
|
|
|
|
$scope.displayNameChange.reset();
|
|
|
|
|
$('#displayNameChangeModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-08 12:56:48 +02:00
|
|
|
// poor man's async
|
|
|
|
|
function asyncForEach(items, handler, callback) {
|
|
|
|
|
var cur = 0;
|
|
|
|
|
|
2016-06-08 13:35:32 +02:00
|
|
|
if (items.length === 0) return callback();
|
|
|
|
|
|
2016-06-08 12:56:48 +02:00
|
|
|
(function iterator() {
|
|
|
|
|
handler(items[cur], function () {
|
|
|
|
|
if (cur >= items.length-1) return callback();
|
|
|
|
|
++cur;
|
|
|
|
|
|
|
|
|
|
iterator();
|
|
|
|
|
});
|
|
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 13:03:21 +02:00
|
|
|
function revokeTokensByClient(client, callback) {
|
2016-06-08 12:56:48 +02:00
|
|
|
Client.delTokensByClientId(client.id, function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.revokeTokens = function () {
|
2016-06-08 13:03:21 +02:00
|
|
|
asyncForEach($scope.activeClients, revokeTokensByClient, function () {
|
|
|
|
|
|
|
|
|
|
// now kill this session if exists
|
|
|
|
|
if (!$scope.webadminClient || !$scope.webadminClient.id) return;
|
|
|
|
|
|
|
|
|
|
revokeTokensByClient($scope.webadminClient, function () {
|
|
|
|
|
// we should be logged out by now
|
|
|
|
|
});
|
2016-06-08 12:56:48 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function refreshClientTokens(client, callback) {
|
|
|
|
|
Client.getTokensByClientId(client.id, function (error, result) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
|
|
|
|
client.activeTokens = result || [];
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.onReady(function () {
|
|
|
|
|
Client.getOAuthClients(function (error, activeClients) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
asyncForEach(activeClients, refreshClientTokens, function () {
|
|
|
|
|
activeClients = activeClients.filter(function (c) { return c.activeTokens.length > 0; });
|
|
|
|
|
|
|
|
|
|
$scope.activeClients = activeClients.filter(function (c) { return c.id !== 'cid-sdk' && c.id !== 'cid-webadmin'; });
|
|
|
|
|
$scope.webadminClient = activeClients.filter(function (c) { return c.id === 'cid-webadmin'; })[0];
|
2016-06-08 13:31:17 +02:00
|
|
|
|
|
|
|
|
$scope.activeTokenCount = $scope.activeClients.reduce(function (prev, cur) { return prev + cur.activeTokens.length; }, 0);
|
|
|
|
|
$scope.activeTokenCount += $scope.webadminClient ? $scope.webadminClient.activeTokens.length : 0;
|
2016-06-08 12:56:48 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
// setup all the dialog focus handling
|
2016-06-07 22:53:36 +02:00
|
|
|
['passwordChangeModal', 'emailChangeModal', 'displayNameChangeModal'].forEach(function (id) {
|
2015-07-20 00:09:47 -07:00
|
|
|
$('#' + id).on('shown.bs.modal', function () {
|
|
|
|
|
$(this).find("[autofocus]:first").focus();
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-11-21 13:22:56 +01:00
|
|
|
|
|
|
|
|
$('.modal-backdrop').remove();
|
2015-07-20 00:09:47 -07:00
|
|
|
}]);
|