work on security uberfire security extension with mongodb

This commit is contained in:
Nicolas Héron 2020-08-10 12:46:42 +02:00
commit f3beaaabab
4 changed files with 93 additions and 25 deletions

View file

@ -16,8 +16,11 @@
package org.chtijbug.guvnor.uberfire.security; package org.chtijbug.guvnor.uberfire.security;
import com.mongodb.Block;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry; import org.bson.codecs.configuration.CodecRegistry;
import org.jboss.errai.security.shared.api.Group; import org.jboss.errai.security.shared.api.Group;
import org.jboss.errai.security.shared.api.GroupImpl; import org.jboss.errai.security.shared.api.GroupImpl;
@ -95,10 +98,16 @@ public class KiePlatformGroupManager implements GroupManager, ContextualManager
@Override @Override
public List<Group> getAll() throws SecurityManagementException { public List<Group> getAll() throws SecurityManagementException {
List<Group> groups = new ArrayList<>(); List<Group> groups = new ArrayList<>();
groups.add(new GroupImpl("main")); MongoCollection<Document> userGroupsCollection = database.getCollection("userGroups");
userGroupsCollection.find().forEach((Block<? super Document>) document -> {
String groupName = document.getString("name");
Group group = new GroupImpl(groupName);
groups.add(group);
});
return groups; return groups;
} }
@Override @Override
public Group create(Group entity) throws SecurityManagementException { public Group create(Group entity) throws SecurityManagementException {
return entity; return entity;

View file

@ -140,11 +140,12 @@ public class KiePlatformRoleManager implements RoleManager, ContextualManager {
if (capability != null) { if (capability != null) {
switch (capability) { switch (capability) {
case CAN_SEARCH_ROLES: case CAN_SEARCH_ROLES:
case CAN_READ_ROLE:
return CapabilityStatus.ENABLED;
case CAN_ADD_ROLE: case CAN_ADD_ROLE:
case CAN_UPDATE_ROLE: case CAN_UPDATE_ROLE:
case CAN_READ_ROLE:
case CAN_DELETE_ROLE: case CAN_DELETE_ROLE:
return CapabilityStatus.ENABLED; return CapabilityStatus.UNSUPPORTED;
} }
} }

View file

@ -41,13 +41,13 @@ public class KiePlatformSecurityService implements UserManagementService {
KiePlatformRoleManager roleManager) { KiePlatformRoleManager roleManager) {
//-DconnectionString=localhost:28017 -Ddatabase=businessProxyDB //-DconnectionString=localhost:28017 -Ddatabase=businessProxyDB
connectionString = System.getProperty("connectionString"); this.connectionString = System.getProperty("connectionString");
databaseName=System.getProperty("database"); this.databaseName=System.getProperty("database");
System.out.println("KiePlatformSecurityService initialized with databaseName = " + connectionString ); System.out.println("KiePlatformSecurityService initialized with databaseName = " + connectionString );
mongoClient = MongoClients.create(connectionString); this.mongoClient = MongoClients.create(connectionString);
pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), this.pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build())); fromProviders(PojoCodecProvider.builder().automatic(true).build()));
database = mongoClient.getDatabase(databaseName).withCodecRegistry(pojoCodecRegistry); this.database = mongoClient.getDatabase(databaseName).withCodecRegistry(pojoCodecRegistry);
System.out.println("All setup"); System.out.println("All setup");
this.userManager = userManager; this.userManager = userManager;
this.groupManager = groupManager; this.groupManager = groupManager;
@ -61,16 +61,16 @@ public class KiePlatformSecurityService implements UserManagementService {
@Override @Override
public UserManager users() { public UserManager users() {
return new KiePlatformUserManager(); return userManager;
} }
@Override @Override
public GroupManager groups() { public GroupManager groups() {
return new KiePlatformGroupManager(); return groupManager;
} }
@Override @Override
public RoleManager roles() { public RoleManager roles() {
return new KiePlatformRoleManager(); return roleManager;
} }
} }

View file

