-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathUserServiceController.java
More file actions
645 lines (594 loc) · 24.2 KB
/
Copy pathUserServiceController.java
File metadata and controls
645 lines (594 loc) · 24.2 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
* Copyright (c) 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.openfire.plugin.rest.controller;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.SharedGroupException;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.jivesoftware.openfire.lockout.LockOutManager;
import org.jivesoftware.openfire.plugin.rest.RESTServicePlugin;
import org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO;
import org.jivesoftware.openfire.plugin.rest.entity.*;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.JiveGlobals;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.StreamError;
import javax.ws.rs.core.Response;
import java.util.*;
/**
* The Class UserServiceController.
*/
public class UserServiceController {
private static final Logger LOG = LoggerFactory.getLogger(UserServiceController.class);
/** The Constant INSTANCE. */
private static UserServiceController INSTANCE = null;
/** The user manager. */
private final UserManager userManager;
/** The roster manager. */
private final RosterManager rosterManager;
/** The server. */
private final XMPPServer server;
/** The lock out manager. */
private final LockOutManager lockOutManager;
/**
* Gets the single instance of UserServiceController.
*
* @return single instance of UserServiceController
*/
public static UserServiceController getInstance() {
if (INSTANCE == null) {
INSTANCE = new UserServiceController();
}
return INSTANCE;
}
/**
* @param instance the mock/stub/spy controller to use.
* @deprecated - for test use only
*/
@Deprecated
public static void setInstance(final UserServiceController instance) {
UserServiceController.INSTANCE = instance;
}
/**
* Instantiates a new user service controller.
*/
private UserServiceController() {
server = XMPPServer.getInstance();
userManager = server.getUserManager();
rosterManager = server.getRosterManager();
lockOutManager = server.getLockOutManager();
}
public static void log(String logMessage) {
if (JiveGlobals.getBooleanProperty(RESTServicePlugin.SERVICE_LOGGING_ENABLED, false)) {
LOG.info(logMessage);
}
}
/**
* Creates the user.
*
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
public void createUser(UserEntity userEntity) throws ServiceException {
if (userEntity != null && !userEntity.getUsername().isEmpty()) {
if (userEntity.getPassword() == null) {
throw new ServiceException("Could not create new user, because password is null",
userEntity.getUsername(), "PasswordIsNull", Response.Status.BAD_REQUEST);
}
log("Create a new user: " + userEntity.getUsername());
try {
userManager.createUser(userEntity.getUsername(), userEntity.getPassword(), userEntity.getName(),
userEntity.getEmail());
} catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create new user", userEntity.getUsername(),
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT);
}
addProperties(userEntity.getUsername(), userEntity.getProperties());
} else {
throw new ServiceException("Could not create new user",
"users", ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
}
}
/**
* Update user.
*
* @param username
* the username
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
public void updateUser(String username, UserEntity userEntity) throws ServiceException {
if (userEntity != null && !username.isEmpty()) {
log("Update the user: " + userEntity.getUsername());
// Payload contains another username than provided over path
// parameter
if (userEntity.getUsername() != null) {
if (!userEntity.getUsername().equals(username)) {
JustMarriedController.changeName(username, userEntity.getUsername(), true, userEntity.getEmail(),
userEntity.getName());
addProperties(userEntity.getUsername(), userEntity.getProperties());
return;
}
}
User user = getAndCheckUser(username);
if (userEntity.getPassword() != null) {
user.setPassword(userEntity.getPassword());
}
if (userEntity.getName() != null) {
user.setName(userEntity.getName());
}
if (userEntity.getEmail() != null) {
user.setEmail(userEntity.getEmail());
}
addProperties(username, userEntity.getProperties());
}
}
/**
* Delete user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public void deleteUser(String username) throws ServiceException {
log("Delete the user: " + username);
User user = getAndCheckUser(username);
userManager.deleteUser(user);
rosterManager.deleteRoster(server.createJID(username, null));
}
/**
* Gets the user entities.
*
* When a property key (and possibly value) is provided, then the user that is returned is one for which the
* specified property has been defined.
*
* @param userSearch
* the user search
* @param propertyKey
* the property key (can be null)
* @param propertyValue
* the property value (can be null)
* @return the user entities
* @throws ServiceException
* the service exception
*/
public UserEntities getUserEntities(String userSearch, String propertyKey, String propertyValue)
throws ServiceException {
if (propertyKey != null) {
log("Get users by property");
return getUserEntitiesByProperty(propertyKey, propertyValue);
}
log("Get all users");
UserEntities userEntities = new UserEntities();
userEntities.setUsers(UserUtils.convertUsersToUserEntities(userManager.getUsers(), userSearch));
return userEntities;
}
/**
* Gets the user entity.
*
* @param username
* the username
* @return the user entity
* @throws ServiceException
* the service exception
*/
public UserEntity getUserEntity(String username) throws ServiceException {
log("Get user entity from user: " + username);
return UserUtils.convertUserToUserEntity(getAndCheckUser(username));
}
/**
* Enable user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public void enableUser(String username) throws ServiceException {
log("Enable the user: " + username);
getAndCheckUser(username);
lockOutManager.enableAccount(username);
}
/**
* Disable user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public void disableUser(String username) throws ServiceException {
log("Disable the user: " + username);
getAndCheckUser(username);
lockOutManager.disableAccount(username, null, null);
if (lockOutManager.isAccountDisabled(username)) {
final StreamError error = new StreamError(StreamError.Condition.not_authorized);
for (ClientSession session : SessionManager.getInstance().getSessions(username)) {
session.deliverRawText(error.toXML());
session.close();
}
}
}
/**
* Gets the roster entities.
*
* @param username
* the username
* @return the roster entities
* @throws ServiceException
* the service exception
*/
public RosterEntities getRosterEntities(String username) throws ServiceException {
log("Get roster entities for user: " + username);
Roster roster = getUserRoster(username);
List<RosterItemEntity> rosterEntities = new ArrayList<>();
for (RosterItem rosterItem : roster.getRosterItems()) {
RosterItemEntity rosterItemEntity = new RosterItemEntity(rosterItem.getJid().toBareJID(),
rosterItem.getNickname(), rosterItem.getSubStatus().getValue());
rosterItemEntity.setGroups(rosterItem.getGroups());
rosterEntities.add(rosterItemEntity);
}
return new RosterEntities(rosterEntities);
}
/**
* Adds the roster item.
*
* @param username
* the username
* @param rosterItemEntity
* the roster item entity
* @throws ServiceException
* the service exception
* @throws UserAlreadyExistsException
* the user already exists exception
* @throws SharedGroupException
* the shared group exception
* @throws UserNotFoundException
* the user not found exception
*/
public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException,
UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
Roster roster = getUserRoster(username);
if (rosterItemEntity.getJid() == null) {
throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
}
JID jid = new JID(rosterItemEntity.getJid());
log("Adding a roster item to: " + rosterItemEntity.getJid());
try {
roster.getRosterItem(jid);
throw new UserAlreadyExistsException(jid.toBareJID());
} catch (UserNotFoundException e) {
// Roster item does not exist. Try to add it.
}
RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(),
rosterItemEntity.getGroups(), false, true);
UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
roster.updateRosterItem(rosterItem);
}
/**
* Update roster item.
*
* @param username
* the username
* @param rosterJid
* the roster jid
* @param rosterItemEntity
* the roster item entity
* @throws ServiceException
* the service exception
* @throws UserNotFoundException
* the user not found exception
* @throws UserAlreadyExistsException
* the user already exists exception
* @throws SharedGroupException
* the shared group exception
*/
public void updateRosterItem(String username, String rosterJid, RosterItemEntity rosterItemEntity)
throws ServiceException, UserNotFoundException, UserAlreadyExistsException, SharedGroupException {
log("Updating a roster item for user: " + username + ", with roster JID: " + rosterJid);
getAndCheckUser(username);
Roster roster = getUserRoster(username);
JID jid = new JID(rosterJid);
RosterItem rosterItem = roster.getRosterItem(jid);
if (rosterItemEntity.getNickname() != null) {
rosterItem.setNickname(rosterItemEntity.getNickname());
}
if (rosterItemEntity.getGroups() != null) {
rosterItem.setGroups(rosterItemEntity.getGroups());
}
UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
roster.updateRosterItem(rosterItem);
}
public Map<String, Integer> syncRosterEntities(String username, RosterEntities entities) throws UserNotFoundException,
ServiceException, UserAlreadyExistsException, SharedGroupException {
Map<String, Integer> ret = new HashMap<>();
ret.put("inserts", 0);
ret.put("updates", 0);
getAndCheckUser(username);
Roster roster = getUserRoster(username);
for(RosterItemEntity rosterItemEntity : entities.getRoster()) {
JID jid = new JID(rosterItemEntity.getJid());
RosterItem rosterItem = roster.getRosterItem(jid);
String key;
if(rosterItem != null) {
key = "updates";
if (rosterItemEntity.getNickname() != null) {
rosterItem.setNickname(rosterItemEntity.getNickname());
}
if (rosterItemEntity.getGroups() != null) {
rosterItem.setGroups(rosterItemEntity.getGroups());
}
}else{
key = "inserts";
rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(),
rosterItemEntity.getGroups(), false, true);
}
UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
roster.updateRosterItem(rosterItem);
int count = ret.get(key);
ret.put(key, count + 1);
}
return ret;
}
/**
* Delete roster item.
*
* @param username
* the username
* @param rosterJid
* the roster jid
* @throws SharedGroupException
* the shared group exception
* @throws ServiceException
* the service exception
*/
public void deleteRosterItem(String username, String rosterJid) throws SharedGroupException, ServiceException {
log("Deleting a roster item for user: " + username + ", with roster JID: " + rosterJid);
getAndCheckUser(username);
Roster roster = getUserRoster(username);
JID jid = new JID(rosterJid);
if (roster.deleteRosterItem(jid, true) == null) {
throw new ServiceException("Roster Item could not deleted", jid.toBareJID(), "RosterItemNotFound",
Response.Status.NOT_FOUND);
}
}
/**
* Gets the user groups.
*
* @param username
* the username
* @return the user groups
* @throws ServiceException
* the service exception
*/
public List<String> getUserGroups(String username) throws ServiceException {
log("Get user groups for user: " + username);
if (username.contains("@")) {
final JID jid = new JID(username);
if (jid.getDomain().equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
username = jid.getNode();
} else {
// Implementing this would require us to iterate over all groups, which is a performance nightmare.
throw new ServiceException("This service cannot be used for non-local users.", username, ExceptionType.GROUP_NOT_FOUND, Response.Status.INTERNAL_SERVER_ERROR);
}
}
User user = getAndCheckUser(username);
Collection<Group> groups = GroupManager.getInstance().getGroups(user);
List<String> groupNames = new ArrayList<>();
for (Group group : groups) {
groupNames.add(group.getName());
}
return groupNames;
}
/**
* Adds the user to group.
*
* @param username
* the username
* @param userGroupsEntity
* the user groups entity
* @throws ServiceException
* the service exception
*/
public void addUserToGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
if (userGroupsEntity != null) {
log("Adding user: " + username + " to groups");
Collection<Group> groups = new ArrayList<>();
for (String groupName : userGroupsEntity.getGroupNames()) {
Group group;
try {
group = GroupManager.getInstance().getGroup(groupName);
log("Adding user: " + username + " to a group: " + groupName);
} catch (GroupNotFoundException e) {
// Create this group
group = GroupController.getInstance().createGroup(new GroupEntity(groupName, ""));
}
groups.add(group);
}
for (Group group : groups) {
group.getMembers().add(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
}
}
}
/**
* Adds the user to group.
*
* @param username the username
* @param groupName the group name
* @throws ServiceException the service exception
*/
public void addUserToGroup(String username, String groupName) throws ServiceException {
log("Adding user: " + username + " to a group: " + groupName);
Group group;
try {
group = GroupManager.getInstance().getGroup(groupName);
} catch (GroupNotFoundException e) {
// Create this group
log("Group: " + groupName + " does not exist. Creating the group");
group = GroupController.getInstance().createGroup(new GroupEntity(groupName, ""));
}
group.getMembers().add(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
}
/**
* Delete user from groups.
*
* @param username
* the username
* @param userGroupsEntity
* the user groups entity
* @throws ServiceException
* the service exception
*/
public void deleteUserFromGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
if (userGroupsEntity != null) {
log("Removing user: " + username + " from groups");
for (String groupName : userGroupsEntity.getGroupNames()) {
log("deleteUserFromGroups, "+username+ ", groupName: "+groupName);
Group group;
try {
group = GroupManager.getInstance().getGroup(groupName);
} catch (GroupNotFoundException e) {
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
Response.Status.NOT_FOUND, e);
}
log("Removing user: " + username + " from the group: " + groupName);
group.getMembers().remove(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
}
}
}
/**
* Delete user from group.
*
* @param username the username
* @param groupName the group name
* @throws ServiceException the service exception
*/
public void deleteUserFromGroup(String username, String groupName) throws ServiceException {
log("Removing user: " + username + " from the group: " + groupName);
Group group;
try {
group = GroupManager.getInstance().getGroup(groupName);
} catch (GroupNotFoundException e) {
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
Response.Status.NOT_FOUND, e);
}
group.getMembers().remove(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
}
/**
* Gets the user entities by property key and or value.
*
* @param propertyKey
* the property key
* @param propertyValue
* the property value (can be null)
* @return the user entities by property
* @throws ServiceException
* the service exception
*/
public UserEntities getUserEntitiesByProperty(String propertyKey, String propertyValue) throws ServiceException {
log("Get user entities by property key : " + propertyKey + "and property value: " + propertyValue);
List<String> usernames = PropertyDAO.getUsernameByProperty(propertyKey, propertyValue);
List<UserEntity> users = new ArrayList<>();
UserEntities userEntities = new UserEntities();
for (String username : usernames) {
users.add(getUserEntity(username));
}
userEntities.setUsers(users);
return userEntities;
}
/**
* Adds the properties.
*
* @param username
* the username
* @param properties
* user properties
* @throws ServiceException
* the service exception
*/
private void addProperties(String username, List<UserProperty> properties) throws ServiceException {
log("Adding a property to user: " + username);
User user = getAndCheckUser(username);
user.getProperties().clear();
if (properties != null) {
for (UserProperty property : properties) {
user.getProperties().put(property.getKey(), property.getValue());
}
}
}
/**
* Gets the and check user.
*
* @param username
* the username
* @return the and check user
* @throws ServiceException
* the service exception
*/
private User getAndCheckUser(String username) throws ServiceException {
JID targetJID = server.createJID(username, null);
if (targetJID.getNode() == null) {
throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND);
}
try {
return userManager.getUser(targetJID.getNode());
} catch (UserNotFoundException e) {
throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e);
}
}
/**
* Gets the user roster.
*
* @param username
* the username
* @return the user roster
* @throws ServiceException
* the service exception
*/
private Roster getUserRoster(String username) throws ServiceException {
log("getUserRoster, "+username);
try {
return rosterManager.getRoster(username);
} catch (UserNotFoundException e) {
throw new ServiceException("Could not get user roster", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e);
}
}
}