support full JID ad-hoc command admin access control

darcs-hash:20070913174313-86b55-51b95c05375a7f6e847d631c1d8e68f13950a51c.gz
This commit is contained in:
David Rousselie
2007-09-13 19:43:13 +02:00
parent 6c09c4dc9d
commit e678dff219
2 changed files with 33 additions and 4 deletions

View File

@@ -86,10 +86,11 @@ class CommandManager(object):
def list_commands(self, jid, disco_items, lang_class): def list_commands(self, jid, disco_items, lang_class):
"""Return DiscoItem for each supported commands""" """Return DiscoItem for each supported commands"""
bare_from_jid = unicode(jid.bare())
for command_name in self.commands.keys(): for command_name in self.commands.keys():
must_be_admin = self.commands[command_name] must_be_admin = self.commands[command_name]
if not must_be_admin or \ if not must_be_admin or \
(must_be_admin and unicode(jid) in self.component.get_admins()): (must_be_admin and bare_from_jid in self.component.get_admins()):
command_desc = self.get_command_desc(command_name, command_desc = self.get_command_desc(command_name,
lang_class) lang_class)
DiscoItem(disco_items, DiscoItem(disco_items,
@@ -112,7 +113,7 @@ class CommandManager(object):
must_be_admin = self.commands[command_name] must_be_admin = self.commands[command_name]
if not must_be_admin or \ if not must_be_admin or \
(must_be_admin and (must_be_admin and
unicode(info_query.get_from()) in self.component.get_admins()): unicode(info_query.get_from().bare()) in self.component.get_admins()):
short_command_name = self.get_short_command_name(command_name) short_command_name = self.get_short_command_name(command_name)
action_command_method = "apply_" + action + "_command" action_command_method = "apply_" + action + "_command"
if hasattr(self, action_command_method): if hasattr(self, action_command_method):

View File

@@ -72,7 +72,7 @@ class CommandManager_TestCase(unittest.TestCase):
command.command_manager.commands["command1"] = True command.command_manager.commands["command1"] = True
command.command_manager.commands["command2"] = False command.command_manager.commands["command2"] = False
command.command_manager.component = MockComponent() command.command_manager.component = MockComponent()
disco_items = command.command_manager.list_commands(jid="user@test.com", disco_items = command.command_manager.list_commands(jid=JID("user@test.com"),
disco_items=DiscoItems(), disco_items=DiscoItems(),
lang_class=Lang.en) lang_class=Lang.en)
items = disco_items.get_items() items = disco_items.get_items()
@@ -85,7 +85,22 @@ class CommandManager_TestCase(unittest.TestCase):
command.command_manager.commands["command1"] = True command.command_manager.commands["command1"] = True
command.command_manager.commands["command2"] = False command.command_manager.commands["command2"] = False
command.command_manager.component = MockComponent() command.command_manager.component = MockComponent()
disco_items = command.command_manager.list_commands(jid="admin@test.com", disco_items = command.command_manager.list_commands(jid=JID("admin@test.com"),
disco_items=DiscoItems(),
lang_class=Lang.en)
items = disco_items.get_items()
self.assertEquals(len(items), 2)
self.assertEquals(items[0].get_node(), "command1")
self.assertEquals(items[0].get_name(), "command1")
self.assertEquals(items[1].get_node(), "command2")
self.assertEquals(items[1].get_name(), "command2")
def test_list_commands_as_admin_fulljid(self):
command.command_manager.commands = {}
command.command_manager.commands["command1"] = True
command.command_manager.commands["command2"] = False
command.command_manager.component = MockComponent()
disco_items = command.command_manager.list_commands(jid=JID("admin@test.com/full"),
disco_items=DiscoItems(), disco_items=DiscoItems(),
lang_class=Lang.en) lang_class=Lang.en)
items = disco_items.get_items() items = disco_items.get_items()
@@ -108,6 +123,19 @@ class CommandManager_TestCase(unittest.TestCase):
"execute") "execute")
self.assertEquals(result, []) self.assertEquals(result, [])
def test_apply_admin_command_action_as_admin_fulljid(self):
command.command_manager.commands["command1"] = True
command.command_manager.apply_execute_command = \
lambda iq, command_name: []
command.command_manager.component = MockComponent()
info_query = Iq(stanza_type="set",
from_jid="admin@test.com/full",
to_jid="jcl.test.com")
result = command.command_manager.apply_command_action(info_query,
"command1",
"execute")
self.assertEquals(result, [])
def test_apply_admin_command_action_as_user(self): def test_apply_admin_command_action_as_user(self):
command.command_manager.commands["command1"] = True command.command_manager.commands["command1"] = True
command.command_manager.apply_execute_command = \ command.command_manager.apply_execute_command = \