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  
23  import org.jdom2.Element;
24  import org.mycore.common.MCRSessionMgr;
25  import org.mycore.common.MCRUserInformation;
26  import org.mycore.parsers.bool.MCRCondition;
27  
28  /**
29   * Implementation of a (group xy) clause
30   * 
31   * @author Matthias Kramm
32   * @author Mathias Fricke
33   */
34  class MCRGroupClause implements MCRCondition<Object> {
35  
36      private String groupname;
37  
38      private boolean not;
39  
40      MCRGroupClause(String group, boolean not) {
41          groupname = group;
42          this.not = not;
43      }
44  
45      public boolean evaluate(Object o) {
46          MCRUserInformation userInformation = Optional.ofNullable(o)
47              .filter(obj -> obj instanceof MCRAccessData)
48              .map(MCRAccessData.class::cast)
49              .map(MCRAccessData::getUserInformation)
50              .orElseGet(MCRSessionMgr.getCurrentSession()::getUserInformation);
51          return userInformation.isUserInRole(groupname) ^ not;
52      }
53  
54      @Override
55      public String toString() {
56          return "group" + (not ? " != " : " = ") + groupname + " ";
57      }
58  
59      public Element toXML() {
60          Element cond = new Element("condition");
61          cond.setAttribute("field", "group");
62          cond.setAttribute("operator", (not ? "!=" : "="));
63          cond.setAttribute("value", groupname);
64          return cond;
65      }
66  }