Tweaked adding/removing roles to realm and applications

This commit is contained in:
Stian Thorgersen 2013-07-25 15:25:39 +01:00
parent 4952849526
commit 2c8ea5d368
5 changed files with 231 additions and 174 deletions

View file

@ -4,162 +4,162 @@ var module = angular.module('keycloak', [ 'keycloak.services', 'keycloak.control
var resourceRequests = 0; var resourceRequests = 0;
module.config([ '$routeProvider', function($routeProvider) { module.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/create/application', { $routeProvider.when('/create/application', {
templateUrl : 'partials/application-detail.html', templateUrl : 'partials/application-detail.html',
resolve : { resolve : {
applications : function(ApplicationListLoader) { applications : function(ApplicationListLoader) {
return ApplicationListLoader(); return ApplicationListLoader();
}, },
application : function(ApplicationLoader) { application : function(ApplicationLoader) {
return {}; return {};
}, },
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
}, },
providers : function(ProviderListLoader) { providers : function(ProviderListLoader) {
return ProviderListLoader(); return ProviderListLoader();
} }
}, },
controller : 'ApplicationDetailCtrl' controller : 'ApplicationDetailCtrl'
}).when('/applications/:application', { }).when('/applications/:application', {
templateUrl : 'partials/application-detail.html', templateUrl : 'partials/application-detail.html',
resolve : { resolve : {
applications : function(ApplicationListLoader) { applications : function(ApplicationListLoader) {
return ApplicationListLoader(); return ApplicationListLoader();
}, },
application : function(ApplicationLoader) { application : function(ApplicationLoader) {
return ApplicationLoader(); return ApplicationLoader();
}, },
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
}, },
providers : function(ProviderListLoader) { providers : function(ProviderListLoader) {
return ProviderListLoader(); return ProviderListLoader();
} }
}, },
controller : 'ApplicationDetailCtrl' controller : 'ApplicationDetailCtrl'
}).when('/applications', { }).when('/applications', {
templateUrl : 'partials/application-list.html', templateUrl : 'partials/application-list.html',
resolve : { resolve : {
applications : function(ApplicationListLoader) { applications : function(ApplicationListLoader) {
return ApplicationListLoader(); return ApplicationListLoader();
} }
}, },
controller : 'ApplicationListCtrl' controller : 'ApplicationListCtrl'
}).when('/realms/:realm/users/:user', { }).when('/realms/:realm/users/:user', {
templateUrl : 'partials/user-detail.html', templateUrl : 'partials/user-detail.html',
resolve : { resolve : {
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
}, },
realm : function(RealmLoader) { realm : function(RealmLoader) {
return RealmLoader(); return RealmLoader();
}, },
user : function(UserLoader) { user : function(UserLoader) {
return UserLoader(); return UserLoader();
} }
}, },
controller : 'UserDetailCtrl' controller : 'UserDetailCtrl'
}).when('/realms/:realm/users', { }).when('/realms/:realm/users', {
templateUrl : 'partials/user-list.html', templateUrl : 'partials/user-list.html',
resolve : { resolve : {
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
}, },
realm : function(RealmLoader) { realm : function(RealmLoader) {
return RealmLoader(); return RealmLoader();
}, },
users : function(UserListLoader) { users : function(UserListLoader) {
return UserListLoader(); return UserListLoader();
} }
}, },
controller : 'UserListCtrl' controller : 'UserListCtrl'
}).when('/create/realm', { }).when('/create/realm', {
templateUrl : 'partials/realm-detail.html', templateUrl : 'partials/realm-detail.html',
resolve : { resolve : {
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
}, },
realm : function(RealmLoader) { realm : function(RealmLoader) {
return {}; return {};
} }
}, },
controller : 'RealmDetailCtrl' controller : 'RealmDetailCtrl'
}).when('/realms/:realm', { }).when('/realms/:realm', {
templateUrl : 'partials/realm-detail.html', templateUrl : 'partials/realm-detail.html',
resolve : { resolve : {
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
}, },
realm : function(RealmLoader) { realm : function(RealmLoader) {
return RealmLoader(); return RealmLoader();
} }
}, },
controller : 'RealmDetailCtrl' controller : 'RealmDetailCtrl'
}).when('/realms', { }).when('/realms', {
templateUrl : 'partials/realm-list.html', templateUrl : 'partials/realm-list.html',
resolve : { resolve : {
realms : function(RealmListLoader) { realms : function(RealmListLoader) {
return RealmListLoader(); return RealmListLoader();
} }
}, },
controller : 'RealmListCtrl' controller : 'RealmListCtrl'
}).otherwise({ }).otherwise({
templateUrl : 'partials/home.html' templateUrl : 'partials/home.html'
}); });
} ]); } ]);
module.config(function($httpProvider) { module.config(function($httpProvider) {
$httpProvider.responseInterceptors.push('errorInterceptor'); $httpProvider.responseInterceptors.push('errorInterceptor');
var spinnerFunction = function(data, headersGetter) { var spinnerFunction = function(data, headersGetter) {
if (resourceRequests == 0) { if (resourceRequests == 0) {
$('#loading').show(); $('#loading').show();
} }
resourceRequests++; resourceRequests++;
return data; return data;
}; };
$httpProvider.defaults.transformRequest.push(spinnerFunction); $httpProvider.defaults.transformRequest.push(spinnerFunction);
$httpProvider.responseInterceptors.push('spinnerInterceptor'); $httpProvider.responseInterceptors.push('spinnerInterceptor');
}); });
module.factory('errorInterceptor', function($q, $window, $rootScope, $location) { module.factory('errorInterceptor', function($q, $window, $rootScope, $location) {
return function(promise) { return function(promise) {
return promise.then(function(response) { return promise.then(function(response) {
$rootScope.httpProviderError = null; $rootScope.httpProviderError = null;
return response; return response;
}, function(response) { }, function(response) {
$rootScope.httpProviderError = response.status; $rootScope.httpProviderError = response.status;
return $q.reject(response); return $q.reject(response);
}); });
}; };
}); });
module.factory('spinnerInterceptor', function($q, $window, $rootScope, $location) { module.factory('spinnerInterceptor', function($q, $window, $rootScope, $location) {
return function(promise) { return function(promise) {
return promise.then(function(response) { return promise.then(function(response) {
resourceRequests--; resourceRequests--;
if (resourceRequests == 0) { if (resourceRequests == 0) {
$('#loading').hide(); $('#loading').hide();
} }
return response; return response;
}, function(response) { }, function(response) {
resourceRequests--; resourceRequests--;
if (resourceRequests == 0) { if (resourceRequests == 0) {
$('#loading').hide(); $('#loading').hide();
} }
return $q.reject(response); return $q.reject(response);
}); });
}; };
}); });
module.directive('kcInput', function() { module.directive('kcInput', function() {
var d = { var d = {
scope : true, scope : true,
replace: false, replace : false,
link : function(scope, element, attrs) { link : function(scope, element, attrs) {
var form = element.closest('form'); var form = element.closest('form');
var label = element.children('label'); var label = element.children('label');
@ -188,15 +188,27 @@ module.directive('kcInput', function() {
}); });
module.directive('kcEnter', function() { module.directive('kcEnter', function() {
return function(scope, element, attrs) { return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) { element.bind("keydown keypress", function(event) {
if(event.which === 13) { if (event.which === 13) {
scope.$apply(function(){ scope.$apply(function() {
scope.$eval(attrs.kcEnter); scope.$eval(attrs.kcEnter);
}); });
event.preventDefault(); event.preventDefault();
} }
}); });
}; };
});
module.filter('remove', function() {
return function(input, remove) {
var out = [];
for (var i = 0; i < input.length; i++) {
if (remove.indexOf(input[i]) == -1) {
out.push(input[i]);
}
}
return out;
};
}); });

View file

@ -39,9 +39,16 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic
} }
}, true); }, true);
$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[i] == $scope.newRole) {
Notifications.warn("Role already exists");
$scope.newRole = null;
return;
}
}
if (!$scope.application.roles) { if (!$scope.application.roles) {
$scope.application.roles = []; $scope.application.roles = [];
} }
@ -51,8 +58,12 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic
} }
} }
$scope.removeRole = function(i) { $scope.removeRole = function(role) {
$scope.application.roles.splice(i, 1); var i = $scope.application.roles.indexOf(role);
if (i > -1) {
$scope.application.roles.splice(i, 1);
}
$scope.removeInitialRole(role);
}; };
$scope.addInitialRole = function() { $scope.addInitialRole = function() {
@ -66,8 +77,11 @@ module.controller('ApplicationDetailCtrl', function($scope, applications, applic
} }
} }
$scope.removeInitialRole = function(i) { $scope.removeInitialRole = function(role) {
$scope.application.initialRoles.splice(i, 1); var i = $scope.application.initialRoles.indexOf(role);
if (i > -1) {
$scope.application.initialRoles.splice(i, 1);
}
}; };
$scope.save = function() { $scope.save = function() {
@ -278,6 +292,14 @@ 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[i] == $scope.newRole) {
Notifications.warn("Role already exists");
$scope.newRole = null;
return;
}
}
if (!$scope.realm.roles) { if (!$scope.realm.roles) {
$scope.realm.roles = []; $scope.realm.roles = [];
} }
@ -287,8 +309,12 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc
} }
} }
$scope.removeRole = function(i) { $scope.removeRole = function(role) {
$scope.realm.roles.splice(i, 1); var i = $scope.realm.roles.indexOf(role);
if (i > -1) {
$scope.realm.roles.splice(i, 1);
}
$scope.removeInitialRole(role);
}; };
$scope.addInitialRole = function() { $scope.addInitialRole = function() {
@ -302,8 +328,11 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc
} }
} }
$scope.removeInitialRole = function(i) { $scope.removeInitialRole = function(role) {
$scope.realm.initialRoles.splice(i, 1); var i = $scope.realm.initialRoles.indexOf(role);
if (i > -1) {
$scope.realm.initialRoles.splice(i, 1);
}
}; };
$scope.save = function() { $scope.save = function() {

View file

@ -21,13 +21,29 @@ module.factory('Notifications', function($rootScope, $timeout) {
$rootScope.notifications = []; $rootScope.notifications = [];
} }
notifications.success = function(message) { notifications.message = function(type, message) {
$rootScope.notification = { $rootScope.notification = {
type : "success", type : type,
message : message message : message
}; };
schedulePop(); schedulePop();
}
notifications.info = function(message) {
notifications.message("info", message);
};
notifications.success = function(message) {
notifications.message("success", message);
};
notifications.error = function(message) {
notifications.message("error", message);
};
notifications.warn = function(message) {
notifications.message("warn", message);
}; };
return notifications; return notifications;

View file

@ -42,7 +42,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label">Roles</label> <label class="control-label">Roles</label>
<div class="controls"> <div class="controls">
<span style="margin-right: 1em;" data-ng-repeat="r in application.roles">{{r}} <button data-ng-click="removeRole($index)"><i class="icon-remove"></i></button></span> <span class="label" style="margin-right: 1em;" data-ng-repeat="r in (application.roles|orderBy:'toString()')">{{r}} <button data-ng-click="removeRole(r)"><i class="icon-remove icon-white"></i></button></span>
<div class="input-append"> <div class="input-append">
<input class="input-small" type="text" data-ng-model="newRole" placeHolder="Role" data-kc-enter="addRole()" /> <input class="input-small" type="text" data-ng-model="newRole" placeHolder="Role" data-kc-enter="addRole()" />
@ -54,11 +54,11 @@
<div class="control-group"> <div class="control-group">
<label class="control-label">Initial Roles</label> <label class="control-label">Initial Roles</label>
<div class="controls"> <div class="controls">
<span style="margin-right: 1em;" data-ng-repeat="r in application.initialRoles">{{r}} <button data-ng-click="removeInitialRole($index)"><i class="icon-remove"></i></button></span> <span class="label" style="margin-right: 1em;" data-ng-repeat="r in (application.initialRoles|orderBy:'toString()')">{{r}} <button data-ng-click="removeInitialRole(r)"><i class="icon-remove icon-white"></i></button></span>
<div class="input-append"> <div class="input-append">
<select style="width: auto;" data-ng-model="newInitialRole" data-ng-click="addInitialRole()"> <select style="width: auto;" data-ng-model="newInitialRole" data-ng-click="addInitialRole()">
<option data-ng-repeat="r in application.roles" value="{{r}}">{{r}}</option> <option data-ng-repeat="r in (application.roles|remove:application.initialRoles|orderBy:'toString()')" value="{{r}}">{{r}}</option>
</select> </select>
</div> </div>
</div> </div>

View file

@ -56,7 +56,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label">Roles</label> <label class="control-label">Roles</label>
<div class="controls"> <div class="controls">
<span style="margin-right: 1em;" data-ng-repeat="r in realm.roles">{{r}} <button data-ng-click="removeRole($index)"><i class="icon-remove"></i></button></span> <span class="label" style="margin-right: 1em;" data-ng-repeat="r in (realm.roles|orderBy:'toString()')">{{r}} <button data-ng-click="removeRole(r)"><i class="icon-remove icon-white"></i></button></span>
<div class="input-append"> <div class="input-append">
<input class="input-small" type="text" data-ng-model="newRole" placeHolder="Role" data-kc-enter="addRole()" /> <input class="input-small" type="text" data-ng-model="newRole" placeHolder="Role" data-kc-enter="addRole()" />
@ -68,11 +68,11 @@
<div class="control-group"> <div class="control-group">
<label class="control-label">Initial Roles</label> <label class="control-label">Initial Roles</label>
<div class="controls"> <div class="controls">
<span style="margin-right: 1em;" data-ng-repeat="r in realm.initialRoles">{{r}} <button data-ng-click="removeInitialRole($index)"><i class="icon-remove"></i></button></span> <span class="label" style="margin-right: 1em;" data-ng-repeat="r in (realm.initialRoles|orderBy:'toString()')">{{r}} <button data-ng-click="removeInitialRole(r)"><i class="icon-remove icon-white"></i></button></span>
<div class="input-append"> <div class="input-append">
<select style="width: auto;" data-ng-model="newInitialRole" data-ng-click="addInitialRole()"> <select style="width: auto;" data-ng-model="newInitialRole" data-ng-click="addInitialRole()">
<option data-ng-repeat="r in realm.roles" value="{{r}}">{{r}}</option> <option data-ng-repeat="r in (realm.roles|remove:realm.initialRoles|orderBy:'toString()')" value="{{r}}">{{r}}</option>
</select> </select>
</div> </div>
</div> </div>