Prevent injecting IdentifiedUser for project REST endpoints
To support gathering of all UiActions (i. e. REST endpoints) defined for project resource, we can not support injection of IdentifiedUser because the actions gathering take also place when user is not authenticated. Refactor the code to not need injection of IdentifiedUser. Change-Id: I3bbfc4701685adfd0e04b12cedc6dff86c9c105b
This commit is contained in:
@@ -19,6 +19,7 @@ import com.google.gerrit.server.GerritPersonIdent;
|
|||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
|
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.assistedinject.Assisted;
|
import com.google.inject.assistedinject.Assisted;
|
||||||
|
|
||||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||||
@@ -35,30 +36,34 @@ public class MetaDataUpdate {
|
|||||||
private final InternalFactory factory;
|
private final InternalFactory factory;
|
||||||
private final GitRepositoryManager mgr;
|
private final GitRepositoryManager mgr;
|
||||||
private final PersonIdent serverIdent;
|
private final PersonIdent serverIdent;
|
||||||
private final PersonIdent userIdent;
|
private final Provider<IdentifiedUser> identifiedUser;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
User(InternalFactory factory, GitRepositoryManager mgr,
|
User(InternalFactory factory, GitRepositoryManager mgr,
|
||||||
@GerritPersonIdent PersonIdent serverIdent, IdentifiedUser currentUser) {
|
@GerritPersonIdent PersonIdent serverIdent,
|
||||||
|
Provider<IdentifiedUser> identifiedUser) {
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
this.mgr = mgr;
|
this.mgr = mgr;
|
||||||
this.serverIdent = serverIdent;
|
this.serverIdent = serverIdent;
|
||||||
this.userIdent = currentUser.newCommitterIdent( //
|
this.identifiedUser = identifiedUser;
|
||||||
serverIdent.getWhen(), //
|
|
||||||
serverIdent.getTimeZone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PersonIdent getUserPersonIdent() {
|
public PersonIdent getUserPersonIdent() {
|
||||||
return userIdent;
|
return createPersonIdent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MetaDataUpdate create(Project.NameKey name)
|
public MetaDataUpdate create(Project.NameKey name)
|
||||||
throws RepositoryNotFoundException, IOException {
|
throws RepositoryNotFoundException, IOException {
|
||||||
MetaDataUpdate md = factory.create(name, mgr.openRepository(name));
|
MetaDataUpdate md = factory.create(name, mgr.openRepository(name));
|
||||||
md.getCommitBuilder().setAuthor(userIdent);
|
md.getCommitBuilder().setAuthor(createPersonIdent());
|
||||||
md.getCommitBuilder().setCommitter(serverIdent);
|
md.getCommitBuilder().setCommitter(serverIdent);
|
||||||
return md;
|
return md;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private PersonIdent createPersonIdent() {
|
||||||
|
return identifiedUser.get().newCommitterIdent(
|
||||||
|
serverIdent.getWhen(), serverIdent.getTimeZone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Server {
|
public static class Server {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import com.google.gerrit.server.auth.AuthException;
|
|||||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||||
import com.google.gerrit.server.project.SetHead.Input;
|
import com.google.gerrit.server.project.SetHead.Input;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Provider;
|
||||||
|
|
||||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
@@ -40,10 +41,10 @@ public class SetHead implements RestModifyView<ProjectResource, Input> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final GitRepositoryManager repoManager;
|
private final GitRepositoryManager repoManager;
|
||||||
private final IdentifiedUser identifiedUser;
|
private final Provider<IdentifiedUser> identifiedUser;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SetHead(GitRepositoryManager repoManager, IdentifiedUser identifiedUser) {
|
SetHead(GitRepositoryManager repoManager, Provider<IdentifiedUser> identifiedUser) {
|
||||||
this.repoManager = repoManager;
|
this.repoManager = repoManager;
|
||||||
this.identifiedUser = identifiedUser;
|
this.identifiedUser = identifiedUser;
|
||||||
}
|
}
|
||||||
@@ -73,7 +74,7 @@ public class SetHead implements RestModifyView<ProjectResource, Input> {
|
|||||||
|
|
||||||
if (!repo.getRef(Constants.HEAD).getTarget().getName().equals(ref)) {
|
if (!repo.getRef(Constants.HEAD).getTarget().getName().equals(ref)) {
|
||||||
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
|
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
|
||||||
u.setRefLogIdent(identifiedUser.newRefLogIdent());
|
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
|
||||||
RefUpdate.Result res = u.link(ref);
|
RefUpdate.Result res = u.link(ref);
|
||||||
switch(res) {
|
switch(res) {
|
||||||
case NO_CHANGE:
|
case NO_CHANGE:
|
||||||
|
|||||||
Reference in New Issue
Block a user