Fix in CapabilityControl when there are no Global Capabilities

A server should have the Global Capabilities block with a default
query limit, but some older installations might not have this set
yet in their All-Projects configuration.

Change-Id: I07292d49a5502648c36fffb2494831b64862cf7a
This commit is contained in:
Shawn O. Pearce
2011-06-16 08:41:27 -07:00
parent d070d81ade
commit 7bd554edcb
3 changed files with 56 additions and 32 deletions

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.common.data.AccountDashboardInfo;
import com.google.gerrit.common.data.ChangeInfo; import com.google.gerrit.common.data.ChangeInfo;
import com.google.gerrit.common.data.ChangeListService; import com.google.gerrit.common.data.ChangeListService;
import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.SingleListChangeInfo; import com.google.gerrit.common.data.SingleListChangeInfo;
import com.google.gerrit.common.data.ToggleStarRequest; import com.google.gerrit.common.data.ToggleStarRequest;
import com.google.gerrit.common.errors.InvalidQueryException; import com.google.gerrit.common.errors.InvalidQueryException;
@@ -302,16 +301,11 @@ public class ChangeListServiceImpl extends BaseServiceImplementation implements
private int safePageSize(final int pageSize) throws InvalidQueryException { private int safePageSize(final int pageSize) throws InvalidQueryException {
int maxLimit; int maxLimit;
try { try {
PermissionRange range = capabilityControlFactory.controlFor() maxLimit = capabilityControlFactory.controlFor()
.getRange(GlobalCapability.QUERY_LIMIT); .getRange(GlobalCapability.QUERY_LIMIT)
if (range != null) { .getMax();
maxLimit = range.getMax(); } catch (NoSuchProjectException e) {
} else { throw new InvalidQueryException("Search Disabled");
maxLimit = GlobalCapability.getRange(GlobalCapability.QUERY_LIMIT)
.getDefaultMax();
}
} catch (NoSuchProjectException noConfig) {
maxLimit = 0;
} }
if (maxLimit == 0) { if (maxLimit == 0) {
throw new InvalidQueryException("Search Disabled"); throw new InvalidQueryException("Search Disabled");

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.account;
import com.google.gerrit.common.data.AccessSection; import com.google.gerrit.common.data.AccessSection;
import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.common.data.Permission; import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange; import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.PermissionRule; import com.google.gerrit.common.data.PermissionRule;
@@ -107,29 +108,64 @@ public class CapabilityControl {
/** All rules that pertain to this user. */ /** All rules that pertain to this user. */
private Map<String, List<PermissionRule>> permissions() { private Map<String, List<PermissionRule>> permissions() {
if (permissions == null) { if (permissions == null) {
permissions = new HashMap<String, List<PermissionRule>>(); permissions = indexPermissions();
AccessSection section = }
state.getConfig().getAccessSection(AccessSection.GLOBAL_CAPABILITIES); return permissions;
for (Permission permission : section.getPermissions()) { }
for (PermissionRule rule : permission.getRules()) {
if (matchGroup(rule.getGroup().getUUID())) { private Map<String, List<PermissionRule>> indexPermissions() {
if (!rule.getDeny()) { Map<String, List<PermissionRule>> res =
List<PermissionRule> r = permissions.get(permission.getName()); new HashMap<String, List<PermissionRule>>();
if (r == null) {
r = new ArrayList<PermissionRule>(2); AccessSection section = state.getConfig()
permissions.put(permission.getName(), r); .getAccessSection(AccessSection.GLOBAL_CAPABILITIES);
} if (section == null) {
r.add(rule); section = new AccessSection(AccessSection.GLOBAL_CAPABILITIES);
}
for (Permission permission : section.getPermissions()) {
for (PermissionRule rule : permission.getRules()) {
if (matchGroup(rule.getGroup().getUUID())) {
if (!rule.getDeny()) {
List<PermissionRule> r = res.get(permission.getName());
if (r == null) {
r = new ArrayList<PermissionRule>(2);
res.put(permission.getName(), r);
} }
r.add(rule);
} }
} }
} }
} }
return permissions;
configureDefaults(res, section);
return res;
} }
private boolean matchGroup(AccountGroup.UUID uuid) { private boolean matchGroup(AccountGroup.UUID uuid) {
Set<AccountGroup.UUID> userGroups = getCurrentUser().getEffectiveGroups(); Set<AccountGroup.UUID> userGroups = getCurrentUser().getEffectiveGroups();
return userGroups.contains(uuid); return userGroups.contains(uuid);
} }
private static final GroupReference anonymous = new GroupReference(
AccountGroup.ANONYMOUS_USERS,
"Anonymous Users");
private static void configureDefaults(
Map<String, List<PermissionRule>> res,
AccessSection section) {
configureDefault(res, section, GlobalCapability.QUERY_LIMIT, anonymous);
}
private static void configureDefault(Map<String, List<PermissionRule>> res,
AccessSection section, String capName, GroupReference group) {
if (section.getPermission(capName) == null) {
PermissionRange.WithDefaults range = GlobalCapability.getRange(capName);
if (range != null) {
PermissionRule rule = new PermissionRule(group);
rule.setRange(range.getDefaultMin(), range.getDefaultMax());
res.put(capName, Collections.singletonList(rule));
}
}
}
} }

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.server.query.change; package com.google.gerrit.server.query.change;
import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.reviewdb.Change; import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.PatchSet; import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.reviewdb.ReviewDb; import com.google.gerrit.reviewdb.ReviewDb;
@@ -88,12 +87,7 @@ public class QueryProcessor {
this.queryRewriter = queryRewriter; this.queryRewriter = queryRewriter;
this.db = db; this.db = db;
PermissionRange range = ctl.controlFor().getRange(GlobalCapability.QUERY_LIMIT); defaultLimit = ctl.controlFor().getRange(GlobalCapability.QUERY_LIMIT).getMax();
if (range != null) {
defaultLimit = range.getMax();
} else {
defaultLimit = GlobalCapability.getRange(GlobalCapability.QUERY_LIMIT).getDefaultMax();
}
} }
public void setIncludePatchSets(boolean on) { public void setIncludePatchSets(boolean on) {