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, 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);
}
}

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'
}).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 : {

View file

@ -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");
});
});

View file

@ -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 id = $route.current.params.user;
var delay = $q.defer();
User.get({
realm : $route.current.params.realm,
name : name
id : id
}, function(user) {
delay.resolve(user);
}, function() {
delay.reject('Unable to fetch user ' + name);
});
return delay.promise;
}
};
});

View file

@ -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>

View file

@ -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>