Search in sources :

Example 1 with MCRRuleMapping

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;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRRuleMapping(org.mycore.access.mcrimpl.MCRRuleMapping) NoResultException(javax.persistence.NoResultException)

Example 2 with MCRRuleMapping

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());
}
Also used : MCRRuleMapping(org.mycore.access.mcrimpl.MCRRuleMapping) Test(org.junit.Test)

Example 3 with MCRRuleMapping

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();
}
Also used : JsonArray(com.google.gson.JsonArray) MCRRuleMapping(org.mycore.access.mcrimpl.MCRRuleMapping) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes) MCRRestrictedAccess(org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)

Example 4 with MCRRuleMapping

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();
}
Also used : JsonArray(com.google.gson.JsonArray) MCRRuleMapping(org.mycore.access.mcrimpl.MCRRuleMapping) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) MCRRestrictedAccess(org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)

Example 5 with MCRRuleMapping

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();
    }
}
Also used : MCRRuleMapping(org.mycore.access.mcrimpl.MCRRuleMapping) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) MCRRestrictedAccess(org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)

Aggregations

MCRRuleMapping (org.mycore.access.mcrimpl.MCRRuleMapping)12 JsonObject (com.google.gson.JsonObject)6 JsonParser (com.google.gson.JsonParser)4 Consumes (javax.ws.rs.Consumes)4 MCRRestrictedAccess (org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)4 Date (java.util.Date)3 Test (org.junit.Test)3 JsonArray (com.google.gson.JsonArray)2 DELETE (javax.ws.rs.DELETE)2 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 MCRAccessStore (org.mycore.access.mcrimpl.MCRAccessStore)2 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1