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.regex.Pattern;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.opends.server.util.TimeThread;
025
026/** This class represents the timeofday keyword in a bind rule. */
027public class TimeOfDay implements KeywordBindRule {
028    /** Regular expression matching a valid timeofday rule value (0-2359). */
029    private static final Pattern timeofdayRegex = Pattern.compile("[0-2]\\d[0-5]\\d");
030
031    /** Enumeration representing the bind rule operation type. */
032    private final EnumBindRuleType type;
033    /** Holds the time value parsed from the ACI. */
034    private final int timeRef;
035
036    /**
037     * Constructor to create a timeofday keyword class.
038     * @param timeVal The time value to check for (0-2359).
039     * @param type An enumeration of the type of the expression.
040     */
041    private TimeOfDay(int timeVal, EnumBindRuleType type) {
042        this.timeRef=timeVal;
043        this.type=type;
044    }
045
046    /**
047     * Decodes a string representation of a timeofday bind rule expression.
048     * @param expr A string representation of the expression.
049     * @param type An enumeration of the type of the expression.
050     * @return  A TimeOfDay class representing the expression.
051     * @throws AciException If the expression is invalid.
052     */
053    public static TimeOfDay decode(String expr,  EnumBindRuleType type)
054    throws AciException  {
055        int valueAsInt = 0;
056        if (!timeofdayRegex.matcher(expr).matches())
057        {
058            LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_TIMEOFDAY.get(expr);
059            throw new AciException(message);
060         }
061        try {
062            valueAsInt = Integer.parseInt(expr);
063        } catch (NumberFormatException nfe) {
064          LocalizableMessage message =
065           WARN_ACI_SYNTAX_INVALID_TIMEOFDAY_FORMAT.get(expr, nfe.getMessage());
066            throw new AciException(message);
067        }
068        if (valueAsInt < 0 || valueAsInt > 2359)
069        {
070            LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_TIMEOFDAY_RANGE.get(expr);
071            throw new AciException(message);
072        }
073
074        return new TimeOfDay(valueAsInt, type);
075    }
076
077    /**
078     * Evaluates the timeofday bind rule using the evaluation context
079     * passed into the method.
080     * @param evalCtx  The evaluation context to use for the evaluation.
081     * @return  An enumeration result representing the result of the evaluation.
082     */
083    @Override
084    public EnumEvalResult evaluate(AciEvalContext evalCtx) {
085        EnumEvalResult matched = evaluate() ? EnumEvalResult.TRUE : EnumEvalResult.FALSE;
086        return matched.getRet(type, false);
087    }
088
089    private boolean evaluate() {
090        int currentTime=TimeThread.getHourAndMinute();
091        switch (type) {
092        case EQUAL_BINDRULE_TYPE:
093        case NOT_EQUAL_BINDRULE_TYPE:
094            return currentTime != timeRef;
095        case LESS_OR_EQUAL_BINDRULE_TYPE:
096            return currentTime <= timeRef;
097        case LESS_BINDRULE_TYPE:
098            return currentTime < timeRef;
099        case GREATER_OR_EQUAL_BINDRULE_TYPE:
100            return currentTime >= timeRef;
101        case GREATER_BINDRULE_TYPE:
102            return currentTime > timeRef;
103        default:
104            return false;
105        }
106    }
107
108    @Override
109    public String toString()
110    {
111        final StringBuilder sb = new StringBuilder();
112        toString(sb);
113        return sb.toString();
114    }
115
116    @Override
117    public final void toString(StringBuilder buffer)
118    {
119        buffer.append(super.toString());
120    }
121}