From 890d92df0e3a4e5381e6f4fca998d02dc8793bad Mon Sep 17 00:00:00 2001 From: Ali Afshar Date: Fri, 25 Mar 2005 13:41:17 +0000 Subject: [PATCH] Admin capability add/remove refactored, and tests changed accordingly. --- plugins/Admin/plugin.py | 94 +++++++++++++++++++++-------------------- plugins/Admin/test.py | 26 ++++++++---- 2 files changed, 66 insertions(+), 54 deletions(-) diff --git a/plugins/Admin/plugin.py b/plugins/Admin/plugin.py index d220caef0..9adddceea 100644 --- a/plugins/Admin/plugin.py +++ b/plugins/Admin/plugin.py @@ -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): - """ + class capability(callbacks.Commands): - Gives the user specified by (or the user to whom - currently maps) the specified 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): + """ - # 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 (or the user to whom + currently maps) the specified 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): - """ - - Takes from the user specified by (or the user to whom - currently maps) the specified 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): + """ + + Takes from the user specified by (or the user to whom + currently maps) the specified 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): """ [] diff --git a/plugins/Admin/test.py b/plugins/Admin/test.py index 4fe7616c7..5b5243902 100644 --- a/plugins/Admin/test.py +++ b/plugins/Admin/test.py @@ -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: