Skip to content

Commit 7571eb0

Browse files
committed
Implement assigning perms to unblinded users
If we know the blinded user already we translate immediately; if we don't then we apply the permission to the unblinded id and insert into the needs_blinding table to be handled as soon as we see the blinded user.
1 parent 3980348 commit 7571eb0

File tree

5 files changed

+313
-70
lines changed

5 files changed

+313
-70
lines changed

sogs/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def print_room(room: Room):
265265

266266
if args.rooms == ['+']:
267267
for sid in args.add_moderators:
268-
u = User(session_id=sid)
268+
u = User(session_id=sid, try_blinding=True)
269269
u.set_moderator(admin=args.admin, visible=args.visible, added_by=sysadmin)
270270
print(
271271
"Added {} as {} global {}".format(
@@ -284,7 +284,7 @@ def print_room(room: Room):
284284
print(f"No such room: '{nsr.token}'", file=sys.stderr)
285285

286286
for sid in args.add_moderators:
287-
u = User(session_id=sid)
287+
u = User(session_id=sid, try_blinding=True)
288288
for room in rooms:
289289
room.set_moderator(u, admin=args.admin, visible=not args.hidden, added_by=sysadmin)
290290
print(
@@ -315,7 +315,7 @@ def print_room(room: Room):
315315

316316
if args.rooms == ['+']:
317317
for sid in args.delete_moderators:
318-
u = User(session_id=sid)
318+
u = User(session_id=sid, try_blinding=True)
319319
was_admin = u.global_admin
320320
if not u.global_admin and not u.global_moderator:
321321
print(f"{u.session_id} was not a global moderator")
@@ -332,7 +332,7 @@ def print_room(room: Room):
332332
print(f"No such room: '{nsr.token}'", file=sys.stderr)
333333

334334
for sid in args.delete_moderators:
335-
u = User(session_id=sid)
335+
u = User(session_id=sid, try_blinding=True)
336336
for room in rooms:
337337
room.remove_moderator(u, removed_by=sysadmin)
338338
print(f"Removed {u.session_id} as moderator/admin of {room.name} ({room.token})")

sogs/model/room.py

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,21 +1045,33 @@ def set_moderator(self, user: User, *, added_by: User, admin=False, visible=True
10451045
)
10461046
raise BadPermission()
10471047

1048-
query(
1049-
f"""
1050-
INSERT INTO user_permission_overrides
1051-
(room, "user", moderator, {'admin,' if admin is not None else ''} visible_mod)
1052-
VALUES (:r, :u, TRUE, {':admin,' if admin is not None else ''} :visible)
1053-
ON CONFLICT (room, "user") DO UPDATE SET
1054-
moderator = excluded.moderator,
1055-
{'admin = excluded.admin,' if admin is not None else ''}
1056-
visible_mod = excluded.visible_mod
1057-
""",
1058-
r=self.id,
1059-
u=user.id,
1060-
admin=admin,
1061-
visible=visible,
1062-
)
1048+
with db.transaction():
1049+
need_blinding = False
1050+
if config.REQUIRE_BLIND_KEYS:
1051+
blinded = user.find_blinded()
1052+
if blinded is not None:
1053+
user = blinded
1054+
else:
1055+
need_blinding = True
1056+
1057+
query(
1058+
f"""
1059+
INSERT INTO user_permission_overrides
1060+
(room, "user", moderator, {'admin,' if admin is not None else ''} visible_mod)
1061+
VALUES (:r, :u, TRUE, {':admin,' if admin is not None else ''} :visible)
1062+
ON CONFLICT (room, "user") DO UPDATE SET
1063+
moderator = excluded.moderator,
1064+
{'admin = excluded.admin,' if admin is not None else ''}
1065+
visible_mod = excluded.visible_mod
1066+
""",
1067+
r=self.id,
1068+
u=user.id,
1069+
admin=admin,
1070+
visible=visible,
1071+
)
1072+
1073+
if need_blinding:
1074+
user.record_needs_blinding()
10631075

10641076
if user.id in self._perm_cache:
10651077
del self._perm_cache[user.id]
@@ -1107,23 +1119,31 @@ def ban_user(self, to_ban: User, *, mod: User, timeout: Optional[float] = None):
11071119
primarily provided for testing.
11081120
"""
11091121

1110-
fail = None
1111-
if not self.check_moderator(mod):
1112-
fail = "user is not a moderator"
1113-
elif to_ban.id == mod.id:
1114-
fail = "self-ban not permitted"
1115-
elif to_ban.global_moderator:
1116-
fail = "global mods/admins cannot be banned"
1117-
elif self.check_moderator(to_ban) and not self.check_admin(mod):
1118-
fail = "only admins can ban room mods/admins"
1119-
1120-
if fail is not None:
1121-
app.logger.warning(f"Error banning {to_ban} from {self} by {mod}: {fail}")
1122-
raise BadPermission()
1122+
with db.transaction():
1123+
need_blinding = False
1124+
if config.REQUIRE_BLIND_KEYS:
1125+
blinded = to_ban.find_blinded()
1126+
if blinded is not None:
1127+
to_ban = blinded
1128+
else:
1129+
need_blinding = True
1130+
1131+
fail = None
1132+
if not self.check_moderator(mod):
1133+
fail = "user is not a moderator"
1134+
elif to_ban.id == mod.id:
1135+
fail = "self-ban not permitted"
1136+
elif to_ban.global_moderator:
1137+
fail = "global mods/admins cannot be banned"
1138+
elif self.check_moderator(to_ban) and not self.check_admin(mod):
1139+
fail = "only admins can ban room mods/admins"
1140+
1141+
if fail is not None:
1142+
app.logger.warning(f"Error banning {to_ban} from {self} by {mod}: {fail}")
1143+
raise BadPermission()
11231144

1124-
# TODO: log the banning action for auditing
1145+
# TODO: log the banning action for auditing
11251146

1126-
with db.transaction():
11271147
query(
11281148
"""
11291149
INSERT INTO user_permission_overrides (room, "user", banned, moderator, admin)
@@ -1152,6 +1172,9 @@ def ban_user(self, to_ban: User, *, mod: User, timeout: Optional[float] = None):
11521172
at=time.time() + timeout,
11531173
)
11541174

1175+
if need_blinding:
1176+
to_ban.record_needs_blinding()
1177+
11551178
if to_ban.id in self._perm_cache:
11561179
del self._perm_cache[to_ban.id]
11571180

@@ -1234,6 +1257,14 @@ def set_permissions(self, user: User, *, mod: User, **perms):
12341257
raise BadPermission()
12351258

12361259
with db.transaction():
1260+
need_blinding = False
1261+
if config.REQUIRE_BLIND_KEYS:
1262+
blinded = user.find_blinded()
1263+
if blinded is not None:
1264+
user = blinded
1265+
else:
1266+
need_blinding = True
1267+
12371268
set_perms = perms.keys()
12381269
query(
12391270
f"""
@@ -1250,6 +1281,9 @@ def set_permissions(self, user: User, *, mod: User, **perms):
12501281
upload=perms.get('upload'),
12511282
)
12521283

1284+
if need_blinding:
1285+
user.record_needs_blinding()
1286+
12531287
if user.id in self._perm_cache:
12541288
del self._perm_cache[user.id]
12551289

0 commit comments

Comments
 (0)