@@ -46,6 +46,7 @@ class Room:
46
46
info_updates - counter on room metadata that is automatically incremented whenever room
47
47
metadata (name, description, image, etc.) changes for the room.
48
48
default_read - True if default user permissions includes read permission
49
+ default_accessible - True if default user permissions include accessible permission
49
50
default_write - True if default user permissions includes write permission
50
51
default_upload - True if default user permissions includes file upload permission
51
52
"""
@@ -104,8 +105,8 @@ def _refresh(self, *, id=None, token=None, row=None, perms=False):
104
105
'info_updates' ,
105
106
)
106
107
)
107
- self ._default_read , self ._default_write , self ._default_upload = (
108
- bool (row [c ]) for c in ('read' , 'write' , 'upload' )
108
+ self ._default_read , self ._default_accessible , self . _default_write , self ._default_upload = (
109
+ bool (row [c ]) for c in ('read' , 'accessible' , ' write' , 'upload' )
109
110
)
110
111
111
112
if (
@@ -310,6 +311,16 @@ def default_read(self):
310
311
"""Returns True if this room is publicly readable (e.g. by a new user)"""
311
312
return self ._default_read
312
313
314
+ @property
315
+ def default_accessible (self ):
316
+ """
317
+ Returns True if this room has the publicly accessible (e.g. by a new user) permission set.
318
+ Note that the the accessible permission only applies when `read` is false: if a user has
319
+ read permission then they implicitly have accessibility permission even if this field is
320
+ false.
321
+ """
322
+ return self ._default_accessible
323
+
313
324
@property
314
325
def default_write (self ):
315
326
"""Returns True if this room is publicly writable (e.g. by a new user)"""
@@ -329,6 +340,19 @@ def default_read(self, read: bool):
329
340
query ("UPDATE rooms SET read = :read WHERE id = :r" , r = self .id , read = read )
330
341
self ._refresh (perms = True )
331
342
343
+ @default_accessible .setter
344
+ def default_accessible (self , accessible : bool ):
345
+ """Sets the default accessible permission of the room"""
346
+
347
+ if accessible != self ._default_accessible :
348
+ with db .transaction ():
349
+ query (
350
+ "UPDATE rooms SET accessible = :accessible WHERE id = :r" ,
351
+ r = self .id ,
352
+ accessible = accessible ,
353
+ )
354
+ self ._refresh (perms = True )
355
+
332
356
@default_write .setter
333
357
def default_write (self , write : bool ):
334
358
"""Sets the default write permission of the room"""
@@ -367,6 +391,7 @@ def check_permission(
367
391
admin = False ,
368
392
moderator = False ,
369
393
read = False ,
394
+ accessible = False ,
370
395
write = False ,
371
396
upload = False ,
372
397
):
@@ -382,6 +407,9 @@ def check_permission(
382
407
- admin -- if true then the user must have admin access to the room
383
408
- moderator -- if true then the user must have moderator (or admin) access to the room
384
409
- read -- if true then the user must have read access
410
+ - accessible -- if true then the user must have accessible access; note that this permission
411
+ is satisfied by *either* the `accessible` or `read` database flags (that is: read implies
412
+ accessible).
385
413
- write -- if true then the user must have write access
386
414
- upload -- if true then the user must have upload access; this should usually be combined
387
415
with write=True.
@@ -392,9 +420,10 @@ def check_permission(
392
420
"""
393
421
394
422
if user is None :
395
- is_banned , can_read , can_write , can_upload , is_mod , is_admin = (
423
+ is_banned , can_read , can_access , can_write , can_upload , is_mod , is_admin = (
396
424
False ,
397
425
bool (self .default_read ),
426
+ bool (self .default_accessible ),
398
427
bool (self .default_write ),
399
428
bool (self .default_upload ),
400
429
False ,
@@ -404,15 +433,24 @@ def check_permission(
404
433
if user .id not in self ._perm_cache :
405
434
row = query (
406
435
"""
407
- SELECT banned, read, write, upload, moderator, admin FROM user_permissions
436
+ SELECT banned, read, accessible, write, upload, moderator, admin
437
+ FROM user_permissions
408
438
WHERE room = :r AND "user" = :u
409
439
""" ,
410
440
r = self .id ,
411
441
u = user .id ,
412
442
).first ()
413
443
self ._perm_cache [user .id ] = [bool (c ) for c in row ]
414
444
415
- is_banned , can_read , can_write , can_upload , is_mod , is_admin = self ._perm_cache [user .id ]
445
+ (
446
+ is_banned ,
447
+ can_read ,
448
+ can_access ,
449
+ can_write ,
450
+ can_upload ,
451
+ is_mod ,
452
+ is_admin ,
453
+ ) = self ._perm_cache [user .id ]
416
454
417
455
if is_admin :
418
456
return True
@@ -424,6 +462,7 @@ def check_permission(
424
462
return False
425
463
return (
426
464
not is_banned
465
+ and (not accessible or can_access or can_read )
427
466
and (not read or can_read )
428
467
and (not write or can_write )
429
468
and (not upload or can_upload )
@@ -437,6 +476,9 @@ def check_unbanned(self, user: Optional[User]):
437
476
def check_read (self , user : Optional [User ] = None ):
438
477
return self .check_permission (user , read = True )
439
478
479
+ def check_accessible (self , user : Optional [User ] = None ):
480
+ return self .check_permission (user , accessible = True )
481
+
440
482
def check_write (self , user : Optional [User ] = None ):
441
483
return self .check_permission (user , write = True )
442
484
@@ -1118,19 +1160,20 @@ def get_bans(self):
1118
1160
1119
1161
def set_permissions (self , user : User , * , mod : User , ** perms ):
1120
1162
"""
1121
- Grants or removes read, write, and/or upload permissions to the given user in this room.
1122
- `mod` must have moderator access in the room.
1163
+ Grants or removes read, accessible, write, and/or upload permissions to the given user in
1164
+ this room. `mod` must have moderator access in the room.
1123
1165
1124
- Permitted keyword args are: read, write, upload. Each can be set to True, False, or None to
1125
- apply an explicit grant, explicit revocation, or return to room defaults, respectively.
1126
- (That is, None removes the override, if currently present, so that the user permission will
1127
- use the room default; the others set this user's permission to allowed/disallowed).
1166
+ Permitted keyword args are: read, accessible, write, upload. Each can be set to True,
1167
+ False, or None to apply an explicit grant, explicit revocation, or return to room defaults,
1168
+ respectively. (That is, None removes the override, if currently present, so that the user
1169
+ permission will use the room default; the others set this user's permission to
1170
+ allowed/disallowed).
1128
1171
1129
1172
If a permission key is omitted then it will not be changed at all if it already exists, and
1130
1173
will be NULL if a new permission row is being created.
1131
1174
"""
1132
1175
1133
- perm_types = ('read' , 'write' , 'upload' )
1176
+ perm_types = ('read' , 'accessible' , ' write' , 'upload' )
1134
1177
1135
1178
if any (k not in perm_types for k in perms .keys ()):
1136
1179
raise ValueError (f"Room.set_permissions: only { ', ' .join (perm_types )} may be specified" )
@@ -1156,6 +1199,7 @@ def set_permissions(self, user: User, *, mod: User, **perms):
1156
1199
r = self .id ,
1157
1200
u = user .id ,
1158
1201
read = perms .get ('read' ),
1202
+ accessible = perms .get ('accessible' ),
1159
1203
write = perms .get ('write' ),
1160
1204
upload = perms .get ('upload' ),
1161
1205
)
@@ -1348,6 +1392,7 @@ def get_rooms_with_permission(
1348
1392
* ,
1349
1393
tokens : Optional [Union [list , tuple ]] = None ,
1350
1394
read : Optional [bool ] = None ,
1395
+ accessible : Optional [bool ] = None ,
1351
1396
write : Optional [bool ] = None ,
1352
1397
upload : Optional [bool ] = None ,
1353
1398
banned : Optional [bool ] = None ,
@@ -1363,7 +1408,7 @@ def get_rooms_with_permission(
1363
1408
omitted, all rooms are returned. Note that rooms are returned sorted by token, *not* in
1364
1409
the order specified here; duplicates are not returned; nor are entries for non-existent
1365
1410
tokens.
1366
- read/write/upload/banned/moderator/admin:
1411
+ read/accessible/ write/upload/banned/moderator/admin:
1367
1412
Any of these that are specified as non-None must match the user's permissions for the room.
1368
1413
For example `read=True, write=False` would return all rooms where the user has read-only
1369
1414
access but not rooms in which the user has both or neither read and write permissions.
@@ -1387,6 +1432,8 @@ def get_rooms_with_permission(
1387
1432
WHERE "user" = :u { 'AND token IN :tokens' if tokens else '' }
1388
1433
{ '' if banned is None else ('AND' if banned else 'AND NOT' ) + ' perm.banned' }
1389
1434
{ '' if read is None else ('AND' if read else 'AND NOT' ) + ' perm.read' }
1435
+ { '' if accessible is None else ('AND' if accessible else 'AND NOT' ) +
1436
+ ' (perm.read OR perm.accessible)' }
1390
1437
{ '' if write is None else ('AND' if write else 'AND NOT' ) + ' perm.write' }
1391
1438
{ '' if upload is None else ('AND' if upload else 'AND NOT' ) + ' perm.upload' }
1392
1439
{ '' if moderator is None else ('AND' if moderator else 'AND NOT' ) + ' perm.moderator' }
@@ -1400,15 +1447,15 @@ def get_rooms_with_permission(
1400
1447
]
1401
1448
1402
1449
1403
- def get_readable_rooms (user : Optional [User ] = None ):
1450
+ def get_accessible_rooms (user : Optional [User ] = None ):
1404
1451
"""
1405
- Get a list of rooms that a user can access; if user is None then return all publicly readable
1452
+ Get a list of rooms that a user can access; if user is None then return all publicly accessible
1406
1453
rooms.
1407
1454
"""
1408
1455
if user is None :
1409
- result = query ("SELECT * FROM rooms WHERE read ORDER BY token" )
1456
+ result = query ("SELECT * FROM rooms WHERE ( read OR accessible) ORDER BY token" )
1410
1457
else :
1411
- return get_rooms_with_permission (user , read = True , banned = False )
1458
+ return get_rooms_with_permission (user , accessible = True , banned = False )
1412
1459
return [Room (row ) for row in result ]
1413
1460
1414
1461
0 commit comments