2016-02-11 22:34:20 -03:00
|
|
|
(function () {
|
|
|
|
|
'use strict';
|
2015-07-21 00:35:58 -04:00
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
angular
|
|
|
|
|
.module('users.admin')
|
|
|
|
|
.controller('UserController', UserController);
|
2015-07-21 00:35:58 -04:00
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
UserController.$inject = ['$scope', '$state', '$window', 'Authentication', 'userResolve', 'Notification'];
|
2016-02-11 22:34:20 -03:00
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
function UserController($scope, $state, $window, Authentication, user, Notification) {
|
2016-02-11 22:34:20 -03:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
|
|
vm.authentication = Authentication;
|
|
|
|
|
vm.user = user;
|
|
|
|
|
vm.remove = remove;
|
|
|
|
|
vm.update = update;
|
2016-10-01 15:17:15 -04:00
|
|
|
vm.isContextUserSelf = isContextUserSelf;
|
2016-02-11 22:34:20 -03:00
|
|
|
|
|
|
|
|
function remove(user) {
|
2016-03-10 15:05:49 +02:00
|
|
|
if ($window.confirm('Are you sure you want to delete this user?')) {
|
2015-07-25 16:53:11 -04:00
|
|
|
if (user) {
|
|
|
|
|
user.$remove();
|
2015-07-21 00:35:58 -04:00
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
vm.users.splice(vm.users.indexOf(user), 1);
|
2016-10-10 17:51:44 -04:00
|
|
|
Notification.success('User deleted successfully!');
|
2015-07-25 16:53:11 -04:00
|
|
|
} else {
|
2016-02-11 22:34:20 -03:00
|
|
|
vm.user.$remove(function () {
|
2015-07-25 16:53:11 -04:00
|
|
|
$state.go('admin.users');
|
2016-10-10 17:51:44 -04:00
|
|
|
Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> User deleted successfully!' });
|
2015-07-25 16:53:11 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:34:20 -03:00
|
|
|
}
|
2015-07-21 00:35:58 -04:00
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
function update(isValid) {
|
2015-08-05 00:40:54 -04:00
|
|
|
if (!isValid) {
|
2016-02-11 22:34:20 -03:00
|
|
|
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
|
2015-08-05 00:40:54 -04:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
var user = vm.user;
|
2015-07-21 00:35:58 -04:00
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
user.$update(function () {
|
|
|
|
|
$state.go('admin.user', {
|
|
|
|
|
userId: user._id
|
|
|
|
|
});
|
2016-10-10 17:51:44 -04:00
|
|
|
Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> User saved successfully!' });
|
2015-07-25 16:53:11 -04:00
|
|
|
}, function (errorResponse) {
|
2016-10-10 17:51:44 -04:00
|
|
|
Notification.error({ message: errorResponse.data.message, title: '<i class="glyphicon glyphicon-remove"></i> User update error!' });
|
2015-07-25 16:53:11 -04:00
|
|
|
});
|
2016-02-11 22:34:20 -03:00
|
|
|
}
|
2016-10-01 15:17:15 -04:00
|
|
|
|
|
|
|
|
function isContextUserSelf() {
|
|
|
|
|
return vm.user.username === vm.authentication.user.username;
|
|
|
|
|
}
|
2015-07-25 16:53:11 -04:00
|
|
|
}
|
2015-12-10 20:31:51 +01:00
|
|
|
}());
|