/
UserManager.java
1335 lines (1162 loc) · 41.8 KB
/
UserManager.java
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2002-2020 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2020 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) 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, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) 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 OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.netmgt.config;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.TreeMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.opennms.core.xml.JaxbUtils;
import org.opennms.netmgt.config.api.UserConfig;
import org.opennms.netmgt.config.users.Contact;
import org.opennms.netmgt.config.users.DutySchedule;
import org.opennms.netmgt.config.users.Header;
import org.opennms.netmgt.config.users.Password;
import org.opennms.netmgt.config.users.User;
import org.opennms.netmgt.config.users.Userinfo;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.model.OnmsUser;
import org.opennms.netmgt.model.OnmsUserList;
/**
* <p>Abstract UserManager class.</p>
*
* @author <a href="mailto:david@opennms.org">David Hustace</a>
* @author <a href="mailto:brozow@opennms.org">Matt Brozowski</a>
* @author <a href="mailto:ranger@opennms.org">Benjamin Reed</a>
* @author <a href="mailto:jeffg@opennms.org">Jeff Gehlbach</a>
*/
public abstract class UserManager implements UserConfig {
public static final String ALLOW_UNSALTED_PROPERTY = "org.opennms.users.allowUnsalted";
private final boolean m_allowUnsalted;
private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private static final PasswordEncryptor s_passwordEncryptor = new StrongPasswordEncryptor();
private final ReadWriteLock m_readWriteLock = new ReentrantReadWriteLock();
private final Lock m_readLock = m_readWriteLock.readLock();
private final Lock m_writeLock = m_readWriteLock.writeLock();
protected GroupManager m_groupManager;
/**
* A mapping of user IDs to the User objects
*/
protected Map<String, User> m_users;
/**
* The duty schedules for each user
*/
protected Map<String, List<DutySchedule>> m_dutySchedules;
private Header oldHeader;
/**
* <p>Constructor for UserManager.</p>
*
* @param groupManager a {@link org.opennms.netmgt.config.GroupManager} object.
*/
protected UserManager(final GroupManager groupManager) {
m_groupManager = groupManager;
m_allowUnsalted = Boolean.parseBoolean(System.getProperty(ALLOW_UNSALTED_PROPERTY, "true"));
}
/**
* <p>parseXML</p>
*
* @param in a {@link java.io.InputStream} object.
*/
public void parseXML(final InputStream in) {
m_writeLock.lock();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(in);
final Userinfo userinfo = JaxbUtils.unmarshal(Userinfo.class, isr);
oldHeader = userinfo.getHeader();
m_users = new TreeMap<String, User>();
for (final User curUser : userinfo.getUsers()) {
m_users.put(curUser.getUserId(), curUser);
}
_buildDutySchedules(m_users);
} finally {
IOUtils.closeQuietly(isr);
m_writeLock.unlock();
}
}
/**
* Adds a new user and overwrites the "users.xml"
*
* @param name a {@link java.lang.String} object.
* @param details a {@link org.opennms.netmgt.config.users.User} object.
* @throws java.lang.Exception if any.
*/
public void saveUser(final String name, final User details) throws Exception {
update();
m_writeLock.lock();
try {
_writeUser(name, details);
} finally {
m_writeLock.unlock();
}
}
private void _assertSaltAcceptable(final User details) throws Exception {
if (!m_allowUnsalted) {
final Password p = details.getPassword();
if (p != null) {
if (!p.getSalt()) {
throw new IllegalStateException("org.opennms.users.allowUnsaltedPasswords=false, but " + details.getUserId() + " contains an unsalted password.");
}
}
}
}
private void _writeUser(final String name, final User details) throws Exception {
if (name == null || details == null) {
throw new Exception("UserFactory:saveUser null");
} else {
_assertSaltAcceptable(details);
m_users.put(name, details);
}
_saveCurrent();
}
public void save(final OnmsUser onmsUser) throws Exception {
update();
m_writeLock.lock();
try {
User xmlUser = _getUser(onmsUser.getUsername());
if (xmlUser == null) {
xmlUser = new User();
xmlUser.setUserId(onmsUser.getUsername());
}
xmlUser.setFullName(onmsUser.getFullName());
xmlUser.setUserComments(onmsUser.getComments());
// Contact info
_setContact(xmlUser, ContactType.email, onmsUser.getEmail());
final Password pass = new Password();
pass.setEncryptedPassword(onmsUser.getPassword());
pass.setSalt(onmsUser.getPasswordSalted());
xmlUser.setPassword(pass);
if (onmsUser.getDutySchedule() != null) {
xmlUser.setDutySchedules(new ArrayList<String>(onmsUser.getDutySchedule()));
}
if (onmsUser.getRoles() != null) {
xmlUser.setRoles(new ArrayList<String>(onmsUser.getRoles()));
}
_writeUser(onmsUser.getUsername(), xmlUser);
} finally {
m_writeLock.unlock();
}
}
/**
* Builds a mapping between user IDs and duty schedules. These are used by
* Notifd when determining to send a notice to a given user. This helps
* speed up the decision process.
*
* @param users
* the map of users parsed from the XML configuration file
*/
private void _buildDutySchedules(final Map<String,User> users) {
m_dutySchedules = new HashMap<String,List<DutySchedule>>();
for (final Entry<String,User> entry : users.entrySet()) {
final String key = entry.getKey();
final User curUser = entry.getValue();
if (curUser.getDutySchedules().size() > 0) {
final List<DutySchedule> dutyList = new ArrayList<DutySchedule>();
for (final String duty : curUser.getDutySchedules()) {
dutyList.add(new DutySchedule(duty));
}
m_dutySchedules.put(key, dutyList);
}
}
}
/**
* Determines if a user is on duty at a given time. If a user has no duty
* schedules listed in the configuration file, that user is assumed to always be on
* duty.
*
* @param user
* the user id
* @param time
* the time to check for a duty schedule
* @return boolean, true if the user is on duty, false otherwise.
* @throws java.io.IOException if any.
*/
public boolean isUserOnDuty(final String user, final Calendar time) throws IOException {
update();
m_readLock.lock();
try {
// if the user has no duty schedules then he is on duty
if (!m_dutySchedules.containsKey(user))
return true;
for (final DutySchedule curSchedule : m_dutySchedules.get(user)) {
if (curSchedule.isInSchedule(time)) {
return true;
}
}
} finally {
m_readLock.unlock();
}
return false;
}
/**
* Return a <code>Map</code> of usernames to user instances.
*
* @return a {@link java.util.Map} object.
* @throws java.io.IOException if any.
*/
public Map<String, User> getUsers() throws IOException {
update();
m_readLock.lock();
try {
return Collections.unmodifiableMap(m_users);
} finally {
m_readLock.unlock();
}
}
public OnmsUserList getOnmsUserList() throws IOException {
update();
final OnmsUserList list = new OnmsUserList();
m_readLock.lock();
try {
for (final String username : _getUserNames()) {
list.add(_getOnmsUser(username));
}
list.setTotalCount(list.getCount());
return list;
} finally {
m_readLock.unlock();
}
}
public OnmsUser getOnmsUser(final String username) throws IOException {
update();
m_readLock.lock();
try {
return _getOnmsUser(username);
} finally {
m_readLock.unlock();
}
}
private OnmsUser _getOnmsUser(final String username) throws IOException {
final User xmlUser = _getUser(username);
if (xmlUser == null) return null;
final OnmsUser user = new OnmsUser(username);
user.setFullName(trim(xmlUser.getFullName()));
user.setComments(trim(xmlUser.getUserComments()));
user.setPassword(xmlUser.getPassword().getEncryptedPassword());
user.setPasswordSalted(Boolean.valueOf(xmlUser.getPassword().getSalt()));
user.setDutySchedule(xmlUser.getDutySchedules());
user.setRoles(xmlUser.getRoles());
user.setEmail(_getContactInfo(xmlUser, ContactType.email));
return user;
}
private String trim(final Optional<String> text) {
return (text == null || !text.isPresent())? null : text.get().trim();
}
private Contact _getContact(final String userId, final ContactType contactType) {
User user = _getUser(userId);
if (user != null && contactType != null) {
for (Contact eachContact : user.getContacts()) {
if (contactType.name().equals(eachContact.getType())) {
return eachContact;
}
}
}
return null;
}
/**
* Returns a boolean indicating if the user name appears in the XML file
*
* @return true if the user exists in the XML file, false otherwise
* @param userName a {@link java.lang.String} object.
* @throws java.io.IOException if any.
*/
public boolean hasUser(final String userName) throws IOException {
update();
m_readLock.lock();
try {
return m_users.containsKey(userName);
} finally {
m_readLock.unlock();
}
}
/**
* <p>getUserNames</p>
*
* @return a {@link java.util.List} object.
* @throws java.io.IOException if any.
*/
public List<String> getUserNames() throws IOException {
update();
m_readLock.lock();
try {
return _getUserNames();
} finally {
m_readLock.unlock();
}
}
private List<String> _getUserNames() {
final List<String> userNames = new ArrayList<String>();
userNames.addAll(m_users.keySet());
return userNames;
}
/**
* Get a user by name
*
* @param name
* the name of the user to return
* @return the user specified by name
* @throws java.io.IOException if any.
*/
public User getUser(final String name) throws IOException {
update();
m_readLock.lock();
try {
return _getUser(name);
} finally {
m_readLock.unlock();
}
}
private User _getUser(final String name) {
return m_users.get(name);
}
/**
* Get a user's telephone PIN by name
*
* @param name
* the name of the user to return
* @return the telephone PIN of the user specified by name
* @throws java.io.IOException if any.
*/
public String getTuiPin(final String name) throws IOException {
update();
m_readLock.lock();
try {
return m_users.get(name).getTuiPin().orElse(null);
} finally {
m_readLock.unlock();
}
}
/**
* Get a user's telephone PIN by User object
*
* @return the telephone PIN of the user specified by user
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @throws java.io.IOException if any.
*/
public String getTuiPin(final User user) throws IOException {
update();
m_readLock.lock();
try {
return m_users.get(user.getUserId()).getTuiPin().orElse(null);
} finally {
m_readLock.unlock();
}
}
/**
* Get a user's microblog username by username
*
* @param name
* the username of the user whose microblog username should be returned
* @return the microblog username of the specified user
* @throws java.io.IOException if any.
* @throws java.io.FileNotFoundException if any.
*/
public String getMicroblogName(final String name) throws FileNotFoundException, IOException {
return getContactInfo(name, ContactType.microblog.toString());
}
/**
* Get a user's microblog username by User
*
* @param user
* the user object of the user whose microblog username should be returned
* @return the microblog username of the specified user
* @throws java.io.IOException if any.
* @throws java.io.FileNotFoundException if any.
*/
public String getMicroblogName(final User user) throws FileNotFoundException, IOException {
return getContactInfo(user, ContactType.microblog.toString());
}
public void setContactInfo(final String userId, final ContactType contactType, final String contactValue) throws Exception {
update();
m_writeLock.lock();
try {
final User user = _getUser(userId);
if (user != null) {
_setContact(user, contactType, contactValue);
}
_saveCurrent();
} finally {
m_writeLock.unlock();
}
}
private void _setContact(final User user, final ContactType contactType, final String value) {
if (user != null && !StringUtils.isEmpty(value)) {
Contact contact = _getContact(user.getUserId(), contactType);
if (contact == null) {
contact = new Contact();
user.addContact(contact);
}
contact.setInfo(value);
contact.setType(contactType.name());
}
}
/**
* @see {@link #getContactInfo(String, String)}
*/
public String getContactInfo(final String userId, final ContactType contactType) throws IOException {
if (userId == null || contactType == null) return null;
return getContactInfo(userId, contactType.name());
}
/**
* Get the contact info given a command string
*
* @param userID
* the name of the user
* @param command
* the command to look up the contact info for
* @return the contact information
* @throws java.io.IOException if any.
*/
public String getContactInfo(final String userID, final String command) throws IOException {
update();
m_readLock.lock();
try {
final User user = m_users.get(userID);
return _getContactInfo(user, command);
} finally {
m_readLock.unlock();
}
}
/**
* <p>getContactInfo</p>
*
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @param command a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
* @throws java.io.IOException if any.
*/
public String getContactInfo(final User user, final String command) throws IOException {
update();
m_readLock.lock();
try {
return _getContactInfo(user, command);
} finally {
m_readLock.unlock();
}
}
private String _getContactInfo(final User user, final ContactType contactType) {
return _getContactInfo(user, contactType.name());
}
private String _getContactInfo(final User user, final String command) {
if (user == null) return "";
for (final Contact contact : user.getContacts()) {
if (contact != null && contact.getType().equals(command)) {
return contact.getInfo().orElse("");
}
}
return "";
}
/**
* Get the contact service provider, given a command string
*
* @param userID
* the name of the user
* @param command
* the command to look up the contact info for
* @return the contact information
* @throws java.io.IOException if any.
*/
public String getContactServiceProvider(final String userID, final String command) throws IOException {
update();
m_readLock.lock();
try {
final User user = m_users.get(userID);
return _getContactServiceProvider(user, command);
} finally {
m_readLock.unlock();
}
}
/**
* <p>getContactServiceProvider</p>
*
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @param command a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
* @throws java.io.IOException if any.
*/
public String getContactServiceProvider(final User user, final String command) throws IOException {
update();
m_readLock.lock();
try {
return _getContactServiceProvider(user, command);
} finally {
m_readLock.unlock();
}
}
private String _getContactServiceProvider(final User user, final String command) {
if (user == null) return "";
for (final Contact contact : user.getContacts()) {
if (contact != null && contact.getType().equals(command)) {
return contact.getServiceProvider().orElse("");
}
}
return "";
}
/**
* Get a email by name
*
* @param userID
* the user ID of the user to return
* @return String the email specified by name
* @throws java.io.IOException if any.
*/
public String getEmail(final String userID) throws IOException {
return getContactInfo(userID, ContactType.email.toString());
}
/**
* Get a email by user
*
* @param user the user to find the email for
* @return String the email specified by name
* @throws java.io.IOException if any.
*/
public String getEmail(final User user) throws IOException {
return getContactInfo(user, ContactType.email.toString());
}
/**
* Get a pager email by name
*
* @param userID
* the user ID of the user to return
* @return String the pager email
* @throws java.io.IOException if any.
*/
public String getPagerEmail(final String userID) throws IOException {
return getContactInfo(userID, ContactType.pagerEmail.toString());
}
/**
* Get a pager email by user
*
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @return String the pager email
* @throws java.io.IOException if any.
*/
public String getPagerEmail(final User user) throws IOException {
return getContactInfo(user, ContactType.pagerEmail.toString());
}
/**
* Get a numeric pin
*
* @param userID
* the user ID of the user to return
* @return String the numeric pin
* @throws java.io.IOException if any.
*/
public String getNumericPin(final String userID) throws IOException {
return getContactInfo(userID, ContactType.numericPage.toString());
}
/**
* Get a numeric pin
*
* @return String the numeric pin
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @throws java.io.IOException if any.
*/
public String getNumericPin(final User user) throws IOException {
return getContactInfo(user, ContactType.numericPage.toString());
}
/**
* Get an XMPP address by name
*
* @param userID
* the user ID of the user to return
* @return String the XMPP address
* @throws java.io.IOException if any.
*/
public String getXMPPAddress(final String userID) throws IOException {
update();
m_readLock.lock();
try {
final User user = m_users.get(userID);
return _getXMPPAddress(user);
} finally {
m_readLock.unlock();
}
}
/**
* Get an XMPP address by name
*
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @return String the XMPP address
* @throws java.io.IOException if any.
*/
public String getXMPPAddress(final User user) throws IOException {
update();
m_readLock.lock();
try {
return _getXMPPAddress(user);
} finally {
m_readLock.unlock();
}
}
private String _getXMPPAddress(final User user) {
if (user == null)
return "";
for (final Contact contact : user.getContacts()) {
if (contact != null && contact.getType().equals(ContactType.xmppAddress.toString())) {
return contact.getInfo().orElse("");
}
}
return "";
}
/**
* Get a numeric service provider
*
* @param userID
* the user ID of the user to return
* @return String the service provider
* @throws java.io.IOException if any.
*/
public String getNumericPage(final String userID) throws IOException {
return getContactServiceProvider(userID, ContactType.numericPage.toString());
}
/**
* Get a numeric service provider
*
* @return String the service provider
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @throws java.io.IOException if any.
*/
public String getNumericPage(final User user) throws IOException {
return getContactServiceProvider(user, ContactType.numericPage.toString());
}
/**
* Get a text pin
*
* @param userID
* the user ID of the user to return
* @return String the text pin
* @throws java.io.IOException if any.
*/
public String getTextPin(final String userID) throws IOException {
return getContactInfo(userID, ContactType.textPage.toString());
}
/**
* Get a text pin
*
* @return String the text pin
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @throws java.io.IOException if any.
*/
public String getTextPin(final User user) throws IOException {
return getContactInfo(user, ContactType.textPage.toString());
}
/**
* Get a Text Page Service Provider
*
* @param userID
* the user ID of the user to return
* @return String the text page service provider.
* @throws java.io.IOException if any.
*/
public String getTextPage(final String userID) throws IOException {
return getContactServiceProvider(userID, ContactType.textPage.toString());
}
/**
* Get a Text Page Service Provider
*
* @return String the text page service provider.
* @param user a {@link org.opennms.netmgt.config.users.User} object.
* @throws java.io.IOException if any.
*/
public String getTextPage(final User user) throws IOException {
return getContactServiceProvider(user, ContactType.textPage.toString());
}
/**
* Get a work phone number
*
* @param userID
* the user ID of the user to return
* @return String the work phone number
* @throws java.io.IOException if any.
*/
public String getWorkPhone(final String userID) throws IOException {
return getContactInfo(userID, ContactType.workPhone.toString());
}
/**
* Get a work phone number
*
* @return String the work phone number
* @throws java.io.IOException if any.
* @param user a {@link org.opennms.netmgt.config.users.User} object.
*/
public String getWorkPhone(final User user) throws IOException {
return getContactInfo(user, ContactType.workPhone.toString());
}
/**
* Get a mobile phone number
*
* @param userID
* the user ID of the user to return
* @return String the mobile phone number
* @throws java.io.IOException if any.
*/
public String getMobilePhone(final String userID) throws IOException {
return getContactInfo(userID, ContactType.mobilePhone.toString());
}
/**
* Get a mobile phone number
*
* @return String the mobile phone number
* @throws java.io.IOException if any.
* @param user a {@link org.opennms.netmgt.config.users.User} object.
*/
public String getMobilePhone(final User user) throws IOException {
return getContactInfo(user, ContactType.mobilePhone.toString());
}
/**
* Get a home phone number
*
* @param userID
* the user ID of the user to return
* @return String the home phone number
* @throws java.io.IOException if any.
*/
public String getHomePhone(final String userID) throws IOException {
return getContactInfo(userID, ContactType.homePhone.toString());
}
/**
* Get a home phone number
*
* @return String the home phone number
* @throws java.io.IOException if any.
* @param user a {@link org.opennms.netmgt.config.users.User} object.
*/
public String getHomePhone(final User user) throws IOException {
return getContactInfo(user, ContactType.homePhone.toString());
}
/**
* <p>saveUsers</p>
*
* @param usersList a {@link java.util.Collection} object.
* @throws java.lang.Exception if any.
*/
public void saveUsers(final Collection<User> usersList) throws Exception {
update();
m_writeLock.lock();
try {
if (!m_allowUnsalted) {
for (final User details : usersList) {
_assertSaltAcceptable(details);
}
}
// clear out the internal structure and reload it
m_users.clear();
for (final User curUser : usersList) {
m_users.put(curUser.getUserId(), curUser);
}
} finally {
m_writeLock.unlock();
}
}
/**
* Removes the user from the list of users. Then overwrites to the
* "users.xml"
*
* @param name a {@link java.lang.String} object.
* @throws java.lang.Exception if any.
*/
public void deleteUser(final String name) throws Exception {
m_writeLock.lock();
try {
// Check if the user exists
if (m_users.containsKey(name)) {
// Delete the user in the user map.
m_users.remove(name);
// Refresh the groups config first
m_groupManager.update();
// Delete the user in the group.
m_groupManager.deleteUser(name);
// Delete the user in the view.
// viewFactory.deleteUser(name);
} else {
throw new Exception("UserFactory:delete The old user name " + name + " is not found");
}
_saveCurrent();
} finally {
m_writeLock.unlock();
}
}
/**
* Saves into "users.xml" file
*/
private void _saveCurrent() throws Exception {
final List<User> users = new ArrayList<>(m_users.values());
final Userinfo userinfo = new Userinfo();
userinfo.setUsers(users);
final Header header = oldHeader;
if (header != null) {
header.setCreated(EventConstants.formatToString(new Date()));
userinfo.setHeader(header);
}
oldHeader = header;
// marshal to a string first, then write the string to the file. This
// way the original configuration
// isn't lost if the XML from the marshal is hosed.
final StringWriter stringWriter = new StringWriter();
JaxbUtils.marshal(userinfo, stringWriter);
final String writerString = stringWriter.toString();
saveXML(writerString);
}
/**
* <p>saveXML</p>
*
* @param writerString a {@link java.lang.String} object.
* @throws java.io.IOException if any.
*/
protected abstract void saveXML(final String writerString) throws IOException ;
/**
* When this method is called users name is changed, so also is the username
* belonging to the group and the view. Also overwrites the "users.xml" file
*
* @param oldName a {@link java.lang.String} object.
* @param newName a {@link java.lang.String} object.
* @throws java.lang.Exception if any.
*/
public void renameUser(final String oldName, final String newName) throws Exception {
update();
m_writeLock.lock();
try {
// Get the old data
if (m_users.containsKey(oldName)) {
final User data = m_users.get(oldName);
if (data == null) {
m_users.remove(oldName);
throw new Exception("UserFactory:rename the data contained for old user " + oldName + " is null");
} else {
if (m_users.containsKey(newName)) {
throw new Exception("UserFactory: cannot rename user " + oldName + ". An user with the given name " + newName + " already exists");
}