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