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