UserUtilities.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.util;

  19. import org.itracker.PasswordException;
  20. import org.itracker.core.AuthenticationConstants;
  21. import org.itracker.core.resources.ITrackerResources;
  22. import org.itracker.model.*;
  23. import org.itracker.util.Base64Coder;

  24. import java.io.UnsupportedEncodingException;
  25. import java.security.MessageDigest;
  26. import java.security.NoSuchAlgorithmException;
  27. import java.util.*;


  28. public class UserUtilities implements AuthenticationConstants {
  29.     protected static final char[] alphabet = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

  30.     public static final int STATUS_DELETED = -1;
  31.     public static final int STATUS_ACTIVE = 1;
  32.     public static final int STATUS_LOCKED = 2;

  33.     // TODO: Could use an enumeration

  34.     /**
  35.      * User Admin Permission.  Currently this is equivalent to super user, since the permission can't be granted, and is only available to an admin.
  36.      */
  37.     public static final int PERMISSION_USER_ADMIN = PermissionType.USER_ADMIN.getCode();
  38.     /**
  39.      * Product Admin Permission
  40.      */
  41.     public static final int PERMISSION_PRODUCT_ADMIN = PermissionType.PRODUCT_ADMIN.getCode();
  42.     /**
  43.      * Issue Create Permission
  44.      */
  45.     public static final int PERMISSION_CREATE = PermissionType.ISSUE_CREATE.getCode();
  46.     /**
  47.      * Issue Edit Permission.  Users with this permission can edit any issue in the project.
  48.      */
  49.     public static final int PERMISSION_EDIT = PermissionType.ISSUE_EDIT_ALL.getCode();
  50.     /**
  51.      * Issue Close Permission.  Users with this permission can close issues in the project.
  52.      */
  53.     public static final int PERMISSION_CLOSE = PermissionType.ISSUE_CLOSE.getCode();
  54.     /**
  55.      * Issue Assign to Self Permission.  Users with this permission can assign issues to themselves.
  56.      */
  57.     public static final int PERMISSION_ASSIGN_SELF = PermissionType.ISSUE_ASSIGN_SELF.getCode();
  58.     /**
  59.      * Issue Assign to Others Permissions.  Users with this permission can assign issues to anyone, given than those users have the ability to recieve the assignment.
  60.      */
  61.     public static final int PERMISSION_ASSIGN_OTHERS = PermissionType.ISSUE_ASSIGN_OTHERS.getCode();
  62.     /**
  63.      * View All Issues Permission.  Users can view all issues in the project.
  64.      */
  65.     public static final int PERMISSION_VIEW_ALL = PermissionType.ISSUE_VIEW_ALL.getCode();
  66.     /**
  67.      * View Users Issues Permission.  Users can view thier own issues.  This includes ones they are the creator or owner of.
  68.      */
  69.     public static final int PERMISSION_VIEW_USERS = PermissionType.ISSUE_VIEW_USERS.getCode();
  70.     /**
  71.      * Edit Users Issues Permission.  Users with this permission can edit any issue they created or own.
  72.      * They are limited to editing the description, adding history entries, and adding attachments.
  73.      */
  74.     public static final int PERMISSION_EDIT_USERS = PermissionType.ISSUE_EDIT_USERS.getCode();
  75.     /**
  76.      * Issue Unassign Self Permission.  Users with this permission can unassign issues they own.
  77.      */
  78.     public static final int PERMISSION_UNASSIGN_SELF = PermissionType.ISSUE_UNASSIGN_SELF.getCode();
  79.     /**
  80.      * Issue Assignable.  Users with this permission can be assigned any issue in the system.  To determine if a user can
  81.      * be assigned an issue, it will be a combination of users with EDIT_ALL, users with EDIT_USERS if they are the creator,
  82.      * and users with this permission and EDIT_USERS.
  83.      */
  84.     public static final int PERMISSION_ASSIGNABLE = PermissionType.ISSUE_ASSIGNABLE.getCode();
  85.     /**
  86.      * Create for Others.  Users with this permission are allowed to create issues on behalf of other users.  The system
  87.      * will treat the issue as if the other user had created it.  The actual creator will be logged in the audit log.
  88.      */
  89.     public static final int PERMISSION_CREATE_OTHERS = PermissionType.ISSUE_CREATE_OTHERS.getCode();
  90.     /**
  91.      * Full edit permission.  This defines what levelof editing a user has for an issue.  Without this permission, users will
  92.      * be limited to editing only the description, attachments, custom fields, and history of an issue.
  93.      */
  94.     public static final int PERMISSION_EDIT_FULL = PermissionType.ISSUE_EDIT_FULL.getCode();


  95.     private static final Integer[] ALL_PERMISSIONS = new Integer[]{
  96.             PERMISSION_PRODUCT_ADMIN,
  97.             PERMISSION_CREATE,
  98.             PERMISSION_EDIT,
  99.             PERMISSION_CLOSE,
  100.             PERMISSION_ASSIGN_SELF,
  101.             PERMISSION_ASSIGN_OTHERS,
  102.             PERMISSION_VIEW_ALL,
  103.             PERMISSION_VIEW_USERS,
  104.             PERMISSION_EDIT_USERS,
  105.             PERMISSION_UNASSIGN_SELF,
  106.             PERMISSION_ASSIGNABLE,
  107.             PERMISSION_CREATE_OTHERS,
  108.             PERMISSION_EDIT_FULL,

  109.     };
  110.     public static final Set<Integer> ALL_PERMISSIONS_SET = Collections.unmodifiableSet(getAllPermissionsSet());

  111.     private static final Set<Integer> getAllPermissionsSet() {
  112.         return new HashSet<Integer>(Arrays.asList(ALL_PERMISSIONS));
  113.     }

  114.     public static final int REGISTRATION_TYPE_ADMIN = 1;
  115.     public static final int REGISTRATION_TYPE_SELF = 2;
  116.     public static final int REGISTRATION_TYPE_IMPORT = 3;

  117.     public static final int PREF_HIDE_ASSIGNED = 1;
  118.     public static final int PREF_HIDE_UNASSIGNED = 2;
  119.     public static final int PREF_HIDE_CREATED = 4;
  120.     public static final int PREF_HIDE_WATCHED = 8;

  121.     public UserUtilities() {
  122.     }

  123.     public static String getStatusName(int value) {
  124.         return getStatusName(value, ITrackerResources.getLocale());
  125.     }

  126.     public static String getStatusName(int value, Locale locale) {
  127.         return ITrackerResources.getString(ITrackerResources.KEY_BASE_USER_STATUS + value, locale);
  128.     }

  129.     public static HashMap<String, String> getStatusNames() {
  130.         return getStatusNames(ITrackerResources.getLocale());
  131.     }

  132.     public static HashMap<String, String> getStatusNames(Locale locale) {
  133.         HashMap<String, String> statuses = new HashMap<String, String>();
  134.         statuses.put(Integer.toString(STATUS_DELETED), getStatusName(STATUS_DELETED, locale));
  135.         statuses.put(Integer.toString(STATUS_ACTIVE), getStatusName(STATUS_ACTIVE, locale));
  136.         statuses.put(Integer.toString(STATUS_LOCKED), getStatusName(STATUS_LOCKED, locale));
  137.         return statuses;
  138.     }

  139.     public static String getPermissionName(int value) {
  140.         return getPermissionName(value, ITrackerResources.getLocale());
  141.     }

  142.     public static String getPermissionName(int value, Locale locale) {
  143.         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PERMISSION + value, locale);
  144.     }

  145.     public static List<NameValuePair> getPermissionNames() {
  146.         return getPermissionNames(ITrackerResources.getLocale());
  147.     }

  148.     public static List<NameValuePair> getPermissionNames(Locale locale) {
  149.         List<NameValuePair> permissions = new ArrayList<>();
  150.         permissions.add(0, new NameValuePair(getPermissionName(PERMISSION_CREATE, locale), Integer.toString(PERMISSION_CREATE)));
  151.         permissions.add(1, new NameValuePair(getPermissionName(PERMISSION_CREATE_OTHERS, locale), Integer.toString(PERMISSION_CREATE_OTHERS)));
  152.         permissions.add(2, new NameValuePair(getPermissionName(PERMISSION_EDIT, locale), Integer.toString(PERMISSION_EDIT)));
  153.         permissions.add(3, new NameValuePair(getPermissionName(PERMISSION_EDIT_USERS, locale), Integer.toString(PERMISSION_EDIT_USERS)));
  154.         permissions.add(4, new NameValuePair(getPermissionName(PERMISSION_EDIT_FULL, locale), Integer.toString(PERMISSION_EDIT_FULL)));
  155.         permissions.add(5, new NameValuePair(getPermissionName(PERMISSION_CLOSE, locale), Integer.toString(PERMISSION_CLOSE)));
  156.         permissions.add(6, new NameValuePair(getPermissionName(PERMISSION_ASSIGNABLE, locale), Integer.toString(PERMISSION_ASSIGNABLE)));
  157.         permissions.add(7, new NameValuePair(getPermissionName(PERMISSION_ASSIGN_SELF, locale), Integer.toString(PERMISSION_ASSIGN_SELF)));
  158.         permissions.add(8, new NameValuePair(getPermissionName(PERMISSION_UNASSIGN_SELF, locale), Integer.toString(PERMISSION_UNASSIGN_SELF)));
  159.         permissions.add(9, new NameValuePair(getPermissionName(PERMISSION_ASSIGN_OTHERS, locale), Integer.toString(PERMISSION_ASSIGN_OTHERS)));
  160.         permissions.add(10, new NameValuePair(getPermissionName(PERMISSION_VIEW_ALL, locale), Integer.toString(PERMISSION_VIEW_ALL)));
  161.         permissions.add(11, new NameValuePair(getPermissionName(PERMISSION_VIEW_USERS, locale), Integer.toString(PERMISSION_VIEW_USERS)));
  162.         permissions.add(12, new NameValuePair(getPermissionName(PERMISSION_PRODUCT_ADMIN, locale), Integer.toString(PERMISSION_PRODUCT_ADMIN)));
  163.         return permissions;
  164.     }
  165.     public static List<NameValuePair> getPermissionTypeNames(Locale locale) {
  166.         List<NameValuePair> permissions = new ArrayList<>();
  167.         permissions.add(0, new NameValuePair(getPermissionName(PERMISSION_CREATE, locale), PermissionType.valueOf(PERMISSION_CREATE).name()));
  168.         permissions.add(1, new NameValuePair(getPermissionName(PERMISSION_CREATE_OTHERS, locale), PermissionType.valueOf(PERMISSION_CREATE_OTHERS).name()));
  169.         permissions.add(2, new NameValuePair(getPermissionName(PERMISSION_EDIT, locale), PermissionType.valueOf(PERMISSION_EDIT).name()));
  170.         permissions.add(3, new NameValuePair(getPermissionName(PERMISSION_EDIT_USERS, locale), PermissionType.valueOf(PERMISSION_EDIT_USERS).name()));
  171.         permissions.add(4, new NameValuePair(getPermissionName(PERMISSION_EDIT_FULL, locale), PermissionType.valueOf(PERMISSION_EDIT_FULL).name()));
  172.         permissions.add(5, new NameValuePair(getPermissionName(PERMISSION_CLOSE, locale), PermissionType.valueOf(PERMISSION_CLOSE).name()));
  173.         permissions.add(6, new NameValuePair(getPermissionName(PERMISSION_ASSIGNABLE, locale), PermissionType.valueOf(PERMISSION_ASSIGNABLE).name()));
  174.         permissions.add(7, new NameValuePair(getPermissionName(PERMISSION_ASSIGN_SELF, locale), PermissionType.valueOf(PERMISSION_ASSIGN_SELF).name()));
  175.         permissions.add(8, new NameValuePair(getPermissionName(PERMISSION_UNASSIGN_SELF, locale), PermissionType.valueOf(PERMISSION_UNASSIGN_SELF).name()));
  176.         permissions.add(9, new NameValuePair(getPermissionName(PERMISSION_ASSIGN_OTHERS, locale), PermissionType.valueOf(PERMISSION_ASSIGN_OTHERS).name()));
  177.         permissions.add(10, new NameValuePair(getPermissionName(PERMISSION_VIEW_ALL, locale), PermissionType.valueOf(PERMISSION_VIEW_ALL).name()));
  178.         permissions.add(11, new NameValuePair(getPermissionName(PERMISSION_VIEW_USERS, locale), PermissionType.valueOf(PERMISSION_VIEW_USERS).name()));
  179.         permissions.add(12, new NameValuePair(getPermissionName(PERMISSION_PRODUCT_ADMIN, locale), PermissionType.valueOf(PERMISSION_PRODUCT_ADMIN).name()));
  180.         return permissions;
  181.     }

  182.     /**
  183.      * Genrates a new random password.  The password that is returned is in plain text.
  184.      *
  185.      * @return a new randon plaintext password
  186.      */
  187.     public static String generatePassword() throws PasswordException {
  188.         StringBuffer buf = new StringBuffer();
  189.         Random rand = new Random();
  190.         for (int i = 0; i < 8; i++) {
  191.             buf.append((rand.nextInt(2) == 0 ? Character.toUpperCase(alphabet[rand.nextInt(34)]) : alphabet[rand.nextInt(34)]));
  192.         }
  193.         return buf.toString();
  194.     }

  195.     /**
  196.      * Returns an encrypted (digest) password from a plain text password.
  197.      *
  198.      * @param password the plain text password to encrypt.
  199.      * @return the encrypted password
  200.      */
  201.     public static String encryptPassword(String password) throws PasswordException {
  202.         String hash = null;
  203.         if (password != null && !password.equals("")) {
  204.             try {
  205.                 MessageDigest md = MessageDigest.getInstance("SHA");
  206.                 md.update(password.getBytes("UTF-8"));
  207.                 byte raw[] = md.digest();
  208.                 // TODO: must we really use this BASE64Encoder()? it seems to be not support by jrockit rt.jar
  209. //                hash = Base64.encodeBytes(raw);
  210.                 hash = String.valueOf(Base64Coder.encode(raw));
  211. //                hash = (new BASE64Encoder()).encode(raw);
  212.             } catch (NoSuchAlgorithmException nsae) {
  213.                 throw new PasswordException(PasswordException.SYSTEM_ERROR);
  214.             } catch (UnsupportedEncodingException uee) {
  215.                 throw new PasswordException(PasswordException.SYSTEM_ERROR);
  216.             }
  217.         }
  218.         return hash;
  219.     }


  220.     /**
  221.      * Checks to see if the user is a super user.
  222.      *
  223.      * @param permissionsMap map of user permissions by project Id
  224.      * @return true is the user is a super user
  225.      */
  226.     @Deprecated
  227.     public static boolean isSuperUser(Map<Integer, Set<PermissionType>> permissionsMap) {
  228.         if (permissionsMap == null) {
  229.             return false;
  230.         }

  231.         // Super user has access to all projects, which is indicated by null.
  232.         final Set<PermissionType> permissionTypes = permissionsMap.get(null);

  233.         return (permissionTypes != null) && permissionTypes.contains(PermissionType.USER_ADMIN);
  234.     }

  235.     @Deprecated
  236.     public static boolean hasPermission(Map<Integer, Set<PermissionType>> permissionsMap, int permissionNeeded) {
  237.         return hasPermission(permissionsMap, PermissionType.valueOf(permissionNeeded));
  238.     }

  239.     /**
  240.      * Returns true if the user has the required permission in any project.
  241.      *
  242.      * @param permissionsMap   a Map of the user's permissions by project ID
  243.      * @param permissionNeeded the permission to check for
  244.      */
  245.     @Deprecated
  246.     public static boolean hasPermission(Map<Integer, Set<PermissionType>> permissionsMap, PermissionType permissionNeeded) {
  247.         if (permissionsMap == null) {
  248.             return false;
  249.         }

  250.         if (isSuperUser(permissionsMap)) {
  251.             return true;
  252.         }

  253.         // Set of project Ids for which the user has permissions.
  254.         Set<Integer> keySet = permissionsMap.keySet();

  255.         for (Iterator<Integer> iterator = keySet.iterator(); iterator.hasNext(); ) {
  256.             Integer projectId = iterator.next();
  257.             if (hasPermission(permissionsMap, projectId, permissionNeeded)) {
  258.                 return true;
  259.             }
  260.         }
  261.         return false;
  262.     }

  263.     /**
  264.      * Returns true if the user has any of required permissions in any project.
  265.      *
  266.      * @param permissionsMap    a HashMap of the user's permissions
  267.      * @param permissionsNeeded a list of permissions that can fulfill the permission check
  268.      */
  269.     @Deprecated
  270.     public static boolean hasPermission(Map<Integer, Set<PermissionType>> permissionsMap, PermissionType[] permissionsNeeded) {
  271.         if (permissionsMap == null) {
  272.             return false;
  273.         }

  274.         if (isSuperUser(permissionsMap)) {
  275.             return true;
  276.         }

  277.         Set<Integer> keySet = permissionsMap.keySet();

  278.         for (Iterator<Integer> iterator = keySet.iterator(); iterator.hasNext(); ) {
  279.             Integer projectId = iterator.next();

  280.             if (hasPermission(permissionsMap, projectId, permissionsNeeded)) {
  281.                 return true;
  282.             }
  283.         }
  284.         return false;
  285.     }

  286.     @Deprecated
  287.     public static boolean hasPermission(Map<Integer, Set<PermissionType>> permissionsMap, Integer projectId, int permissionNeeded) {
  288.         return hasPermission(permissionsMap, projectId, PermissionType.valueOf(permissionNeeded));
  289.     }
  290.     /**
  291.      * Returns true if the user has the required permission for the given project.
  292.      *
  293.      * @param permissionsMap   a HashMap of the user's permissions
  294.      * @param projectId        the project that the permission is required for
  295.      * @param permissionNeeded the permission to check for
  296.      */
  297.     public static boolean hasPermission(Map<Integer, Set<PermissionType>> permissionsMap, Integer projectId, PermissionType permissionNeeded) {
  298.         if (permissionsMap == null) {
  299.             return false;
  300.         }

  301.         if (isSuperUser(permissionsMap)) {
  302.             return true;
  303.         }

  304.         final Set<PermissionType> permissionTypes = permissionsMap.get(projectId);

  305.         if ((permissionTypes != null) && permissionTypes.contains(permissionNeeded)) {
  306.             return true;
  307.         } else {
  308.             return false;
  309.         }
  310.     }

  311.     /**
  312.      * Returns true if the user has any of required permissions for the given project.
  313.      *
  314.      * @param permissionsMap    a HashMap of the user's permissions
  315.      * @param projectId         the project that the permission is required for
  316.      * @param permissionsNeeded a list of permissions that can fulfill the permission check
  317.      */
  318.     public static boolean hasPermission(Map<Integer, Set<PermissionType>> permissionsMap, Integer projectId, PermissionType[] permissionsNeeded) {
  319.         if (permissionsMap == null) {
  320.             return false;
  321.         }

  322.         if (isSuperUser(permissionsMap)) {
  323.             return true;
  324.         }

  325.         final Set<PermissionType> permissionTypes = permissionsMap.get(projectId);

  326.         if (permissionTypes != null) {
  327.             for (int i = 0; i < permissionsNeeded.length; i++) {
  328.                 if (permissionTypes.contains(permissionsNeeded[i])) {
  329.                     return true;
  330.                 }
  331.             }
  332.         }
  333.         return false;
  334.     }

  335.     public static String getInitial(String name) {
  336.         return (name != null && name.length() > 0 ? name.substring(0, 1).toUpperCase() + "." : "");
  337.     }

  338.     public static Permission[] createPermissionArray(User user, Project project, int[] permissions) {
  339.         Permission[] permissionsArray = new Permission[0];

  340.         List<Permission> permissionsList = new ArrayList<Permission>();

  341.         if (user.isSuperUser()) {
  342.             permissionsList.add(new Permission(PermissionType.valueOf(-1), user, (Project) null));
  343.         }

  344.         for (int i = 0; i < permissions.length; i++) {
  345.             permissionsList.add(new Permission(PermissionType.valueOf(permissions[i]), user, project));
  346.         }
  347.         permissionsArray = new Permission[permissionsList.size()];
  348.         permissionsArray = permissionsList.toArray(new Permission[]{});

  349.         return permissionsArray;
  350.     }

  351.     /**
  352.      * Maps sets of permission types by project ID.
  353.      */
  354.     public static Map<Integer, Set<PermissionType>> mapPermissionTypesByProjectId(
  355.             List<Permission> permissionsList) {

  356.         final Map<Integer, Set<PermissionType>> permissionsByProjectId =
  357.                 new HashMap<Integer, Set<PermissionType>>();

  358.         for (int i = 0; i < permissionsList.size(); i++) {
  359.             Permission permission = permissionsList.get(i);

  360.             // Super user has access to all projects, which is indicated by the "null" project.
  361.             final Integer projectId = (permission.getProject() == null)
  362.                     ? null : permission.getProject().getId();

  363.             Set<PermissionType> projectPermissions = permissionsByProjectId.get(projectId);

  364.             if (projectPermissions == null) {
  365.                 // First permission for the project.
  366.                 projectPermissions = new HashSet<PermissionType>();
  367.                 permissionsByProjectId.put(projectId, projectPermissions);
  368.             } //else { // Add the permission to the existing set of permissions for the project. }

  369.             PermissionType permissionType = permission.getPermissionType();
  370.             projectPermissions.add(permissionType);
  371.         }
  372.         return permissionsByProjectId;
  373.     }

  374.     /**
  375.      * Returns whether the user is currently hiding a particular section on the myItracker page.
  376.      *
  377.      * @param section  the section to check if it is hidden
  378.      * @param sections an integer of all sections the user is hiding
  379.      * @return true if the current section is hidden
  380.      */
  381.     public static boolean hideIndexSection(int section, int sections) {
  382.         return ((section & sections) == section);
  383.     }

  384.     public static Integer[] getHiddenIndexSections(int sections) {
  385.         List<Integer> sectionsList = new ArrayList<Integer>();
  386.         if (hideIndexSection(PREF_HIDE_ASSIGNED, sections)) {
  387.             sectionsList.add(Integer.valueOf(PREF_HIDE_ASSIGNED));
  388.         }
  389.         if (hideIndexSection(PREF_HIDE_UNASSIGNED, sections)) {
  390.             sectionsList.add(Integer.valueOf(PREF_HIDE_UNASSIGNED));
  391.         }
  392.         if (hideIndexSection(PREF_HIDE_CREATED, sections)) {
  393.             sectionsList.add(Integer.valueOf(PREF_HIDE_CREATED));
  394.         }
  395.         if (hideIndexSection(PREF_HIDE_WATCHED, sections)) {
  396.             sectionsList.add(Integer.valueOf(PREF_HIDE_WATCHED));
  397.         }
  398.         Integer[] sectionsArray = new Integer[sectionsList.size()];
  399.         sectionsList.toArray(sectionsArray);

  400.         return sectionsArray;
  401.     }

  402. }