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-2016 ForgeRock AS.
016 */
017package org.opends.server.authorization.dseecompat;
018
019import static org.opends.messages.AccessControlMessages.*;
020
021import java.util.Set;
022
023/** This class represents an ACI's targetcontrol keyword. */
024public class TargetControl {
025  /** HashSet of OID strings parsed from the decode. */
026  private final Set<String> controlOIDS;
027  /** Enumeration representing the targetcontrol operator. */
028  private final EnumTargetOperator op;
029
030  /**
031   * Creates a class that can be used to evaluate a targetcontrol.
032   *
033   * @param op The operator of the targetcontrol expression (=, !=).
034   * @param controlOIDS  Set of control OIDS to use in the evaluation (may
035   *                     contain wild-card '*').
036   */
037  private TargetControl(EnumTargetOperator op, Set<String> controlOIDS)
038  {
039    this.controlOIDS=controlOIDS;
040    this.op=op;
041  }
042
043  /**
044   *  Decode an targetcontrol expression string.
045   *
046   * @param operator  An enumeration representing the operator type.
047   * @param expr A string representing the targetcontrol expression.
048   * @return  A class representing the targetcontrol expression that can be
049   *          used to evaluate an ACI.
050   *
051   * @throws AciException If the specified expression string is invalid.
052   */
053  public static TargetControl decode(EnumTargetOperator operator, String expr)
054          throws AciException {
055    Set<String> controlOIDs = Aci.decodeOID(expr,
056                  WARN_ACI_SYNTAX_INVALID_TARGETCONTROL_EXPRESSION.get(expr));
057    return new TargetControl(operator, controlOIDs);
058  }
059
060  /**
061   * Check if a targetcontrol is applicable based on the provided target match
062   * context.
063   *
064   * @param matchCtx The target match context to use in the check.
065   * @return True if the targetcontrol is applicable based on the context.
066   */
067  public boolean isApplicable(AciTargetMatchContext matchCtx) {
068    if(matchCtx.getControlOID() == null)
069    {
070      return false;
071    }
072    boolean ret = isApplicable(matchCtx.getControlOID());
073    if (EnumTargetOperator.NOT_EQUALITY.equals(op))
074    {
075      return !ret;
076    }
077    return ret;
078  }
079
080  private boolean isApplicable(String matchControlOID)
081  {
082    for (String oid : controlOIDS)
083    {
084      if (oid.equals("*") || matchControlOID.equals(oid))
085      {
086        return true;
087      }
088    }
089    return false;
090  }
091}