-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...main/java/edu/internet2/tier/shibboleth/admin/ui/envers/PrincipalAwareRevisionEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.envers; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.envers.DefaultRevisionEntity; | ||
import org.hibernate.envers.RevisionEntity; | ||
|
||
import javax.persistence.Entity; | ||
|
||
/** | ||
* Extension of the default envers revision entity to track authenticated principals | ||
*/ | ||
@Entity | ||
@RevisionEntity(PrincipalEnhancingRevisionListener.class) | ||
@Getter | ||
@Setter | ||
public class PrincipalAwareRevisionEntity extends DefaultRevisionEntity { | ||
|
||
private String principalUserName; | ||
} |
20 changes: 20 additions & 0 deletions
20
...ava/edu/internet2/tier/shibboleth/admin/ui/envers/PrincipalEnhancingRevisionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.envers; | ||
|
||
import org.hibernate.envers.RevisionListener; | ||
|
||
import static edu.internet2.tier.shibboleth.admin.ui.security.springsecurity.PrincipalAccessor.currentPrincipalIfLoggedIn; | ||
|
||
/** | ||
* Implementation of envers revision listener to enhance revision entity with authenticated principal username. | ||
*/ | ||
public class PrincipalEnhancingRevisionListener implements RevisionListener { | ||
|
||
private static final String ANONYMOUS = "anonymous"; | ||
|
||
@Override | ||
public void newRevision(Object revisionEntity) { | ||
PrincipalAwareRevisionEntity rev = (PrincipalAwareRevisionEntity) revisionEntity; | ||
String user = currentPrincipalIfLoggedIn().orElse(ANONYMOUS); | ||
rev.setPrincipalUserName(user); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ava/edu/internet2/tier/shibboleth/admin/ui/security/springsecurity/PrincipalAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.springsecurity; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.util.Optional; | ||
|
||
public final class PrincipalAccessor { | ||
|
||
//Non-instantiable utility class | ||
private PrincipalAccessor() { | ||
} | ||
|
||
public static Optional<String> currentPrincipalIfLoggedIn() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(authentication.getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters