Skip to content

Commit 361b8cc

Browse files
committed
Few more improvements for nullability
Expect the domain object to be non-null in SecurityService. Make UserDetailManager.getCurrentUsername() non-null Fix getGroupsUserCanEdit() to pass the supplied username to getGroupsUserCanView(), otherwise the groups are based on the current user.
1 parent a199f9b commit 361b8cc

3 files changed

Lines changed: 13 additions & 34 deletions

File tree

gsec/src/main/java/gemma/gsec/SecurityServiceImpl.java

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public List<String> getGroupAuthoritiesNameFromGroupName( String groupName ) {
261261
@Override
262262
@Transactional(readOnly = true)
263263
public <T extends Securable> Map<T, Collection<String>> getGroupsEditableBy( Collection<T> securables ) {
264-
Collection<String> groupNames = getGroupsUserCanView();
264+
Collection<String> groupNames = getGroupsUserCanView( userDetailsManager.getCurrentUsername() );
265265

266266
Map<T, Collection<String>> result = new HashMap<>( securables.size() );
267267

@@ -278,7 +278,7 @@ public <T extends Securable> Map<T, Collection<String>> getGroupsEditableBy( Col
278278
@Override
279279
@Transactional(readOnly = true)
280280
public Collection<String> getGroupsEditableBy( Securable s ) {
281-
Collection<String> groupNames = getGroupsUserCanView();
281+
Collection<String> groupNames = getGroupsUserCanView( userDetailsManager.getCurrentUsername() );
282282

283283
Collection<String> result = new HashSet<>();
284284

@@ -299,7 +299,7 @@ public <T extends Securable> Map<T, Collection<String>> getGroupsReadableBy( Col
299299

300300
if ( securables.isEmpty() ) return result;
301301

302-
Collection<String> groupNames = getGroupsUserCanView();
302+
Collection<String> groupNames = getGroupsUserCanView( userDetailsManager.getCurrentUsername() );
303303

304304
for ( String groupName : groupNames ) {
305305
populateGroupPermissions( result, groupName,
@@ -314,7 +314,7 @@ public <T extends Securable> Map<T, Collection<String>> getGroupsReadableBy( Col
314314
@Override
315315
@Transactional(readOnly = true)
316316
public Collection<String> getGroupsReadableBy( Securable s ) {
317-
Collection<String> groupNames = getGroupsUserCanView();
317+
Collection<String> groupNames = getGroupsUserCanView( userDetailsManager.getCurrentUsername() );
318318

319319
Collection<String> result = new HashSet<>();
320320

@@ -330,7 +330,7 @@ public Collection<String> getGroupsReadableBy( Securable s ) {
330330
@Override
331331
@Transactional(readOnly = true)
332332
public Collection<String> getGroupsUserCanEdit( String userName ) {
333-
Collection<String> groupNames = getGroupsUserCanView();
333+
Collection<String> groupNames = getGroupsUserCanView( userName );
334334

335335
Collection<String> result = new HashSet<>();
336336
for ( String gname : groupNames ) {
@@ -497,11 +497,6 @@ public boolean isPublic( Securable s ) {
497497
@Override
498498
@Transactional(readOnly = true)
499499
public boolean isPrivate( Securable s ) {
500-
if ( s == null ) {
501-
log.warn( "Null object: considered public!" );
502-
return false;
503-
}
504-
505500
/*
506501
* Note: in theory, it should pay attention to the sid we ask for and return nothing if there is no acl.
507502
* However, the implementation actually ignores the sid argument.
@@ -517,10 +512,6 @@ public boolean isPrivate( Securable s ) {
517512
@Override
518513
@Transactional(readOnly = true)
519514
public boolean isShared( Securable s ) {
520-
if ( s == null ) {
521-
return false;
522-
}
523-
524515
/*
525516
* Implementation note: this code mimics AclEntryVoter.vote, but in adminsitrative mode so no auditing etc
526517
* happens.
@@ -576,10 +567,6 @@ public void makePrivate( Collection<? extends Securable> objs ) {
576567
@Override
577568
@Transactional
578569
public void makePrivate( Securable object ) {
579-
if ( object == null ) {
580-
return;
581-
}
582-
583570
if ( isPrivate( object ) ) {
584571
log.warn( "Object is already private: " + object );
585572
return;
@@ -608,13 +595,8 @@ public void makePublic( Collection<? extends Securable> objs ) {
608595
@Override
609596
@Transactional
610597
public void makePublic( Securable object ) {
611-
612-
if ( object == null ) {
613-
return;
614-
}
615-
616598
if ( isPublic( object ) ) {
617-
log.warn( "Object is already public" );
599+
log.warn( "Object is already public: " + object );
618600
return;
619601
}
620602

@@ -639,9 +621,8 @@ public void makePublic( Securable object ) {
639621
@Override
640622
@Transactional
641623
public void makeReadableByGroup( Securable s, String groupName ) throws AccessDeniedException {
642-
643624
if ( StringUtils.isEmpty( groupName.trim() ) ) {
644-
throw new IllegalArgumentException( "'group' cannot be null" );
625+
throw new IllegalArgumentException( "The group name cannot be empty." );
645626
}
646627

647628
Collection<String> groups = checkForGroupAccessByCurrentUser( groupName );
@@ -661,7 +642,6 @@ public void makeReadableByGroup( Securable s, String groupName ) throws AccessDe
661642
@Override
662643
@Transactional
663644
public void makeUnreadableByGroup( Securable s, String groupName ) throws AccessDeniedException {
664-
665645
if ( StringUtils.isEmpty( groupName.trim() ) ) {
666646
throw new IllegalArgumentException( "'group' cannot be null" );
667647
}
@@ -675,7 +655,6 @@ public void makeUnreadableByGroup( Securable s, String groupName ) throws Access
675655
@Override
676656
@Transactional
677657
public void makeUneditableByGroup( Securable s, String groupName ) throws AccessDeniedException {
678-
679658
if ( StringUtils.isEmpty( groupName.trim() ) ) {
680659
throw new IllegalArgumentException( "'group' cannot be null" );
681660
}
@@ -688,7 +667,6 @@ public void makeUneditableByGroup( Securable s, String groupName ) throws Access
688667
@Override
689668
@Transactional
690669
public void makeEditableByGroup( Securable s, String groupName ) throws AccessDeniedException {
691-
692670
if ( StringUtils.isEmpty( groupName.trim() ) ) {
693671
throw new IllegalArgumentException( "'group' cannot be null" );
694672
}
@@ -819,15 +797,15 @@ private Collection<String> checkForGroupAccessByCurrentUser( String groupName )
819797
/**
820798
* @return groups that the current user can view. For administrators, this is all groups.
821799
*/
822-
private Collection<String> getGroupsUserCanView() {
800+
private Collection<String> getGroupsUserCanView( String userName ) {
823801
Collection<String> groupNames;
824802
try {
825803
// administrator...
826804
groupNames = groupManager.findAllGroups();
827805
} catch ( AccessDeniedException e ) {
828806
// I'm not sure this actually happens. Usermanager.findAllGroups should just show all of the user's viewable
829807
// groups.
830-
groupNames = groupManager.findGroupsForUser( userDetailsManager.getCurrentUsername() );
808+
groupNames = groupManager.findGroupsForUser( userName );
831809
}
832810
return groupNames;
833811
}

gsec/src/main/java/gemma/gsec/acl/ObjectIdentityRetrievalStrategyImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class ObjectIdentityRetrievalStrategyImpl implements ObjectIdentityRetrie
3737

3838
@Override
3939
public ObjectIdentity getObjectIdentity( Object domainObject ) {
40-
Assert.isInstanceOf( Securable.class, domainObject, "The domain object must implement the Securable interface" );
40+
Assert.notNull( domainObject, "The domain object must be non-null." );
41+
Assert.isInstanceOf( Securable.class, domainObject, "The domain object must implement the " + Securable.class.getName() + " interface." );
4142
if ( domainObject instanceof SecureValueObject ) {
4243
SecureValueObject svo = ( SecureValueObject ) domainObject;
4344
return new AclObjectIdentity( svo.getSecurableClass(), svo.getId() );

gsec/src/main/java/gemma/gsec/authentication/UserDetailsManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.springframework.security.core.userdetails.UsernameNotFoundException;
44

5-
import javax.annotation.Nullable;
65
import java.util.Collection;
76

87
/**
@@ -44,8 +43,9 @@ public interface UserDetailsManager extends org.springframework.security.provisi
4443

4544
/**
4645
* Returns a String username (the principal).
46+
*
47+
* @throws IllegalStateException if no user is currently authenticated.
4748
*/
48-
@Nullable
4949
String getCurrentUsername();
5050

5151
boolean loggedIn();

0 commit comments

Comments
 (0)