-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read authentication rules in a separate transaction
- Loading branch information
Showing
2 changed files
with
98 additions
and
49 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
74 changes: 74 additions & 0 deletions
74
src/main/java/org/icatproject/core/manager/GateKeeperHelper.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,74 @@ | ||
package org.icatproject.core.manager; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import jakarta.ejb.Stateless; | ||
import jakarta.ejb.TransactionManagement; | ||
import jakarta.ejb.TransactionManagementType; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
||
import org.icatproject.core.entity.PublicStep; | ||
import org.icatproject.core.entity.Rule; | ||
|
||
/* | ||
* This class contains methods that provide authorization rules from the | ||
* database to the GateKeeper. They need to be run outside of the transaction | ||
* that is active in GateKeeper, so that they cannot be affected by any changes | ||
* made in that transaction. This is acheived by having them in a separate EJB | ||
* with bean-managed transactions (these never use the transaction of the | ||
* calling bean). | ||
*/ | ||
@Stateless | ||
@TransactionManagement(TransactionManagementType.BEAN) | ||
public class GateKeeperHelper { | ||
|
||
@PersistenceContext(unitName = "icat") | ||
private EntityManager gateKeeperManager; | ||
|
||
public List<String> getRules(String ruleQuery, String member, String bean, String attribute) { | ||
return gateKeeperManager | ||
.createNamedQuery(ruleQuery, String.class) | ||
.setParameter("member", member) | ||
.setParameter("bean", bean) | ||
.setParameter("attribute", attribute) | ||
.getResultList(); | ||
} | ||
|
||
public List<String> getRules(String ruleQuery, String member, String bean) { | ||
return gateKeeperManager | ||
.createNamedQuery(ruleQuery, String.class) | ||
.setParameter("member", member) | ||
.setParameter("bean", bean) | ||
.getResultList(); | ||
} | ||
|
||
public Map<String, Set<String>> getPublicSteps() { | ||
Map<String, Set<String>> publicSteps = new HashMap<>(); | ||
List<PublicStep> steps = gateKeeperManager.createNamedQuery(PublicStep.GET_ALL_QUERY, PublicStep.class).getResultList(); | ||
|
||
for (PublicStep step : steps) { | ||
Set<String> fieldNames = publicSteps.get(step.getOrigin()); | ||
if (fieldNames == null) { | ||
fieldNames = new HashSet<>(); | ||
publicSteps.put(step.getOrigin(), fieldNames); | ||
} | ||
fieldNames.add(step.getField()); | ||
} | ||
|
||
// return unmodifiable copy | ||
publicSteps.replaceAll((k, v) -> Set.copyOf(v)); | ||
return Map.copyOf(publicSteps); | ||
} | ||
|
||
public Set<String> getPublicTables() { | ||
List<String> tableNames = gateKeeperManager.createNamedQuery(Rule.PUBLIC_QUERY, String.class).getResultList(); | ||
|
||
// return unmodifiable copy | ||
return Set.copyOf(tableNames); | ||
} | ||
} |