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
029/**
030 * A class representing a permission-bind rule pair. There can be multiple
031 * of these in an ACI.
032 */
033public class PermBindRulePair {
034
035    /** The Bind Rule part. */
036    private BindRule bindRule;
037
038    /** The permission part. */
039    private Permission perm;
040
041    /**
042     * This constructor calls the permission and bind rule decodes
043     * with the appropriate strings.
044     * @param perm  A string representing the permissions.
045     * @param rights  A string representing the rights.
046     * @param bindRule A string representing the bind rule.
047     * @throws AciException  If any of the strings fail to decode.
048     */
049    private  PermBindRulePair(String perm, String rights, String bindRule)
050    throws AciException {
051     this.perm=Permission.decode(perm, rights);
052     this.bindRule=BindRule.decode(bindRule);
053    }
054
055    /**
056     * Decodes a permission bind rule pair.
057     * @param perm  A string representing the permissions.
058     * @param rights  A string representing the rights.
059     * @param bRule A string representing the bind rule.
060     * @return An permission bind rule pair class representing this pair.
061     * @throws AciException  If any of the strings fail to decode.
062     */
063    public static PermBindRulePair decode(String perm, String rights,
064                                          String bRule) throws AciException {
065       return new PermBindRulePair(perm, rights, bRule);
066    }
067
068    /**
069     * Gets the bind rule part of this pair.
070     * @return  The bind rule part of this pair.
071     */
072    public BindRule getBindRule () {
073        return bindRule;
074    }
075
076    /**
077     * Checks the permission to see if it has this access type.
078     * @param accessType An enumeration of the desired access type.
079     * @return True if the access type equals the permission access type.
080     */
081    public boolean hasAccessType(EnumAccessType accessType) {
082        return perm.hasAccessType(accessType);
083    }
084
085    /**
086     * Try and match one or more of the specified rights against a rights set
087     * of the permission class.
088     * @param right  The rights to match.
089     * @return True if one or more of the specified rights match a right in
090     * the rights set of the permission class.
091     */
092    public boolean hasRights(int right) {
093        return perm.hasRights(right);
094    }
095
096    /** {@inheritDoc} */
097    @Override
098    public String toString() {
099        final StringBuilder sb = new StringBuilder();
100        toString(sb);
101        return sb.toString();
102    }
103
104    /**
105     * Appends a string representation of this object to the provided buffer.
106     *
107     * @param buffer
108     *          The buffer into which a string representation of this object
109     *          should be appended.
110     */
111    public final void toString(StringBuilder buffer) {
112        if (this.perm != null) {
113            this.perm.toString(buffer);
114        }
115        buffer.append(" ");
116        if (this.bindRule != null) {
117            this.bindRule.toString(buffer);
118        }
119        buffer.append(")"); // not sure why, but we need this extra parenthesis
120    }
121}