Add/remove users to ui

This commit is contained in:
Stian Thorgersen 2013-07-25 16:12:00 +01:00
parent 362cf14d49
commit 44d19f837d
8 changed files with 210 additions and 42 deletions

View file

@ -28,34 +28,37 @@ public class Admin extends javax.ws.rs.core.Application {
private static Map<String, Realm> realms = new HashMap<String, Realm>(); private static Map<String, Realm> realms = new HashMap<String, Realm>();
private static Map<String, Map<String, User>> users = new HashMap<String, Map<String, User>>();
@DELETE @DELETE
@Path("/applications/{id}") @Path("applications/{id}")
public void delete(@PathParam("id") String id) { public void deleteApplication(@PathParam("id") String id) {
applications.remove(id); applications.remove(id);
} }
@DELETE @DELETE
@Path("/realms/{id}") @Path("realms/{id}")
public void deleteRealm(@PathParam("id") String id) { public void deleteRealm(@PathParam("id") String id) {
realms.remove(id); realms.remove(id);
users.remove(id);
} }
@GET @GET
@Path("/applications/{id}") @Path("applications/{id}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Application getApplication(@PathParam("id") String id) { public Application getApplication(@PathParam("id") String id) {
return applications.get(id); return applications.get(id);
} }
@GET @GET
@Path("/applications") @Path("applications")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public List<Application> getApplications() { public List<Application> getApplications() {
return new LinkedList<Application>(applications.values()); return new LinkedList<Application>(applications.values());
} }
@GET @GET
@Path("/realms/{id}") @Path("realms/{id}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Realm getRealm(@PathParam("id") String id) { public Realm getRealm(@PathParam("id") String id) {
return realms.get(id); return realms.get(id);
@ -63,14 +66,14 @@ public class Admin extends javax.ws.rs.core.Application {
@GET @GET
@Path("/realms") @Path("realms")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public List<Realm> getRealms() { public List<Realm> getRealms() {
return new LinkedList<Realm>(realms.values()); return new LinkedList<Realm>(realms.values());
} }
@POST @POST
@Path("/applications") @Path("applications")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public Response save(Application application) { public Response save(Application application) {
String id = UUID.randomUUID().toString(); String id = UUID.randomUUID().toString();
@ -80,26 +83,56 @@ public class Admin extends javax.ws.rs.core.Application {
} }
@POST @POST
@Path("/realms") @Path("realms")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public Response save(Realm realm) { public Response save(Realm realm) {
String id = UUID.randomUUID().toString(); String id = UUID.randomUUID().toString();
realm.setId(id); realm.setId(id);
realms.put(id, realm); realms.put(id, realm);
users.put(id, new HashMap<String, User>());
return Response.created(URI.create("/realms/" + id)).build(); return Response.created(URI.create("/realms/" + id)).build();
} }
@PUT @PUT
@Path("/applications/{id}") @Path("applications/{id}")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public void save(@PathParam("id") String id, Application application) { public void save(@PathParam("id") String id, Application application) {
applications.put(id, application); applications.put(id, application);
} }
@PUT @PUT
@Path("/realms/{id}") @Path("realms/{id}")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public void save(@PathParam("id") String id, Realm realm) { public void save(@PathParam("id") String id, Realm realm) {
realms.put(id, realm); realms.put(id, realm);
users.put(id, new HashMap<String, User>());
} }
@GET
@Path("realms/{realm}/users/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("realm") String realm, @PathParam("id") String id) {
return users.get(realm).get(id);
}
@GET
@Path("realms/{realm}/users")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(@PathParam("realm") String realm) {
return new LinkedList<User>(users.get(realm).values());
}
@PUT
@Path("realms/{realm}/users/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public void save(@PathParam("realm") String realm, @PathParam("id") String id, User user) {
users.get(realm).put(id, user);
}
@DELETE
@Path("realms/{realm}/users/{id}")
public void deleteUser(@PathParam("realm") String realm, @PathParam("id") String id) {
users.get(realm).remove(id);
}
} }

View file

@ -0,0 +1,40 @@
package org.keycloak.ui.example;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Attribute implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String value;
public Attribute() {
}
public Attribute(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public void setName(String name) {
this.name = name;
}
public void setValue(String value) {
this.value = value;
}
}

View file

@ -0,0 +1,84 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.ui.example;
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private List<Attribute> attributes;
private String email;
private String firstName;
private String lastName;
private String userId;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Attribute> getAttributes() {
return attributes;
}
public String getEmail() {
return email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getUserId() {
return userId;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
public void setEmail(String email) {
this.email = email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setUserId(String userId) {
this.userId = userId;
}
}

View file

@ -46,6 +46,20 @@ module.config([ '$routeProvider', function($routeProvider) {
} }
}, },
controller : 'ApplicationListCtrl' controller : 'ApplicationListCtrl'
}).when('/create/user/:realm', {
templateUrl : 'partials/user-detail.html',
resolve : {
realms : function(RealmListLoader) {
return RealmListLoader();
},
realm : function(RealmLoader) {
return RealmLoader();
},
user : function(UserLoader) {
return {};
}
},
controller : 'UserDetailCtrl'
}).when('/realms/:realm/users/:user', { }).when('/realms/:realm/users/:user', {
templateUrl : 'partials/user-detail.html', templateUrl : 'partials/user-detail.html',
resolve : { resolve : {

View file

@ -215,13 +215,14 @@ module.controller('UserDetailCtrl', function($scope, realms, realm, user, User,
$scope.save = function() { $scope.save = function() {
if ($scope.userForm.$valid) { if ($scope.userForm.$valid) {
User.save({ User.save({
realmKey : realm.key realm : realm.id,
id : $scope.user.userId
}, $scope.user, function() { }, $scope.user, function() {
$scope.changed = false; $scope.changed = false;
user = angular.copy($scope.user); user = angular.copy($scope.user);
if ($scope.create) { if ($scope.create) {
$location.url("/realms/" + realm.key + "/users/" + user.userId); $location.url("/realms/" + realm.id + "/users/" + $scope.user.userId);
Notifications.success("Created user"); Notifications.success("Created user");
} else { } else {
Notifications.success("Saved changes to user"); Notifications.success("Saved changes to user");
@ -239,16 +240,16 @@ module.controller('UserDetailCtrl', function($scope, realms, realm, user, User,
}; };
$scope.cancel = function() { $scope.cancel = function() {
$location.url("/realms/" + realm.key + "/users"); $location.url("/realms/" + realm.id + "/users");
}; };
$scope.remove = function() { $scope.remove = function() {
Dialog.confirmDelete($scope.user.userId, 'user', function() { Dialog.confirmDelete($scope.user.userId, 'user', function() {
$scope.user.$remove({ $scope.user.$remove({
realmKey : realm.key, realm : realm.id,
userId : $scope.user.userId id : $scope.user.userId
}, function() { }, function() {
$location.url("/realms/" + realm.key + "/users"); $location.url("/realms/" + realm.id + "/users");
Notifications.success("Deleted user"); Notifications.success("Deleted user");
}); });
}); });

View file

@ -23,11 +23,11 @@ module.factory('Notifications', function($rootScope, $timeout) {
notifications.message = function(type, message) { notifications.message = function(type, message) {
$rootScope.notification = { $rootScope.notification = {
type : type, type : type,
message : message message : message
}; };
schedulePop(); schedulePop();
} }
notifications.info = function(message) { notifications.info = function(message) {
@ -140,7 +140,7 @@ module.factory('RealmLoader', function(Realm, $route, $q) {
}); });
module.factory('User', function($resource) { module.factory('User', function($resource) {
return $resource('/ejs-identity/api/im/:realm/users/:id', { return $resource('/keycloak-server/ui/api/realms/:realm/users/:id', {
realm : '@realm', realm : '@realm',
id : '@id' id : '@id'
}, { }, {
@ -154,7 +154,7 @@ module.factory('UserListLoader', function(User, $route, $q) {
return function() { return function() {
var delay = $q.defer(); var delay = $q.defer();
User.query({ User.query({
realmKey : $route.current.params.realmKey realm : $route.current.params.realm
}, function(users) { }, function(users) {
delay.resolve(users); delay.resolve(users);
}, function() { }, function() {
@ -166,21 +166,17 @@ module.factory('UserListLoader', function(User, $route, $q) {
module.factory('UserLoader', function(User, $route, $q) { module.factory('UserLoader', function(User, $route, $q) {
return function() { return function() {
var name = $route.current.params.user; var id = $route.current.params.user;
if (name == 'new') { var delay = $q.defer();
return {}; User.get({
} else { realm : $route.current.params.realm,
var delay = $q.defer(); id : id
User.get({ }, function(user) {
realm : $route.current.params.realm, delay.resolve(user);
name : name }, function() {
}, function(user) { delay.reject('Unable to fetch user ' + name);
delay.resolve(user); });
}, function() { return delay.promise;
delay.reject('Unable to fetch user ' + name);
});
return delay.promise;
}
}; };
}); });

View file

@ -77,7 +77,7 @@
<div class="form-actions" data-ng-show="!create"> <div class="form-actions" data-ng-show="!create">
<button type="submit" data-ng-click="save()" class="btn btn-primary" data-ng-show="changed">Save changes</button> <button type="submit" data-ng-click="save()" class="btn btn-primary" data-ng-show="changed">Save changes</button>
<button type="submit" data-ng-click="reset()" class="btn" data-ng-show="changed">Clear changes</button> <button type="submit" data-ng-click="reset()" class="btn" data-ng-show="changed">Clear changes</button>
<a href="#/realms/{{realm.name}}/users" data-ng-hide="changed">View users &#187;</a> <a href="#/realms/{{realm.id}}/users" data-ng-hide="changed">View users &#187;</a>
<button type="submit" data-ng-click="remove()" class="btn btn-danger" data-ng-hide="changed">Delete</button> <button type="submit" data-ng-click="remove()" class="btn btn-danger" data-ng-hide="changed">Delete</button>
</div> </div>

View file

@ -4,7 +4,7 @@
<div id="actions-bg"></div> <div id="actions-bg"></div>
<div id="container-right" class="span9"> <div id="container-right" class="span9">
<a class="btn btn-small pull-right" href="#/realms/{{realm.key}}/users/new">Add User</a> <a class="btn btn-small pull-right" href="#/create/user/{{realm.id}}">Add User</a>
<h1> <h1>
<span class="gray">{{realm.name}}</span> users <span class="gray">{{realm.name}}</span> users
@ -20,7 +20,7 @@
</tr> </tr>
</thead> </thead>
<tr data-ng-repeat="user in users"> <tr data-ng-repeat="user in users">
<td><a href="#/realms/{{realm.key}}/users/{{user.userId}}">{{user.userId}}</a></td> <td><a href="#/realms/{{realm.id}}/users/{{user.userId}}">{{user.userId}}</a></td>
<td>{{user.firstName}}</td> <td>{{user.firstName}}</td>
<td>{{user.lastName}}</td> <td>{{user.lastName}}</td>
<td>{{user.email}}</td> <td>{{user.email}}</td>