blob: 4e5edf2ccb8dc8c65825b3c9afd1f111c2d4ef76 [file] [log] [blame]
Michael Hanl1e18cb42015-08-06 20:57:35 +02001package de.ids_mannheim.korap.resource.rewrite;
2
3import com.fasterxml.jackson.databind.JsonNode;
Michael Hanl59bff812015-10-27 23:10:32 +01004import de.ids_mannheim.korap.config.KustvaktConfiguration;
Michael Hanlac4962c2015-09-21 22:21:05 +02005import de.ids_mannheim.korap.exceptions.KustvaktException;
6import de.ids_mannheim.korap.resources.Corpus;
7import de.ids_mannheim.korap.resources.KustvaktResource;
8import de.ids_mannheim.korap.security.ac.SecurityManager;
Michael Hanl59bff812015-10-27 23:10:32 +01009import de.ids_mannheim.korap.user.User;
Michael Hanl1e18cb42015-08-06 20:57:35 +020010
11/**
12 * @author hanl
13 * @date 03/07/2015
14 */
Michael Hanlac4962c2015-09-21 22:21:05 +020015// todo: test
Michael Hanlf0785322015-11-13 16:14:45 +010016public class CollectionConstraint implements RewriteTask.RewriteNode {
Michael Hanlac4962c2015-09-21 22:21:05 +020017
Michael Hanl1e18cb42015-08-06 20:57:35 +020018 @Override
Michael Hanlf0785322015-11-13 16:14:45 +010019 public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
Michael Hanl59bff812015-10-27 23:10:32 +010020 User user) {
Michael Hanlf0785322015-11-13 16:14:45 +010021
22 if (node.get("@type").equals("koral:doc")) {
23 if (node.get("key").equals("corpusID") && !check(node, user)) {
24 node.removeNode();
Michael Hanl1e18cb42015-08-06 20:57:35 +020025 // todo: add message that node was removed!
26 }
27 }
Michael Hanlf0785322015-11-13 16:14:45 +010028 return node.rawNode();
Michael Hanl1e18cb42015-08-06 20:57:35 +020029 }
30
Michael Hanlf0785322015-11-13 16:14:45 +010031 /**
32 * @param node
33 * @param user
34 * @return boolean if true access granted
35 */
Michael Hanl59bff812015-10-27 23:10:32 +010036 private boolean check(KoralNode node, User user) {
Michael Hanlf0785322015-11-13 16:14:45 +010037 // todo: can be used to circumvent access control!
Michael Hanl59bff812015-10-27 23:10:32 +010038 if (user == null)
Michael Hanlac4962c2015-09-21 22:21:05 +020039 return true;
40
Michael Hanlf0785322015-11-13 16:14:45 +010041 String id = node.get("value");
Michael Hanlac4962c2015-09-21 22:21:05 +020042 KustvaktResource corpus;
43 try {
44 SecurityManager m = SecurityManager
Michael Hanl59bff812015-10-27 23:10:32 +010045 .findbyId(id, user, Corpus.class);
Michael Hanlac4962c2015-09-21 22:21:05 +020046 corpus = m.getResource();
Michael Hanlf0785322015-11-13 16:14:45 +010047 }catch (RuntimeException | KustvaktException e) {
48 e.printStackTrace();
Michael Hanlac4962c2015-09-21 22:21:05 +020049 return false;
50 }
Michael Hanlf0785322015-11-13 16:14:45 +010051
Michael Hanlac4962c2015-09-21 22:21:05 +020052 return corpus != null;
Michael Hanl1e18cb42015-08-06 20:57:35 +020053 }
54
55}