@@ -18,8 +18,8 @@ class User:
18
18
created - unix timestamp when the user was created
19
19
last_active - unix timestamp when the user was last active
20
20
banned - True if the user is (globally) banned
21
- admin - True if the user is a global admin
22
- moderator - True if the user is a global moderator
21
+ global_admin - True if the user is a global admin
22
+ global_moderator - True if the user is a global moderator
23
23
visible_mod - True if the user's admin/moderator status should be visible in rooms
24
24
"""
25
25
@@ -63,8 +63,7 @@ def __init__(self, row=None, *, id=None, session_id=None, autovivify=True, touch
63
63
)
64
64
65
65
if touch :
66
- with db .transaction ():
67
- self ._touch ()
66
+ self ._touch ()
68
67
69
68
def __str__ (self ):
70
69
"""Returns string representation of a user: U[050123…cdef], the id prefixed with @ or % if
@@ -96,8 +95,7 @@ def touch(self, force=False):
96
95
to True.
97
96
"""
98
97
if not self ._touched or force :
99
- with db .transaction ():
100
- self ._touch ()
98
+ self ._touch ()
101
99
102
100
def update_room_activity (self , room ):
103
101
query (
@@ -124,17 +122,16 @@ def set_moderator(self, *, added_by: User, admin=False, visible=False):
124
122
)
125
123
raise BadPermission ()
126
124
127
- with db .transaction ():
128
- query (
129
- """
130
- UPDATE users
131
- SET moderator = TRUE, admin = :admin, visible_mod = :visible
132
- WHERE id = :u
133
- """ ,
134
- admin = admin ,
135
- visible = visible ,
136
- u = self .id ,
137
- )
125
+ query (
126
+ """
127
+ UPDATE users
128
+ SET moderator = TRUE, admin = :admin, visible_mod = :visible
129
+ WHERE id = :u
130
+ """ ,
131
+ admin = admin ,
132
+ visible = visible ,
133
+ u = self .id ,
134
+ )
138
135
self .global_admin = admin
139
136
self .global_moderator = True
140
137
self .visible_mod = visible
@@ -148,11 +145,38 @@ def remove_moderator(self, *, removed_by: User):
148
145
)
149
146
raise BadPermission ()
150
147
151
- with db .transaction ():
152
- query ("UPDATE users SET moderator = FALSE, admin = FALSE WHERE id = :u" , u = self .id )
148
+ query ("UPDATE users SET moderator = FALSE, admin = FALSE WHERE id = :u" , u = self .id )
153
149
self .global_admin = False
154
150
self .global_moderator = False
155
151
152
+ def ban (self , * , banned_by : User ):
153
+ """Globally bans this user from the server; can only be applied by a global moderator or
154
+ global admin, and cannot be applied to another global moderator or admin (to prevent
155
+ accidental mod/admin banning; to ban them, first explicitly remove them as moderator/admin
156
+ and then ban)."""
157
+
158
+ if not banned_by .global_moderator :
159
+ app .logger .warning (f"Cannot ban { self } : { banned_by } is not a global mod/admin" )
160
+ raise BadPermission ()
161
+
162
+ if self .global_moderator :
163
+ app .logger .warning (f"Cannot ban { self } : user is a global moderator/admin" )
164
+ raise BadPermission ()
165
+
166
+ query ("UPDATE users SET banned = TRUE WHERE id = :u" , u = self .id )
167
+ app .logger .debug (f"{ banned_by } globally banned { self } " )
168
+ self .banned = True
169
+
170
+ def unban (self , * , unbanned_by : User ):
171
+ """Undoes a global ban. `unbanned_by` must be a global mod/admin."""
172
+ if not unbanned_by .global_moderator :
173
+ app .logger .warning (f"Cannot unban { self } : { unbanned_by } is not a global mod/admin" )
174
+ raise BadPermission ()
175
+
176
+ query ("UPDATE users SET banned = FALSE WHERE id = :u" , u = self .id )
177
+ app .logger .debug (f"{ unbanned_by } removed global ban on { self } " )
178
+ self .banned = False
179
+
156
180
@property
157
181
def system_user (self ):
158
182
"""True iff this is the special SOGS system user created for internal database tasks"""
0 commit comments