-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
DeleteUser.groovy
163 lines (142 loc) · 5.02 KB
/
DeleteUser.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Tigase XMPP Server - The instant messaging server
* Copyright (C) 2004 Tigase, Inc. (office@tigase.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. Look for COPYING file in the top folder.
* If not, see http://www.gnu.org/licenses/.
*/
/*
User delete script as described in XEP-0133:
http://xmpp.org/extensions/xep-0133.html#delete-user
AS:Description: Delete user
AS:CommandId: http://jabber.org/protocol/admin#delete-user
AS:Component: sess-man
AS:Group: Users
*/
package tigase.admin
import tigase.db.AuthRepository
import tigase.db.TigaseDBException
import tigase.db.UserNotFoundException
import tigase.db.UserRepository
import tigase.server.Command
import tigase.server.Packet
import tigase.server.Permissions
import tigase.vhosts.VHostManagerIfc
import tigase.xmpp.StanzaType
import tigase.xmpp.jid.BareJID
def JIDS = "accountjids"
def p = (Packet) packet
def auth_repo = (AuthRepository) authRepository
def user_repo = (UserRepository) userRepository
def vhost_man = (VHostManagerIfc) vhostMan
def admins = (Set) adminsSet
def stanzaFromBare = p.getStanzaFrom().getBareJID()
def isServiceAdmin = admins.contains(stanzaFromBare)
def NOTIFY_CLUSTER = "notify-cluster"
boolean clusterMode = Boolean.valueOf(System.getProperty("cluster-mode", false.toString()));
def notifyClusterStr = Command.getFieldValue(packet, NOTIFY_CLUSTER);
boolean notifyCluster = (notifyClusterStr != null) ? Boolean.valueOf(notifyClusterStr) : true;
def user_sessions = (Map) userSessions;
def userJids = Command.getFieldValues(packet, JIDS)
if (userJids == null) {
def result = p.commandResult(Command.DataType.form);
Command.addTitle(result, "Deleting a User")
Command.addInstructions(result, "Fill out this form to delete a user.")
Command.addFieldValue(result, "FORM_TYPE", "http://jabber.org/protocol/admin",
"hidden")
Command.addFieldValue(result, JIDS, userJids ?: "", "jid-multi",
"The Jabber ID(s) to delete")
if (clusterMode) {
Command.addHiddenField(result, NOTIFY_CLUSTER, true.toString())
}
return result
}
def results = new LinkedList<Packet>();
def closeUserSessions = { userJid ->
try {
def bareJID = BareJID.bareJIDInstance(userJid)
def sess = user_sessions.get(bareJID);
if (sess != null) {
def conns = sess.getConnectionIds();
for (conn in conns) {
def res = sess.getResourceForConnectionId(conn);
if (res != null) {
def commandClose = Command.CLOSE.getPacket(p.getStanzaTo(), conn,
StanzaType.set, res.nextStanzaId());
results.offer(commandClose);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
};
if (clusterMode) {
if (!notifyCluster) {
for (userJid in userJids) {
closeUserSessions(userJid);
}
return results;
}
}
def result = p.commandResult(Command.DataType.result)
results.offer(result);
def msgs = [ ];
def errors = [ ];
for (userJid in userJids) {
try {
def bareJID = BareJID.bareJIDInstance(userJid)
if (isAllowedForDomain.apply(bareJID.getDomain())) {
if (user_repo.userExists(bareJID)) {
auth_repo.removeUser(bareJID)
try {
user_repo.removeUser(bareJID)
} catch (UserNotFoundException ex) {
// We ignore this error here. If auth_repo and user_repo are in fact the same
// database, then user has been already removed with the auth_repo.removeUser(...)
// then the second call to user_repo may throw the exception which is fine.
}
if (clusterMode && notifyCluster) {
def nodes = (List) clusterStrategy.getNodesConnected();
if (nodes && nodes.size() > 0) {
nodes.each { node ->
def forward = p.copyElementOnly();
Command.removeFieldValue(forward, NOTIFY_CLUSTER)
Command.addHiddenField(forward, NOTIFY_CLUSTER, false.toString())
forward.setPacketTo(node);
forward.setPermissions(Permissions.ADMIN);
results.offer(forward)
}
}
}
closeUserSessions(userJid);
msgs.add("Operation successful for user " + userJid);
} else {
errors.add("User " + userJid + " not found, can't be deleted.");
}
} else {
errors.add("You do not have enough permissions to delete accounts for domain " + bareJID.getDomain() + ".");
}
} catch (UserNotFoundException ex) {
errors.add("User " + userJid + " not exists, can't be deleted.");
} catch (TigaseDBException ex) {
errors.add("Problem accessing database, user " + userJid + " not deleted.");
}
}
if (!msgs.isEmpty()) {
Command.addFieldMultiValue(result, "Notes", msgs)
};
if (!errors.isEmpty()) {
Command.addFieldMultiValue(result, "Errors", errors)
};
return results