PageRenderTime 428ms CodeModel.GetById 44ms RepoModel.GetById 5ms app.codeStats 0ms

/java/com/google/gerrit/server/restapi/project/GetAccess.java

https://gitlab.com/chenfengxu/gerrit
Java | 348 lines | 281 code | 36 blank | 31 comment | 53 complexity | 42370f3e8b9a078da493da30b5dd654b MD5 | raw file
  1. // Copyright (C) 2016 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package com.google.gerrit.server.restapi.project;
  15. import static com.google.gerrit.server.permissions.GlobalPermission.ADMINISTRATE_SERVER;
  16. import static com.google.gerrit.server.permissions.ProjectPermission.CREATE_REF;
  17. import static com.google.gerrit.server.permissions.RefPermission.CREATE_CHANGE;
  18. import static com.google.gerrit.server.permissions.RefPermission.READ;
  19. import static com.google.gerrit.server.permissions.RefPermission.WRITE_CONFIG;
  20. import static java.util.stream.Collectors.toMap;
  21. import com.google.common.collect.ImmutableBiMap;
  22. import com.google.common.collect.Iterables;
  23. import com.google.gerrit.common.data.AccessSection;
  24. import com.google.gerrit.common.data.GroupDescription;
  25. import com.google.gerrit.common.data.Permission;
  26. import com.google.gerrit.common.data.PermissionRule;
  27. import com.google.gerrit.common.data.RefConfigSection;
  28. import com.google.gerrit.common.data.WebLinkInfoCommon;
  29. import com.google.gerrit.extensions.api.access.AccessSectionInfo;
  30. import com.google.gerrit.extensions.api.access.PermissionInfo;
  31. import com.google.gerrit.extensions.api.access.PermissionRuleInfo;
  32. import com.google.gerrit.extensions.api.access.ProjectAccessInfo;
  33. import com.google.gerrit.extensions.common.GroupInfo;
  34. import com.google.gerrit.extensions.common.WebLinkInfo;
  35. import com.google.gerrit.extensions.restapi.AuthException;
  36. import com.google.gerrit.extensions.restapi.ResourceConflictException;
  37. import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
  38. import com.google.gerrit.extensions.restapi.RestReadView;
  39. import com.google.gerrit.reviewdb.client.AccountGroup;
  40. import com.google.gerrit.reviewdb.client.Project;
  41. import com.google.gerrit.reviewdb.client.RefNames;
  42. import com.google.gerrit.server.CurrentUser;
  43. import com.google.gerrit.server.WebLinks;
  44. import com.google.gerrit.server.account.GroupBackend;
  45. import com.google.gerrit.server.config.AllProjectsName;
  46. import com.google.gerrit.server.git.meta.MetaDataUpdate;
  47. import com.google.gerrit.server.permissions.GlobalPermission;
  48. import com.google.gerrit.server.permissions.PermissionBackend;
  49. import com.google.gerrit.server.permissions.PermissionBackendException;
  50. import com.google.gerrit.server.permissions.ProjectPermission;
  51. import com.google.gerrit.server.permissions.RefPermission;
  52. import com.google.gerrit.server.project.ProjectCache;
  53. import com.google.gerrit.server.project.ProjectConfig;
  54. import com.google.gerrit.server.project.ProjectJson;
  55. import com.google.gerrit.server.project.ProjectResource;
  56. import com.google.gerrit.server.project.ProjectState;
  57. import com.google.inject.Inject;
  58. import com.google.inject.Provider;
  59. import com.google.inject.Singleton;
  60. import java.io.IOException;
  61. import java.util.ArrayList;
  62. import java.util.HashMap;
  63. import java.util.HashSet;
  64. import java.util.Map;
  65. import org.eclipse.jgit.errors.ConfigInvalidException;
  66. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  67. import org.slf4j.Logger;
  68. import org.slf4j.LoggerFactory;
  69. @Singleton
  70. public class GetAccess implements RestReadView<ProjectResource> {
  71. private static final Logger LOG = LoggerFactory.getLogger(GetAccess.class);
  72. public static final ImmutableBiMap<PermissionRule.Action, PermissionRuleInfo.Action> ACTION_TYPE =
  73. ImmutableBiMap.of(
  74. PermissionRule.Action.ALLOW,
  75. PermissionRuleInfo.Action.ALLOW,
  76. PermissionRule.Action.BATCH,
  77. PermissionRuleInfo.Action.BATCH,
  78. PermissionRule.Action.BLOCK,
  79. PermissionRuleInfo.Action.BLOCK,
  80. PermissionRule.Action.DENY,
  81. PermissionRuleInfo.Action.DENY,
  82. PermissionRule.Action.INTERACTIVE,
  83. PermissionRuleInfo.Action.INTERACTIVE);
  84. private final Provider<CurrentUser> user;
  85. private final PermissionBackend permissionBackend;
  86. private final AllProjectsName allProjectsName;
  87. private final ProjectJson projectJson;
  88. private final ProjectCache projectCache;
  89. private final MetaDataUpdate.Server metaDataUpdateFactory;
  90. private final GroupBackend groupBackend;
  91. private final WebLinks webLinks;
  92. @Inject
  93. public GetAccess(
  94. Provider<CurrentUser> self,
  95. PermissionBackend permissionBackend,
  96. AllProjectsName allProjectsName,
  97. ProjectCache projectCache,
  98. MetaDataUpdate.Server metaDataUpdateFactory,
  99. ProjectJson projectJson,
  100. GroupBackend groupBackend,
  101. WebLinks webLinks) {
  102. this.user = self;
  103. this.permissionBackend = permissionBackend;
  104. this.allProjectsName = allProjectsName;
  105. this.projectJson = projectJson;
  106. this.projectCache = projectCache;
  107. this.metaDataUpdateFactory = metaDataUpdateFactory;
  108. this.groupBackend = groupBackend;
  109. this.webLinks = webLinks;
  110. }
  111. public ProjectAccessInfo apply(Project.NameKey nameKey)
  112. throws ResourceNotFoundException, ResourceConflictException, IOException,
  113. PermissionBackendException {
  114. ProjectState state = projectCache.checkedGet(nameKey);
  115. if (state == null) {
  116. throw new ResourceNotFoundException(nameKey.get());
  117. }
  118. return apply(new ProjectResource(state, user.get()));
  119. }
  120. @Override
  121. public ProjectAccessInfo apply(ProjectResource rsrc)
  122. throws ResourceNotFoundException, ResourceConflictException, IOException,
  123. PermissionBackendException {
  124. // Load the current configuration from the repository, ensuring it's the most
  125. // recent version available. If it differs from what was in the project
  126. // state, force a cache flush now.
  127. Project.NameKey projectName = rsrc.getNameKey();
  128. ProjectAccessInfo info = new ProjectAccessInfo();
  129. ProjectState projectState = projectCache.checkedGet(projectName);
  130. PermissionBackend.ForProject perm = permissionBackend.currentUser().project(projectName);
  131. ProjectConfig config;
  132. try (MetaDataUpdate md = metaDataUpdateFactory.create(projectName)) {
  133. config = ProjectConfig.read(md);
  134. info.configWebLinks = new ArrayList<>();
  135. // config may have a null revision if the repo doesn't have its own refs/meta/config.
  136. if (config.getRevision() != null) {
  137. // WebLinks operates in terms of the data types used in the GWT UI. Once the GWT UI is
  138. // gone, WebLinks should be fixed to use the extension data types.
  139. for (WebLinkInfoCommon wl :
  140. webLinks.getFileHistoryLinks(
  141. projectName.get(), config.getRevision().getName(), ProjectConfig.PROJECT_CONFIG)) {
  142. info.configWebLinks.add(new WebLinkInfo(wl.name, wl.imageUrl, wl.url, wl.target));
  143. }
  144. }
  145. if (config.updateGroupNames(groupBackend)) {
  146. md.setMessage("Update group names\n");
  147. config.commit(md);
  148. projectCache.evict(config.getProject());
  149. projectState = projectCache.checkedGet(projectName);
  150. perm = permissionBackend.currentUser().project(projectName);
  151. } else if (config.getRevision() != null
  152. && !config.getRevision().equals(projectState.getConfig().getRevision())) {
  153. projectCache.evict(config.getProject());
  154. projectState = projectCache.checkedGet(projectName);
  155. perm = permissionBackend.currentUser().project(projectName);
  156. }
  157. } catch (ConfigInvalidException e) {
  158. throw new ResourceConflictException(e.getMessage());
  159. } catch (RepositoryNotFoundException e) {
  160. throw new ResourceNotFoundException(rsrc.getName());
  161. }
  162. // The following implementation must match the ProjectAccessFactory JSON RPC endpoint.
  163. info.local = new HashMap<>();
  164. info.ownerOf = new HashSet<>();
  165. Map<AccountGroup.UUID, GroupInfo> groups = new HashMap<>();
  166. boolean canReadConfig = check(perm, RefNames.REFS_CONFIG, READ);
  167. boolean canWriteConfig = check(perm, ProjectPermission.WRITE_CONFIG);
  168. // Check if the project state permits read only when the user is not allowed to write the config
  169. // (=owner). This is so that the owner can still read (and in the next step write) the project's
  170. // config to set the project state to any state that is not HIDDEN.
  171. if (!canWriteConfig) {
  172. projectState.checkStatePermitsRead();
  173. }
  174. for (AccessSection section : config.getAccessSections()) {
  175. String name = section.getName();
  176. if (AccessSection.GLOBAL_CAPABILITIES.equals(name)) {
  177. if (canWriteConfig) {
  178. info.local.put(name, createAccessSection(groups, section));
  179. info.ownerOf.add(name);
  180. } else if (canReadConfig) {
  181. info.local.put(section.getName(), createAccessSection(groups, section));
  182. }
  183. } else if (RefConfigSection.isValid(name)) {
  184. if (check(perm, name, WRITE_CONFIG)) {
  185. info.local.put(name, createAccessSection(groups, section));
  186. info.ownerOf.add(name);
  187. } else if (canReadConfig) {
  188. info.local.put(name, createAccessSection(groups, section));
  189. } else if (check(perm, name, READ)) {
  190. // Filter the section to only add rules describing groups that
  191. // are visible to the current-user. This includes any group the
  192. // user is a member of, as well as groups they own or that
  193. // are visible to all users.
  194. AccessSection dst = null;
  195. for (Permission srcPerm : section.getPermissions()) {
  196. Permission dstPerm = null;
  197. for (PermissionRule srcRule : srcPerm.getRules()) {
  198. AccountGroup.UUID groupId = srcRule.getGroup().getUUID();
  199. if (groupId == null) {
  200. continue;
  201. }
  202. loadGroup(groups, groupId);
  203. if (dstPerm == null) {
  204. if (dst == null) {
  205. dst = new AccessSection(name);
  206. info.local.put(name, createAccessSection(groups, dst));
  207. }
  208. dstPerm = dst.getPermission(srcPerm.getName(), true);
  209. }
  210. dstPerm.add(srcRule);
  211. }
  212. }
  213. }
  214. }
  215. }
  216. if (info.ownerOf.isEmpty()
  217. && permissionBackend.currentUser().test(GlobalPermission.ADMINISTRATE_SERVER)) {
  218. // Special case: If the section list is empty, this project has no current
  219. // access control information. Fall back to site administrators.
  220. info.ownerOf.add(AccessSection.ALL);
  221. }
  222. if (config.getRevision() != null) {
  223. info.revision = config.getRevision().name();
  224. }
  225. ProjectState parent = Iterables.getFirst(projectState.parents(), null);
  226. if (parent != null) {
  227. info.inheritsFrom = projectJson.format(parent.getProject());
  228. }
  229. if (projectName.equals(allProjectsName)
  230. && permissionBackend.currentUser().testOrFalse(ADMINISTRATE_SERVER)) {
  231. info.ownerOf.add(AccessSection.GLOBAL_CAPABILITIES);
  232. }
  233. info.isOwner = toBoolean(canWriteConfig);
  234. info.canUpload =
  235. toBoolean(
  236. projectState.statePermitsWrite()
  237. && (canWriteConfig
  238. || (canReadConfig
  239. && perm.ref(RefNames.REFS_CONFIG).testOrFalse(CREATE_CHANGE))));
  240. info.canAdd = toBoolean(perm.testOrFalse(CREATE_REF));
  241. info.configVisible = canReadConfig || canWriteConfig;
  242. info.groups =
  243. groups
  244. .entrySet()
  245. .stream()
  246. .filter(e -> e.getValue() != null)
  247. .collect(toMap(e -> e.getKey().get(), Map.Entry::getValue));
  248. return info;
  249. }
  250. private void loadGroup(Map<AccountGroup.UUID, GroupInfo> groups, AccountGroup.UUID id) {
  251. if (!groups.containsKey(id)) {
  252. GroupDescription.Basic basic = groupBackend.get(id);
  253. GroupInfo group;
  254. if (basic != null) {
  255. group = new GroupInfo();
  256. // The UI only needs name + URL, so don't populate other fields to avoid leaking data
  257. // about groups invisible to the user.
  258. group.name = basic.getName();
  259. group.url = basic.getUrl();
  260. } else {
  261. LOG.warn("no such group: " + id);
  262. group = null;
  263. }
  264. groups.put(id, group);
  265. }
  266. }
  267. private static boolean check(PermissionBackend.ForProject ctx, String ref, RefPermission perm)
  268. throws PermissionBackendException {
  269. try {
  270. ctx.ref(ref).check(perm);
  271. return true;
  272. } catch (AuthException denied) {
  273. return false;
  274. }
  275. }
  276. private static boolean check(PermissionBackend.ForProject ctx, ProjectPermission perm)
  277. throws PermissionBackendException {
  278. try {
  279. ctx.check(perm);
  280. return true;
  281. } catch (AuthException denied) {
  282. return false;
  283. }
  284. }
  285. private AccessSectionInfo createAccessSection(
  286. Map<AccountGroup.UUID, GroupInfo> groups, AccessSection section) {
  287. AccessSectionInfo accessSectionInfo = new AccessSectionInfo();
  288. accessSectionInfo.permissions = new HashMap<>();
  289. for (Permission p : section.getPermissions()) {
  290. PermissionInfo pInfo = new PermissionInfo(p.getLabel(), p.getExclusiveGroup() ? true : null);
  291. pInfo.rules = new HashMap<>();
  292. for (PermissionRule r : p.getRules()) {
  293. PermissionRuleInfo info =
  294. new PermissionRuleInfo(ACTION_TYPE.get(r.getAction()), r.getForce());
  295. if (r.hasRange()) {
  296. info.max = r.getMax();
  297. info.min = r.getMin();
  298. }
  299. AccountGroup.UUID group = r.getGroup().getUUID();
  300. if (group != null) {
  301. pInfo.rules.put(group.get(), info);
  302. loadGroup(groups, group);
  303. }
  304. }
  305. accessSectionInfo.permissions.put(p.getName(), pInfo);
  306. }
  307. return accessSectionInfo;
  308. }
  309. private static Boolean toBoolean(boolean value) {
  310. return value ? true : null;
  311. }
  312. }