Added confirmation when removing role from app or realm

This commit is contained in:
Stian Thorgersen 2013-07-25 15:47:34 +01:00
parent 2c8ea5d368
commit 362cf14d49
3 changed files with 79 additions and 83 deletions

View file

@ -203,6 +203,10 @@ module.directive('kcEnter', function() {
module.filter('remove', function() { module.filter('remove', function() {
return function(input, remove) { return function(input, remove) {
if (!input || !remove) {
return input;
}
var out = []; var out = [];
for (var i = 0; i < input.length; i++) { for (var i = 0; i < input.length; i++) {
if (remove.indexOf(input[i]) == -1) { if (remove.indexOf(input[i]) == -1) {

View file

@ -20,7 +20,7 @@ module.controller('ApplicationListCtrl', function($scope, applications) {
$scope.applications = applications; $scope.applications = applications;
}); });
module.controller('ApplicationDetailCtrl', function($scope, applications, application, Application, realms, providers, $location, $window, $dialog, module.controller('ApplicationDetailCtrl', function($scope, applications, application, Application, realms, providers, $location, $window, Dialog,
Notifications) { Notifications) {
$scope.application = angular.copy(application); $scope.application = angular.copy(application);
$scope.applications = applications; $scope.applications = applications;
@ -41,11 +41,13 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic
$scope.addRole = function() { $scope.addRole = function() {
if ($scope.newRole) { if ($scope.newRole) {
for (var i = 0; i < $scope.application.roles.length; i++) { if ($scope.application.roles) {
if ($scope.application.roles[i] == $scope.newRole) { for ( var i = 0; i < $scope.application.roles.length; i++) {
Notifications.warn("Role already exists"); if ($scope.application.roles[i] == $scope.newRole) {
$scope.newRole = null; Notifications.warn("Role already exists");
return; $scope.newRole = null;
return;
}
} }
} }
@ -59,11 +61,13 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic
} }
$scope.removeRole = function(role) { $scope.removeRole = function(role) {
var i = $scope.application.roles.indexOf(role); Dialog.confirmDelete(role, 'role', function() {
if (i > -1) { var i = $scope.application.roles.indexOf(role);
$scope.application.roles.splice(i, 1); if (i > -1) {
} $scope.application.roles.splice(i, 1);
$scope.removeInitialRole(role); }
$scope.removeInitialRole(role);
});
}; };
$scope.addInitialRole = function() { $scope.addInitialRole = function() {
@ -116,24 +120,11 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic
}; };
$scope.remove = function() { $scope.remove = function() {
var title = 'Delete ' + $scope.application.name; Dialog.confirmDelete($scope.application.name, 'application', function() {
var msg = 'Are you sure you want to permanently delete this application?'; $scope.application.$remove(function() {
var btns = [ { $location.url("/applications");
result : 'cancel', Notifications.success("Deleted application");
label : 'Cancel' });
}, {
result : 'ok',
label : 'Delete this application',
cssClass : 'btn-primary'
} ];
$dialog.messageBox(title, msg, btns).open().then(function(result) {
if (result == "ok") {
$scope.application.$remove(function() {
$location.url("/applications");
Notifications.success("Deleted application");
});
}
}); });
}; };
@ -207,7 +198,7 @@ module.controller('UserListCtrl', function($scope, realms, realm, users) {
$scope.users = users; $scope.users = users;
}); });
module.controller('UserDetailCtrl', function($scope, realms, realm, user, User, $location, $dialog, Notifications) { module.controller('UserDetailCtrl', function($scope, realms, realm, user, User, $location, Dialog, Notifications) {
$scope.realms = realms; $scope.realms = realms;
$scope.realm = realm; $scope.realm = realm;
$scope.user = angular.copy(user); $scope.user = angular.copy(user);
@ -252,32 +243,19 @@ module.controller('UserDetailCtrl', function($scope, realms, realm, user, User,
}; };
$scope.remove = function() { $scope.remove = function() {
var title = 'Delete ' + $scope.user.userId; Dialog.confirmDelete($scope.user.userId, 'user', function() {
var msg = 'Are you sure you want to permanently delete this user?'; $scope.user.$remove({
var btns = [ { realmKey : realm.key,
result : 'cancel', userId : $scope.user.userId
label : 'Cancel' }, function() {
}, { $location.url("/realms/" + realm.key + "/users");
result : 'ok', Notifications.success("Deleted user");
label : 'Delete this user', });
cssClass : 'btn-primary'
} ];
$dialog.messageBox(title, msg, btns).open().then(function(result) {
if (result == "ok") {
$scope.user.$remove({
realmKey : realm.key,
userId : $scope.user.userId
}, function() {
$location.url("/realms/" + realm.key + "/users");
Notifications.success("Deleted user");
});
}
}); });
}; };
}); });
module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $location, $dialog, Notifications) { module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $location, Dialog, Notifications) {
$scope.realms = realms; $scope.realms = realms;
$scope.realm = angular.copy(realm); $scope.realm = angular.copy(realm);
$scope.create = !realm.name; $scope.create = !realm.name;
@ -292,11 +270,13 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc
$scope.addRole = function() { $scope.addRole = function() {
if ($scope.newRole) { if ($scope.newRole) {
for (var i = 0; i < $scope.realm.roles.length; i++) { if ($scope.realm.roles) {
if ($scope.realm.roles[i] == $scope.newRole) { for ( var i = 0; i < $scope.realm.roles.length; i++) {
Notifications.warn("Role already exists"); if ($scope.realm.roles[i] == $scope.newRole) {
$scope.newRole = null; Notifications.warn("Role already exists");
return; $scope.newRole = null;
return;
}
} }
} }
@ -310,11 +290,13 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc
} }
$scope.removeRole = function(role) { $scope.removeRole = function(role) {
var i = $scope.realm.roles.indexOf(role); Dialog.confirmDelete(role, 'role', function() {
if (i > -1) { var i = $scope.realm.roles.indexOf(role);
$scope.realm.roles.splice(i, 1); if (i > -1) {
} $scope.realm.roles.splice(i, 1);
$scope.removeInitialRole(role); }
$scope.removeInitialRole(role);
});
}; };
$scope.addInitialRole = function() { $scope.addInitialRole = function() {
@ -367,24 +349,11 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc
}; };
$scope.remove = function() { $scope.remove = function() {
var title = 'Delete ' + $scope.realm.name; Dialog.confirmDelete($scope.realm.name, 'realm', function() {
var msg = 'Are you sure you want to permanently delete this realm?'; Realm.remove($scope.realm, function() {
var btns = [ { $location.url("/realms");
result : 'cancel', Notifications.success("Deleted realm");
label : 'Cancel' });
}, {
result : 'ok',
label : 'Delete this realm',
cssClass : 'btn-primary'
} ];
$dialog.messageBox(title, msg, btns).open().then(function(result) {
if (result == "ok") {
Realm.remove($scope.realm, function() {
$location.url("/realms");
Notifications.success("Deleted realm");
});
}
}); });
}; };
}); });

View file

@ -184,6 +184,29 @@ module.factory('UserLoader', function(User, $route, $q) {
}; };
}); });
module.service('Dialog', function($dialog) {
var dialog = {};
dialog.confirmDelete = function(name, type, success) {
var title = 'Delete ' + name;
var msg = 'Are you sure you want to permanently delete this ' + type + '?';
var btns = [ {
result : 'cancel',
label : 'Cancel'
}, {
result : 'ok',
label : 'Delete this ' + type,
cssClass : 'btn-primary'
} ];
$dialog.messageBox(title, msg, btns).open().then(function(result) {
if (result == "ok") {
success();
}
});
}
return dialog
});
module.service('Auth', function($resource, $http, $location, $routeParams) { module.service('Auth', function($resource, $http, $location, $routeParams) {
var auth = { var auth = {
loggedIn : true loggedIn : true