DirectoryRights.java

/*
** Module   : DirectoryRights.java
** Abstract : Implements the Rights interface for the "directory" resource.
**
** Copyright (c) 2004-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ----------------Description----------------------
** 001 NVS 20090701   @43002 Made it a separate class due to a new requirement
**                           for all rights classes to be separate and
**                           serializable.
** 002 NVS 20090703   @43041 Implemented toDirectoryNode() method, which was
**                           added to the Rights interface.
** 003 NVS 20090713   @43162 Added getPermissions() method.
** 004 ECF 20150715          Replace StringBuffer with StringBuilder.
** 005 SBI 20170224          Changed to be a simple DTO, moved business methods out.
*/
/*
** 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.directory;

import com.goldencode.p2j.security.*;

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

/**
 * Implements the "directory" rights objects. 
 * <p>Directory resources come with rights objects made of one mandatory 
 * bitfield of 8 permissions ERWACDLN and one optional logical expression.
 * <p>
 * The string representation of these objects is either:
 * <br>
 * {ERWACDN}
 * <br>
 * or
 * {ERWACDN}-OR-{condition} or {ERWACDN}-AND-{condition} 
 */
public class DirectoryRights
implements Rights,
           Serializable
{
   /** ENUMERATE permissions bit for directory resource */
   public static final int BIT_ENUMERATE_ACCESS = 0;

   /** READ permissions bit for directory resource */
   public static final int BIT_READ_ACCESS = 1;

   /** WRITE permissions bit for directory resource */
   public static final int BIT_WRITE_ACCESS = 2;

   /** ADD permissions bit for directory resource */
   public static final int BIT_ADD_ACCESS = 3;

   /** CREATE permissions bit for directory resource */
   public static final int BIT_CREATE_ACCESS = 4;

   /** DELETE permissions bit for directory resource */
   public static final int BIT_DELETE_ACCESS = 5;

   /** DENIED permissions bit for directory resource */
   public static final int BIT_DENIED_ACCESS = 6;

   /** LOGIC permissions bit for directory resource */
   public static final int BIT_LOGIC = 7;
   
   /** the only mandatory field of this class - permissions */
   BitSet permissions = null;

   /** the only optional field of this class - condition */
   String condition = null;

   /**
    * Default constructor. 
    */
   public DirectoryRights()
   {
      permissions = new BitSet(BIT_LOGIC + 1);
      permissions.unset(BIT_ENUMERATE_ACCESS);
      permissions.unset(BIT_LOGIC);
   }
   
   /**
    * Constructor.
    *
    * @param permissions
    *        bitfield with permissions
    * @param condition
    *        extra boolean expression, may be <code>null</code>
    * Just copies the arguments into its members.
    */
   public DirectoryRights(BitSet permissions, String condition)
   {
      this.permissions = permissions;
      this.condition   = condition;
   }

   /**
    * 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 entire set of permissions. 
    *
    * @return all permissions as a <code>BitField</code>
    */
   public BitSet getPermissions()
   {
      return permissions;
   }
   
   /**
    * Gets the condition. 
    *
    * @return the condition
    */
   public String getCondition()
   {
      return condition;
   }

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

      sb.append("{");
      if (permissions.isSet(BIT_ENUMERATE_ACCESS))
         sb.append("E");
      else
         sb.append(".");

      if (permissions.isSet(BIT_READ_ACCESS))
         sb.append("R");
      else
         sb.append(".");

      if (permissions.isSet(BIT_WRITE_ACCESS))
         sb.append("W");
      else
         sb.append(".");

      if (permissions.isSet(BIT_ADD_ACCESS))
         sb.append("A");
      else
         sb.append(".");

      if (permissions.isSet(BIT_CREATE_ACCESS))
         sb.append("C");
      else
         sb.append(".");

      if (permissions.isSet(BIT_DELETE_ACCESS))
         sb.append("D");
      else
         sb.append(".");

      if (permissions.isSet(BIT_DENIED_ACCESS))
         sb.append("N");
      else
         sb.append(".");
      sb.append("}");

      if (condition != null)
      {
         if (permissions.isSet(BIT_LOGIC))
            sb.append("-AND-");
         else
            sb.append("-OR-");
         sb.append("{" + condition + "}");
      }

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

   /**
    * Returns its attributes map.
    * 
    * @return   A map of an attribute name to its value
    */
   @Override
   public Map<String, Object[]> getAttributes()
   {
      int size = 0;
      
      if (condition != null)
      {
         size = 2;
      }
      else
      {
         size = 1;
      }
      
      Map<String, Object[]> map = new LinkedHashMap<>(size);
      
      map.put("permissions", new Object[] {permissions});
      
      if (size == 2)
      {
         map.put("condition", new Object[] {condition});
      }
      
      return map;
   }
}