work on security uberfire security extension with mongodb
This commit is contained in:
parent
d79f1affc8
commit
f3beaaabab
4 changed files with 93 additions and 25 deletions
|
|
@ -16,8 +16,11 @@
|
|||
|
||||
package org.chtijbug.guvnor.uberfire.security;
|
||||
|
||||
import com.mongodb.Block;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.jboss.errai.security.shared.api.Group;
|
||||
import org.jboss.errai.security.shared.api.GroupImpl;
|
||||
|
|
@ -95,10 +98,16 @@ public class KiePlatformGroupManager implements GroupManager, ContextualManager
|
|||
@Override
|
||||
public List<Group> getAll() throws SecurityManagementException {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Group create(Group entity) throws SecurityManagementException {
|
||||
return entity;
|
||||
|
|
|
|||
|
|
@ -140,11 +140,12 @@ public class KiePlatformRoleManager implements RoleManager, ContextualManager {
|
|||
if (capability != null) {
|
||||
switch (capability) {
|
||||
case CAN_SEARCH_ROLES:
|
||||
case CAN_READ_ROLE:
|
||||
return CapabilityStatus.ENABLED;
|
||||
case CAN_ADD_ROLE:
|
||||
case CAN_UPDATE_ROLE:
|
||||
case CAN_READ_ROLE:
|
||||
case CAN_DELETE_ROLE:
|
||||
return CapabilityStatus.ENABLED;
|
||||
return CapabilityStatus.UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,13 +41,13 @@ public class KiePlatformSecurityService implements UserManagementService {
|
|||
KiePlatformRoleManager roleManager) {
|
||||
//-DconnectionString=localhost:28017 -Ddatabase=businessProxyDB
|
||||
|
||||
connectionString = System.getProperty("connectionString");
|
||||
databaseName=System.getProperty("database");
|
||||
this.connectionString = System.getProperty("connectionString");
|
||||
this.databaseName=System.getProperty("database");
|
||||
System.out.println("KiePlatformSecurityService initialized with databaseName = " + connectionString );
|
||||
mongoClient = MongoClients.create(connectionString);
|
||||
pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
|
||||
this.mongoClient = MongoClients.create(connectionString);
|
||||
this.pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
|
||||
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
|
||||
database = mongoClient.getDatabase(databaseName).withCodecRegistry(pojoCodecRegistry);
|
||||
this.database = mongoClient.getDatabase(databaseName).withCodecRegistry(pojoCodecRegistry);
|
||||
System.out.println("All setup");
|
||||
this.userManager = userManager;
|
||||
this.groupManager = groupManager;
|
||||
|
|
@ -61,16 +61,16 @@ public class KiePlatformSecurityService implements UserManagementService {
|
|||
|
||||
@Override
|
||||
public UserManager users() {
|
||||
return new KiePlatformUserManager();
|
||||
return userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupManager groups() {
|
||||
return new KiePlatformGroupManager();
|
||||
return groupManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoleManager roles() {
|
||||
return new KiePlatformRoleManager();
|
||||
return roleManager;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,19 @@
|
|||
|
||||
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.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
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.UserImpl;
|
||||
import org.slf4j.Logger;
|
||||
|
|
@ -31,6 +41,9 @@ import org.uberfire.ext.security.management.impl.UserManagerSettingsImpl;
|
|||
import org.uberfire.ext.security.management.util.SecurityManagementUtils;
|
||||
|
||||
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>
|
||||
|
|
@ -42,7 +55,6 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
|
|||
private static final Logger LOG = LoggerFactory.getLogger(KiePlatformUserManager.class);
|
||||
|
||||
|
||||
|
||||
private MongoClient mongoClient;
|
||||
private CodecRegistry pojoCodecRegistry;
|
||||
private MongoDatabase database;
|
||||
|
|
@ -59,16 +71,16 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
|
|||
//loadConfig(gitPrefs);
|
||||
}
|
||||
|
||||
public void setMongo (MongoClient mongoClient,CodecRegistry pojoCodecRegistry,MongoDatabase database){
|
||||
this.mongoClient=mongoClient;
|
||||
public void setMongo(MongoClient mongoClient, CodecRegistry pojoCodecRegistry, MongoDatabase database) {
|
||||
this.mongoClient = mongoClient;
|
||||
this.pojoCodecRegistry = pojoCodecRegistry;
|
||||
this.database=database;
|
||||
this.database = database;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(final UserSystemManager userSystemManager) throws Exception {
|
||||
|
||||
System.out.println("All setup");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -78,7 +90,23 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
|
|||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -90,9 +118,39 @@ public class KiePlatformUserManager implements UserManager, ContextualManager {
|
|||
@Override
|
||||
public List<User> getAll() throws SecurityManagementException {
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
public User create(User entity) throws SecurityManagementException {
|
||||
return entity;
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue