Skip to content

Commit c6ab8c2

Browse files
Jeffjagerman
authored andcommitted
* add future permissions
* make permissions and future permissions an attribute * add unit tests
1 parent 9303607 commit c6ab8c2

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

sogs/model/room.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,12 @@ def url(self):
15031503
"""
15041504
return utils.server_url(self.token)
15051505

1506-
def export_permissions(self, mod):
1506+
@property
1507+
def permissions(self):
15071508
"""
15081509
export room permissions in full,
15091510
returns a dict of session_id -> permissions dict (a dict of permission type to bool)
15101511
"""
1511-
if not self.check_moderator(mod):
1512-
app.logger.warning("unable to get room permissions for user")
1513-
raise BadPermission()
1514-
15151512
ret = dict()
15161513
for row in query(
15171514
"""SELECT session_id, upo.* FROM user_permission_overrides upo
@@ -1528,6 +1525,33 @@ def export_permissions(self, mod):
15281525
ret[row['session_id']] = data
15291526
return ret
15301527

1528+
@property
1529+
def future_permissions(self):
1530+
"""
1531+
returns a list of future permission changes in this room
1532+
"""
1533+
ret = list()
1534+
for row in query(
1535+
"""SELECT session_id, futures.* FROM (
1536+
SELECT "user", at, read, write, upload, null AS banned
1537+
FROM user_permission_futures WHERE room = :r
1538+
UNION ALL
1539+
SELECT "user", at, null AS read, null AS write, null AS banned, banned
1540+
FROM user_ban_futures WHERE room = :r
1541+
) futures JOIN users ON futures."user" = users.id""",
1542+
r=self.id,
1543+
):
1544+
data = dict()
1545+
for k in row.keys():
1546+
if k in ('room', 'user'):
1547+
continue
1548+
if k in ('at', 'session_id'):
1549+
data[k] = row[k]
1550+
else:
1551+
data[k] = bool(row[k])
1552+
ret.append(data)
1553+
return ret
1554+
15311555

15321556
def get_rooms():
15331557
"""Get a list of all rooms; does not check permissions."""

sogs/routes/rooms.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ def update_room(room):
278278
return jsonify({})
279279

280280

281+
def compact_permissions(perms):
282+
""" take a dict of user permssions and convert them into a compact form """
283+
compact = str()
284+
keys = list(perms.keys())
285+
keys.sort()
286+
for k in keys:
287+
if not perms[k]:
288+
compact += '-'
289+
if k == 'admin':
290+
compact += 'A'
291+
else:
292+
compact += k[0]
293+
compact += ','
294+
if keys:
295+
compact = compact[:-1]
296+
return compact
297+
298+
281299
@rooms.get("/room/<Room:room>/permInfo")
282300
@auth.mod_required
283301
def get_permission_info(room):
@@ -286,14 +304,33 @@ def get_permission_info(room):
286304
287305
# Query Parameters
288306
289-
TODO: implement me
307+
- `compact` — Set to 1 if we should we send each permission as a "compacted" string.
290308
291309
# Return Value
292310
293-
dict of session_id to current permission info
311+
dict of session_id to current permissions,
312+
if compact is set to 1 this will be a string,
313+
otherwise it will be a dict containing the name of the permission mapped to a boolean value.
314+
"""
315+
transform = lambda x: x # noqa: E731
316+
if request.args.get("compact", type=int, default=0):
317+
transform = compact_permissions
318+
return jsonify(transform(room.permissions))
319+
294320

321+
@rooms.get("/room/<Room:room>/futurePermInfo")
322+
@auth.mod_required
323+
def get_future_permission_info(room):
295324
"""
296-
return jsonify(room.export_permissions(g.user))
325+
Fetches permission changes scheduled in the future.
326+
327+
# Return Value
328+
329+
list of all future permission changes scheduled
330+
331+
"""
332+
333+
return jsonify(room.future_permissions)
297334

298335

299336
@rooms.get("/room/<Room:room>/pollInfo/<int:info_updated>")

sogs/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,21 @@ def add_session_message_padding(data: Union[bytes, memoryview], length):
182182
data = bytes(data)
183183
data += b'\x80' + b'\x00' * (length - len(data) - 1)
184184
return data
185+
186+
187+
def compact_permissions(perms):
188+
""" take a dict of user permssions and convert them into a compact form """
189+
compact = str()
190+
keys = list(perms.keys())
191+
keys.sort()
192+
for k in keys:
193+
if not perms[k]:
194+
compact += '-'
195+
if k == 'admin':
196+
compact += 'A'
197+
else:
198+
compact += k[0]
199+
compact += ','
200+
if keys:
201+
compact = compact[:-1]
202+
return compact

tests/test_room_routes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,3 +1160,14 @@ def test_get_room_perms_as_admin(client, room, admin):
11601160
assert admin.session_id in r.json
11611161
perm_info = r.json[admin.session_id]
11621162
assert perm_info['admin'] is True
1163+
1164+
1165+
def test_get_room_future_perms(client, room, mod):
1166+
r = sogs_get(client, f'/room/{room.token}/futurePermInfo', mod)
1167+
assert r.status_code == 200
1168+
assert r.json == []
1169+
1170+
1171+
def test_get_room_future_perms_not_allowed(client, room, user):
1172+
r = sogs_get(client, f'/room/{room.token}/futurePermInfo', user)
1173+
assert r.status_code == 403

0 commit comments

Comments
 (0)