Started adding role mapping

This commit is contained in:
Stian Thorgersen 2013-07-25 17:39:31 +01:00
parent 44d19f837d
commit 23922d6ab7
8 changed files with 191 additions and 10 deletions

View file

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import javax.ws.rs.ApplicationPath;
@ -28,7 +29,7 @@ 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>>();
private static Map<UserId, User> users = new HashMap<UserId, User>();
@DELETE
@Path("applications/{id}")
@ -89,7 +90,6 @@ public class Admin extends javax.ws.rs.core.Application {
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();
}
@ -105,34 +105,39 @@ public class Admin extends javax.ws.rs.core.Application {
@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);
return users.get(new UserId(realm, 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());
LinkedList<User> list = new LinkedList<User>();
for (Entry<UserId, User> e : users.entrySet()) {
if (e.getKey().getRealm().equals(realm)) {
list.add(e.getValue());
}
}
return list;
}
@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);
users.put(new UserId(realm, id), user);
}
@DELETE
@Path("realms/{realm}/users/{id}")
public void deleteUser(@PathParam("realm") String realm, @PathParam("id") String id) {
users.get(realm).remove(id);
users.remove(new UserId(realm, id));
}
}

View file

@ -32,6 +32,15 @@ public class User implements Serializable {
private String lastName;
private String userId;
private String password;
private String[] roles;
public String[] getRoles() {
return roles;
}
public void setRoles(String[] roles) {
this.roles = roles;
}
public String getPassword() {
return password;

View file

@ -0,0 +1,52 @@
package org.keycloak.ui.example;
public class UserId {
private String realm;
private String user;
public UserId(String realm, String user) {
this.realm = realm;
this.user = user;
}
public String getRealm() {
return realm;
}
public String getUser() {
return user;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((realm == null) ? 0 : realm.hashCode());
result = prime * result + ((user == null) ? 0 : user.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserId other = (UserId) obj;
if (realm == null) {
if (other.realm != null)
return false;
} else if (!realm.equals(other.realm))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}
}

View file

@ -110,6 +110,40 @@ module.config([ '$routeProvider', function($routeProvider) {
}
},
controller : 'RealmDetailCtrl'
}).when('/realms/:realm/roles', {
templateUrl : 'partials/role-mapping.html',
resolve : {
realms : function(RealmListLoader) {
return RealmListLoader();
},
realm : function(RealmLoader) {
return RealmLoader();
},
users : function(UserListLoader) {
return UserListLoader();
},
role : function() {
return null;
}
},
controller : 'RoleMappingCtrl'
}).when('/realms/:realm/roles/:role', {
templateUrl : 'partials/role-mapping.html',
resolve : {
realms : function(RealmListLoader) {
return RealmListLoader();
},
realm : function(RealmLoader) {
return RealmLoader();
},
users : function(UserListLoader) {
return UserListLoader();
},
role : function($route) {
return $route.current.params.role;
}
},
controller : 'RoleMappingCtrl'
}).when('/realms', {
templateUrl : 'partials/realm-list.html',
resolve : {

View file

@ -358,3 +358,38 @@ module.controller('RealmDetailCtrl', function($scope, Realm, realms, realm, $loc
});
};
});
module.controller('RoleMappingCtrl', function($scope, realms, realm, users, role, Notifications) {
$scope.realms = realms;
$scope.realm = realm;
$scope.allUsers = users;
$scope.users = [];
$scope.name = realm.name;
$scope.role = role;
console.debug("role: " + role)
$scope.addUser = function() {
for (var i = 0; i < $scope.allUsers.length; i++) {
if ($scope.allUsers[i].userId == $scope.newUser) {
console.debug("add user " + $scope.allUsers[i]);
$scope.users.push($scope.allUsers[i]);
$scope.newUser = null;
// Send notification when rest call is success
Notifications.success("Saved role mapping for user");
}
}
}
$scope.removeUser = function(id) {
for (var i = 0; i < $scope.users.length; i++) {
if ($scope.users[i].userId == id) {
$scope.users.splice(i, 1);
// Send notification when rest call is success
Notifications.success("Removed role mapping for user");
}
}
}
});

View file

@ -5,8 +5,8 @@
<div id="container-right" class="span9">
<h1>
<span class="gray" data-ng-show="create">New Application</span> <span class="gray" data-ng-hide="create">{{application.name}}</span>
configuration
<span class="gray" data-ng-show="create">New Application</span>
<span class="gray" data-ng-hide="create">{{application.name}}</span> configuration
</h1>
<div data-ng-show="applicationForm.showErrors && applicationForm.$error.required" class="alert alert-error">Please fill in all required fields</div>

View file

@ -10,6 +10,7 @@
<ul class="sub-items" data-ng-show="realm.id == r.id">
<li data-ng-class="!path[2] && 'active'"><a href="#/realms/{{r.id}}">Configuration</a></li>
<li data-ng-class="path[2] == 'users' && 'active'"><a href="#/realms/{{r.id}}/users">Users</a></li>
<li data-ng-class="path[2] == 'roles' && 'active'"><a href="#/realms/{{r.id}}/roles">Role mapping</a></li>
</ul>
</li>
</ul>

View file

@ -0,0 +1,45 @@
<div id="wrapper" class="container">
<div class="row">
<aside class="span3" data-ng-include data-src="'partials/realm-menu.html'"></aside>
<div id="actions-bg"></div>
<div id="container-right" class="span9">
<h1>
<span class="gray" data-ng-hide="create">{{name}}</span> role mapping
</h1>
<ul class="nav nav-tabs">
<li data-ng-class="path[3] == r && 'active'" data-ng-repeat="r in (realm.roles|orderBy:'toString()')"><a href="#/realms/{{realm.id}}/roles/{{r}}">{{r}}</a></li>
</ul>
<div data-ng-show="role">
<select id="realm" name="realm" data-ng-model="newUser" data-ng-click="addUser(u)">
<option data-ng-repeat="u in (allUsers|remove:users)" value="{{u.userId}}">{{u.userId}}</option>
</select>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tr data-ng-repeat="user in users">
<td>{{user.userId}}</td>
<td>{{user.firstName}}</td>
<td>{{user.lastName}}</td>
<td>{{user.email}}</td>
<td><button ng-click="removeUser(user.userId)">
<i class="icon-remove"></i>
</button></td>
</tr>
</table>
</div>
</div>
<div id="container-right-bg"></div>
</div>
</div>