Fixes to ChannelIdDatabasePlugin as pointed out by @ProgVal

- Quote: factorize tests
- Make supybot.databases.plugins channel specific
This commit is contained in:
James Lu 2015-06-28 11:51:58 -07:00
parent 1fe663ddb2
commit 8e51209c3f
3 changed files with 9 additions and 11 deletions

View File

@ -43,13 +43,9 @@ class QuoteTestCase(ChannelPluginTestCase):
def testUnauthenticatedAdd(self):
# This should fail because the user isn't registered
self.assertError('quote add hello world!')
original_value = conf.supybot.databases.plugins.requireRegistration()
conf.supybot.databases.plugins.requireRegistration.setValue(False)
try:
with conf.supybot.databases.plugins.requireRegistration.context(False):
self.assertNotError('quote add hello world!')
self.assertRegexp('quote get 1', 'hello')
self.assertNotError('quote remove 1')
finally:
conf.supybot.databases.plugins.requireRegistration.setValue(original_value)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -330,6 +330,8 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
(self.name(), id, channel))
def checkChangeAllowed(self, irc, msg, channel, user, record):
# Checks and returns True if either the user ID (integer)
# or the hostmask of the caller match.
if (hasattr(user, 'id') and user.id == record.by) or user == record.by:
return True
cap = ircdb.makeChannelCapability(channel, 'op')
@ -341,12 +343,12 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
"""This should irc.error or raise an exception if text is invalid."""
pass
def getUserId(self, irc, prefix):
def getUserId(self, irc, prefix, channel=None):
try:
user = ircdb.users.getUser(prefix)
return user.id
except KeyError:
if conf.supybot.databases.plugins.requireRegistration():
if conf.get(conf.supybot.databases.plugins.requireRegistration, channel):
irc.errorNotRegistered(Raise=True)
return
@ -357,7 +359,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
user = self.getUserId(irc, msg.prefix) or msg.prefix
user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
at = time.time()
self.addValidator(irc, text)
if text is not None:
@ -372,7 +374,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
user = self.getUserId(irc, msg.prefix) or msg.prefix
user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
try:
record = self.db.get(channel, id)
self.checkChangeAllowed(irc, msg, channel, user, record)
@ -448,7 +450,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
<regexp>. <channel> is only necessary if the message isn't sent in the
channel itself.
"""
user = self.getUserId(irc, msg.prefix) or msg.prefix
user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
try:
record = self.db.get(channel, id)
self.checkChangeAllowed(irc, msg, channel, user, record)

View File

@ -932,7 +932,7 @@ class ChannelSpecific(registry.Boolean):
registerGroup(supybot.databases, 'plugins')
registerGlobalValue(supybot.databases.plugins, 'requireRegistration',
registerChannelValue(supybot.databases.plugins, 'requireRegistration',
registry.Boolean(True, _("""Determines whether the bot will require user
registration to use 'add' commands in database-based Supybot
plugins.""")))