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

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