Admin capability add/remove refactored, and tests changed accordingly.

This commit is contained in:
Ali Afshar 2005-03-25 13:41:17 +00:00
parent 71830d0c84
commit 890d92df0e
2 changed files with 66 additions and 54 deletions

View File

@ -237,57 +237,59 @@ class Admin(callbacks.Plugin):
irc.replySuccess()
part = wrap(part, [optional('validChannel'), additional('text')])
def addcapability(self, irc, msg, args, user, capability):
"""<name|hostmask> <capability>
class capability(callbacks.Commands):
Gives the user specified by <name> (or the user to whom <hostmask>
currently maps) the specified capability <capability>
"""
# Ok, the concepts that are important with capabilities:
#
### 1) No user should be able to elevate his privilege to owner.
### 2) Admin users are *not* superior to #channel.ops, and don't
### have God-like powers over channels.
### 3) We assume that Admin users are two things: non-malicious and
### and greedy for power. So they'll try to elevate their privilege
### to owner, but they won't try to crash the bot for no reason.
def add(self, irc, msg, args, user, capability):
"""<name|hostmask> <capability>
# Thus, the owner capability can't be given in the bot. Admin users
# can only give out capabilities they have themselves (which will
# depend on supybot.capabilities and its child default) but generally
# means they can't mess with channel capabilities.
if ircutils.strEqual(capability, 'owner'):
irc.error('The "owner" capability can\'t be added in the bot. '
'Use the supybot-adduser program (or edit the '
'users.conf file yourself) to add an owner capability.')
return
if ircdb.isAntiCapability(capability) or \
ircdb.checkCapability(msg.prefix, capability):
user.addCapability(capability)
ircdb.users.setUser(user)
irc.replySuccess()
else:
irc.error('You can\'t add capabilities you don\'t have.')
addcapability = wrap(addcapability, ['otherUser', 'lowered'])
Gives the user specified by <name> (or the user to whom <hostmask>
currently maps) the specified capability <capability>
"""
# Ok, the concepts that are important with capabilities:
#
### 1) No user should be able to elevate his privilege to owner.
### 2) Admin users are *not* superior to #channel.ops, and don't
### have God-like powers over channels.
### 3) We assume that Admin users are two things: non-malicious and
### and greedy for power. So they'll try to elevate their privilege
### to owner, but they won't try to crash the bot for no reason.
def removecapability(self, irc, msg, args, user, capability):
"""<name|hostmask> <capability>
Takes from the user specified by <name> (or the user to whom
<hostmask> currently maps) the specified capability <capability>
"""
if ircdb.checkCapability(msg.prefix, capability) or \
ircdb.isAntiCapability(capability):
try:
user.removeCapability(capability)
# Thus, the owner capability can't be given in the bot. Admin users
# can only give out capabilities they have themselves (which will
# depend on supybot.capabilities and its child default) but generally
# means they can't mess with channel capabilities.
if ircutils.strEqual(capability, 'owner'):
irc.error('The "owner" capability can\'t be added in the bot. '
'Use the supybot-adduser program (or edit the '
'users.conf file yourself) to add an owner capability.')
return
if ircdb.isAntiCapability(capability) or \
ircdb.checkCapability(msg.prefix, capability):
user.addCapability(capability)
ircdb.users.setUser(user)
irc.replySuccess()
except KeyError:
irc.error('That user doesn\'t have that capability.')
else:
s = 'You can\'t remove capabilities you don\'t have.'
irc.error(s)
removecapability = wrap(removecapability, ['otherUser','lowered'])
else:
irc.error('You can\'t add capabilities you don\'t have.')
add = wrap(add, ['otherUser', 'lowered'])
def remove(self, irc, msg, args, user, capability):
"""<name|hostmask> <capability>
Takes from the user specified by <name> (or the user to whom
<hostmask> currently maps) the specified capability <capability>
"""
if ircdb.checkCapability(msg.prefix, capability) or \
ircdb.isAntiCapability(capability):
try:
user.removeCapability(capability)
ircdb.users.setUser(user)
irc.replySuccess()
except KeyError:
irc.error('That user doesn\'t have that capability.')
else:
s = 'You can\'t remove capabilities you don\'t have.'
irc.error(s)
remove = wrap(remove, ['otherUser','lowered'])
def ignore(self, irc, msg, args, hostmask, expires):
"""<hostmask|nick> [<expires>]

View File

@ -61,16 +61,27 @@ class AdminTestCase(PluginTestCase):
self.assertNotError('admin ignore foo!bar@baz')
self.assertNotError('admin ignores')
def testAddcapability(self):
self.assertError('addcapability sdlkfj foo')
def testCapabilityAdd(self):
self.assertError('capability add foo bar')
u = ircdb.users.newUser()
u.name = 'foo'
ircdb.users.setUser(u)
self.assertError('removecapability foo bar')
self.assertNotRegexp('removecapability foo bar', 'find')
self.assertNotError('capability add foo bar')
self.assertError('addcapability foo baz')
self.assert_('bar' in u.capabilities)
ircdb.users.delUser(u.id)
def testRemoveCapability(self):
self.assertError('removecapability alsdfkjasd foo')
def testCapabilityRemove(self):
self.assertError('capability remove foo bar')
u = ircdb.users.newUser()
u.name = 'foo'
ircdb.users.setUser(u)
self.assertNotError('capability add foo bar')
self.assert_('bar' in u.capabilities)
self.assertError('removecapability foo bar')
self.assertNotError('capability remove foo bar')
self.assert_(not 'bar' in u.capabilities)
ircdb.users.delUser(u.id)
def testJoin(self):
m = self.getMsg('join #foo')
@ -110,8 +121,7 @@ class AdminTestCase(PluginTestCase):
conf.supybot.nick.setValue(original)
def testAddCapabilityOwner(self):
self.assertError('admin addcapability %s owner' % self.nick)
self.assertError('admin capability add %s owner' % self.nick)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: