Mercurial > prosody-hg
comparison plugins/mod_c2s.lua @ 11899:6d06068363aa
mod_c2s: Disconnect user sessions on a role change event
The overlapping logic for deletion and password changed has been merged into
a single function.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 12 Nov 2021 13:26:05 +0000 |
| parents | ae093c259da2 |
| children | a7c183bb4e64 |
comparison
equal
deleted
inserted
replaced
| 11898:89aa591bb895 | 11899:6d06068363aa |
|---|---|
| 237 local reason_text = (reason and (reason.name or reason.text or reason.condition)) or reason; | 237 local reason_text = (reason and (reason.name or reason.text or reason.condition)) or reason; |
| 238 sm_destroy_session(session, reason_text); | 238 sm_destroy_session(session, reason_text); |
| 239 end | 239 end |
| 240 end | 240 end |
| 241 | 241 |
| 242 module:hook_global("user-deleted", function(event) | 242 -- Close all user sessions with the specified reason. If leave_resource is |
| 243 local username, host = event.username, event.host; | 243 -- true, the resource named by event.resource will not be closed. |
| 244 local user = hosts[host].sessions[username]; | 244 local function disconnect_user_sessions(reason, leave_resource) |
| 245 if user and user.sessions then | 245 return function (event) |
| 246 for _, session in pairs(user.sessions) do | 246 local username, host, resource = event.username, event.host, event.resource; |
| 247 session:close{ condition = "not-authorized", text = "Account deleted" }; | 247 local user = hosts[host].sessions[username]; |
| 248 end | 248 if user and user.sessions then |
| 249 end | 249 for r, session in pairs(user.sessions) do |
| 250 end, 200); | 250 if not leave_resource or r ~= resource then |
| 251 | 251 session:close(reason); |
| 252 module:hook_global("user-password-changed", function(event) | 252 end |
| 253 local username, host, resource = event.username, event.host, event.resource; | 253 end |
| 254 local user = hosts[host].sessions[username]; | 254 end |
| 255 if user and user.sessions then | 255 end |
| 256 for r, session in pairs(user.sessions) do | 256 end |
| 257 if r ~= resource then | 257 |
| 258 session:close{ condition = "reset", text = "Password changed" }; | 258 module:hook_global("user-password-changed", disconnect_user_sessions({ condition = "reset", text = "Password changed" }, true), 200); |
| 259 end | 259 module:hook_global("user-roles-changed", disconnect_user_sessions({ condition = "reset", text = "Roles changed" }), 200); |
| 260 end | 260 module:hook_global("user-deleted", disconnect_user_sessions({ condition = "not-authorized", text = "Account deleted" }), 200); |
| 261 end | |
| 262 end, 200); | |
| 263 | 261 |
| 264 function runner_callbacks:ready() | 262 function runner_callbacks:ready() |
| 265 if self.data.conn then | 263 if self.data.conn then |
| 266 self.data.conn:resume(); | 264 self.data.conn:resume(); |
| 267 else | 265 else |
