001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS
026 */
027package org.opends.server.authorization.dseecompat;
028
029import static org.opends.messages.AccessControlMessages.*;
030
031import java.util.HashSet;
032import java.util.Set;
033
034/**
035 * This class represents an ACI's extop keyword rule.
036 */
037public class ExtOp {
038
039  /**
040   * HashSet of OID strings parsed from the decode.
041   */
042  private Set<String> extOpOIDs = new HashSet<>();
043
044  /**
045   * Enumeration representing the extop operator.
046   */
047  private EnumTargetOperator op = EnumTargetOperator.EQUALITY;
048
049  /**
050   * Creates a class that can be used to evaluate a extop rule.
051   *
052   * @param op The operator of the extop expression (=, !=).
053   * @param extOpOIDs  Set of extended operation OIDS to use in the evaluation
054   *                  (wild-card '*' allowed).
055   */
056  private ExtOp(EnumTargetOperator op, Set<String> extOpOIDs)
057  {
058    this.extOpOIDs=extOpOIDs;
059    this.op=op;
060  }
061
062  /**
063   *  Decode an extop expression string.
064   *
065   * @param operator  An enumeration representing the operator type.
066   * @param expr A string representing the extop expression.
067   * @return  A class representing the extop expression that can be
068   *          used to evaluate an ACI.
069   *
070   * @throws AciException If the specified expression string is invalid.
071   */
072  public static ExtOp decode(EnumTargetOperator operator, String expr)
073          throws AciException {
074    Set<String> extOpOIDs = Aci.decodeOID(expr,
075                  WARN_ACI_SYNTAX_INVALID_TARGEXTOP_EXPRESSION.get(expr));
076    return new ExtOp(operator, extOpOIDs);
077  }
078
079   /**
080   * Check if a extop is applicable based on the provided target match
081   * context.
082   *
083   * @param matchCtx The target match context to use in the check.
084   * @return True if the extop is applicable based on the context.
085   */
086  public boolean isApplicable(AciTargetMatchContext matchCtx) {
087    if(matchCtx.getExtOpOID() == null)
088    {
089      return false;
090    }
091    boolean ret = false;
092    for(String oid : extOpOIDs)
093    {
094      if(oid.equals("*") || matchCtx.getExtOpOID().equals(oid)) {
095        ret=true;
096        break;
097      }
098    }
099    if(op.equals(EnumTargetOperator.NOT_EQUALITY))
100    {
101      ret = !ret;
102    }
103    return ret;
104  }
105}