# AdminRights.java

    /*
    ** Module   : AdminRights.java
    ** Abstract : Implements the Rights interface for the "admin" resource.
    **
    ** Copyright (c) 2009-2022, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ----------------Description----------------------
    ** 001 NVS 20090921   @44094 Created the initial version.
    ** 002 NVS 20091007   @44107 Access bits are public now.
    ** 003 SIY 20091216   @44510 Added ID for "refresh" node.
    ** 004 SBI 20170224          Changed to be a simple DTO, moved business methods out.
    ** 005 TJD 20220504          Java 11 compatibility minor changes
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.security;

    import java.io.Serializable;
    import java.util.LinkedHashMap;
    import java.util.Map;

    /**
     * Implements the "admin" rights objects.
     * <p>Admin resources come with rights objects made of one mandatory
     * bitfield of a variable number of permissions.
     */
    public class AdminRights
    implements Rights,
               Serializable
    {
       /** Rights variation type */
       public static final int ADMT_PATH = 0;

       /** Rights variation type */
       public static final int ADMT_LEAF_REFRESH = 2;

       /** Path: UNLIMITED permissions bit */
       public static final int ADTP_UNLIMITED_ACCESS = 0;

       /** Path: DENIED permissions bit */
       public static final int ADTP_DENIED_ACCESS = 1;

       /** Path: size of the bitfield */
       static final int ADTP_SIZE = 2;

       /** Rights variation type */
       public static final int ADMT_LEAF_USER = 1;

       /** ENUMERATE permissions bit */
       public static final int ADLU_ENUMERATE_ACCESS = 0;

       /** PASSWORD permissions bit */
       public static final int ADLU_PASSWORD_ACCESS = 1;

       /** PASSWORD permissions bit */
       public static final int ADLU_GROUP_ACCESS = 2;

       /** READ permissions bit */
       public static final int ADLU_READ_ACCESS = 3;

       /** WRITE permissions bit */
       public static final int ADLU_WRITE_ACCESS = 4;

       /** CREATE permissions bit */
       public static final int ADLU_CREATE_ACCESS = 5;

       /** DELETE permissions bit  */
       public static final int ADLU_DELETE_ACCESS = 6;

       /** DENIED permissions bit */
       public static final int ADLU_DENIED_ACCESS = 7;

       /** User: size of the bitfield */
       static final int ADLU_SIZE = 8;

       /** Refresh: REFRESH permissions bit */
       public static final int ADLR_REFRESH_ACCESS = 0;

       /** Refresh: DENIED permissions bit */
       public static final int ADLR_DENIED_ACCESS = 1;

       /** Refresh: size of the bitfield */
       static final int ADLR_SIZE = 2;

       /** mandatory field - variation type */
       int varType = -1;

       /** mandatory field - permissions */
       BitSet permissions = null;

       /**
        * Default constructor.
        * Creates an instance of permissions of the type ADMT_PATH with both
        * permissions bits reset.
        */
       public AdminRights()
       {
          varType = ADMT_PATH;
          permissions = new BitSet(ADTP_SIZE);
       }

       /**
        * Constructor.
        *
        * @param type
        *        type of permissions bitfield
        */
       public AdminRights(Integer type)
       {
          varType = type.intValue();

          switch (varType)
          {
             case ADMT_PATH:
                permissions = new BitSet(ADTP_SIZE);
                break;

             case ADMT_LEAF_USER:
                permissions = new BitSet(ADLU_SIZE);
                break;

             case ADMT_LEAF_REFRESH:
                permissions = new BitSet(ADLR_SIZE);
                break;

             default:
                varType= ADMT_PATH;
                permissions = new BitSet(ADTP_SIZE);
          }
       }

       /**
        * Constructor.
        *
        * @param type
        *        type of permissions bitfield
        * @param permissions
        *        bitfield with permissions
        */
       public AdminRights(Integer type, BitSet permissions)
       {
          varType = type.intValue();
          this.permissions = permissions;
       }

       /**
        * Gets the indexed bit of permissions.
        *
        * @param  index
        *         index of the permissions bit to query
        * @return the bit's value
        */
       boolean getPermission(int index)
       {
          return permissions.isSet(index);
       }

       /**
        * Gets the type of the bitfield.
        *
        * @return type of the bitfield as either ADMT_PATH or ADMT_LEAF_USER.
        */
       public int getType()
       {
          return varType;
       }

       /**
        * Gets the entire set of permissions.
        *
        * @return all permissions as a <code>BitSet</code>
        */
       public BitSet getPermissions()
       {
          return permissions;
       }

       /**
        * Converts this object to string.
        *
        * @return string representation of this object
        */
       public String toString()
       {
          StringBuilder sb = new StringBuilder();

          switch (varType)
          {
             case ADMT_PATH:
                sb.append("{");

                if (permissions.isSet(ADTP_UNLIMITED_ACCESS))
                   sb.append("U");
                else
                   sb.append(".");
                if (permissions.isSet(ADTP_DENIED_ACCESS))
                   sb.append("N");
                else
                   sb.append(".");

                sb.append("}");
                break;

             case ADMT_LEAF_USER:
                sb.append("{");

                if (permissions.isSet(ADLU_ENUMERATE_ACCESS))
                   sb.append("E");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_PASSWORD_ACCESS))
                   sb.append("P");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_GROUP_ACCESS))
                   sb.append("G");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_READ_ACCESS))
                   sb.append("R");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_WRITE_ACCESS))
                   sb.append("W");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_CREATE_ACCESS))
                   sb.append("C");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_DELETE_ACCESS))
                   sb.append("D");
                else
                   sb.append(".");
                if (permissions.isSet(ADLU_DENIED_ACCESS))
                   sb.append("N");
                else
                   sb.append(".");

                sb.append("}");
                break;

             case ADMT_LEAF_REFRESH:
                sb.append("{");
                if (permissions.isSet(ADLR_REFRESH_ACCESS))
                   sb.append("R");
                else
                   sb.append(".");
                if (permissions.isSet(ADLR_DENIED_ACCESS))
                   sb.append("N");
                else
                   sb.append(".");
                sb.append("}");
                break;

             default:
                sb.append("{unknown}");
          }

          return new String(sb);
       }

       /**
        * Returns rights name, a directory known class name.
        *
        * @return   a directory known class name
        */
       @Override
       public String getRightsName()
       {
          return "adminRights";
       }

       /**
        * Returns its attributes map.
        *
        * @return   A map of an attribute name to its value
        */
       @Override
       public Map<String, Object[]> getAttributes()
       {
          Map<String, Object[]> map = new LinkedHashMap<>(2);
          map.put("type", new Object[] {Integer.valueOf(varType)});
          map.put("permissions", new Object[] {permissions});

          return map;
       }
    }