/** * Creates a {@link MongoDBBasedUserManager} instance with no parameters. * This method is same as <code>new MongoDBBasedUserManager("classpath:mongo.properties")</code>. * It will read a configuration file in class path named "mongo.properties". * * @return Instance of <code>MongoDBBasedUserManager</code> */ public static MongoDBBasedUserManager newDefaultUserManager() { return new MongoDBBasedUserManager(MONGO_CONFIG_FILE); }
@Override public User load(String key) throws Exception { User user; user = fetchUserFromMongoDB(key); if (user == null) { return new User(); } else { return user; } }
private String generateEncryptPassword(final User user) { return generateEncryptPassword(user, null); }
@Override public void update(final User user) { if (user == null) { throw new IllegalArgumentException("User can't null"); } if (Strings.isNullOrEmpty(user.getUsername())) { throw new IllegalArgumentException("Username of the user can't be null or empty"); } User old = find(user.getUsername()); String newEncryptPassword = generateEncryptPassword(user); if (!old.getPassword().equals(newEncryptPassword)) { user.setPassword(newEncryptPassword); } mongoDBUtil.execute(userCollectionName, collection -> { collection.updateOne(new Document(usernameKey, user.getUsername()), new Document("$set", new Document(usernameKey, user.getPassword()))); return null; }); cache.put(user.getUsername(), user); }
@Override public List<User> findAll() { return mongoDBUtil.execute(userCollectionName, collection -> { FindIterable<Document> result = collection.find(); List<User> users = new ArrayList<>(); for (Document document : result) { users.add(formUser(document)); } return users; }); }
@Override public void update(final User user) { if (user == null) { throw new IllegalArgumentException("User can't null"); } if (Strings.isNullOrEmpty(user.getUsername())) { throw new IllegalArgumentException("Username of the user can't be null or empty"); } User old = find(user.getUsername()); String newEncryptPassword = generateEncryptPassword(user); if (!old.getPassword().equals(newEncryptPassword)) { user.setPassword(newEncryptPassword); } mongoDBUtil.execute(userCollectionName, collection -> { collection.updateOne(new Document(usernameKey, user.getUsername()), new Document("$set", new Document(usernameKey, user.getPassword()))); return null; }); cache.put(user.getUsername(), user); }
@Override public List<User> findAll() { return mongoDBUtil.execute(userCollectionName, collection -> { FindIterable<Document> result = collection.find(); List<User> users = new ArrayList<>(); for (Document document : result) { users.add(formUser(document)); } return users; }); }
private String generateEncryptPassword(final User user) { return generateEncryptPassword(user, null); }
public User fetchUserFromMongoDB(final String username) { return mongoDBUtil.execute(userCollectionName, collection -> { Document document = collection.find(new Document(usernameKey, username)).first(); if (document != null) { return formUser(document); } return null; }); }
@Override public User load(String key) throws Exception { User user; user = fetchUserFromMongoDB(key); if (user == null) { return new User(); } else { return user; } }
/** * Creates a {@link MongoDBBasedUserManager} instance with no parameters. * This method is same as <code>new MongoDBBasedUserManager("classpath:mongo.properties")</code>. * It will read a configuration file in class path named "mongo.properties". * * @return Instance of <code>MongoDBBasedUserManager</code> */ public static MongoDBBasedUserManager newDefaultUserManager() { return new MongoDBBasedUserManager(MONGO_CONFIG_FILE); }
@Override public User check(String username, String password) { if (username == null || password == null) { return null; } User user = cache.getUnchecked(username); if (user == null || user.getUsername() == null || user.getPassword() == null) { return null; } String encryptPassword = generateEncryptPassword(user, password); if (user.getPassword().equals(encryptPassword)) { return user; } return null; }
public User fetchUserFromMongoDB(final String username) { return mongoDBUtil.execute(userCollectionName, collection -> { Document document = collection.find(new Document(usernameKey, username)).first(); if (document != null) { return formUser(document); } return null; }); }
@Override public User check(String username, String password) { if (username == null || password == null) { return null; } User user = cache.getUnchecked(username); if (user == null || user.getUsername() == null || user.getPassword() == null) { return null; } String encryptPassword = generateEncryptPassword(user, password); if (user.getPassword().equals(encryptPassword)) { return user; } return null; }
@Override public void create(final User user) { user.setPassword(generateEncryptPassword(user)); mongoDBUtil.execute(userCollectionName, collection -> { collection.insertOne(new Document().append(usernameKey, user.getUsername()) .append(passwordKey, user.getPassword())); return null; }); }
@Override public void create(final User user) { user.setPassword(generateEncryptPassword(user)); mongoDBUtil.execute(userCollectionName, collection -> { collection.insertOne(new Document().append(usernameKey, user.getUsername()) .append(passwordKey, user.getPassword())); return null; }); }
@Override public UserManager addUser(final String username, final String password) { final User user = new User(username, password); user.setPassword(generateEncryptPassword(user)); mongoDBUtil.execute(userCollectionName, collection -> { collection.insertOne(new Document().append(usernameKey, user.getUsername()) .append(passwordKey, user.getPassword())); return null; }); return this; }
@Override public UserManager addUser(final String username, final String password) { final User user = new User(username, password); user.setPassword(generateEncryptPassword(user)); mongoDBUtil.execute(userCollectionName, collection -> { collection.insertOne(new Document().append(usernameKey, user.getUsername()) .append(passwordKey, user.getPassword())); return null; }); return this; }