added [registration] notify_jid option to send notification about user registration/unregistration

This commit is contained in:
HanzZ 2012-12-06 14:57:38 +01:00
parent 468f8b2a01
commit 7f76ef28bd
3 changed files with 85 additions and 0 deletions

View file

@ -114,6 +114,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
("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)")

View file

@ -28,6 +28,8 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
CPPUNIT_TEST(registerUser);
CPPUNIT_TEST(unregisterUser);
CPPUNIT_TEST(unregisterEmptyPayload);
CPPUNIT_TEST(registerUserNotify);
CPPUNIT_TEST(unregisterUserNotify);
CPPUNIT_TEST(changePassword);
CPPUNIT_TEST(registerUserEmpty);
CPPUNIT_TEST(registerUserNoNeedPassword);
@ -139,6 +141,72 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
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();

View file

@ -63,6 +63,14 @@ bool UserRegistration::registerUser(const UserInfo &row) {
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;
}
@ -165,6 +173,14 @@ void UserRegistration::handleUnregisterRemoteRosterResponse(boost::shared_ptr<Sw
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) {