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.net.UnknownHostException;
22  import java.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.Date;
25  import java.util.Locale;
26  
27  import org.apache.logging.log4j.LogManager;
28  import org.jdom2.Element;
29  import org.mycore.common.MCRSession;
30  import org.mycore.common.MCRSessionMgr;
31  import org.mycore.common.MCRSystemUserInformation;
32  import org.mycore.common.MCRUserInformation;
33  import org.mycore.parsers.bool.MCRCondition;
34  import org.mycore.parsers.bool.MCRParseException;
35  
36  public class MCRAccessRule implements org.mycore.access.MCRAccessRule {
37      private String id = "";
38  
39      private String creator = "";
40  
41      private Date creationTime = new Date();
42  
43      String rule = "";
44  
45      private String description = "";
46  
47      private MCRCondition<MCRAccessData> parsedRule;
48  
49      private static MCRRuleParser parser = new MCRRuleParser();
50  
51      public MCRAccessRule(String id, String creator, Date creationTime, String rule, String description)
52          throws MCRParseException {
53          setId(id);
54          setCreator(creator);
55          setCreationTime(creationTime);
56          setRule(rule);
57          setDescription(description);
58  
59      }
60  
61      @Deprecated
62      public boolean checkAccess(String userID, Date date, MCRIPAddress ip) {
63          if (parsedRule == null) {
64              if (userID.equals(MCRSystemUserInformation.getSuperUserInstance().getUserID())) {
65                  LogManager.getLogger(MCRAccessRule.class).debug("No rule defined, grant access to super user.");
66                  return true;
67              }
68              return false;
69          }
70          LogManager.getLogger(this.getClass()).debug("new MCRAccessData");
71          MCRAccessData data = new MCRAccessData(userID, date, ip);
72          LogManager.getLogger(this.getClass()).debug("new MCRAccessData done.");
73  
74          LogManager.getLogger(this.getClass()).debug("evaluate MCRAccessData");
75          boolean returns = parsedRule.evaluate(data);
76          LogManager.getLogger(this.getClass()).debug("evaluate MCRAccessData done.");
77          return returns;
78      }
79  
80      public boolean checkAccess(MCRUserInformation userInfo, Date date, MCRIPAddress ip) {
81          if (parsedRule == null) {
82              if (userInfo.getUserID().equals(MCRSystemUserInformation.getSuperUserInstance().getUserID())) {
83                  LogManager.getLogger(MCRAccessRule.class).debug("No rule defined, grant access to super user.");
84                  return true;
85              }
86              return false;
87          }
88          LogManager.getLogger(this.getClass()).debug("new MCRAccessData");
89          MCRAccessData data = new MCRAccessData(userInfo, date, ip);
90          LogManager.getLogger(this.getClass()).debug("new MCRAccessData done.");
91  
92          LogManager.getLogger(this.getClass()).debug("evaluate MCRAccessData");
93          boolean returns = parsedRule.evaluate(data);
94          LogManager.getLogger(this.getClass()).debug("evaluate MCRAccessData done.");
95          return returns;
96      }
97  
98      public MCRCondition<MCRAccessData> getRule() {
99          return parsedRule;
100     }
101 
102     public void setRule(String rule) {
103         this.rule = rule;
104         parsedRule = rule == null ? null : parser.parse(rule);
105     }
106 
107     public String getRuleString() {
108         if (rule == null) {
109             return "";
110         }
111         return rule;
112     }
113 
114     public Date getCreationTime() {
115         return new Date(creationTime.getTime());
116     }
117 
118     public void setCreationTime(Date creationTime) {
119         this.creationTime = creationTime == null ? null : new Date(creationTime.getTime());
120     }
121 
122     public String getCreator() {
123         return creator;
124     }
125 
126     public void setCreator(String creator) {
127         this.creator = creator;
128     }
129 
130     public String getDescription() {
131         return description;
132     }
133 
134     public void setDescription(String description) {
135         this.description = description;
136     }
137 
138     public String getId() {
139         return id;
140     }
141 
142     public void setId(String id) {
143         this.id = id;
144     }
145 
146     public Element getRuleElement() {
147         Element el = new Element("mcraccessrule");
148         el.addContent(new Element("id").setText(id));
149         el.addContent(new Element("creator").setText(id));
150         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
151         el.addContent(new Element("creationdate").setText(df.format(creationTime)));
152         el.addContent(new Element("rule").setText(rule));
153         el.addContent(new Element("description").setText("" + description));
154         return el;
155     }
156 
157     @Override
158     public boolean validate() {
159         MCRSession session = MCRSessionMgr.getCurrentSession();
160         MCRUserInformation userInfo = session.getUserInformation();
161         MCRIPAddress mcripAddress;
162         try {
163             mcripAddress = new MCRIPAddress(session.getCurrentIP());
164         } catch (UnknownHostException e) {
165             LogManager.getLogger(MCRAccessRule.class).warn("Error while checking rule.", e);
166             return false;
167         }
168         return checkAccess(userInfo, new Date(), mcripAddress);
169     }
170 }