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