Search in sources :

Example 1 with MCRAccessRule

use of org.mycore.access.mcrimpl.MCRAccessRule in project mycore by MyCoRe-Org.

the class MCRRestAPIUtil method checkRestAPIAccess.

/**
 * checks if the given REST API operation is allowed
 * @param request - the HTTP request
 * @param permission "read" or "write"
 * @param path - the REST API path, e.g. /v1/messages
 *
 * @throws MCRRestAPIException if access is restricted
 */
public static void checkRestAPIAccess(HttpServletRequest request, MCRRestAPIACLPermission permission, String path) throws MCRRestAPIException {
    // save the current user and set REST API user into session,
    // because ACL System can only validate the current user in session.
    MCRUserInformation oldUser = MCRSessionMgr.getCurrentSession().getUserInformation();
    try {
        String userID = MCRJSONWebTokenUtil.retrieveUsernameFromAuthenticationToken(request);
        if (userID != null) {
            if (MCRSystemUserInformation.getGuestInstance().getUserID().equals(userID)) {
                MCRSessionMgr.getCurrentSession().setUserInformation(MCRSystemUserInformation.getGuestInstance());
            } else {
                MCRSessionMgr.getCurrentSession().setUserInformation(MCRUserManager.getUser(userID));
            }
        }
        MCRIPAddress theIP = new MCRIPAddress(MCRFrontendUtil.getRemoteAddr(request));
        String thePath = path.startsWith("/") ? path : "/" + path;
        boolean hasAPIAccess = ((MCRAccessControlSystem) MCRAccessControlSystem.instance()).checkAccess("restapi:/", permission.toString(), userID, theIP);
        if (hasAPIAccess) {
            MCRAccessRule rule = (MCRAccessRule) MCRAccessControlSystem.instance().getAccessRule("restapi:" + thePath, permission.toString());
            if (rule != null) {
                if (rule.checkAccess(userID, new Date(), theIP)) {
                    return;
                }
            } else {
                return;
            }
        }
    } catch (UnknownHostException e) {
    // ignore
    } finally {
        MCRSessionMgr.getCurrentSession().setUserInformation(oldUser);
    }
    throw new MCRRestAPIException(Status.FORBIDDEN, new MCRRestAPIError(MCRRestAPIError.CODE_ACCESS_DENIED, "REST-API action is not allowed.", "Check access right '" + permission + "' on ACLs 'restapi:/' and 'restapi:" + path + "'!"));
}
Also used : MCRIPAddress(org.mycore.access.mcrimpl.MCRIPAddress) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) UnknownHostException(java.net.UnknownHostException) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) MCRAccessRule(org.mycore.access.mcrimpl.MCRAccessRule) MCRAccessControlSystem(org.mycore.access.mcrimpl.MCRAccessControlSystem) MCRUserInformation(org.mycore.common.MCRUserInformation) Date(java.util.Date)

Example 2 with MCRAccessRule

use of org.mycore.access.mcrimpl.MCRAccessRule in project mycore by MyCoRe-Org.

the class MCRAclEditorResource method editRule.

@PUT
@Path("rule")
@MCRRestrictedAccess(MCRAclEditorPermission.class)
@Consumes(MediaType.APPLICATION_JSON)
public Response editRule(String data) {
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = jsonParser.parse(data).getAsJsonObject();
    String ruleID = jsonObject.get("ruleID").getAsString();
    String ruleDesc = jsonObject.get("ruleDesc").getAsString();
    String ruleText = jsonObject.get("ruleText").getAsString();
    String uid = MCRSessionMgr.getCurrentSession().getUserInformation().getUserID();
    if (RULE_STORE.existsRule(ruleID)) {
        try {
            MCRAccessRule accessRule = new MCRAccessRule(ruleID, uid, new Date(), ruleText, ruleDesc);
            RULE_STORE.updateRule(accessRule);
            return Response.ok().build();
        } catch (Exception e) {
            return Response.status(Status.CONFLICT).build();
        }
    } else {
        return Response.status(Status.CONFLICT).build();
    }
}
Also used : JsonObject(com.google.gson.JsonObject) MCRAccessRule(org.mycore.access.mcrimpl.MCRAccessRule) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) 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 3 with MCRAccessRule

