19
19
# Add a global moderator visible as a moderator of all rooms:
20
20
python3 -msogs --add-moderators 050123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef --rooms=+ --visible
21
21
22
+ # Set default read/write True and upload False on all rooms
23
+ python3 -msogs --set-perms --add-perms rw --remove-perms u --rooms='*'
24
+
25
+ # Remove overrides for user 0501234... on all rooms
26
+ python3 -msogs --set-perms --clear-perms rwua --rooms='*' --users 050123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
27
+
22
28
# List room info:
23
29
python3 -msogs -L
24
30
51
57
metavar = 'SESSIONID' ,
52
58
help = "Delete the the given Session ID(s) as moderator and admins of the room given by --rooms" ,
53
59
)
60
+ actions .add_argument (
61
+ '--set-perms' ,
62
+ action = 'store_true' ,
63
+ help = "Sets default or user-specific permissions for the room given by --rooms; specify the "
64
+ "permissions using --add-perms or --remove-perms" ,
65
+ )
66
+ ap .add_argument (
67
+ '--users' ,
68
+ help = "One or more specific users to set permissions for with --set-perms; if omitted then the "
69
+ "room default permissions will be set for the given room(s) instead." ,
70
+ nargs = '+' ,
71
+ metavar = 'SESSIONID' ,
72
+ )
73
+ ap .add_argument (
74
+ "--add-perms" ,
75
+ help = "With --add-room or --set-perms, set these permissions to true; takes a string of 1-4 of "
76
+ "the letters \" rwua\" for [r]ead, [w]rite, [u]pload, and [a]ccess." ,
77
+ )
78
+ ap .add_argument (
79
+ "--remove-perms" ,
80
+ help = "With --add-room or --set-perms, set these permissions to false; takes the same string as "
81
+ "--add-perms, but denies the listed permissions rather than granting them." ,
82
+ )
83
+ ap .add_argument (
84
+ "--clear-perms" ,
85
+ help = "With --add-room or --set-perms, clear room or user overrides on these permissions, "
86
+ "returning them to the default setting. Takes the same argument as --add-perms." ,
87
+ )
54
88
ap .add_argument (
55
89
'--admin' ,
56
90
action = 'store_true' ,
60
94
'--rooms' ,
61
95
nargs = '+' ,
62
96
metavar = 'TOKEN' ,
63
- help = "Room(s) to use when adding/removing moderators/admins. If a single room name of '+' is "
64
- "given then the user will be added/removed as a global admin/moderator. If a single room name "
65
- "of '* ' is given then the user is added/removed as an admin/moderator from each of the "
66
- "server's current rooms." ,
97
+ help = "Room(s) to use when adding/removing moderators/admins or when setting permissions. "
98
+ "If a single room name of '+' is given then the user will be added/removed as a global "
99
+ "admin/moderator. '+ ' is not valid for setting permissions. If a single room name "
100
+ "of '*' is given then the changes take effect on each of the server's current rooms." ,
67
101
)
68
102
vis_group = ap .add_mutually_exclusive_group ()
69
103
vis_group .add_argument (
@@ -177,6 +211,13 @@ def print_room(room: Room):
177
211
admins = len (a ) + len (ha )
178
212
mods = len (m ) + len (hm )
179
213
214
+ perms = "{}read, {}write, {}upload, {}accessible" .format (
215
+ "+" if room .default_read else "-" ,
216
+ "+" if room .default_write else "-" ,
217
+ "+" if room .default_upload else "-" ,
218
+ "+" if room .default_accessible else "-" ,
219
+ )
220
+
180
221
print (
181
222
f"""
182
223
{ room .token }
@@ -188,6 +229,7 @@ def print_room(room: Room):
188
229
Attachments: { files } ({ files_size :.1f} MB)
189
230
Reactions: { r_total } ; top 5: { ', ' .join (f"{ r } ({ c } )" for r , c in reactions [0 :5 ])}
190
231
Active users: { active [0 ]} (1d), { active [1 ]} (7d), { active [2 ]} (14d), { active [3 ]} (30d)
232
+ Default permissions: { perms }
191
233
Moderators: { admins } admins ({ len (ha )} hidden), { mods } moderators ({ len (hm )} hidden)""" ,
192
234
end = '' ,
193
235
)
@@ -205,6 +247,52 @@ def print_room(room: Room):
205
247
print ()
206
248
207
249
250
+ def room_token_valid (room ):
251
+ if not re .fullmatch (r'[\w-]{1,64}' , room ):
252
+ print (
253
+ "Error: room tokens may only contain a-z, A-Z, 0-9, _, and - characters" ,
254
+ file = sys .stderr ,
255
+ )
256
+ sys .exit (1 )
257
+
258
+
259
+ def perm_flag_to_word (char ):
260
+ if char == 'r' :
261
+ return "read"
262
+ if char == 'w' :
263
+ return "write"
264
+ if char == 'u' :
265
+ return "upload"
266
+ if char == 'a' :
267
+ return "accessible"
268
+
269
+ print (f"Error: invalid permission flag '{ char } '" )
270
+ sys .exit (1 )
271
+
272
+
273
+ perms = {}
274
+
275
+
276
+ def parse_and_set_perm_flags (flags , perm_setting ):
277
+ for char in flags :
278
+ perm_type = perm_flag_to_word (char )
279
+ if perm_type in perms :
280
+ print (
281
+ f"Error: permission flag '{ char } ' in more than one permission set "
282
+ "(add/remove/clear)"
283
+ )
284
+ sys .exit (1 )
285
+ perms [perm_type ] = perm_setting
286
+
287
+
288
+ if args .add_room or args .set_perms :
289
+ if args .add_perms :
290
+ parse_and_set_perm_flags (args .add_perms , True )
291
+ if args .remove_perms :
292
+ parse_and_set_perm_flags (args .remove_perms , False )
293
+ if args .clear_perms :
294
+ parse_and_set_perm_flags (args .clear_perms , None )
295
+
208
296
if args .initialize :
209
297
print ("Database schema created." )
210
298
@@ -215,17 +303,21 @@ def print_room(room: Room):
215
303
print ("No database upgrades required." )
216
304
217
305
elif args .add_room :
218
- if not re .fullmatch (r'[\w-]{1,64}' , args .add_room ):
219
- print (
220
- "Error: room tokens may only contain a-z, A-Z, 0-9, _, and - characters" ,
221
- file = sys .stderr ,
222
- )
223
- sys .exit (1 )
306
+ room_token_valid (args .add_room )
224
307
225
308
try :
226
309
room = Room .create (
227
310
token = args .add_room , name = args .name or args .add_room , description = args .description
228
311
)
312
+ if "read" in perms :
313
+ room .default_read = perms ["read" ]
314
+ if "write" in perms :
315
+ room .default_write = perms ["write" ]
316
+ if "accessible" in perms :
317
+ room .default_accessible = perms ["accessible" ]
318
+ if "upload" in perms :
319
+ room .default_upload = perms ["upload" ]
320
+
229
321
except AlreadyExists :
230
322
print (f"Error: room '{ args .add_room } ' already exists!" , file = sys .stderr )
231
323
sys .exit (1 )
@@ -367,6 +459,49 @@ def print_room(room: Room):
367
459
f"Removed { u2 .session_id } as moderator/admin of { room .name } ({ room .token } )"
368
460
)
369
461
462
+ elif args .set_perms :
463
+ if not args .rooms :
464
+ print ("Error: --rooms is required when using --set-perms" , file = sys .stderr )
465
+ sys .exit (1 )
466
+
467
+ if args .rooms == ['+' ]:
468
+ print ("Error: --rooms cannot be '+' (i.e. global) with --set-perms" , file = sys .stderr )
469
+ sys .exit (1 )
470
+
471
+ users = []
472
+ if args .users :
473
+ users = [User (session_id = sid , try_blinding = True ) for sid in args .users ]
474
+
475
+ rooms = []
476
+ if args .rooms == ['*' ]:
477
+ rooms = get_rooms ()
478
+ else :
479
+ try :
480
+ rooms = [Room (token = r ) for r in args .rooms ]
481
+ except NoSuchRoom as nsr :
482
+ print (f"No such room: '{ nsr .token } '" , file = sys .stderr )
483
+
484
+ if not len (rooms ):
485
+ print ("Error: no valid rooms specified for call to --set-perms" )
486
+ sys .exit (1 )
487
+
488
+ # users not specified means set room defaults
489
+ if not len (users ):
490
+ for room in rooms :
491
+ if "read" in perms :
492
+ room .default_read = perms ["read" ]
493
+ if "write" in perms :
494
+ room .default_write = perms ["write" ]
495
+ if "accessible" in perms :
496
+ room .default_accessible = perms ["accessible" ]
497
+ if "upload" in perms :
498
+ room .default_upload = perms ["upload" ]
499
+ else :
500
+ sysadmin = SystemUser ()
501
+ for room in rooms :
502
+ for user in users :
503
+ room .set_permissions (user , mod = sysadmin , ** perms )
504
+
370
505
elif args .list_rooms :
371
506
rooms = get_rooms ()
372
507
if rooms :
0 commit comments