本文整理汇总了Python中system.command_manager.CommandManager.set_permissions_handler方法的典型用法代码示例。如果您正苦于以下问题:Python CommandManager.set_permissions_handler方法的具体用法?Python CommandManager.set_permissions_handler怎么用?Python CommandManager.set_permissions_handler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类system.command_manager.CommandManager
的用法示例。
在下文中一共展示了CommandManager.set_permissions_handler方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AuthPlugin
# 需要导入模块: from system.command_manager import CommandManager [as 别名]
# 或者: from system.command_manager.CommandManager import set_permissions_handler [as 别名]
class AuthPlugin(plugin.PluginObject):
"""
Auth plugin. In charge of logins and permissions.
"""
config = None
passwords = None
permissions = None
blacklist = None
commands = None
events = None
storage = None
auth_h = None
perms_h = None
def setup(self):
"""
Called when the plugin is loaded. Performs initial setup.
"""
reload(auth_handler)
reload(permissions_handler)
self.logger.trace(_("Entered setup method."))
self.storage = StorageManager()
try:
self.config = self.storage.get_file(self, "config", YAML,
"plugins/auth.yml")
except Exception:
self.logger.exception(_("Error loading configuration!"))
self.logger.error(_("Disabling.."))
self._disable_self()
return
if not self.config.exists:
self.logger.error(_("Unable to find config/plugins/auth.yml"))
self.logger.error(_("Disabling.."))
self._disable_self()
return
self.commands = CommandManager()
self.events = EventManager()
if self.config["use-permissions"]:
try:
self.permissions = self.storage.get_file(self, "data", YAML,
"plugins/auth/" # PEP
"permissions.yml")
except Exception:
self.logger.exception(_("Unable to load permissions. They "
"will be unavailable!"))
else:
self.perms_h = permissions_handler.permissionsHandler(
self, self.permissions)
result = self.commands.set_permissions_handler(self.perms_h)
if not result:
self.logger.warn(_("Unable to set permissions handler!"))
if self.config["use-auth"]:
try:
self.passwords = self.storage.get_file(self, "data", YAML,
"plugins/auth/" # PEP!
"passwords.yml")
self.blacklist = self.storage.get_file(self, "data", YAML,
"plugins/auth/" # PEP!
"blacklist.yml")
except Exception:
self.logger.exception(_("Unable to load user accounts. They "
"will be unavailable!"))
else:
self.auth_h = auth_handler.authHandler(self, self.passwords,
self.blacklist)
result = self.commands.set_auth_handler(self.auth_h)
if not result:
self.logger.warn(_("Unable to set auth handler!"))
self.logger.debug(_("Registering commands."))
self.commands.register_command("login", self.login_command,
self, "auth.login", default=True)
self.commands.register_command("logout", self.logout_command,
self, "auth.login", default=True)
self.commands.register_command("register", self.register_command,
self, "auth.register", default=True)
self.commands.register_command("passwd", self.passwd_command, self,
"auth.passwd", default=True)
self.events.add_callback("PreCommand", self, self.pre_command, 10000)
def pre_command(self, event=PreCommand):
"""
Pre-command hook to remove passwords from the log output.
"""
self.logger.trace(_("Command: %s") % event.command)
if event.command.lower() in ["login", "register"]:
if len(event.args) >= 2:
split_ = event.printable.split("%s " % event.command)
second_split = split_[1].split()
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from system.command_manager import CommandManager [as 别名]
# 或者: from system.command_manager.CommandManager import set_permissions_handler [as 别名]
#.........这里部分代码省略.........
# Reset mock
self.plugin.handler.reset_mock()
r = self.manager.run_command("test5", caller, source, protocol, "")
nosetools.assert_equals(r, (CommandState.Success, None))
self.plugin.handler.assert_called_with(protocol, caller, source,
"test4", "", [])
# Reset mock
self.plugin.handler.reset_mock()
self.manager.register_command("test5", self.plugin.handler,
self.plugin, default=True)
r = self.manager.run_command("test5", caller, source, protocol, "")
nosetools.assert_equals(r, (CommandState.Success, None))
self.plugin.handler.assert_called_with(protocol, caller, source,
"test5", "", [])
@nose.with_setup(teardown=teardown)
def test_run_commands_auth(self):
"""CMNDS | Test running commands directly | Auth"""
caller = Mock(name="caller")
source = Mock(name="source")
protocol = Mock(name="protocol")
auth = Mock(name="auth")
perms = Mock(name="perms")
self.manager.set_auth_handler(auth)
self.manager.set_permissions_handler(perms)
### COMMAND WITH PERMISSION ###
auth.authorized.return_value = True
perms.check.return_value = True
self.manager.register_command("test5", self.plugin.handler,
self.plugin, "test.test",
aliases=["test6"])
r = self.manager.run_command("test5", caller, source, protocol, "")
nosetools.assert_equals(r, (CommandState.Success, None))
nosetools.assert_equals(self.plugin.handler.call_count, 1)
auth.authorized.reset_mock()
perms.check.reset_mock()
self.plugin.handler.reset_mock()
### ALIAS WITH PERMISSION ###
r = self.manager.run_command("test6", caller, source, protocol, "")
nosetools.assert_equals(r, (CommandState.Success, None))
nosetools.assert_equals(self.plugin.handler.call_count, 1)
auth.authorized.reset_mock()
perms.check.reset_mock()
auth.authorized.return_value = True
perms.check.return_value = False
self.plugin.handler.reset_mock()