Changeset - 7f76ef28bd61
[Not reviewed]
0 3 0
HanzZ - 13 years ago 2012-12-06 14:57:38
hanzz.k@gmail.com
added [registration] notify_jid option to send notification about user registration/unregistration
3 files changed with 85 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/config.cpp
Show inline comments
 
@@ -111,12 +111,13 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
 
		("registration.username_label", value<std::string>()->default_value("Legacy network username:"), "Label for username field")
 
		("registration.username_mask", value<std::string>()->default_value(""), "Username mask")
 
		("registration.allowed_usernames", value<std::string>()->default_value(""), "Allowed usernames")
 
		("registration.auto_register", value<bool>()->default_value(false), "Register new user automatically when the presence arrives.")
 
		("registration.encoding", value<std::string>()->default_value("utf8"), "Default encoding in registration form")
 
		("registration.require_local_account", value<bool>()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.")
 
		("registration.notify_jid", value<std::vector<std::string> >()->multitoken(), "Send message to this JID if user registers/unregisters")
 
		("registration.local_username_label", value<std::string>()->default_value("Local username:"), "Label for local usernme field")
 
		("registration.local_account_server", value<std::string>()->default_value("localhost"), "The server on which the local accounts will be checked for validity")
 
		("registration.local_account_server_timeout", value<int>()->default_value(10000), "Timeout when checking local user on local_account_server (msecs)")
 
		("gateway_responder.prompt", value<std::string>()->default_value("Contact ID"), "Value of <prompt> </promt> field")
 
		("gateway_responder.label", value<std::string>()->default_value("Enter legacy network contact ID."), "Label for add contact ID field")
 
		("database.type", value<std::string>()->default_value("none"), "Database type.")
src/tests/userregistration.cpp
Show inline comments
 
@@ -25,12 +25,14 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
 
	CPPUNIT_TEST_SUITE(UserRegistrationTest);
 
	CPPUNIT_TEST(getForm);
 
	CPPUNIT_TEST(getFormRegistered);
 
	CPPUNIT_TEST(registerUser);
 
	CPPUNIT_TEST(unregisterUser);
 
	CPPUNIT_TEST(unregisterEmptyPayload);
 
	CPPUNIT_TEST(registerUserNotify);
 
	CPPUNIT_TEST(unregisterUserNotify);
 
	CPPUNIT_TEST(changePassword);
 
	CPPUNIT_TEST(registerUserEmpty);
 
	CPPUNIT_TEST(registerUserNoNeedPassword);
 
	CPPUNIT_TEST_SUITE_END();
 

	
 
	public:
 
@@ -136,12 +138,78 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
 
			CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
 

	
 
			UserInfo user;
 
			CPPUNIT_ASSERT_EQUAL(false, storage->getUser("user@localhost", user));
 
		}
 

	
 
		void registerUserNotify() {
 
			std::istringstream ifs("service.server_mode = 1\nregistration.notify_jid=user@localhost\nservice.jid=localhost\nservice.more_resources=1\n");
 
			cfg->load(ifs);
 

	
 
			Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload();
 
			reg->setUsername("legacyname");
 
			reg->setPassword("password");
 
			boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(reg));
 
			iq->setFrom("user@localhost");
 
			injectIQ(iq);
 
			loop->processEvents();
 

	
 
			CPPUNIT_ASSERT_EQUAL(3, (int) received.size());
 

	
 
			CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
 
			CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
 

	
 
			CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
 
			CPPUNIT_ASSERT_EQUAL(std::string("registered: user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getBody());
 

	
 
			CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[2])));
 
			CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[2]))->getType());
 

	
 
			UserInfo user;
 
			CPPUNIT_ASSERT_EQUAL(true, storage->getUser("user@localhost", user));
 

	
 
			CPPUNIT_ASSERT_EQUAL(std::string("legacyname"), user.uin);
 
			CPPUNIT_ASSERT_EQUAL(std::string("password"), user.password);
 
		}
 

	
 
		void unregisterUserNotify() {
 
			registerUserNotify();
 
			received.clear();
 

	
 
			Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload();
 
			reg->setRemove(true);
 
			boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(reg));
 
			iq->setFrom("user@localhost");
 
			injectIQ(iq);
 
			loop->processEvents();
 

	
 
			CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
 

	
 
			CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
 

	
 
			CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[1])));
 
			CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[1]))->getType());
 

	
 
			iq = Swift::IQ::createResult(Swift::JID("localhost"), getStanza(received[0])->getTo(), getStanza(received[0])->getID(), boost::shared_ptr<Swift::Payload>(new Swift::RosterPayload()));
 
			received.clear();
 
			injectIQ(iq);
 
			loop->processEvents();
 

	
 
			CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
 
			CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
 
			CPPUNIT_ASSERT_EQUAL(Swift::IQ::Set, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
 
			CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
 

	
 
			CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
 
			CPPUNIT_ASSERT_EQUAL(std::string("unregistered: user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getBody());
 

	
 

	
 
			UserInfo user;
 
			CPPUNIT_ASSERT_EQUAL(false, storage->getUser("user@localhost", user));
 
		}
 

	
 
		void changePassword() {
 
			registerUser();
 
			received.clear();
 

	
 
			Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload();
 
			reg->setUsername("legacyname");
src/userregistration.cpp
Show inline comments
 
@@ -60,12 +60,20 @@ bool UserRegistration::registerUser(const UserInfo &row) {
 
	response->setFrom(m_component->getJID());
 
	response->setTo(Swift::JID(row.jid));
 
	response->setType(Swift::Presence::Subscribe);
 
	m_component->getStanzaChannel()->sendPresence(response);
 

	
 
	onUserRegistered(row);
 

	
 
	BOOST_FOREACH(const std::string &notify_jid, CONFIG_VECTOR(m_component->getConfig(),"registration.notify_jid")) {
 
		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
		msg->setBody(std::string("registered: ") + row.jid);
 
		msg->setTo(notify_jid);
 
		msg->setFrom(m_component->getJID());
 
		m_component->getStanzaChannel()->sendMessage(msg);
 
	}
 
	return true;
 
}
 

	
 
bool UserRegistration::unregisterUser(const std::string &barejid) {
 
	UserInfo userInfo;
 
	bool registered = m_storageBackend->getUser(barejid, userInfo);
 
@@ -162,12 +170,20 @@ void UserRegistration::handleUnregisterRemoteRosterResponse(boost::shared_ptr<Sw
 
		item.setSubscription(Swift::RosterItemPayload::Remove);
 
		payload->addItem(item);
 

	
 
		Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, barejid, m_component->getIQRouter());
 
		request->send();
 
	}
 

	
 
	BOOST_FOREACH(const std::string &notify_jid, CONFIG_VECTOR(m_component->getConfig(),"registration.notify_jid")) {
 
		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
		msg->setBody(std::string("unregistered: ") + barejid);
 
		msg->setTo(notify_jid);
 
		msg->setFrom(m_component->getJID());
 
		m_component->getStanzaChannel()->sendMessage(msg);
 
	}
 
}
 

	
 
bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload) {
 
	// TODO: backend should say itself if registration is needed or not...
 
	if (CONFIG_STRING(m_config, "service.protocol") == "irc") {
 
		sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify);
0 comments (0 inline, 0 general)