use of org.mycore.access.mcrimpl.MCRAccessRule in project mycore by MyCoRe-Org.

the class MCRAclEditorResource method createAccessRule.

private MCRAccessRule createAccessRule(String ruleDesc, String ruleText) {
    int freeRuleID = RULE_STORE.getNextFreeRuleID("SYSTEMRULE");
    String ruleID = "0000000000" + String.valueOf(freeRuleID);
    ruleID = ruleID.substring(ruleID.length() - "0000000000".length());
    String newRuleID = "SYSTEMRULE" + ruleID;
    String uid = MCRSessionMgr.getCurrentSession().getUserInformation().getUserID();
    return new MCRAccessRule(newRuleID, uid, new Date(), ruleText, ruleDesc);
}
Also used : MCRAccessRule(org.mycore.access.mcrimpl.MCRAccessRule) Date(java.util.Date)

Example 4 with MCRAccessRule

use of org.mycore.access.mcrimpl.MCRAccessRule in project mycore by MyCoRe-Org.

the class MCRWCMSAccessResource method getRuleList.

@GET
public String getRuleList() {
    JsonObject returnObject = new JsonObject();
    MCRRuleStore store = MCRRuleStore.getInstance();
    Collection<String> ruleIds = store.retrieveAllIDs();
    for (String id : ruleIds) {
        MCRAccessRule rule = store.getRule(id);
        returnObject.addProperty(rule.getId(), rule.getDescription());
    }
    return returnObject.toString();
}
Also used : JsonObject(com.google.gson.JsonObject) MCRAccessRule(org.mycore.access.mcrimpl.MCRAccessRule) MCRRuleStore(org.mycore.access.mcrimpl.MCRRuleStore) GET(javax.ws.rs.GET)

Example 5 with MCRAccessRule

use of org.mycore.access.mcrimpl.MCRAccessRule in project mycore by MyCoRe-Org.

the class MCRAclEditorResource method addRule.

@POST
@Path("rule")
@MCRRestrictedAccess(MCRAclEditorPermission.class)
@Consumes(MediaType.APPLICATION_JSON)
public String addRule(String data) {
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = jsonParser.parse(data).getAsJsonObject();
    String ruleDesc = jsonObject.get("ruleDesc").getAsString();
    String ruleText = jsonObject.get("ruleText").getAsString();
    MCRAccessRule accessRule;
    try {
        accessRule = createAccessRule(ruleDesc, ruleText);
    } catch (Exception e) {
        return "";
    }
    RULE_STORE.createRule(accessRule);
    return accessRule.getId();
}
Also used : JsonObject(com.google.gson.JsonObject) MCRAccessRule(org.mycore.access.mcrimpl.MCRAccessRule) WebApplicationException(javax.ws.rs.WebApplicationException) JsonParser(com.google.gson.JsonParser) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) MCRRestrictedAccess(org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)

Aggregations

MCRAccessRule (org.mycore.access.mcrimpl.MCRAccessRule)6 JsonObject (com.google.gson.JsonObject)4 Date (java.util.Date)3 MCRRestrictedAccess (org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)3 JsonParser (com.google.gson.JsonParser)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 JsonArray (com.google.gson.JsonArray)1 UnknownHostException (java.net.UnknownHostException)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 MCRAccessControlSystem (org.mycore.access.mcrimpl.MCRAccessControlSystem)1 MCRIPAddress (org.mycore.access.mcrimpl.MCRIPAddress)1 MCRRuleStore (org.mycore.access.mcrimpl.MCRRuleStore)1 MCRUserInformation (org.mycore.common.MCRUserInformation)1 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)1 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)1