You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModelUtils.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.gitblit.utils;
  2. import com.gitblit.IStoredSettings;
  3. import com.gitblit.Keys;
  4. /**
  5. * Utility functions for model classes that do not fit in any other category.
  6. *
  7. * @author Florian Zschocke
  8. */
  9. public class ModelUtils
  10. {
  11. /**
  12. * Default value for the prefix for user repository directories.
  13. */
  14. private static final String DEFAULT_USER_REPO_PREFIX = "~";
  15. private static String userRepoPrefix = DEFAULT_USER_REPO_PREFIX;
  16. /**
  17. * Set the user repository prefix from configuration settings.
  18. * @param settings
  19. */
  20. public static void setUserRepoPrefix(IStoredSettings settings)
  21. {
  22. String newPrefix = DEFAULT_USER_REPO_PREFIX;
  23. if (settings != null) {
  24. String prefix = settings.getString(Keys.git.userRepositoryPrefix, DEFAULT_USER_REPO_PREFIX);
  25. if (prefix != null && !prefix.trim().isEmpty()) {
  26. if (prefix.charAt(0) == '/') prefix = prefix.substring(1);
  27. newPrefix = prefix;
  28. }
  29. }
  30. userRepoPrefix = newPrefix;
  31. }
  32. /**
  33. * Get the active user repository project prefix.
  34. */
  35. public static String getUserRepoPrefix()
  36. {
  37. return userRepoPrefix;
  38. }
  39. /**
  40. * Get the user project name for a user.
  41. *
  42. * @param username name of user
  43. * @return the active user repository project prefix concatenated with the user name
  44. */
  45. public static String getPersonalPath(String username)
  46. {
  47. return userRepoPrefix + username.toLowerCase();
  48. }
  49. /**
  50. * Test if a repository path is for a personal repository.
  51. *
  52. * @param name
  53. * A project name, a relative path to a repository.
  54. * @return true, if the name starts with the active user repository project prefix. False, otherwise.
  55. */
  56. public static boolean isPersonalRepository(String name)
  57. {
  58. if ( name.startsWith(getUserRepoPrefix()) ) return true;
  59. return false;
  60. }
  61. /**
  62. * Test if a repository path is for a personal repository of a specific user.
  63. *
  64. * @param username
  65. * Name of a user
  66. * @param name
  67. * A project name, a relative path to a repository.
  68. * @return true, if the name starts with the active user repository project prefix. False, otherwise.
  69. */
  70. public static boolean isUsersPersonalRepository(String username, String name)
  71. {
  72. if ( name.equalsIgnoreCase(getPersonalPath(username)) ) return true;
  73. return false;
  74. }
  75. /**
  76. * Exrtract a user's name from a personal repository path.
  77. *
  78. * @param path
  79. * A project name, a relative path to a repository.
  80. * @return If the path does not point to a personal repository, an empty string is returned.
  81. * Otherwise the name of the user the personal repository belongs to is returned.
  82. */
  83. public static String getUserNameFromRepoPath(String path)
  84. {
  85. if ( !isPersonalRepository(path) ) return "";
  86. return path.substring(getUserRepoPrefix().length());
  87. }
  88. }