summaryrefslogtreecommitdiff
path: root/redis/commands/core.py
diff options
context:
space:
mode:
authordvora-h <67596500+dvora-h@users.noreply.github.com>2022-05-03 14:04:14 +0300
committerGitHub <noreply@github.com>2022-05-03 14:04:14 +0300
commit5c99e27459a047cd1334e1b87fb0623ac2c881db (patch)
tree67434cb616fdcfa0dfaf852f42fba0934cf3a0fe /redis/commands/core.py
parentfa7b3f6213625f248764b134ed2c82fcdba95d62 (diff)
downloadredis-py-5c99e27459a047cd1334e1b87fb0623ac2c881db.tar.gz
ACL SETUSER - add selectors and key based permissions (#2161)
* acl setuser * async tests Co-authored-by: Chayim <chayim@users.noreply.github.com>
Diffstat (limited to 'redis/commands/core.py')
-rw-r--r--redis/commands/core.py32
1 files changed, 28 insertions, 4 deletions
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)