1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.access.mcrimpl;
20
21 import java.util.Optional;
22 import java.util.regex.Pattern;
23
24 import org.jdom2.Element;
25 import org.mycore.common.MCRSessionMgr;
26 import org.mycore.common.MCRUserInformation;
27 import org.mycore.parsers.bool.MCRCondition;
28
29
30
31
32
33
34 class MCRUserClause implements MCRCondition<Object> {
35 private String user;
36
37 private Pattern userRegEx;
38
39 private boolean not;
40
41 MCRUserClause(String user, boolean not) {
42 if (user.contains("*")) {
43 userRegEx = toRegex(user);
44 }
45 this.user = user;
46 this.not = not;
47 }
48
49 private Pattern toRegex(String userExp) {
50 StringBuilder regex = new StringBuilder("^");
51
52 for (int i = 0; i < userExp.length(); i++) {
53 char c = userExp.charAt(i);
54 if (c == '*') {
55 regex.append(".*");
56 } else {
57 regex.append(c);
58 }
59 }
60
61 regex = regex.append('$');
62 return Pattern.compile(regex.toString());
63 }
64
65 public boolean evaluate(Object o) {
66 MCRUserInformation userInformation = Optional.ofNullable(o)
67 .filter(obj -> obj instanceof MCRAccessData)
68 .map(MCRAccessData.class::cast)
69 .map(MCRAccessData::getUserInformation)
70 .orElseGet(MCRSessionMgr.getCurrentSession()::getUserInformation);
71 if (userRegEx != null) {
72 return userRegEx.matcher(userInformation.getUserID()).matches() ^ not;
73 }
74 return user.equals(userInformation.getUserID()) ^ not;
75 }
76
77 @Override
78 public String toString() {
79 return "user" + (not ? " != " : " = ") + user + " ";
80 }
81
82 public Element toXML() {
83 Element cond = new Element("condition");
84 cond.setAttribute("field", "user");
85 cond.setAttribute("operator", (not ? "!=" : "="));
86 cond.setAttribute("value", user);
87 return cond;
88 }
89 }