From 5c99e27459a047cd1334e1b87fb0623ac2c881db Mon Sep 17 00:00:00 2001 From: dvora-h <67596500+dvora-h@users.noreply.github.com> Date: Tue, 3 May 2022 14:04:14 +0300 Subject: ACL SETUSER - add selectors and key based permissions (#2161) * acl setuser * async tests Co-authored-by: Chayim --- redis/commands/core.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'redis/commands/core.py') diff --git a/redis/commands/core.py b/redis/commands/core.py index 8bbcda3..6526ef1 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -186,9 +186,11 @@ class ACLCommands(CommandsProtocol): nopass: bool = False, passwords: Union[str, Iterable[str], None] = None, hashed_passwords: Union[str, Iterable[str], None] = None, - categories: Union[Iterable[str], None] = None, - commands: Union[Iterable[str], None] = None, - keys: Union[Iterable[KeyT], None] = None, + categories: Optional[Iterable[str]] = None, + commands: Optional[Iterable[str]] = None, + keys: Optional[Iterable[KeyT]] = None, + channels: Optional[Iterable[ChannelT]] = None, + selectors: Optional[Iterable[Tuple[str, KeyT]]] = None, reset: bool = False, reset_keys: bool = False, reset_passwords: bool = False, @@ -342,7 +344,29 @@ class ACLCommands(CommandsProtocol): if keys: for key in keys: key = encoder.encode(key) - pieces.append(b"~%s" % key) + if not key.startswith(b"%") and not key.startswith(b"~"): + key = b"~%s" % key + pieces.append(key) + + if channels: + for channel in channels: + channel = encoder.encode(channel) + pieces.append(b"&%s" % channel) + + if selectors: + for cmd, key in selectors: + cmd = encoder.encode(cmd) + if not cmd.startswith(b"+") and not cmd.startswith(b"-"): + raise DataError( + f'Command "{encoder.decode(cmd, force=True)}" ' + 'must be prefixed with "+" or "-"' + ) + + key = encoder.encode(key) + if not key.startswith(b"%") and not key.startswith(b"~"): + key = b"~%s" % key + + pieces.append(b"(%s %s)" % (cmd, key)) return self.execute_command("ACL SETUSER", *pieces, **kwargs) -- cgit v1.2.1