2016-05-09 12:53:40 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.layout.account.setting')
|
|
|
|
.controller('AccountSettingController', AccountSettingController);
|
|
|
|
|
2016-06-21 07:28:21 +02:00
|
|
|
AccountSettingController.$inject = ['ChangePasswordService', 'UpdateUserService', '$filter', 'trFilter', '$scope', '$window', 'currentUser'];
|
2016-05-09 12:53:40 +02:00
|
|
|
|
2016-06-21 07:28:21 +02:00
|
|
|
function AccountSettingController(ChangePasswordService, UpdateUserService, $filter, trFilter, $scope, $window, currentUser) {
|
2016-06-25 21:12:17 +02:00
|
|
|
|
2016-05-09 12:53:40 +02:00
|
|
|
var vm = this;
|
|
|
|
vm.isOpen = false;
|
2016-06-21 07:28:21 +02:00
|
|
|
|
2016-05-14 07:34:54 +02:00
|
|
|
vm.hasError = false;
|
|
|
|
vm.errorMessage = '';
|
|
|
|
|
|
|
|
vm.reset = reset;
|
|
|
|
vm.toggleChangePassword = toggleChangePassword;
|
2016-06-21 12:42:53 +02:00
|
|
|
vm.confirm = confirm;
|
2016-06-21 07:28:21 +02:00
|
|
|
vm.updateUser = updateUser;
|
2016-05-09 19:48:05 +02:00
|
|
|
vm.cancel = cancel;
|
|
|
|
|
2016-06-21 07:28:21 +02:00
|
|
|
$scope.user = currentUser.get();
|
|
|
|
var userId = $scope.user.user_id;
|
|
|
|
|
2016-05-14 07:34:54 +02:00
|
|
|
function reset() {
|
2016-06-21 07:28:21 +02:00
|
|
|
$scope.form.$setUntouched();
|
|
|
|
$scope.form.$setPristine();
|
2016-05-14 07:34:54 +02:00
|
|
|
vm.hasError = false;
|
|
|
|
vm.errorMessage = '';
|
|
|
|
}
|
2016-05-11 09:57:52 +02:00
|
|
|
|
2016-05-09 12:53:40 +02:00
|
|
|
function toggleChangePassword() {
|
|
|
|
if(vm.isOpen) {
|
|
|
|
vm.isOpen = false;
|
|
|
|
}else{
|
|
|
|
vm.isOpen = true;
|
2016-05-14 07:34:54 +02:00
|
|
|
}
|
2016-05-09 12:53:40 +02:00
|
|
|
}
|
2016-05-09 19:48:05 +02:00
|
|
|
|
2016-06-21 12:42:53 +02:00
|
|
|
function confirm() {
|
|
|
|
$window.location.href = '/dashboard';
|
2016-06-21 07:28:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 12:42:53 +02:00
|
|
|
function updateUser(user) {
|
2016-06-21 07:28:21 +02:00
|
|
|
if(vm.isOpen){
|
2016-06-21 12:42:53 +02:00
|
|
|
if(user && angular.isDefined(user.oldPassword) && angular.isDefined(user.password)) {
|
|
|
|
ChangePasswordService(userId, user.oldPassword, user.password)
|
|
|
|
.success(changePasswordSuccess)
|
|
|
|
.error(changePasswordFailed);
|
|
|
|
}
|
2016-06-21 07:28:21 +02:00
|
|
|
}else{
|
2016-06-21 12:42:53 +02:00
|
|
|
if(user && angular.isDefined(user.username) && angular.isDefined(user.password) &&
|
|
|
|
angular.isDefined(user.realname)) {
|
|
|
|
UpdateUserService(userId, user)
|
|
|
|
.success(updateUserSuccess)
|
|
|
|
.error(updateUserFailed);
|
|
|
|
currentUser.set(user);
|
|
|
|
}
|
2016-05-14 07:34:54 +02:00
|
|
|
}
|
2016-05-09 12:53:40 +02:00
|
|
|
}
|
|
|
|
|
2016-05-09 19:48:05 +02:00
|
|
|
function changePasswordSuccess(data, status) {
|
2016-06-21 12:42:53 +02:00
|
|
|
vm.modalTitle = $filter('tr')('change_password', []);
|
|
|
|
vm.modalMessage = $filter('tr')('successful_changed_password', []);
|
|
|
|
$scope.$broadcast('showDialog', true);
|
2016-05-09 19:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function changePasswordFailed(data, status) {
|
|
|
|
console.log('Failed changed password:' + data);
|
2016-06-16 08:10:35 +02:00
|
|
|
if(data == 'old_password_is_not_correct') {
|
2016-05-14 07:34:54 +02:00
|
|
|
vm.hasError = true;
|
|
|
|
vm.errorMessage = 'old_password_is_incorrect';
|
2016-05-09 19:48:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 07:28:21 +02:00
|
|
|
function updateUserSuccess(data, status) {
|
2016-06-21 12:42:53 +02:00
|
|
|
vm.modalTitle = $filter('tr')('change_profile', []);
|
|
|
|
vm.modalMessage = $filter('tr')('successful_changed_profile', []);
|
|
|
|
$scope.$broadcast('showDialog', true);
|
2016-06-21 07:28:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateUserFailed(data, status) {
|
|
|
|
console.log('Failed update user.');
|
|
|
|
}
|
|
|
|
|
2016-05-14 07:34:54 +02:00
|
|
|
function cancel(form) {
|
2016-06-21 07:28:21 +02:00
|
|
|
$window.location.href = '/dashboard';
|
2016-05-09 19:48:05 +02:00
|
|
|
}
|
2016-05-09 12:53:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|