From 362cf14d49754bb9c49781bbd7f18d2ed5f992fd Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Thu, 25 Jul 2013 15:47:34 +0100 Subject: [PATCH] Added confirmation when removing role from app or realm --- .../resources/META-INF/resources/ui/js/app.js | 4 + .../META-INF/resources/ui/js/controllers.js | 135 +++++++----------- .../META-INF/resources/ui/js/services.js | 23 +++ 3 files changed, 79 insertions(+), 83 deletions(-) diff --git a/ui/src/main/resources/META-INF/resources/ui/js/app.js b/ui/src/main/resources/META-INF/resources/ui/js/app.js index 93ec13bc7a..7b5546f6a8 100644 --- a/ui/src/main/resources/META-INF/resources/ui/js/app.js +++ b/ui/src/main/resources/META-INF/resources/ui/js/app.js @@ -203,6 +203,10 @@ module.directive('kcEnter', function() { module.filter('remove', function() { return function(input, remove) { + if (!input || !remove) { + return input; + } + var out = []; for (var i = 0; i < input.length; i++) { if (remove.indexOf(input[i]) == -1) { diff --git a/ui/src/main/resources/META-INF/resources/ui/js/controllers.js b/ui/src/main/resources/META-INF/resources/ui/js/controllers.js index fc25e460c5..a18d6e3c99 100644 --- a/ui/src/main/resources/META-INF/resources/ui/js/controllers.js +++ b/ui/src/main/resources/META-INF/resources/ui/js/controllers.js @@ -20,7 +20,7 @@ module.controller('ApplicationListCtrl', function($scope, 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) { $scope.application = angular.copy(application); $scope.applications = applications; @@ -41,14 +41,16 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic $scope.addRole = function() { if ($scope.newRole) { - for (var i = 0; i < $scope.application.roles.length; i++) { - if ($scope.application.roles[i] == $scope.newRole) { - Notifications.warn("Role already exists"); - $scope.newRole = null; - return; + if ($scope.application.roles) { + for ( var i = 0; i < $scope.application.roles.length; i++) { + if ($scope.application.roles[i] == $scope.newRole) { + Notifications.warn("Role already exists"); + $scope.newRole = null; + return; + } } } - + if (!$scope.application.roles) { $scope.application.roles = []; } @@ -59,11 +61,13 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic } $scope.removeRole = function(role) { - var i = $scope.application.roles.indexOf(role); - if (i > -1) { - $scope.application.roles.splice(i, 1); - } - $scope.removeInitialRole(role); + Dialog.confirmDelete(role, 'role', function() { + var i = $scope.application.roles.indexOf(role); + if (i > -1) { + $scope.application.roles.splice(i, 1); + } + $scope.removeInitialRole(role); + }); }; $scope.addInitialRole = function() { @@ -83,7 +87,7 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic $scope.application.initialRoles.splice(i, 1); } }; - + $scope.save = function() { if ($scope.applicationForm.$valid) { if ($scope.create) { @@ -116,24 +120,11 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic }; $scope.remove = function() { - var title = 'Delete ' + $scope.application.name; - var msg = 'Are you sure you want to permanently delete this application?'; - var btns = [ { - result : 'cancel', - 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"); - }); - } + Dialog.confirmDelete($scope.application.name, 'application', function() { + $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; }); -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.realm = realm; $scope.user = angular.copy(user); @@ -252,32 +243,19 @@ module.controller('UserDetailCtrl', function($scope, realms, realm, user, User, }; $scope.remove = function() { - var title = 'Delete ' + $scope.user.userId; - var msg = 'Are you sure you want to permanently delete this user?'; - var btns = [ { - result : 'cancel', - label : 'Cancel' - }, { - result : 'ok', - 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"); - }); - } + Dialog.confirmDelete($scope.user.userId, 'user', function() { + $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.realm = angular.copy(realm); $scope.create = !realm.name; @@ -292,14 +270,16 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc $scope.addRole = function() { if ($scope.newRole) { - for (var i = 0; i < $scope.realm.roles.length; i++) { - if ($scope.realm.roles[i] == $scope.newRole) { - Notifications.warn("Role already exists"); - $scope.newRole = null; - return; + if ($scope.realm.roles) { + for ( var i = 0; i < $scope.realm.roles.length; i++) { + if ($scope.realm.roles[i] == $scope.newRole) { + Notifications.warn("Role already exists"); + $scope.newRole = null; + return; + } } } - + if (!$scope.realm.roles) { $scope.realm.roles = []; } @@ -310,11 +290,13 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc } $scope.removeRole = function(role) { - var i = $scope.realm.roles.indexOf(role); - if (i > -1) { - $scope.realm.roles.splice(i, 1); - } - $scope.removeInitialRole(role); + Dialog.confirmDelete(role, 'role', function() { + var i = $scope.realm.roles.indexOf(role); + if (i > -1) { + $scope.realm.roles.splice(i, 1); + } + $scope.removeInitialRole(role); + }); }; $scope.addInitialRole = function() { @@ -367,24 +349,11 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc }; $scope.remove = function() { - var title = 'Delete ' + $scope.realm.name; - var msg = 'Are you sure you want to permanently delete this realm?'; - var btns = [ { - result : 'cancel', - 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"); - }); - } + Dialog.confirmDelete($scope.realm.name, 'realm', function() { + Realm.remove($scope.realm, function() { + $location.url("/realms"); + Notifications.success("Deleted realm"); + }); }); }; }); \ No newline at end of file diff --git a/ui/src/main/resources/META-INF/resources/ui/js/services.js b/ui/src/main/resources/META-INF/resources/ui/js/services.js index 90328f4463..e39d0b4513 100644 --- a/ui/src/main/resources/META-INF/resources/ui/js/services.js +++ b/ui/src/main/resources/META-INF/resources/ui/js/services.js @@ -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) { var auth = { loggedIn : true