View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * Implementation of a (user xy) clause
31   * 
32   * @author Matthias Kramm
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  }