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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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.HashSet;
  22. import java.util.LinkedHashMap;
  23. import java.util.LinkedHashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import java.util.TreeSet;
  28. import com.gitblit.Constants.AccessPermission;
  29. import com.gitblit.Constants.AccessRestrictionType;
  30. import com.gitblit.Constants.AccountType;
  31. import com.gitblit.Constants.AuthorizationControl;
  32. import com.gitblit.Constants.PermissionType;
  33. import com.gitblit.Constants.RegistrantType;
  34. import com.gitblit.Constants.Unused;
  35. import com.gitblit.utils.ArrayUtils;
  36. import com.gitblit.utils.ModelUtils;
  37. import com.gitblit.utils.StringUtils;
  38. /**
  39. * UserModel is a serializable model class that represents a user and the user's
  40. * restricted repository memberships. Instances of UserModels are also used as
  41. * servlet user principals.
  42. *
  43. * @author James Moger
  44. *
  45. */
  46. public class UserModel implements Principal, Serializable, Comparable<UserModel> {
  47. private static final long serialVersionUID = 1L;
  48. public static final UserModel ANONYMOUS = new UserModel();
  49. // field names are reflectively mapped in EditUser page
  50. public String username;
  51. public String password;
  52. public String cookie;
  53. public String displayName;
  54. public String emailAddress;
  55. public String organizationalUnit;
  56. public String organization;
  57. public String locality;
  58. public String stateProvince;
  59. public String countryCode;
  60. public boolean canAdmin;
  61. public boolean canFork;
  62. public boolean canCreate;
  63. public boolean excludeFromFederation;
  64. // retained for backwards-compatibility with RPC clients
  65. @Deprecated
  66. public final Set<String> repositories = new HashSet<String>();
  67. public final Map<String, AccessPermission> permissions = new LinkedHashMap<String, AccessPermission>();
  68. public final Set<TeamModel> teams = new TreeSet<TeamModel>();
  69. // non-persisted fields
  70. public boolean isAuthenticated;
  71. public AccountType accountType;
  72. public UserPreferences userPreferences;
  73. public UserModel(String username) {
  74. this.username = username;
  75. this.isAuthenticated = true;
  76. this.accountType = AccountType.LOCAL;
  77. this.userPreferences = new UserPreferences(this.username);
  78. }
  79. private UserModel() {
  80. this.username = "$anonymous";
  81. this.isAuthenticated = false;
  82. this.accountType = AccountType.LOCAL;
  83. this.userPreferences = new UserPreferences(this.username);
  84. }
  85. public boolean isLocalAccount() {
  86. return accountType.isLocal();
  87. }
  88. /**
  89. * This method does not take into consideration Ownership where the
  90. * administrator has not explicitly granted access to the owner.
  91. *
  92. * @param repositoryName
  93. * @return
  94. */
  95. @Deprecated
  96. public boolean canAccessRepository(String repositoryName) {
  97. return canAdmin() || repositories.contains(repositoryName.toLowerCase())
  98. || hasTeamAccess(repositoryName);
  99. }
  100. @Deprecated
  101. @Unused
  102. public boolean canAccessRepository(RepositoryModel repository) {
  103. boolean isOwner = repository.isOwner(username);
  104. boolean allowAuthenticated = isAuthenticated && AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl);
  105. return canAdmin() || isOwner || repositories.contains(repository.name.toLowerCase())
  106. || hasTeamAccess(repository.name) || allowAuthenticated;
  107. }
  108. @Deprecated
  109. @Unused
  110. public boolean hasTeamAccess(String repositoryName) {
  111. for (TeamModel team : teams) {
  112. if (team.hasRepositoryPermission(repositoryName)) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. @Deprecated
  119. @Unused
  120. public boolean hasRepository(String name) {
  121. return hasRepositoryPermission(name);
  122. }
  123. @Deprecated
  124. @Unused
  125. public void addRepository(String name) {
  126. addRepositoryPermission(name);
  127. }
  128. @Deprecated
  129. @Unused
  130. public void removeRepository(String name) {
  131. removeRepositoryPermission(name);
  132. }
  133. /**
  134. * Returns a list of repository permissions for this user exclusive of
  135. * permissions inherited from team memberships.
  136. *
  137. * @return the user's list of permissions
  138. */
  139. public List<RegistrantAccessPermission> getRepositoryPermissions() {
  140. List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
  141. if (canAdmin()) {
  142. // user has REWIND access to all repositories
  143. return list;
  144. }
  145. for (Map.Entry<String, AccessPermission> entry : permissions.entrySet()) {
  146. String registrant = entry.getKey();
  147. AccessPermission ap = entry.getValue();
  148. String source = null;
  149. boolean mutable = true;
  150. PermissionType pType = PermissionType.EXPLICIT;
  151. if (isMyPersonalRepository(registrant)) {
  152. pType = PermissionType.OWNER;
  153. ap = AccessPermission.REWIND;
  154. mutable = false;
  155. } else if (StringUtils.findInvalidCharacter(registrant) != null) {
  156. // a regex will have at least 1 invalid character
  157. pType = PermissionType.REGEX;
  158. source = registrant;
  159. }
  160. list.add(new RegistrantAccessPermission(registrant, ap, pType, RegistrantType.REPOSITORY, source, mutable));
  161. }
  162. Collections.sort(list);
  163. // include immutable team permissions, being careful to preserve order
  164. Set<RegistrantAccessPermission> set = new LinkedHashSet<RegistrantAccessPermission>(list);
  165. for (TeamModel team : teams) {
  166. for (RegistrantAccessPermission teamPermission : team.getRepositoryPermissions()) {
  167. // we can not change an inherited team permission, though we can override
  168. teamPermission.registrantType = RegistrantType.REPOSITORY;
  169. teamPermission.permissionType = PermissionType.TEAM;
  170. teamPermission.source = team.name;
  171. teamPermission.mutable = false;
  172. set.add(teamPermission);
  173. }
  174. }
  175. return new ArrayList<RegistrantAccessPermission>(set);
  176. }
  177. /**
  178. * Returns true if the user has any type of specified access permission for
  179. * this repository.
  180. *
  181. * @param name
  182. * @return true if user has a specified access permission for the repository
  183. */
  184. public boolean hasRepositoryPermission(String name) {
  185. String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
  186. if (permissions.containsKey(repository)) {
  187. // exact repository permission specified
  188. return true;
  189. } else {
  190. // search for regex permission match
  191. for (String key : permissions.keySet()) {
  192. if (name.matches(key)) {
  193. AccessPermission p = permissions.get(key);
  194. if (p != null) {
  195. return true;
  196. }
  197. }
  198. }
  199. }
  200. return false;
  201. }
  202. /**
  203. * Returns true if the user has an explicitly specified access permission for
  204. * this repository.
  205. *
  206. * @param name
  207. * @return if the user has an explicitly specified access permission
  208. */
  209. public boolean hasExplicitRepositoryPermission(String name) {
  210. String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
  211. return permissions.containsKey(repository);
  212. }
  213. /**
  214. * Returns true if the user's team memberships specify an access permission for
  215. * this repository.
  216. *
  217. * @param name
  218. * @return if the user's team memberships specifi an access permission
  219. */
  220. public boolean hasTeamRepositoryPermission(String name) {
  221. if (teams != null) {
  222. for (TeamModel team : teams) {
  223. if (team.hasRepositoryPermission(name)) {
  224. return true;
  225. }
  226. }
  227. }
  228. return false;
  229. }
  230. /**
  231. * Adds a repository permission to the team.
  232. * <p>
  233. * Role may be formatted as:
  234. * <ul>
  235. * <li> myrepo.git <i>(this is implicitly RW+)</i>
  236. * <li> RW+:myrepo.git
  237. * </ul>
  238. * @param role
  239. */
  240. public void addRepositoryPermission(String role) {
  241. AccessPermission permission = AccessPermission.permissionFromRole(role);
  242. String repository = AccessPermission.repositoryFromRole(role).toLowerCase();
  243. repositories.add(repository);
  244. permissions.put(repository, permission);
  245. }
  246. public AccessPermission removeRepositoryPermission(String name) {
  247. String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
  248. repositories.remove(repository);
  249. return permissions.remove(repository);
  250. }
  251. public void setRepositoryPermission(String repository, AccessPermission permission) {
  252. if (permission == null) {
  253. // remove the permission
  254. permissions.remove(repository.toLowerCase());
  255. } else {
  256. // set the new permission
  257. permissions.put(repository.toLowerCase(), permission);
  258. }
  259. }
  260. public RegistrantAccessPermission getRepositoryPermission(RepositoryModel repository) {
  261. RegistrantAccessPermission ap = new RegistrantAccessPermission();
  262. ap.registrant = username;
  263. ap.registrantType = RegistrantType.USER;
  264. ap.permission = AccessPermission.NONE;
  265. ap.mutable = false;
  266. // determine maximum permission for the repository
  267. final AccessPermission maxPermission =
  268. (repository.isFrozen || !repository.isBare || repository.isMirror) ?
  269. AccessPermission.CLONE : AccessPermission.REWIND;
  270. if (AccessRestrictionType.NONE.equals(repository.accessRestriction)) {
  271. // anonymous rewind
  272. ap.permissionType = PermissionType.ANONYMOUS;
  273. if (AccessPermission.REWIND.atMost(maxPermission)) {
  274. ap.permission = AccessPermission.REWIND;
  275. } else {
  276. ap.permission = maxPermission;
  277. }
  278. return ap;
  279. }
  280. // administrator
  281. if (canAdmin()) {
  282. ap.permissionType = PermissionType.ADMINISTRATOR;
  283. if (AccessPermission.REWIND.atMost(maxPermission)) {
  284. ap.permission = AccessPermission.REWIND;
  285. } else {
  286. ap.permission = maxPermission;
  287. }
  288. if (!canAdmin) {
  289. // administator permission from team membership
  290. for (TeamModel team : teams) {
  291. if (team.canAdmin) {
  292. ap.source = team.name;
  293. break;
  294. }
  295. }
  296. }
  297. return ap;
  298. }
  299. // repository owner - either specified owner or personal repository
  300. if (repository.isOwner(username) || repository.isUsersPersonalRepository(username)) {
  301. ap.permissionType = PermissionType.OWNER;
  302. if (AccessPermission.REWIND.atMost(maxPermission)) {
  303. ap.permission = AccessPermission.REWIND;
  304. } else {
  305. ap.permission = maxPermission;
  306. }
  307. return ap;
  308. }
  309. if (AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl) && isAuthenticated) {
  310. // AUTHENTICATED is a shortcut for authorizing all logged-in users RW+ access
  311. if (AccessPermission.REWIND.atMost(maxPermission)) {
  312. ap.permission = AccessPermission.REWIND;
  313. } else {
  314. ap.permission = maxPermission;
  315. }
  316. return ap;
  317. }
  318. // explicit user permission OR user regex match is used
  319. // if that fails, then the best team permission is used
  320. if (permissions.containsKey(repository.name.toLowerCase())) {
  321. // exact repository permission specified, use it
  322. AccessPermission p = permissions.get(repository.name.toLowerCase());
  323. if (p != null && repository.accessRestriction.isValidPermission(p)) {
  324. ap.permissionType = PermissionType.EXPLICIT;
  325. if (p.atMost(maxPermission)) {
  326. ap.permission = p;
  327. } else {
  328. ap.permission = maxPermission;
  329. }
  330. ap.mutable = true;
  331. return ap;
  332. }
  333. } else {
  334. // search for case-insensitive regex permission match
  335. for (String key : permissions.keySet()) {
  336. if (StringUtils.matchesIgnoreCase(repository.name, key)) {
  337. AccessPermission p = permissions.get(key);
  338. if (p != null && repository.accessRestriction.isValidPermission(p)) {
  339. // take first match
  340. ap.permissionType = PermissionType.REGEX;
  341. if (p.atMost(maxPermission)) {
  342. ap.permission = p;
  343. } else {
  344. ap.permission = maxPermission;
  345. }
  346. ap.source = key;
  347. return ap;
  348. }
  349. }
  350. }
  351. }
  352. // try to find a team match
  353. for (TeamModel team : teams) {
  354. RegistrantAccessPermission p = team.getRepositoryPermission(repository);
  355. if (p.permission.atMost(maxPermission) && p.permission.exceeds(ap.permission) && PermissionType.ANONYMOUS != p.permissionType) {
  356. // use highest team permission that is not an implicit permission
  357. ap.permission = p.permission;
  358. ap.source = team.name;
  359. ap.permissionType = PermissionType.TEAM;
  360. }
  361. }
  362. // still no explicit, regex, or team match, check for implicit permissions
  363. if (AccessPermission.NONE == ap.permission) {
  364. switch (repository.accessRestriction) {
  365. case VIEW:
  366. // no implicit permissions possible
  367. break;
  368. case CLONE:
  369. // implied view permission
  370. ap.permission = AccessPermission.VIEW;
  371. ap.permissionType = PermissionType.ANONYMOUS;
  372. break;
  373. case PUSH:
  374. // implied clone permission
  375. ap.permission = AccessPermission.CLONE;
  376. ap.permissionType = PermissionType.ANONYMOUS;
  377. break;
  378. case NONE:
  379. // implied REWIND or CLONE
  380. ap.permission = maxPermission;
  381. ap.permissionType = PermissionType.ANONYMOUS;
  382. break;
  383. }
  384. }
  385. return ap;
  386. }
  387. protected boolean canAccess(RepositoryModel repository, AccessRestrictionType ifRestriction, AccessPermission requirePermission) {
  388. if (repository.accessRestriction.atLeast(ifRestriction)) {
  389. RegistrantAccessPermission ap = getRepositoryPermission(repository);
  390. return ap.permission.atLeast(requirePermission);
  391. }
  392. return true;
  393. }
  394. public boolean canView(RepositoryModel repository) {
  395. return canAccess(repository, AccessRestrictionType.VIEW, AccessPermission.VIEW);
  396. }
  397. public boolean canView(RepositoryModel repository, String ref) {
  398. // Default UserModel doesn't implement ref-level security.
  399. // Other Realms (i.e. Gerrit) may override this method.
  400. return canView(repository);
  401. }
  402. public boolean canClone(RepositoryModel repository) {
  403. return canAccess(repository, AccessRestrictionType.CLONE, AccessPermission.CLONE);
  404. }
  405. public boolean canPush(RepositoryModel repository) {
  406. if (repository.isFrozen) {
  407. return false;
  408. }
  409. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.PUSH);
  410. }
  411. public boolean canCreateRef(RepositoryModel repository) {
  412. if (repository.isFrozen) {
  413. return false;
  414. }
  415. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.CREATE);
  416. }
  417. public boolean canDeleteRef(RepositoryModel repository) {
  418. if (repository.isFrozen) {
  419. return false;
  420. }
  421. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.DELETE);
  422. }
  423. public boolean canRewindRef(RepositoryModel repository) {
  424. if (repository.isFrozen) {
  425. return false;
  426. }
  427. return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.REWIND);
  428. }
  429. public boolean canFork(RepositoryModel repository) {
  430. if (repository.isUsersPersonalRepository(username)) {
  431. // can not fork your own repository
  432. return false;
  433. }
  434. if (canAdmin() || repository.isOwner(username)) {
  435. return true;
  436. }
  437. if (!repository.allowForks) {
  438. return false;
  439. }
  440. if (!isAuthenticated || !canFork()) {
  441. return false;
  442. }
  443. return canClone(repository);
  444. }
  445. public boolean canDelete(RepositoryModel model) {
  446. return canAdmin() || model.isUsersPersonalRepository(username);
  447. }
  448. public boolean canEdit(RepositoryModel model) {
  449. return canAdmin() || model.isUsersPersonalRepository(username) || model.isOwner(username);
  450. }
  451. /**
  452. * This returns true if the user has fork privileges or the user has fork
  453. * privileges because of a team membership.
  454. *
  455. * @return true if the user can fork
  456. */
  457. public boolean canFork() {
  458. if (canFork) {
  459. return true;
  460. }
  461. if (!ArrayUtils.isEmpty(teams)) {
  462. for (TeamModel team : teams) {
  463. if (team.canFork) {
  464. return true;
  465. }
  466. }
  467. }
  468. return false;
  469. }
  470. /**
  471. * This returns true if the user has admin privileges or the user has admin
  472. * privileges because of a team membership.
  473. *
  474. * @return true if the user can admin
  475. */
  476. public boolean canAdmin() {
  477. if (canAdmin) {
  478. return true;
  479. }
  480. if (!ArrayUtils.isEmpty(teams)) {
  481. for (TeamModel team : teams) {
  482. if (team.canAdmin) {
  483. return true;
  484. }
  485. }
  486. }
  487. return false;
  488. }
  489. /**
  490. * This returns true if the user has create privileges or the user has create
  491. * privileges because of a team membership.
  492. *
  493. * @return true if the user can admin
  494. */
  495. public boolean canCreate() {
  496. if (canCreate) {
  497. return true;
  498. }
  499. if (!ArrayUtils.isEmpty(teams)) {
  500. for (TeamModel team : teams) {
  501. if (team.canCreate) {
  502. return true;
  503. }
  504. }
  505. }
  506. return false;
  507. }
  508. /**
  509. * Returns true if the user is allowed to create the specified repository.
  510. *
  511. * @param repository
  512. * @return true if the user can create the repository
  513. */
  514. public boolean canCreate(String repository) {
  515. if (canAdmin()) {
  516. // admins can create any repository
  517. return true;
  518. }
  519. if (canCreate) {
  520. String projectPath = StringUtils.getFirstPathElement(repository);
  521. if (!StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase(getPersonalPath())) {
  522. // personal repository
  523. return true;
  524. }
  525. }
  526. return false;
  527. }
  528. public boolean isTeamMember(String teamname) {
  529. for (TeamModel team : teams) {
  530. if (team.name.equalsIgnoreCase(teamname)) {
  531. return true;
  532. }
  533. }
  534. return false;
  535. }
  536. public TeamModel getTeam(String teamname) {
  537. if (teams == null) {
  538. return null;
  539. }
  540. for (TeamModel team : teams) {
  541. if (team.name.equalsIgnoreCase(teamname)) {
  542. return team;
  543. }
  544. }
  545. return null;
  546. }
  547. @Override
  548. public String getName() {
  549. return username;
  550. }
  551. public String getDisplayName() {
  552. if (StringUtils.isEmpty(displayName)) {
  553. return username;
  554. }
  555. return displayName;
  556. }
  557. public String getPersonalPath() {
  558. return ModelUtils.getPersonalPath(username);
  559. }
  560. public UserPreferences getPreferences() {
  561. return userPreferences;
  562. }
  563. @Override
  564. public int hashCode() {
  565. return username.hashCode();
  566. }
  567. @Override
  568. public boolean equals(Object o) {
  569. if (o instanceof UserModel) {
  570. return username.equals(((UserModel) o).username);
  571. }
  572. return false;
  573. }
  574. @Override
  575. public String toString() {
  576. return username;
  577. }
  578. @Override
  579. public int compareTo(UserModel o) {
  580. return username.compareTo(o.username);
  581. }
  582. /**
  583. * Returns true if the name/email pair match this user account.
  584. *
  585. * @param name
  586. * @param email
  587. * @return true, if the name and email address match this account
  588. */
  589. public boolean is(String name, String email) {
  590. // at a minimum a username or display name AND email address must be supplied
  591. if (StringUtils.isEmpty(name) || StringUtils.isEmpty(email)) {
  592. return false;
  593. }
  594. boolean nameVerified = name.equalsIgnoreCase(username) || name.equalsIgnoreCase(getDisplayName());
  595. boolean emailVerified = false;
  596. if (StringUtils.isEmpty(emailAddress)) {
  597. // user account has not specified an email address
  598. // fail
  599. emailVerified = false;
  600. } else {
  601. // user account has specified an email address
  602. emailVerified = email.equalsIgnoreCase(emailAddress);
  603. }
  604. return nameVerified && emailVerified;
  605. }
  606. @Deprecated
  607. public boolean hasBranchPermission(String repositoryName, String branch) {
  608. // Default UserModel doesn't implement branch-level security. Other Realms (i.e. Gerrit) may override this method.
  609. return hasRepositoryPermission(repositoryName) || hasTeamRepositoryPermission(repositoryName);
  610. }
  611. public boolean isMyPersonalRepository(String repository) {
  612. String projectPath = StringUtils.getFirstPathElement(repository);
  613. return !StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase(getPersonalPath());
  614. }
  615. }