Define user scope when parsing server config
To ensure that no groups are filtered out due to scope visibility while reading configuration from gerrit.config at server start, the current user is set to InternalUser for the duration of the lookup. Bug: issue 2045 Change-Id: I2a55925cc81c768c269f1c9b0a0eb3a1d5c8cff1
This commit is contained in:
committed by
Shawn Pearce
parent
50bc936427
commit
f71fdfe271
@@ -16,6 +16,8 @@ package com.google.gerrit.server.config;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
import com.google.gerrit.server.account.GroupBackend;
|
||||
import com.google.gerrit.server.util.ServerRequestContext;
|
||||
import com.google.gerrit.server.util.ThreadLocalRequestContext;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
@@ -25,8 +27,10 @@ import java.util.Collections;
|
||||
public class GitReceivePackGroupsProvider extends GroupSetProvider {
|
||||
@Inject
|
||||
public GitReceivePackGroupsProvider(GroupBackend gb,
|
||||
@GerritServerConfig Config config) {
|
||||
super(gb, config, "receive", null, "allowGroup");
|
||||
@GerritServerConfig Config config,
|
||||
ThreadLocalRequestContext threadContext,
|
||||
ServerRequestContext serverCtx) {
|
||||
super(gb, config, threadContext, serverCtx, "receive", null, "allowGroup");
|
||||
|
||||
// If no group was set, default to "registered users"
|
||||
//
|
||||
|
||||
@@ -16,6 +16,8 @@ package com.google.gerrit.server.config;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
import com.google.gerrit.server.account.GroupBackend;
|
||||
import com.google.gerrit.server.util.ServerRequestContext;
|
||||
import com.google.gerrit.server.util.ThreadLocalRequestContext;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
@@ -26,8 +28,10 @@ import java.util.HashSet;
|
||||
public class GitUploadPackGroupsProvider extends GroupSetProvider {
|
||||
@Inject
|
||||
public GitUploadPackGroupsProvider(GroupBackend gb,
|
||||
@GerritServerConfig Config config) {
|
||||
super(gb, config, "upload", null, "allowGroup");
|
||||
@GerritServerConfig Config config,
|
||||
ThreadLocalRequestContext threadContext,
|
||||
ServerRequestContext serverCtx) {
|
||||
super(gb, config, threadContext, serverCtx, "upload", null, "allowGroup");
|
||||
|
||||
// If no group was set, default to "registered users" and "anonymous"
|
||||
//
|
||||
|
||||
@@ -19,6 +19,9 @@ import com.google.gerrit.common.data.GroupReference;
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
import com.google.gerrit.server.account.GroupBackend;
|
||||
import com.google.gerrit.server.account.GroupBackends;
|
||||
import com.google.gerrit.server.util.RequestContext;
|
||||
import com.google.gerrit.server.util.ServerRequestContext;
|
||||
import com.google.gerrit.server.util.ThreadLocalRequestContext;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
@@ -37,19 +40,26 @@ public abstract class GroupSetProvider implements
|
||||
|
||||
@Inject
|
||||
protected GroupSetProvider(GroupBackend groupBackend,
|
||||
@GerritServerConfig Config config, String section,
|
||||
@GerritServerConfig Config config,
|
||||
ThreadLocalRequestContext threadContext,
|
||||
ServerRequestContext serverCtx, String section,
|
||||
String subsection, String name) {
|
||||
RequestContext ctx = threadContext.setContext(serverCtx);
|
||||
try {
|
||||
String[] groupNames = config.getStringList(section, subsection, name);
|
||||
ImmutableSet.Builder<AccountGroup.UUID> builder = ImmutableSet.builder();
|
||||
for (String n : groupNames) {
|
||||
GroupReference g = GroupBackends.findBestSuggestion(groupBackend, n);
|
||||
if (g == null) {
|
||||
log.warn("Group \"{0}\" not in database, skipping.", n);
|
||||
log.warn("Group \"{}\" not in database, skipping.", n);
|
||||
} else {
|
||||
builder.add(g.getUUID());
|
||||
}
|
||||
}
|
||||
groupIds = builder.build();
|
||||
} finally {
|
||||
threadContext.setContext(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
package com.google.gerrit.server.config;
|
||||
|
||||
import com.google.gerrit.server.account.GroupBackend;
|
||||
import com.google.gerrit.server.util.ServerRequestContext;
|
||||
import com.google.gerrit.server.util.ThreadLocalRequestContext;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
@@ -33,7 +35,9 @@ import org.eclipse.jgit.lib.Config;
|
||||
public class ProjectOwnerGroupsProvider extends GroupSetProvider {
|
||||
@Inject
|
||||
public ProjectOwnerGroupsProvider(GroupBackend gb,
|
||||
@GerritServerConfig final Config config) {
|
||||
super(gb, config, "repository", "*", "ownerGroup");
|
||||
@GerritServerConfig final Config config,
|
||||
ThreadLocalRequestContext context,
|
||||
ServerRequestContext serverCtx) {
|
||||
super(gb, config, context, serverCtx, "repository", "*", "ownerGroup");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.util;
|
||||
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.InternalUser;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.ProvisionException;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/** RequestContext with an InternalUser making the internals visible. */
|
||||
public class ServerRequestContext implements RequestContext {
|
||||
private final InternalUser user;
|
||||
|
||||
@Inject
|
||||
ServerRequestContext(InternalUser.Factory userFactory) {
|
||||
this.user = userFactory.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CurrentUser getCurrentUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Provider<ReviewDb> getReviewDbProvider() {
|
||||
return new Provider<ReviewDb>() {
|
||||
@Override
|
||||
public ReviewDb get() {
|
||||
throw new ProvisionException(
|
||||
"Automatic ReviewDb only available in request scope");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user