Permission.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.model;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.apache.commons.lang.builder.ToStringBuilder;

  21. /**
  22.  * A user permission on a project.
  23.  * <p/>
  24.  * <p>
  25.  * The permission type tells what kind of action the user is allowed perform.
  26.  * </p>
  27.  *
  28.  * @author ready
  29.  */
  30. public class Permission extends AbstractEntity {

  31.     /**
  32.      * Comparator for comparing the main properties type, user, project
  33.      */
  34.     public static final PermissionPropertiesComparator PERMISSION_PROPERTIES_COMPARATOR = new PermissionPropertiesComparator();
  35.     /**
  36.      *
  37.      */
  38.     private static final long serialVersionUID = 1L;

  39.     /**
  40.      * The type of permission granted. TODO: use PermissionType enum
  41.      */
  42.     private PermissionType type;

  43.     /**
  44.      * The project on which this permission is granted. May be <tt>null</tt>
  45.      * to indicate the permission is granted on all projects.
  46.      */
  47.     private Project project;

  48.     /**
  49.      * The user who's granted this permission.
  50.      */
  51.     private User user;

  52.     /**
  53.      * Default constructor (required by Hibernate).
  54.      * <p/>
  55.      * <p>
  56.      * PENDING: should be <code>private</code> so that it can only be used by
  57.      * Hibernate, to ensure that the fields which form an instance's identity
  58.      * are always initialized/never <tt>null</tt>.
  59.      * </p>
  60.      */
  61.     public Permission() {
  62.     }

  63.     /**
  64.      * Grants permissions on all projects to the given user.
  65.      *
  66.      * @param type permission type
  67.      * @param user grantee
  68.      */
  69.     public Permission(PermissionType type, User user) {
  70.         this(type, user, null);
  71.     }

  72.     /**
  73.      * Grants permissions on all projects to the given user.
  74.      *
  75.      * @param type    permission type
  76.      * @param user    grantee
  77.      * @param project on which permission is granted, or <tt>null</tt> for all
  78.      *                projects
  79.      */
  80.     public Permission(PermissionType type, User user, Project project) {
  81.         setPermissionType(type);
  82.         setUser(user);
  83.         setProject(project);
  84.     }

  85.     public PermissionType getPermissionType() {
  86.         return type;
  87.     }

  88.     public void setPermissionType(PermissionType type) {
  89.         this.type = type;
  90.     }

  91.     public User getUser() {
  92.         return user;
  93.     }

  94.     public void setUser(User user) {
  95.         if (user == null) {
  96.             throw new IllegalArgumentException("null user");
  97.         }
  98.         this.user = user;
  99.     }

  100.     public Project getProject() {
  101.         return project;
  102.     }

  103.     /**
  104.      * May be null to indicate a permission on all projects.
  105.      */
  106.     public void setProject(Project project) {
  107.         this.project = project;
  108.     }


  109.     @Override
  110.     public String toString() {
  111.         return new ToStringBuilder(this).append("id", getId()).append("type", getPermissionType())
  112.                 .append("user", getUser()).append("project", getProject()).toString();
  113.     }


  114.     public static final class PermissionPropertiesComparator implements java.util.Comparator<Permission> {
  115.         public int compare(Permission lhs, Permission rhs) {
  116.             return new CompareToBuilder().append(lhs.type, rhs.type).append(lhs.user, rhs.user, User.NAME_COMPARATOR).append(lhs.project, rhs.project, Project.PROJECT_COMPARATOR).toComparison();
  117.         }
  118.     }
  119. }