ForumPermissions.java :  » Forum » nemesis-forum » org » nemesis » forum » Java Open Source

Java Open Source » Forum » nemesis forum 
nemesis forum » org » nemesis » forum » ForumPermissions.java
/*
 * NEMESIS-FORUM.
 * Copyright (C) 2002  David Laurent(lithium2@free.fr). All rights reserved.
 * 
 * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
 * 
 * Copyright (C) 2001 Yasna.com. All rights reserved.
 * 
 * Copyright (C) 2000 CoolServlets.com. All rights reserved.
 * 
 * NEMESIS-FORUM. is free software; you can redistribute it and/or
 * modify it under the terms of the Apache Software License, Version 1.1,
 * or (at your option) any later version.
 * 
 * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
 * application are parts of NEMESIS-FORUM and are distributed under
 * same terms of licence.
 * 
 * 
 * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
 * and software developed by CoolServlets.com (http://www.coolservlets.com).
 * and software developed by Yasna.com (http://www.yasna.com).
 * 
 */


package org.nemesis.forum;

import org.nemesis.forum.config.Constants;
import org.nemesis.forum.util.cache.CacheSizes;
import org.nemesis.forum.util.cache.Cacheable;

/**
 * Defines a set of permissions for objects in the forum system that
 * users can be granted. Forum permissions are used by the protection
 * proxy objects defined for each major component of the system.
 */
public class ForumPermissions implements Cacheable {

  
  private boolean[] values = new boolean[8];

  /**
   * Factory method to create full permissions.
   */
  public static ForumPermissions full() {
    return new ForumPermissions(
      true,
      true,
      true,
      true,
      true,
      true,
      true,
      true);
  }

  /**
   * Factory method to create an object with no permissions.
   */
  public static ForumPermissions none() {
    return new ForumPermissions(
      false,
      false,
      false,
      false,
      false,
      false,
      false,
      false);
  }

  /**
   * Factory method to create an object with read-only permissions.
   */
  public static ForumPermissions readOnly() {
    return new ForumPermissions(
      true,
      false,
      false,
      false,
      false,
      false,
      false,
      false);
  }

  /**
   * Create a new permissions object with the specified permissions.
   */
  public ForumPermissions(
    boolean READ,
    boolean SYSTEM_ADMIN,
    boolean FORUM_ADMIN,
    boolean USER_ADMIN,
    boolean GROUP_ADMIN,
    boolean MODERATOR,
    boolean CREATE_THREAD,
    boolean CREATE_MESSAGE) {
    values[0] = READ;
    values[1] = SYSTEM_ADMIN;
    values[2] = FORUM_ADMIN;
    values[3] = USER_ADMIN;
    values[4] = GROUP_ADMIN;
    values[5] = MODERATOR;
    values[6] = CREATE_THREAD;
    values[7] = CREATE_MESSAGE;
  }

  /**
   * Creates a new ForumPermission object by combining two permissions
   * objects. The higher permission of each permission type will be used.
   */
  public ForumPermissions(ForumPermissions perm1, ForumPermissions perm2) {
    values[0] = perm1.get(0) || perm2.get(0);
    values[1] = perm1.get(1) || perm2.get(1);
    values[2] = perm1.get(2) || perm2.get(2);
    values[3] = perm1.get(3) || perm2.get(3);
    values[4] = perm1.get(4) || perm2.get(4);
    values[5] = perm1.get(5) || perm2.get(5);
    values[6] = perm1.get(6) || perm2.get(6);
    values[7] = perm1.get(7) || perm2.get(7);
  }

  public ForumPermissions(boolean[] permissions) {
    this.values = permissions;
  }

  public String toString() {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < values.length - 1; i++) {
      buf.append(values[i]).append(",");
    }
    buf.append(values[values.length - 1]);
    return buf.toString();
  }

  /**
   * Returns true if the permission of a particular type is allowed.
   */
  public boolean get(int type) {
    if (type < 0 || type > 7) {
      return false;
    }
    return values[type];
  }

  /**
   * Returns true if the permissions include system or forum admin
   * permissions.
   */
  public boolean isSystemOrForumAdmin() {
    return (values[Constants.FORUM_ADMIN] || values[Constants.SYSTEM_ADMIN]);
  }

  //FROM THE CACHEABLE INTERFACE//

  public int getSize() {
    //Approximate the size of the object in bytes by calculating the size
    //of each field.
    int size = 0;
    size += CacheSizes.sizeOfObject(); //overhead of object
    size += CacheSizes.sizeOfObject(); //ref to array
    size += CacheSizes.sizeOfBoolean() * 8; //boolean array vals
    return size;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.