-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
InternalFlatFileRealm.java
819 lines (739 loc) · 27.5 KB
/
InternalFlatFileRealm.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.server.security.enterprise.auth;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.credential.AllowAllCredentialsMatcher;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import org.neo4j.commandline.admin.security.SetDefaultAdminCommand;
import org.neo4j.kernel.api.exceptions.InvalidArgumentsException;
import org.neo4j.kernel.api.security.AuthToken;
import org.neo4j.internal.kernel.api.security.AuthenticationResult;
import org.neo4j.kernel.api.security.PasswordPolicy;
import org.neo4j.kernel.api.security.exception.InvalidAuthTokenException;
import org.neo4j.server.security.auth.LegacyCredential;
import org.neo4j.kernel.impl.security.User;
import org.neo4j.scheduler.Group;
import org.neo4j.scheduler.JobHandle;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.server.security.auth.AuthenticationStrategy;
import org.neo4j.server.security.auth.ListSnapshot;
import org.neo4j.server.security.auth.UserRepository;
import org.neo4j.server.security.auth.exception.ConcurrentModificationException;
import org.neo4j.server.security.enterprise.auth.plugin.api.PredefinedRoles;
import org.neo4j.server.security.enterprise.configuration.SecuritySettings;
import org.neo4j.string.UTF8;
import static java.lang.String.format;
import static java.util.Collections.emptySet;
/**
* Shiro realm wrapping FileUserRepository and FileRoleRepository
* @deprecated This class will be removed in the next major release. Please consider using SystemGraphRealm instead.
*/
@Deprecated
public class InternalFlatFileRealm extends AuthorizingRealm implements RealmLifecycle, EnterpriseUserManager,
ShiroAuthorizationInfoProvider
{
/**
* This flag is used in the same way as User.PASSWORD_CHANGE_REQUIRED, but it's
* placed here because of user suspension not being a part of community edition
*/
static final String IS_SUSPENDED = "is_suspended";
private static int MAX_READ_ATTEMPTS = 10;
private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final UserRepository initialUserRepository;
private final UserRepository defaultAdminRepository;
private final PasswordPolicy passwordPolicy;
private final AuthenticationStrategy authenticationStrategy;
private final boolean authenticationEnabled;
private final boolean authorizationEnabled;
private final JobScheduler jobScheduler;
private volatile JobHandle reloadJobHandle;
@Deprecated
public InternalFlatFileRealm( UserRepository userRepository, RoleRepository roleRepository,
PasswordPolicy passwordPolicy, AuthenticationStrategy authenticationStrategy,
JobScheduler jobScheduler, UserRepository initialUserRepository,
UserRepository defaultAdminRepository )
{
this( userRepository,roleRepository, passwordPolicy, authenticationStrategy, true, true,
jobScheduler, initialUserRepository, defaultAdminRepository );
}
InternalFlatFileRealm( UserRepository userRepository, RoleRepository roleRepository,
PasswordPolicy passwordPolicy, AuthenticationStrategy authenticationStrategy,
boolean authenticationEnabled, boolean authorizationEnabled, JobScheduler jobScheduler,
UserRepository initialUserRepository, UserRepository defaultAdminRepository )
{
super();
setName( SecuritySettings.NATIVE_REALM_NAME );
this.userRepository = userRepository;
this.roleRepository = roleRepository;
this.initialUserRepository = initialUserRepository;
this.defaultAdminRepository = defaultAdminRepository;
this.passwordPolicy = passwordPolicy;
this.authenticationStrategy = authenticationStrategy;
this.authenticationEnabled = authenticationEnabled;
this.authorizationEnabled = authorizationEnabled;
this.jobScheduler = jobScheduler;
setAuthenticationCachingEnabled( false ); // NOTE: If this is ever changed to true it is not secure to use
// AllowAllCredentialsMatcher anymore
setAuthorizationCachingEnabled( false );
setCredentialsMatcher( new AllowAllCredentialsMatcher() ); // Since we do not cache authentication info we can
// disable the credentials matcher
setRolePermissionResolver( PredefinedRolesBuilder.rolePermissionResolver );
}
@Deprecated
@Override
public void initialize() throws Throwable
{
initialUserRepository.init();
defaultAdminRepository.init();
userRepository.init();
roleRepository.init();
}
@Deprecated
@Override
public void start() throws Throwable
{
initialUserRepository.start();
defaultAdminRepository.start();
userRepository.start();
roleRepository.start();
Set<String> addedDefaultUsers = ensureDefaultUsers();
ensureDefaultRoles( addedDefaultUsers );
scheduleNextFileReload();
}
protected void scheduleNextFileReload()
{
reloadJobHandle = jobScheduler.schedule( Group.NATIVE_SECURITY,
this::readFilesFromDisk,
10, TimeUnit.SECONDS );
}
private void readFilesFromDisk()
{
try
{
readFilesFromDisk( MAX_READ_ATTEMPTS, new LinkedList<>() );
}
finally
{
scheduleNextFileReload();
}
}
private void readFilesFromDisk( int attemptLeft, java.util.List<String> failures )
{
if ( attemptLeft < 0 )
{
throw new RuntimeException( "Unable to load valid flat file repositories! Attempts failed with:\n\t" +
String.join( "\n\t", failures ) );
}
try
{
final boolean valid;
final boolean needsUpdate;
synchronized ( this )
{
ListSnapshot<User> users = userRepository.getPersistedSnapshot();
ListSnapshot<RoleRecord> roles = roleRepository.getPersistedSnapshot();
needsUpdate = users.fromPersisted() || roles.fromPersisted();
valid = needsUpdate && RoleRepository.validate( users.values(), roles.values() );
if ( valid )
{
if ( users.fromPersisted() )
{
userRepository.setUsers( users );
}
if ( roles.fromPersisted() )
{
roleRepository.setRoles( roles );
}
}
}
if ( needsUpdate && !valid )
{
failures.add( "Role-auth file combination not valid." );
Thread.sleep( 10 );
readFilesFromDisk( attemptLeft - 1, failures );
}
}
catch ( IOException | IllegalStateException | InterruptedException | InvalidArgumentsException e )
{
failures.add( e.getMessage() );
readFilesFromDisk( attemptLeft - 1, failures );
}
}
/* Adds neo4j user if no users exist */
private Set<String> ensureDefaultUsers() throws Throwable
{
if ( authenticationEnabled || authorizationEnabled )
{
if ( userRepository.numberOfUsers() == 0 )
{
User neo4j = newUser( INITIAL_USER_NAME, UTF8.encode( INITIAL_PASSWORD ), true );
if ( initialUserRepository.numberOfUsers() > 0 )
{
User initUser = initialUserRepository.getUserByName( INITIAL_USER_NAME );
if ( initUser != null )
{
userRepository.update( neo4j, initUser );
}
}
return Collections.singleton( INITIAL_USER_NAME );
}
}
return Collections.emptySet();
}
/* Builds all predefined roles if no roles exist. Adds 'neo4j' to admin role if no admin is assigned */
private void ensureDefaultRoles( Set<String> addedDefaultUsers ) throws IOException, InvalidArgumentsException
{
if ( authenticationEnabled || authorizationEnabled )
{
List<String> newAdmins = new LinkedList<>( addedDefaultUsers );
if ( numberOfRoles() == 0 )
{
if ( newAdmins.isEmpty() )
{
Set<String> usernames = userRepository.getAllUsernames();
if ( defaultAdminRepository.numberOfUsers() > 1 )
{
throw new InvalidArgumentsException(
"No roles defined, and multiple users defined as default admin user." +
" Please use `neo4j-admin " + SetDefaultAdminCommand.COMMAND_NAME +
"` to select a valid admin." );
}
else if ( defaultAdminRepository.numberOfUsers() == 1 )
{
// We currently support only one default admin
String newAdminUsername = defaultAdminRepository.getAllUsernames().iterator().next();
if ( userRepository.getUserByName( newAdminUsername ) == null )
{
throw new InvalidArgumentsException(
"No roles defined, and default admin user '" + newAdminUsername +
"' does not exist. Please use `neo4j-admin " +
SetDefaultAdminCommand.COMMAND_NAME + "` to select a valid admin." );
}
newAdmins.add( newAdminUsername );
}
else if ( usernames.size() == 1 )
{
newAdmins.add( usernames.iterator().next() );
}
else if ( usernames.contains( INITIAL_USER_NAME ) )
{
newAdmins.add( INITIAL_USER_NAME );
}
else
{
throw new InvalidArgumentsException(
"No roles defined, and cannot determine which user should be admin. " +
"Please use `neo4j-admin " + SetDefaultAdminCommand.COMMAND_NAME +
"` to select an " + "admin." );
}
}
for ( String role : PredefinedRolesBuilder.roles.keySet() )
{
newRole( role );
}
}
for ( String username : newAdmins )
{
addRoleToUser( PredefinedRoles.ADMIN, username );
}
}
}
@Deprecated
@Override
public void stop() throws Throwable
{
initialUserRepository.stop();
defaultAdminRepository.stop();
userRepository.stop();
roleRepository.stop();
if ( reloadJobHandle != null )
{
reloadJobHandle.cancel( true );
reloadJobHandle = null;
}
}
@Deprecated
@Override
public void shutdown() throws Throwable
{
initialUserRepository.shutdown();
defaultAdminRepository.shutdown();
userRepository.shutdown();
roleRepository.shutdown();
setCacheManager( null );
}
@Deprecated
@Override
public boolean supports( AuthenticationToken token )
{
try
{
if ( token instanceof ShiroAuthToken )
{
ShiroAuthToken shiroAuthToken = (ShiroAuthToken) token;
return shiroAuthToken.getScheme().equals( AuthToken.BASIC_SCHEME ) &&
(shiroAuthToken.supportsRealm( AuthToken.NATIVE_REALM ));
}
return false;
}
catch ( InvalidAuthTokenException e )
{
return false;
}
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals )
{
if ( !authorizationEnabled )
{
return null;
}
String username = (String) getAvailablePrincipal( principals );
if ( username == null )
{
return null;
}
User user = userRepository.getUserByName( username );
if ( user == null )
{
return null;
}
if ( user.passwordChangeRequired() || user.hasFlag( IS_SUSPENDED ) )
{
return new SimpleAuthorizationInfo();
}
else
{
Set<String> roles = roleRepository.getRoleNamesByUsername( user.name() );
return new SimpleAuthorizationInfo( roles );
}
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token ) throws AuthenticationException
{
if ( !authenticationEnabled )
{
return null;
}
ShiroAuthToken shiroAuthToken = (ShiroAuthToken) token;
String username;
byte[] password;
try
{
username = AuthToken.safeCast( AuthToken.PRINCIPAL, shiroAuthToken.getAuthTokenMap() );
password = AuthToken.safeCastCredentials( AuthToken.CREDENTIALS, shiroAuthToken.getAuthTokenMap() );
}
catch ( InvalidAuthTokenException e )
{
throw new UnsupportedTokenException( e );
}
User user = userRepository.getUserByName( username );
if ( user == null )
{
throw new UnknownAccountException();
}
AuthenticationResult result = authenticationStrategy.authenticate( user, password );
switch ( result )
{
case FAILURE:
throw new IncorrectCredentialsException();
case TOO_MANY_ATTEMPTS:
throw new ExcessiveAttemptsException();
default:
break;
}
if ( user.hasFlag( InternalFlatFileRealm.IS_SUSPENDED ) )
{
throw new DisabledAccountException( "User '" + user.name() + "' is suspended." );
}
if ( user.passwordChangeRequired() )
{
result = AuthenticationResult.PASSWORD_CHANGE_REQUIRED;
}
// NOTE: We do not cache the authentication info using the Shiro cache manager,
// so all authentication request will go through this method.
// Hence the credentials matcher is set to AllowAllCredentialsMatcher,
// and we do not need to store hashed credentials in the AuthenticationInfo.
return new ShiroAuthenticationInfo( user.name(), getName(), result );
}
@Deprecated
@Override
public AuthorizationInfo getAuthorizationInfoSnapshot( PrincipalCollection principalCollection )
{
return getAuthorizationInfo( principalCollection );
}
private int numberOfUsers()
{
return userRepository.numberOfUsers();
}
private int numberOfRoles()
{
return roleRepository.numberOfRoles();
}
@Deprecated
@Override
public User newUser( String username, byte[] initialPassword, boolean requirePasswordChange )
throws IOException, InvalidArgumentsException
{
try
{
userRepository.assertValidUsername( username );
passwordPolicy.validatePassword( initialPassword );
User user = new User.Builder()
.withName( username )
.withCredentials( LegacyCredential.forPassword( initialPassword ) )
.withRequiredPasswordChange( requirePasswordChange )
.build();
synchronized ( this )
{
userRepository.create( user );
}
return user;
}
finally
{
// Clear password
if ( initialPassword != null )
{
Arrays.fill( initialPassword, (byte) 0 );
}
}
}
@Deprecated
@Override
public void newRole( String roleName, String... usernames ) throws IOException, InvalidArgumentsException
{
roleRepository.assertValidRoleName( roleName );
for ( String username : usernames )
{
userRepository.assertValidUsername( username );
}
SortedSet<String> userSet = new TreeSet<>( Arrays.asList( usernames ) );
RoleRecord role = new RoleRecord.Builder().withName( roleName ).withUsers( userSet ).build();
synchronized ( this )
{
for ( String username : usernames )
{
getUser( username ); // assert that user exists
}
roleRepository.create( role );
}
}
@Deprecated
@Override
public boolean deleteRole( String roleName ) throws IOException, InvalidArgumentsException
{
assertNotPredefinedRoleName( roleName );
boolean result = false;
synchronized ( this )
{
RoleRecord role = getRole( roleName ); // asserts role name exists
if ( roleRepository.delete( role ) )
{
result = true;
}
else
{
// We should not get here, but if we do the assert will fail and give a nice error msg
assertRoleExists( roleName );
}
}
return result;
}
private RoleRecord getRole( String roleName ) throws InvalidArgumentsException
{
RoleRecord role = roleRepository.getRoleByName( roleName );
if ( role == null )
{
throw new InvalidArgumentsException( "Role '" + roleName + "' does not exist." );
}
return role;
}
@Deprecated
@Override
public void assertRoleExists( String roleName ) throws InvalidArgumentsException
{
getRole( roleName );
}
private RoleRecord silentlyGetRole( String roleName )
{
return roleRepository.getRoleByName( roleName );
}
@Deprecated
@Override
public void addRoleToUser( String roleName, String username ) throws IOException, InvalidArgumentsException
{
roleRepository.assertValidRoleName( roleName );
userRepository.assertValidUsername( username );
synchronized ( this )
{
getUser( username );
RoleRecord role = getRole( roleName );
RoleRecord newRole = role.augment().withUser( username ).build();
try
{
roleRepository.update( role, newRole );
}
catch ( ConcurrentModificationException e )
{
// Try again
addRoleToUser( roleName, username );
}
}
clearCachedAuthorizationInfoForUser( username );
}
@Deprecated
@Override
public void removeRoleFromUser( String roleName, String username ) throws IOException, InvalidArgumentsException
{
roleRepository.assertValidRoleName( roleName );
userRepository.assertValidUsername( username );
synchronized ( this )
{
getUser( username );
RoleRecord role = getRole( roleName );
RoleRecord newRole = role.augment().withoutUser( username ).build();
try
{
roleRepository.update( role, newRole );
}
catch ( ConcurrentModificationException e )
{
// Try again
removeRoleFromUser( roleName, username );
}
}
clearCachedAuthorizationInfoForUser( username );
}
@Deprecated
@Override
public boolean deleteUser( String username ) throws IOException, InvalidArgumentsException
{
synchronized ( this )
{
User user = getUser( username ); // throws if user does not exists
removeUserFromAllRoles( username ); // performed first to always maintain auth-roles repo consistency
userRepository.delete( user ); // this will not fail as we know the user exists in this lock
// assuming no one messes with the user and role repositories
// outside this instance
}
clearCacheForUser( username );
return true;
}
@Deprecated
@Override
public User getUser( String username ) throws InvalidArgumentsException
{
User u = userRepository.getUserByName( username );
if ( u == null )
{
throw new InvalidArgumentsException( "User '" + username + "' does not exist." );
}
return u;
}
@Deprecated
@Override
public User silentlyGetUser( String username )
{
return userRepository.getUserByName( username );
}
@Deprecated
@Override
public void setUserPassword( String username, byte[] password, boolean requirePasswordChange )
throws IOException, InvalidArgumentsException
{
try
{
User existingUser = getUser( username );
passwordPolicy.validatePassword( password );
if ( existingUser.credentials().matchesPassword( password ) )
{
throw new InvalidArgumentsException( "Old password and new password cannot be the same." );
}
try
{
User updatedUser = existingUser.augment()
.withCredentials( LegacyCredential.forPassword( password ) )
.withRequiredPasswordChange( requirePasswordChange )
.build();
synchronized ( this )
{
userRepository.update( existingUser, updatedUser );
}
}
catch ( ConcurrentModificationException e )
{
// try again
setUserPassword( username, password, requirePasswordChange );
}
clearCacheForUser( username );
}
finally
{
// Clear password
if ( password != null )
{
Arrays.fill( password, (byte) 0 );
}
}
}
@Deprecated
@Override
public void suspendUser( String username ) throws IOException, InvalidArgumentsException
{
User user = getUser( username );
if ( !user.hasFlag( IS_SUSPENDED ) )
{
User suspendedUser = user.augment().withFlag( IS_SUSPENDED ).build();
try
{
synchronized ( this )
{
userRepository.update( user, suspendedUser );
}
}
catch ( ConcurrentModificationException e )
{
// Try again
suspendUser( username );
}
}
clearCacheForUser( username );
}
@Deprecated
@Override
public void activateUser( String username, boolean requirePasswordChange )
throws IOException, InvalidArgumentsException
{
User user = getUser( username );
if ( user.hasFlag( IS_SUSPENDED ) )
{
User activatedUser = user.augment()
.withoutFlag( IS_SUSPENDED )
.withRequiredPasswordChange( requirePasswordChange )
.build();
try
{
synchronized ( this )
{
userRepository.update( user, activatedUser );
}
}
catch ( ConcurrentModificationException e )
{
// Try again
activateUser( username, requirePasswordChange );
}
}
clearCacheForUser( username );
}
@Deprecated
@Override
public Set<String> getAllRoleNames()
{
return roleRepository.getAllRoleNames();
}
@Deprecated
@Override
public Set<String> getRoleNamesForUser( String username ) throws InvalidArgumentsException
{
getUser( username );
return roleRepository.getRoleNamesByUsername( username );
}
@Deprecated
@Override
public Set<String> silentlyGetRoleNamesForUser( String username )
{
return roleRepository.getRoleNamesByUsername( username );
}
@Deprecated
@Override
public Set<String> getUsernamesForRole( String roleName ) throws InvalidArgumentsException
{
RoleRecord role = getRole( roleName );
return role.users();
}
@Deprecated
@Override
public Set<String> silentlyGetUsernamesForRole( String roleName )
{
RoleRecord role = silentlyGetRole( roleName );
return role == null ? emptySet() : role.users();
}
@Deprecated
@Override
public Set<String> getAllUsernames()
{
return userRepository.getAllUsernames();
}
// this is only used from already synchronized code blocks
private void removeUserFromAllRoles( String username ) throws IOException
{
try
{
roleRepository.removeUserFromAllRoles( username );
}
catch ( ConcurrentModificationException e )
{
// Try again
removeUserFromAllRoles( username );
}
}
private void assertNotPredefinedRoleName( String roleName ) throws InvalidArgumentsException
{
if ( roleName != null && PredefinedRolesBuilder.roles.keySet().contains( roleName ) )
{
throw new InvalidArgumentsException(
format( "'%s' is a predefined role and can not be deleted.", roleName ) );
}
}
private void clearCachedAuthorizationInfoForUser( String username )
{
clearCachedAuthorizationInfo( new SimplePrincipalCollection( username, this.getName() ) );
}
private void clearCacheForUser( String username )
{
clearCache( new SimplePrincipalCollection( username, this.getName() ) );
}
}