Skip to content

Commit 4026da2

Browse files
authored
Merge pull request #142 from tewinget/set-perms
add --set-perms to pysogs, let --add-room also use them
2 parents aba8233 + 1e8b118 commit 4026da2

File tree

1 file changed

+145
-10
lines changed

1 file changed

+145
-10
lines changed

sogs/__main__.py

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
# Add a global moderator visible as a moderator of all rooms:
2020
python3 -msogs --add-moderators 050123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef --rooms=+ --visible
2121
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+
2228
# List room info:
2329
python3 -msogs -L
2430
@@ -51,6 +57,34 @@
5157
metavar='SESSIONID',
5258
help="Delete the the given Session ID(s) as moderator and admins of the room given by --rooms",
5359
)
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+
)
5488
ap.add_argument(
5589
'--admin',
5690
action='store_true',
@@ -60,10 +94,10 @@
6094
'--rooms',
6195
nargs='+',
6296
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.",
67101
)
68102
vis_group = ap.add_mutually_exclusive_group()
69103
vis_group.add_argument(
@@ -177,6 +211,13 @@ def print_room(room: Room):
177211
admins = len(a) + len(ha)
178212
mods = len(m) + len(hm)
179213

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+
180221
print(
181222
f"""
182223
{room.token}
@@ -188,6 +229,7 @@ def print_room(room: Room):
188229
Attachments: {files} ({files_size:.1f} MB)
189230
Reactions: {r_total}; top 5: {', '.join(f"{r} ({c})" for r, c in reactions[0:5])}
190231
Active users: {active[0]} (1d), {active[1]} (7d), {active[2]} (14d), {active[3]} (30d)
232+
Default permissions: {perms}
191233
Moderators: {admins} admins ({len(ha)} hidden), {mods} moderators ({len(hm)} hidden)""",
192234
end='',
193235
)
@@ -205,6 +247,52 @@ def print_room(room: Room):
205247
print()
206248

207249

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+
208296
if args.initialize:
209297
print("Database schema created.")
210298

@@ -215,17 +303,21 @@ def print_room(room: Room):
215303
print("No database upgrades required.")
216304

217305
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)
224307

225308
try:
226309
room = Room.create(
227310
token=args.add_room, name=args.name or args.add_room, description=args.description
228311
)
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+
229321
except AlreadyExists:
230322
print(f"Error: room '{args.add_room}' already exists!", file=sys.stderr)
231323
sys.exit(1)
@@ -367,6 +459,49 @@ def print_room(room: Room):
367459
f"Removed {u2.session_id} as moderator/admin of {room.name} ({room.token})"
368460
)
369461

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+
370505
elif args.list_rooms:
371506
rooms = get_rooms()
372507
if rooms:

0 commit comments

Comments
 (0)