001 /*
002 *
003 * $Revision$ $Date$
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.access.mcrimpl;
025
026 import java.text.DateFormat;
027 import java.text.SimpleDateFormat;
028 import java.util.Date;
029
030 import org.apache.log4j.Logger;
031 import org.jdom.Element;
032 import org.mycore.user.MCRUser;
033
034 import org.mycore.parsers.bool.MCRCondition;
035 import org.mycore.parsers.bool.MCRParseException;
036
037 public class MCRAccessRule {
038 String id = "";
039
040 String creator = "";
041
042 Date creationTime = new Date();
043
044 String rule = "";
045
046 String description = "";
047
048 MCRCondition parsedRule;
049
050 static MCRRuleParser parser = new MCRRuleParser();
051
052 public MCRAccessRule(String id, String creator, Date creationTime, String rule, String description) throws MCRParseException {
053 this.id = id;
054 this.creator = creator;
055 this.creationTime = creationTime;
056 this.rule = rule;
057 this.description = description;
058
059 if (this.rule != null) {
060 this.parsedRule = parser.parse(this.rule);
061 }
062 }
063
064 public boolean checkAccess(MCRUser user, Date date, MCRIPAddress ip) {
065 if (this.parsedRule == null) {
066 if (user.getID().equals(MCRAccessControlSystem.superuserID)) {
067 Logger.getLogger(MCRAccessRule.class).debug("No rule defined, grant access to super user.");
068 return true;
069 }
070 return false;
071 }
072 Logger.getLogger(this.getClass()).debug("new MCRAccessData");
073 MCRAccessData data = new MCRAccessData(user, date, ip);
074 Logger.getLogger(this.getClass()).debug("new MCRAccessData done.");
075
076 Logger.getLogger(this.getClass()).debug("evaluate MCRAccessData");
077 boolean returns = this.parsedRule.evaluate(data);
078 Logger.getLogger(this.getClass()).debug("evaluate MCRAccessData done.");
079 return returns;
080 }
081
082 public MCRCondition getRule() {
083 return this.parsedRule;
084 }
085
086 /**
087 * rule
088 *
089 * @param rule
090 */
091 public void setRule(String rule) {
092 this.rule = rule;
093 }
094
095 public String getRuleString() {
096 if (rule == null) {
097 return "";
098 }
099 return rule;
100 }
101
102 public Date getCreationTime() {
103 return creationTime;
104 }
105
106 public void setCreationTime(Date creationTime) {
107 this.creationTime = creationTime;
108 }
109
110 public String getCreator() {
111 return creator;
112 }
113
114 public void setCreator(String creator) {
115 this.creator = creator;
116 }
117
118 public String getDescription() {
119 return description;
120 }
121
122 public void setDescription(String description) {
123 this.description = description;
124 }
125
126 public String getId() {
127 return id;
128 }
129
130 public void setId(String id) {
131 this.id = id;
132 }
133
134 public Element getRuleElement() {
135 Element el = new Element("mcraccessrule");
136 el.addContent(new Element("id").setText(this.id));
137 el.addContent(new Element("creator").setText(this.id));
138 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
139 el.addContent(new Element("creationdate").setText(df.format(this.creationTime)));
140 el.addContent(new Element("rule").setText(this.rule));
141 el.addContent(new Element("description").setText("" + this.description));
142 return el;
143 }
144 }