]> git.somenet.org - pub/jan/mattermost.git/blob - management/account_handling.py
[management/account_handling] convert accounts to guests and back to full-users.
[pub/jan/mattermost.git] / management / account_handling.py
1 #!/usr/bin/env python3
2 #
3 # Someone's Mattermost scripts.
4 #  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
5 #  published under MIT-License
6 #
7 # consider running as cronjob
8 #   0 * * * *  (cd /home/someone/mattermost/management; python3 -u account_handling.py)
9 #
10
11 import mattermost
12 import re
13 import config
14 import time
15 import pprint
16 import sys
17
18 from inspect import cleandoc
19
20 import config
21
22
23 def confine_guest(user):
24     #print("confine_guest()")
25     global mm, all_team_ids
26
27     for team_id in config.guest_team_ids:
28         mm.add_user_to_team(team_id, user["id"])
29
30     user_pub_chans = set()
31     for team_id in all_team_ids:
32         try:
33             user_pub_chans = user_pub_chans.union({c["id"] for c in mm.get_channels_for_user(user["id"], team_id, exc=True) if c["type"]=="O"})
34         except mattermost.ApiException as e:
35             pass
36             
37     to_add = config.guest_channels - user_pub_chans
38     #pprint.pprint(to_add)
39     for chan_id in to_add:
40         mm.add_user_to_channel(chan_id, user["id"])
41         
42     to_kick = user_pub_chans - config.guest_channels
43     #pprint.pprint(to_kick)
44     for chan_id in to_kick:
45         mm.remove_user_from_channel(chan_id, user["id"])
46
47
48 cnt = 0
49 cnt_g = 0
50 cnt_u = 0
51 cnt_u2g = 0
52 cnt_g2u = 0
53 TS_START = time.time()
54 rex = re.compile(config.allowed_emails_regex)
55
56 mm = mattermost.MMApi(config.mm_api_url)
57 mm.login(config.mm_user, config.mm_user_pw)
58 print("* ["+("%07.6g"%round(time.time() - TS_START, 5))+"] login")
59
60 all_team_ids = [t["id"] for t in mm.get_teams()]
61 print("* ["+("%07.6g"%round(time.time() - TS_START, 5))+"] list all team_ids")
62
63 for user in mm.get_users():
64     cnt += 1
65     if "system_user" in user["roles"]:
66         cnt_u += 1
67
68         if not rex.match(user["email"]):
69             cnt_u2g += 1
70             u2g = user
71
72             #print("demote")
73             #pprint.pprint(mm.demote_a_user(user["id"]))
74             mm.demote_a_user(user["id"])
75             confine_guest(user)
76
77             c = mm.create_dm_channel_with(user["id"])
78             mm.create_post(c["id"], cleandoc("""
79                 ### ``Confirm your student email to get full access to Mattermost``
80                 As previously announced your account has been converted to a [guest account](https://docs.mattermost.com/deployment/guest-accounts.html), which means you will be restricted to three channels:
81                 * Beratung
82                 * Guest-Talk
83                 * Aufnahmeverfahren
84
85                 Mattermost has a dedicated channel for every course, to access them you need to go to your Account Settings and change your email to ``eXXXXXXXX@student.tuwien.ac.at`` and then click on the link in the confirmation email. You will then get full access within an hour.
86
87                 Sorry for the inconvenience, we hope to see you soon!
88                 If something seems off, contact my master @someone by mentioning him in the ``Guest-Talk`` channel.
89                 """))
90
91         else:
92             pass
93             # todo: inform of guest status and to change email
94
95
96     elif "system_guest" in user["roles"]:
97         cnt_g += 1
98         if rex.match(user["email"]):
99             cnt_g2u += 1
100             g2u = user
101
102             # convert account to user.
103             #print("promote")
104             #pprint.pprint(mm.promote_a_guest(user["id"]))
105             mm.promote_a_guest(user["id"])
106             c = mm.create_dm_channel_with(user["id"])
107             mm.create_post(c["id"], cleandoc("""
108                 ### ``Your account has been converted to a FULL-USER account!``
109                 You are now able to use all the features mattermost and this instance has to offer!
110                 """))
111         else:
112             # re-confine guests
113             confine_guest(user)
114             
115     else:
116         print("weird user")
117         pprint.pprint(user)
118         
119     print("* ["+("%07.6g"%round(time.time() - TS_START, 5))+"] cnt:", cnt, " cnt_u:", cnt_u, " cnt_g:", cnt_g, " cnt_u2g:", cnt_u2g, " cnt_g2u:", cnt_g2u)
120
121 mm.logout()