keycloak-memoizeit

Add/remove users to ui

7/25/2013 12:12:00 PM

Details

diff --git a/ui/src/main/java/org/keycloak/ui/example/Admin.java b/ui/src/main/java/org/keycloak/ui/example/Admin.java
index 2a70f78..912b959 100644
--- a/ui/src/main/java/org/keycloak/ui/example/Admin.java
+++ b/ui/src/main/java/org/keycloak/ui/example/Admin.java
@@ -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, Map<String, User>> users = new HashMap<String, Map<String, User>>();
+
     @DELETE
-    @Path("/applications/{id}")
-    public void delete(@PathParam("id") String id) {
+    @Path("applications/{id}")
+    public void deleteApplication(@PathParam("id") String id) {
         applications.remove(id);
     }
 
     @DELETE
-    @Path("/realms/{id}")
+    @Path("realms/{id}")
     public void deleteRealm(@PathParam("id") String id) {
         realms.remove(id);
+        users.remove(id);
     }
 
     @GET
-    @Path("/applications/{id}")
+    @Path("applications/{id}")
     @Produces(MediaType.APPLICATION_JSON)
     public Application getApplication(@PathParam("id") String id) {
         return applications.get(id);
     }
 
     @GET
-    @Path("/applications")
+    @Path("applications")
     @Produces(MediaType.APPLICATION_JSON)
     public List<Application> getApplications() {
         return new LinkedList<Application>(applications.values());
     }
 
     @GET
-    @Path("/realms/{id}")
+    @Path("realms/{id}")
     @Produces(MediaType.APPLICATION_JSON)
     public Realm getRealm(@PathParam("id") String id) {
         return realms.get(id);
@@ -63,14 +66,14 @@ public class Admin extends javax.ws.rs.core.Application {
 
 
     @GET
-    @Path("/realms")
+    @Path("realms")
     @Produces(MediaType.APPLICATION_JSON)
     public List<Realm> getRealms() {
         return new LinkedList<Realm>(realms.values());
     }
 
     @POST
-    @Path("/applications")
+    @Path("applications")
     @Consumes(MediaType.APPLICATION_JSON)
     public Response save(Application application) {
         String id = UUID.randomUUID().toString();
@@ -80,26 +83,56 @@ public class Admin extends javax.ws.rs.core.Application {
     }
 
     @POST
-    @Path("/realms")
+    @Path("realms")
     @Consumes(MediaType.APPLICATION_JSON)
     public Response save(Realm realm) {
         String id = UUID.randomUUID().toString();
         realm.setId(id);
         realms.put(id, realm);
+        users.put(id, new HashMap<String, User>());
         return Response.created(URI.create("/realms/" + id)).build();
     }
 
     @PUT
-    @Path("/applications/{id}")
+    @Path("applications/{id}")
     @Consumes(MediaType.APPLICATION_JSON)
     public void save(@PathParam("id") String id, Application application) {
         applications.put(id, application);
     }
 
     @PUT
-    @Path("/realms/{id}")
+    @Path("realms/{id}")
     @Consumes(MediaType.APPLICATION_JSON)
     public void save(@PathParam("id") String id, Realm 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);
+    }
+
 }
\ No newline at end of file
diff --git a/ui/src/main/java/org/keycloak/ui/example/Attribute.java b/ui/src/main/java/org/keycloak/ui/example/Attribute.java
new file mode 100644
index 0000000..7e95923
--- /dev/null
+++ b/ui/src/main/java/org/keycloak/ui/example/Attribute.java
@@ -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;
+    }
+
+}
diff --git a/ui/src/main/java/org/keycloak/ui/example/User.java b/ui/src/main/java/org/keycloak/ui/example/User.java
new file mode 100644
index 0000000..e38778f
--- /dev/null
+++ b/ui/src/main/java/org/keycloak/ui/example/User.java
@@ -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;
+    }
+
+}
\ No newline at end of file
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 7b5546f..f3559c0 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
@@ -46,6 +46,20 @@ module.config([ '$routeProvider', function($routeProvider) {
 			}
 		},
 		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', {
 		templateUrl : 'partials/user-detail.html',
 		resolve : {
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 a18d6e3..a90d3bb 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
@@ -215,13 +215,14 @@ module.controller('UserDetailCtrl', function($scope, realms, realm, user, User, 
 	$scope.save = function() {
 		if ($scope.userForm.$valid) {
 			User.save({
-				realmKey : realm.key
+				realm : realm.id,
+				id : $scope.user.userId
 			}, $scope.user, function() {
 				$scope.changed = false;
 				user = angular.copy($scope.user);
 
 				if ($scope.create) {
-					$location.url("/realms/" + realm.key + "/users/" + user.userId);
+					$location.url("/realms/" + realm.id + "/users/" + $scope.user.userId);
 					Notifications.success("Created user");
 				} else {
 					Notifications.success("Saved changes to user");
@@ -239,16 +240,16 @@ module.controller('UserDetailCtrl', function($scope, realms, realm, user, User, 
 	};
 
 	$scope.cancel = function() {
-		$location.url("/realms/" + realm.key + "/users");
+		$location.url("/realms/" + realm.id + "/users");
 	};
 
 	$scope.remove = function() {
 		Dialog.confirmDelete($scope.user.userId, 'user', function() {
 			$scope.user.$remove({
-				realmKey : realm.key,
-				userId : $scope.user.userId
+				realm : realm.id,
+				id : $scope.user.userId
 			}, function() {
-				$location.url("/realms/" + realm.key + "/users");
+				$location.url("/realms/" + realm.id + "/users");
 				Notifications.success("Deleted user");
 			});
 		});
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 e39d0b4..f8d46c2 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
@@ -20,14 +20,14 @@ module.factory('Notifications', function($rootScope, $timeout) {
 	if (!$rootScope.notifications) {
 		$rootScope.notifications = [];
 	}
-	
+
 	notifications.message = function(type, message) {
 		$rootScope.notification = {
-				type : type,
-				message : message
-			};
+			type : type,
+			message : message
+		};
 
-			schedulePop();
+		schedulePop();
 	}
 
 	notifications.info = function(message) {
@@ -140,7 +140,7 @@ module.factory('RealmLoader', function(Realm, $route, $q) {
 });
 
 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',
 		id : '@id'
 	}, {
@@ -154,7 +154,7 @@ module.factory('UserListLoader', function(User, $route, $q) {
 	return function() {
 		var delay = $q.defer();
 		User.query({
-			realmKey : $route.current.params.realmKey
+			realm : $route.current.params.realm
 		}, function(users) {
 			delay.resolve(users);
 		}, function() {
@@ -166,21 +166,17 @@ module.factory('UserListLoader', function(User, $route, $q) {
 
 module.factory('UserLoader', function(User, $route, $q) {
 	return function() {
-		var name = $route.current.params.user;
-		if (name == 'new') {
-			return {};
-		} else {
-			var delay = $q.defer();
-			User.get({
-				realm : $route.current.params.realm,
-				name : name
-			}, function(user) {
-				delay.resolve(user);
-			}, function() {
-				delay.reject('Unable to fetch user ' + name);
-			});
-			return delay.promise;
-		}
+		var id = $route.current.params.user;
+		var delay = $q.defer();
+		User.get({
+			realm : $route.current.params.realm,
+			id : id
+		}, function(user) {
+			delay.resolve(user);
+		}, function() {
+			delay.reject('Unable to fetch user ' + name);
+		});
+		return delay.promise;
 	};
 });
 
diff --git a/ui/src/main/resources/META-INF/resources/ui/partials/user-detail.html b/ui/src/main/resources/META-INF/resources/ui/partials/user-detail.html
index 7427115..8591a8b 100644
--- a/ui/src/main/resources/META-INF/resources/ui/partials/user-detail.html
+++ b/ui/src/main/resources/META-INF/resources/ui/partials/user-detail.html
@@ -77,7 +77,7 @@
                 <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="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>
                 </div>
                 
diff --git a/ui/src/main/resources/META-INF/resources/ui/partials/user-list.html b/ui/src/main/resources/META-INF/resources/ui/partials/user-list.html
index 0b31608..d5aec2c 100644
--- a/ui/src/main/resources/META-INF/resources/ui/partials/user-list.html
+++ b/ui/src/main/resources/META-INF/resources/ui/partials/user-list.html
@@ -4,7 +4,7 @@
         <div id="actions-bg"></div>
 
         <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>
                     <span class="gray">{{realm.name}}</span> users
@@ -20,7 +20,7 @@
                         </tr>
                     </thead>
                     <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.lastName}}</td>
                         <td>{{user.email}}</td>