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 targetcontrol keyword.
036 */
037public class TargetControl {
038
039  /** HashSet of OID strings parsed from the decode. */
040  private Set<String> controlOIDS = new HashSet<>();
041  /** Enumeration representing the targetcontrol operator. */
042  private EnumTargetOperator op = EnumTargetOperator.EQUALITY;
043
044  /**
045   * Creates a class that can be used to evaluate a targetcontrol.
046   *
047   * @param op The operator of the targetcontrol expression (=, !=).
048   * @param controlOIDS  Set of control OIDS to use in the evaluation (may
049   *                     contain wild-card '*').
050   */
051  private TargetControl(EnumTargetOperator op, Set<String> controlOIDS)
052  {
053    this.controlOIDS=controlOIDS;
054    this.op=op;
055  }
056
057  /**
058   *  Decode an targetcontrol expression string.
059   *
060   * @param operator  An enumeration representing the operator type.
061   * @param expr A string representing the targetcontrol expression.
062   * @return  A class representing the targetcontrol expression that can be
063   *          used to evaluate an ACI.
064   *
065   * @throws AciException If the specified expression string is invalid.
066   */
067  public static TargetControl decode(EnumTargetOperator operator, String expr)
068          throws AciException {
069    Set<String> controlOIDs = Aci.decodeOID(expr,
070                  WARN_ACI_SYNTAX_INVALID_TARGETCONTROL_EXPRESSION.get(expr));
071    return new TargetControl(operator, controlOIDs);
072  }
073
074  /**
075   * Check if a targetcontrol is applicable based on the provided target match
076   * context.
077   *
078   * @param matchCtx The target match context to use in the check.
079   * @return True if the targetcontrol is applicable based on the context.
080   */
081  public boolean isApplicable(AciTargetMatchContext matchCtx) {
082    if(matchCtx.getControlOID() == null)
083    {
084      return false;
085    }
086    boolean ret = false;
087    for(String oid : controlOIDS)
088    {
089      if(oid.equals("*") || matchCtx.getControlOID().equals(oid)) {
090        ret=true;
091        break;
092      }
093    }
094    if(op.equals(EnumTargetOperator.NOT_EQUALITY))
095    {
096      ret = !ret;
097    }
098    return ret;
099  }
100}