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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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.ArrayList;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import com.gitblit.Constants.AccessPermission;
  27. import com.gitblit.Constants.AccessRestrictionType;
  28. import com.gitblit.Constants.AuthorizationControl;
  29. import com.gitblit.Constants.RegistrantType;
  30. import com.gitblit.Constants.Unused;
  31. import com.gitblit.utils.ArrayUtils;
  32. import com.gitblit.utils.StringUtils;
  33. /**
  34. * UserModel is a serializable model class that represents a user and the user's
  35. * restricted repository memberships. Instances of UserModels are also used as
  36. * servlet user principals.
  37. *
  38. * @author James Moger
  39. *
  40. */
  41. public class UserModel implements Principal, Serializable, Comparable<UserModel> {
  42. private static final long serialVersionUID = 1L;
  43. public static final UserModel ANONYMOUS = new UserModel();
  44. // field names are reflectively mapped in EditUser page
  45. public String username;
  46. public String password;
  47. public String cookie;
  48. public String displayName;
  49. public String emailAddress;
  50. public boolean canAdmin;
  51. public boolean canFork;
  52. public boolean canCreate;
  53. public boolean excludeFromFederation;
  54. // retained for backwards-compatibility with RPC clients
  55. @Deprecated
  56. public final Set<String> repositories = new HashSet<String>();
  57. public final Map<String, AccessPermission> permissions = new HashMap<String, AccessPermission>();
  58. public final Set<TeamModel> teams = new HashSet<TeamModel>();
  59. // non-persisted fields
  60. public boolean isAuthenticated;
  61. public UserModel(String username) {
  62. this.username = username;
  63. this.isAuthenticated = true;
  64. }
  65. private UserModel() {
  66. this.username = "$anonymous";
  67. this.isAuthenticated = false;
  68. }
  69. /**
  70. * This method does not take into consideration Ownership where the
  71. * administrator has not explicitly granted access to the owner.
  72. *
  73. * @param repositoryName
  74. * @return
  75. */
  76. @Deprecated
  77. public boolean canAccessRepository(String repositoryName) {
  78. return canAdmin() || repositories.contains(repositoryName.toLowerCase())
  79. || hasTeamAccess(repositoryName);
  80. }
  81. @Deprecated
  82. @Unused
  83. public boolean canAccessRepository(RepositoryModel repository) {
  84. boolean isOwner = !StringUtils.isEmpty(repository.owner)
  85. && repository.owner.equals(username);
  86. boolean allowAuthenticated = isAuthenticated && AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl);
  87. return canAdmin() || isOwner || repositories.contains(repository.name.toLowerCase())
  88. || hasTeamAccess(repository.name) || allowAuthenticated;
  89. }
  90. @Deprecated
  91. @Unused
  92. public boolean hasTeamAccess(String repositoryName) {
  93. for (TeamModel team : teams) {
  94. if (team.hasRepositoryPermission(repositoryName)) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. @Deprecated
  101. @Unused
  102. public boolean hasRepository(String name) {
  103. return hasRepositoryPermission(name);
  104. }
  105. @Deprecated
  106. @Unused
  107. public void addRepository(String name) {
  108. addRepositoryPermission(name);
  109. }
  110. @Deprecated
  111. @Unused
  112. public void removeRepository(String name) {
  113. removeRepositoryPermission(name);
  114. }
  115. /**
  116. * Returns a list of repository permissions for this user exclusive of
  117. * permissions inherited from team memberships.
  118. *
  119. * @return the user's list of permissions
  120. */
  121. public List<RegistrantAccessPermission> getRepositoryPermissions() {
  122. List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
  123. for (Map.Entry<String, AccessPermission> entry : permissions.entrySet()) {
  124. list.add(new RegistrantAccessPermission(entry.getKey(), entry.getValue(), true, RegistrantType.REPOSITORY));
  125. }
  126. Collections.sort(list);
  127. return list;
  128. }
  129. /**
  130. * Returns true if the user has any type of specified access permission for
  131. * this repository.
  132. *
  133. * @param name
  134. * @return true if user has a specified access permission for the repository
  135. */
  136. public boolean hasRepositoryPermission(String name) {
  137. String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
  138. if (permissions.containsKey(repository)) {
  139. // exact repository permission specified
  140. return true;
  141. } else {
  142. // search for regex permission match
  143. for (String key : permissions.keySet()) {
  144. if (name.matches(key)) {
  145. AccessPermission p = permissions.get(key);
  146. if (p != null) {
  147. return true;
  148. }
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. /**
  155. * Returns true if the user has an explicitly specified access permission for
  156. * this repository.
  157. *
  158. * @param name
  159. * @return if the user has an explicitly specified access permission
  160. */
  161. public boolean hasExplicitRepositoryPermission(String name) {
  162. String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
  163. return permissions.containsKey(repository);
  164. }
  165. /**
  166. * Adds a repository permission to the team.
  167. * <p>
  168. * Role may be formatted as:
  169. * <ul>
  170. * <li> myrepo.git <i>(this is implicitly RW+)</i>
  171. * <li> RW+:myrepo.git
  172. * </ul>
  173. * @param role
  174. */
  175. public void addRepositoryPermission(String role) {
  176. AccessPermission permission = AccessPermission.permissionFromRole(role);
  177. String repository = AccessPermission.repositoryFromRole(role).toLowerCase();
  178. repositories.add(repository);
  179. permissions.put(repository, permission);
  180. }
  181. public AccessPermission removeRepositoryPermission(String name) {
  182. String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
  183. repositories.remove(repository);
  184. return permissions.remove(repository);
  185. }
  186. public void setRepositoryPermission(String repository, AccessPermission permission) {
  187. permissions.put(repository.toLowerCase(), permission);
  188. }
  189. public AccessPermission getRepositoryPermission(RepositoryModel repository) {
  190. if (canAdmin() || repository.isOwner(username) || repository.isUsersPersonalRepository(username)) {
  191. return AccessPermission.REWIND;
  192. }
  193. if (AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl) && isAuthenticated) {
  194. // AUTHENTICATED is a shortcut for authorizing all logged-in users RW access
  195. return AccessPermission.REWIND;
  196. }
  197. // determine best permission available based on user's personal permissions
  198. // and the permissions of teams of which the user belongs
  199. AccessPermission permission = AccessPermission.NONE;
  200. if (permissions.containsKey(repository.name.toLowerCase())) {
  201. // exact repository permission specified, use it
  202. AccessPermission p = permissions.get(repository.name.toLowerCase());
  203. if (p != null) {
  204. return p;
  205. }
  206. } else {
  207. // search for case-insensitive regex permission match
  208. for (String key : permissions.keySet()) {
  209. if (StringUtils.matchesIgnoreCase(repository.name, key)) {
  210. AccessPermission p = permissions.get(key);
  211. if (p != null) {
  212. permission = p;
  213. }
  214. }
  215. }
  216. }
  217. for (TeamModel team : teams) {
  218. AccessPermission p = team.getRepositoryPermission(repository);
  219. if (permission == null || p.exceeds(permission)) {
  220. // use team permission
  221. permission = p;
  222. }
  223. }
  224. return permission;
  225. }
  226. protected boolean canAccess(RepositoryModel repository, AccessRestrictionType ifRestriction, AccessPermission requirePermission) {
  227. if (repository.accessRestriction.atLeast(ifRestriction)) {
  228. AccessPermission permission = getRepositoryPermission(repository);
  229. return permission.atLeast(requirePermission);
  230. }
  231. return true;
  232. }
  233. public boolean canView(RepositoryModel repository) {
  234. return canAccess(repository, AccessRestrictionType.VIEW, AccessPermission.VIEW);
  235. }
  236. public boolean canClone(RepositoryModel repository) {
  237. return canAccess(repository, AccessRestrictionType.CLONE, AccessPermission.CLONE);
  238. }
  239. public boolean canPush(RepositoryModel repository) {
  240. if (repository.isFrozen) {
  241. return false;
  242. }
  243. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.PUSH);
  244. }
  245. public boolean canCreateRef(RepositoryModel repository) {
  246. if (repository.isFrozen) {
  247. return false;
  248. }
  249. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.CREATE);
  250. }
  251. public boolean canDeleteRef(RepositoryModel repository) {
  252. if (repository.isFrozen) {
  253. return false;
  254. }
  255. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.DELETE);
  256. }
  257. public boolean canRewindRef(RepositoryModel repository) {
  258. if (repository.isFrozen) {
  259. return false;
  260. }
  261. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.REWIND);
  262. }
  263. public boolean canFork(RepositoryModel repository) {
  264. if (repository.isUsersPersonalRepository(username)) {
  265. // can not fork your own repository
  266. return false;
  267. }
  268. if (canAdmin() || repository.isOwner(username)) {
  269. return true;
  270. }
  271. if (!repository.allowForks) {
  272. return false;
  273. }
  274. if (!isAuthenticated || !canFork()) {
  275. return false;
  276. }
  277. return canClone(repository);
  278. }
  279. public boolean canDelete(RepositoryModel model) {
  280. return canAdmin() || model.isUsersPersonalRepository(username);
  281. }
  282. public boolean canEdit(RepositoryModel model) {
  283. return canAdmin() || model.isUsersPersonalRepository(username) || model.isOwner(username);
  284. }
  285. /**
  286. * This returns true if the user has fork privileges or the user has fork
  287. * privileges because of a team membership.
  288. *
  289. * @return true if the user can fork
  290. */
  291. public boolean canFork() {
  292. if (canFork) {
  293. return true;
  294. }
  295. if (!ArrayUtils.isEmpty(teams)) {
  296. for (TeamModel team : teams) {
  297. if (team.canFork) {
  298. return true;
  299. }
  300. }
  301. }
  302. return false;
  303. }
  304. /**
  305. * This returns true if the user has admin privileges or the user has admin
  306. * privileges because of a team membership.
  307. *
  308. * @return true if the user can admin
  309. */
  310. public boolean canAdmin() {
  311. if (canAdmin) {
  312. return true;
  313. }
  314. if (!ArrayUtils.isEmpty(teams)) {
  315. for (TeamModel team : teams) {
  316. if (team.canAdmin) {
  317. return true;
  318. }
  319. }
  320. }
  321. return false;
  322. }
  323. /**
  324. * This returns true if the user has create privileges or the user has create
  325. * privileges because of a team membership.
  326. *
  327. * @return true if the user can admin
  328. */
  329. public boolean canCreate() {
  330. if (canCreate) {
  331. return true;
  332. }
  333. if (!ArrayUtils.isEmpty(teams)) {
  334. for (TeamModel team : teams) {
  335. if (team.canCreate) {
  336. return true;
  337. }
  338. }
  339. }
  340. return false;
  341. }
  342. /**
  343. * Returns true if the user is allowed to create the specified repository.
  344. *
  345. * @param repository
  346. * @return true if the user can create the repository
  347. */
  348. public boolean canCreate(String repository) {
  349. if (canAdmin()) {
  350. // admins can create any repository
  351. return true;
  352. }
  353. if (canCreate) {
  354. String projectPath = StringUtils.getFirstPathElement(repository);
  355. if (!StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase("~" + username)) {
  356. // personal repository
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. public boolean isTeamMember(String teamname) {
  363. for (TeamModel team : teams) {
  364. if (team.name.equalsIgnoreCase(teamname)) {
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. public TeamModel getTeam(String teamname) {
  371. if (teams == null) {
  372. return null;
  373. }
  374. for (TeamModel team : teams) {
  375. if (team.name.equalsIgnoreCase(teamname)) {
  376. return team;
  377. }
  378. }
  379. return null;
  380. }
  381. @Override
  382. public String getName() {
  383. return username;
  384. }
  385. public String getDisplayName() {
  386. if (StringUtils.isEmpty(displayName)) {
  387. return username;
  388. }
  389. return displayName;
  390. }
  391. public String getPersonalPath() {
  392. return "~" + username;
  393. }
  394. @Override
  395. public int hashCode() {
  396. return username.hashCode();
  397. }
  398. @Override
  399. public boolean equals(Object o) {
  400. if (o instanceof UserModel) {
  401. return username.equals(((UserModel) o).username);
  402. }
  403. return false;
  404. }
  405. @Override
  406. public String toString() {
  407. return username;
  408. }
  409. @Override
  410. public int compareTo(UserModel o) {
  411. return username.compareTo(o.username);
  412. }
  413. /**
  414. * Returns true if the name/email pair match this user account.
  415. *
  416. * @param name
  417. * @param email
  418. * @return true, if the name and email address match this account
  419. */
  420. public boolean is(String name, String email) {
  421. // at a minimum a usename or display name must be supplied
  422. if (StringUtils.isEmpty(name)) {
  423. return false;
  424. }
  425. boolean nameVerified = name.equalsIgnoreCase(username) || name.equalsIgnoreCase(getDisplayName());
  426. boolean emailVerified = false;
  427. if (StringUtils.isEmpty(emailAddress)) {
  428. // user account has not specified an email address
  429. // rely on username/displayname verification
  430. emailVerified = true;
  431. } else {
  432. // user account has specified an email address
  433. // require email address verification
  434. if (!StringUtils.isEmpty(email)) {
  435. emailVerified = email.equalsIgnoreCase(emailAddress);
  436. }
  437. }
  438. return nameVerified && emailVerified;
  439. }
  440. public boolean hasBranchPermission(String repositoryName, String branch) {
  441. // Default UserModel doesn't implement branch-level security. Other Realms (i.e. Gerrit) may override this method.
  442. return hasRepositoryPermission(repositoryName);
  443. }
  444. }