@ -1,12 +1,12 @@
/* /*
* Copyright 2016 Red Hat, Inc. and/or its affiliates. * Copyright 2016 Red Hat, Inc. and/or its affiliates.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -16,9 +16,19 @@
package org.chtijbug.guvnor.uberfire.security; package org.chtijbug.guvnor.uberfire.security;
import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.DBRef;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry; import org.bson.codecs.configuration.CodecRegistry;
import org.jboss.errai.security.shared.api.Group;
import org.jboss.errai.security.shared.api.GroupImpl;
import org.jboss.errai.security.shared.api.Role;
import org.jboss.errai.security.shared.api.RoleImpl;
import org.jboss.errai.security.shared.api.identity.User; import org.jboss.errai.security.shared.api.identity.User;
import org.jboss.errai.security.shared.api.identity.UserImpl; import org.jboss.errai.security.shared.api.identity.UserImpl;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -31,18 +41,20 @@ import org.uberfire.ext.security.management.impl.UserManagerSettingsImpl;
import org.uberfire.ext.security.management.util.SecurityManagementUtils; import org.uberfire.ext.security.management.util.SecurityManagementUtils;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import static com.mongodb.client.model.Filters.eq;
/** /**
* <p>Users manager service provider implementation for Apache tomcat, when using default realm based on properties files.</p> * <p>Users manager service provider implementation for Apache tomcat, when using default realm based on properties files.</p>
* *
* @since 0.8.0 * @since 0.8.0
*/ */
public class KiePlatformUserManager implements UserManager, ContextualManager { public class KiePlatformUserManager implements UserManager, ContextualManager {
private static final Logger LOG = LoggerFactory.getLogger(KiePlatformUserManager.class); private static final Logger LOG = LoggerFactory.getLogger(KiePlatformUserManager.class);
private MongoClient mongoClient; private MongoClient mongoClient;
private CodecRegistry pojoCodecRegistry; private CodecRegistry pojoCodecRegistry;
private MongoDatabase database; private MongoDatabase database;
@ -59,16 +71,16 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
//loadConfig(gitPrefs); //loadConfig(gitPrefs);
} }
public void setMongo (MongoClient mongoClient,CodecRegistry pojoCodecRegistry,MongoDatabase database){ public void setMongo(MongoClient mongoClient, CodecRegistry pojoCodecRegistry, MongoDatabase database) {
this.mongoClient=mongoClient; this.mongoClient = mongoClient;
this.pojoCodecRegistry = pojoCodecRegistry; this.pojoCodecRegistry = pojoCodecRegistry;
this.database=database; this.database = database;
} }
@Override @Override
public void initialize(final UserSystemManager userSystemManager) throws Exception { public void initialize(final UserSystemManager userSystemManager) throws Exception {
System.out.println("All setup");
} }
@Override @Override
@ -78,21 +90,67 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
@Override @Override
public SearchResponse<User> search(SearchRequest request) throws SecurityManagementException { public SearchResponse<User> search(SearchRequest request) throws SecurityManagementException {
SearchResponse<User> response = new SearchResponseImpl<>();
MongoCollection<Document> userCollection = database.getCollection("user");
BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("login", new BasicDBObject("$regex", request.getSearchPattern() + ".*").append("$options", "i"));
List<User> users = new ArrayList<>();
long totalNumber = userCollection.countDocuments(regexQuery);
FindIterable<Document> documents = userCollection.find(regexQuery).skip(request.getPageSize() * (request.getPage() - 1)).limit(request.getPageSize());
documents.forEach((Block<? super Document>) document -> {
String userName = document.getString("login");
User user = fillUser(userName, document);
users.add(user);
});
boolean hasNextPage=true;
if ((request.getPageSize() * (request.getPage())>totalNumber)){
hasNextPage=false;
}
SearchResponse<User> response = new SearchResponseImpl(users, request.getPage(),request.getPageSize(),Long.valueOf(totalNumber).intValue(), hasNextPage);
return response; return response;
} }
@Override @Override
public User get(String identifier) throws SecurityManagementException { public User get(String identifier) throws SecurityManagementException {
return new UserImpl(identifier); return new UserImpl(identifier);
} }
@Override @Override
public List<User> getAll() throws SecurityManagementException { public List<User> getAll() throws SecurityManagementException {
List<User> users = new ArrayList<>(); List<User> users = new ArrayList<>();
MongoCollection<Document> userCollection = database.getCollection("user");
userCollection.find().forEach((Block<? super Document>) document -> {
String userName = document.getString("login");
User user = fillUser(userName, document);
users.add(user);
});
return users; return users;
} }
private User fillUser(String userName, Document document) {
AtomicReference<ArrayList<DBRef>> roles = new AtomicReference<ArrayList<DBRef>>(new ArrayList());
AtomicReference<ArrayList<DBRef>> groups = new AtomicReference<ArrayList<DBRef>>(new ArrayList());
roles.set((ArrayList) document.get("userRoles"));
groups.set((ArrayList) document.get("userGroups"));
MongoCollection<Document> userRolesCollection = database.getCollection("userRoles");
List<Role> roleList = new ArrayList<>();
for (DBRef dbRef : roles.get()) {
Document roleDocument = userRolesCollection.find(eq("_id", dbRef.getId())).first();
Role role = new RoleImpl(roleDocument.getString("name"));
roleList.add(role);
}
MongoCollection<Document> userGroupsCollection = database.getCollection("userGroups");
List<Group> groupList = new ArrayList<>();
for (DBRef dbRef : groups.get()) {
Document groupDocument = userGroupsCollection.find(eq("_id", dbRef.getId())).first();
Group group = new GroupImpl(groupDocument.getString("name"));
groupList.add(group);
}
User user = new UserImpl(userName,roleList,groupList);
return user;
}
@Override @Override
public User create(User entity) throws SecurityManagementException { public User create(User entity) throws SecurityManagementException {
return entity; return entity;
@ -100,7 +158,7 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
@Override @Override
public User update(User entity) throws SecurityManagementException { public User update(User entity) throws SecurityManagementException {
return entity; return entity;
} }
@Override @Override
@ -114,10 +172,10 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
final Map<Capability, CapabilityStatus> capabilityStatusMap = new HashMap<Capability, CapabilityStatus>(8); final Map<Capability, CapabilityStatus> capabilityStatusMap = new HashMap<Capability, CapabilityStatus>(8);
for (final Capability capability : SecurityManagementUtils.USERS_CAPABILITIES) { for (final Capability capability : SecurityManagementUtils.USERS_CAPABILITIES) {
capabilityStatusMap.put(capability, capabilityStatusMap.put(capability,
getCapabilityStatus(capability)); getCapabilityStatus(capability));
} }
return new UserManagerSettingsImpl(capabilityStatusMap, return new UserManagerSettingsImpl(capabilityStatusMap,
null); null);
} }
@Override @Override