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