use of org.mycore.access.mcrimpl.MCRRuleMapping in project mycore by MyCoRe-Org.
the class MCRJPAAccessStore method getAccessDefinition.
/**
* method returns AccessDefinition for given key values
*
* @param pool
* name of accesspool
* @param objid
* objectid of MCRObject
* @return MCRAccessData
*/
@Override
public MCRRuleMapping getAccessDefinition(String pool, String objid) {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRACCESS> query = cb.createQuery(MCRACCESS.class);
Root<MCRACCESS> root = query.from(MCRACCESS.class);
MCRRuleMapping rulemapping = new MCRRuleMapping();
try {
MCRACCESS data = em.createQuery(query.where(cb.equal(root.get(MCRACCESS_.key), new MCRACCESSPK(pool, objid)))).getSingleResult();
rulemapping.setCreationdate(data.getCreationdate());
rulemapping.setCreator(data.getCreator());
rulemapping.setObjId(data.getKey().getObjid());
rulemapping.setPool(data.getKey().getAcpool());
rulemapping.setRuleId(data.getRule().getRid());
em.detach(data);
} catch (NoResultException e) {
// returning empty rulemapping is fine
}
return rulemapping;
}
use of org.mycore.access.mcrimpl.MCRRuleMapping in project mycore by MyCoRe-Org.
the class MCRJPAAccessStoreTest method getAccessDefinition.
/**
* Test method for
* {@link org.mycore.backend.jpa.access.MCRJPAAccessStore#getAccessDefinition(java.lang.String, java.lang.String)}.
*/
@Test
public void getAccessDefinition() {
final String objID = "test";
final String permission = "maytest";
MCRRuleMapping ruleMapping = addRuleMapping(objID, permission, TRUE_RULE.getRid());
startNewTransaction();
MCRRuleMapping ruleMapping2 = ACCESS_STORE.getAccessDefinition(permission, objID);
// We will remove milliseconds as they don't need to be saved
assertEquals((long) Math.floor(ruleMapping.getCreationdate().getTime() / 1000), ruleMapping2.getCreationdate().getTime() / 1000);
assertEquals(ruleMapping.getCreator(), ruleMapping2.getCreator());
assertEquals(ruleMapping.getObjId(), ruleMapping2.getObjId());
assertEquals(ruleMapping.getPool(), ruleMapping2.getPool());
assertEquals(ruleMapping.getRuleId(), ruleMapping2.getRuleId());
}
use of org.mycore.access.mcrimpl.MCRRuleMapping in project mycore by MyCoRe-Org.
the class MCRAclEditorResource method remove.
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@MCRRestrictedAccess(MCRAclEditorPermission.class)
public String remove(String data) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(data).getAsJsonObject();
JsonArray jsonArray = jsonObject.getAsJsonArray("access");
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject accessAsJsonObject = jsonArray.get(i).getAsJsonObject();
String accessID = accessAsJsonObject.get("accessID").getAsString();
String accessPool = accessAsJsonObject.get("accessPool").getAsString();
if (ACCESS_STORE.existsRule(accessID, accessPool)) {
MCRRuleMapping accessRule = ACCESS_STORE.getAccessDefinition(accessPool, accessID);
if (!accessRule.getObjId().equals("")) {
ACCESS_STORE.deleteAccessDefinition(accessRule);
accessAsJsonObject.addProperty("success", "1");
} else {
accessAsJsonObject.addProperty("success", "0");
}
} else {
accessAsJsonObject.addProperty("success", "0");
}
}
return jsonObject.toString();
}
use of org.mycore.access.mcrimpl.MCRRuleMapping in project mycore by MyCoRe-Org.
the class MCRAclEditorResource method editMulti.
@PUT
@Path("multi")
@MCRRestrictedAccess(MCRAclEditorPermission.class)
@Consumes(MediaType.APPLICATION_JSON)
public String editMulti(String data) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(data).getAsJsonObject();
JsonArray jsonArray = jsonObject.getAsJsonArray("access");
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject accessAsJsonObject = jsonArray.get(i).getAsJsonObject();
String accessID = accessAsJsonObject.get("accessID").getAsString();
String accessPool = accessAsJsonObject.get("accessPool").getAsString();
String accessRule = accessAsJsonObject.get("accessRule").getAsString();
if (ACCESS_STORE.existsRule(accessID, accessPool) && RULE_STORE.existsRule(accessRule)) {
MCRRuleMapping newAccessRule = createRuleMap(accessID, accessPool, accessRule);
MCRRuleMapping oldAccessRule = ACCESS_STORE.getAccessDefinition(accessPool, accessID);
if (oldAccessRule != null && !oldAccessRule.getObjId().equals("")) {
ACCESS_STORE.updateAccessDefinition(newAccessRule);
accessAsJsonObject.addProperty("success", "1");
} else {
ACCESS_STORE.createAccessDefinition(newAccessRule);
accessAsJsonObject.addProperty("success", "1");
}
} else {
accessAsJsonObject.addProperty("success", "0");
}
}
return jsonObject.toString();
}
use of org.mycore.access.mcrimpl.MCRRuleMapping in project mycore by MyCoRe-Org.
the class MCRAclEditorResource method edit.
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@MCRRestrictedAccess(MCRAclEditorPermission.class)
public Response edit(String data) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(data).getAsJsonObject();
String accessIDOld = jsonObject.get("accessIDOld").getAsString();
String accessPoolOld = jsonObject.get("accessPoolOld").getAsString();
String mode = jsonObject.get("mode").getAsString();
String accessIDNew = jsonObject.get("accessIDNew").getAsString();
String accessPoolNew = jsonObject.get("accessPoolNew").getAsString();
String accessRuleNew = jsonObject.get("accessRuleNew").getAsString();
if (!ACCESS_STORE.existsRule(accessIDNew, accessPoolNew) || mode.equals("rule")) {
if (ACCESS_STORE.existsRule(accessIDOld, accessPoolOld) && RULE_STORE.existsRule(accessRuleNew) && !accessIDNew.equals("") && !accessPoolNew.equals("")) {
MCRRuleMapping accessRule = createRuleMap(accessIDNew, accessPoolNew, accessRuleNew);
MCRRuleMapping oldAccessRule = ACCESS_STORE.getAccessDefinition(accessPoolOld, accessIDOld);
if (oldAccessRule != null && !oldAccessRule.getObjId().equals("")) {
if (mode.equals("rule")) {
ACCESS_STORE.updateAccessDefinition(accessRule);
} else {
ACCESS_STORE.deleteAccessDefinition(oldAccessRule);
ACCESS_STORE.createAccessDefinition(accessRule);
}
} else {
ACCESS_STORE.createAccessDefinition(accessRule);
}
return Response.ok().build();
} else {
return Response.status(Status.CONFLICT).build();
}
} else {
return Response.status(Status.CONFLICT).build();
}
}
Aggregations