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.

UserModel.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.models;
  17. import java.io.Serializable;
  18. import java.security.Principal;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import com.gitblit.Constants.AccessRestrictionType;
  22. import com.gitblit.Constants.AuthorizationControl;
  23. import com.gitblit.utils.StringUtils;
  24. /**
  25. * UserModel is a serializable model class that represents a user and the user's
  26. * restricted repository memberships. Instances of UserModels are also used as
  27. * servlet user principals.
  28. *
  29. * @author James Moger
  30. *
  31. */
  32. public class UserModel implements Principal, Serializable, Comparable<UserModel> {
  33. private static final long serialVersionUID = 1L;
  34. // field names are reflectively mapped in EditUser page
  35. public String username;
  36. public String password;
  37. public String cookie;
  38. public String displayName;
  39. public String emailAddress;
  40. public boolean canAdmin;
  41. public boolean canFork;
  42. public boolean excludeFromFederation;
  43. public final Set<String> repositories = new HashSet<String>();
  44. public final Set<TeamModel> teams = new HashSet<TeamModel>();
  45. // non-persisted fields
  46. public boolean isAuthenticated;
  47. public UserModel(String username) {
  48. this.username = username;
  49. this.isAuthenticated = true;
  50. }
  51. /**
  52. * This method does not take into consideration Ownership where the
  53. * administrator has not explicitly granted access to the owner.
  54. *
  55. * @param repositoryName
  56. * @return
  57. */
  58. @Deprecated
  59. public boolean canAccessRepository(String repositoryName) {
  60. return canAdmin || repositories.contains(repositoryName.toLowerCase())
  61. || hasTeamAccess(repositoryName);
  62. }
  63. public boolean canAccessRepository(RepositoryModel repository) {
  64. boolean isOwner = !StringUtils.isEmpty(repository.owner)
  65. && repository.owner.equals(username);
  66. boolean allowAuthenticated = isAuthenticated && AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl);
  67. return canAdmin || isOwner || repositories.contains(repository.name.toLowerCase())
  68. || hasTeamAccess(repository.name) || allowAuthenticated;
  69. }
  70. public boolean hasTeamAccess(String repositoryName) {
  71. for (TeamModel team : teams) {
  72. if (team.hasRepository(repositoryName)) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. public boolean canForkRepository(RepositoryModel repository) {
  79. if (canAdmin) {
  80. return true;
  81. }
  82. if (!canFork) {
  83. // user has been prohibited from forking
  84. return false;
  85. }
  86. if (!isAuthenticated) {
  87. // unauthenticated user model
  88. return false;
  89. }
  90. if (("~" + username).equalsIgnoreCase(repository.projectPath)) {
  91. // this repository is already a personal repository
  92. return false;
  93. }
  94. if (!repository.allowForks) {
  95. // repository prohibits forks
  96. return false;
  97. }
  98. if (repository.accessRestriction.atLeast(AccessRestrictionType.CLONE)) {
  99. return canAccessRepository(repository);
  100. }
  101. // repository is not clone-restricted
  102. return true;
  103. }
  104. public boolean hasRepository(String name) {
  105. return repositories.contains(name.toLowerCase());
  106. }
  107. public void addRepository(String name) {
  108. repositories.add(name.toLowerCase());
  109. }
  110. public void removeRepository(String name) {
  111. repositories.remove(name.toLowerCase());
  112. }
  113. public boolean isTeamMember(String teamname) {
  114. for (TeamModel team : teams) {
  115. if (team.name.equalsIgnoreCase(teamname)) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. public TeamModel getTeam(String teamname) {
  122. if (teams == null) {
  123. return null;
  124. }
  125. for (TeamModel team : teams) {
  126. if (team.name.equalsIgnoreCase(teamname)) {
  127. return team;
  128. }
  129. }
  130. return null;
  131. }
  132. @Override
  133. public String getName() {
  134. return username;
  135. }
  136. public String getDisplayName() {
  137. if (StringUtils.isEmpty(displayName)) {
  138. return username;
  139. }
  140. return displayName;
  141. }
  142. @Override
  143. public String toString() {
  144. return username;
  145. }
  146. @Override
  147. public int compareTo(UserModel o) {
  148. return username.compareTo(o.username);
  149. }
  150. }