From f4f96127894fd5f954dc4c19cfcca0128492cf89 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 23 Dec 2016 10:14:41 +0000 Subject: [PATCH] Fix MongoDB auth-less connection --- .../storage/backing/MongoDBBacking.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java index 33b03518..cb7f2b1e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java @@ -122,18 +122,34 @@ public class MongoDBBacking extends AbstractBacking { @Override public void init() { - MongoCredential credential = MongoCredential.createCredential( - configuration.getUsername(), - configuration.getDatabase(), - configuration.getPassword().toCharArray() - ); + MongoCredential credential = null; + + if (configuration.getUsername() != null && !configuration.getUsername().equals("") && configuration.getDatabase() != null && !configuration.getDatabase().equals("")) { + if (configuration.getPassword() == null || configuration.getPassword().equals("")) { + credential = MongoCredential.createCredential( + configuration.getUsername(), + configuration.getDatabase(), null + ); + } else { + credential = MongoCredential.createCredential( + configuration.getUsername(), + configuration.getDatabase(), + configuration.getPassword().toCharArray() + ); + } + } ServerAddress address = new ServerAddress( configuration.getAddress().split(":")[0], Integer.parseInt(configuration.getAddress().split(":")[1]) ); - mongoClient = new MongoClient(address, Collections.singletonList(credential)); + if (credential == null) { + mongoClient = new MongoClient(address, Collections.emptyList()); + } else { + mongoClient = new MongoClient(address, Collections.singletonList(credential)); + } + database = mongoClient.getDatabase(configuration.getDatabase()); setAcceptingLogins(true); }