Native ChangeControl for user instead of delegate

We can now get a ChangeControl object on behalf
of another user, so that we can create Changes
owned by the Pull Request sender instead of the
currently logged in Gerrit user.

There is no need of using a ChangeControl delegate
with package access modifier in order to impersonate
another user.

Change-Id: Icdeca9875b9724c49b34980e3c4224f22b11a5b8
diff --git a/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java b/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java
deleted file mode 100644
index 7ccaa69..0000000
--- a/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java
+++ /dev/null
@@ -1,189 +0,0 @@
-// 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.project;
-
-import java.util.List;
-
-import com.google.gerrit.common.data.LabelTypes;
-import com.google.gerrit.common.data.PermissionRange;
-import com.google.gerrit.common.data.SubmitRecord;
-import com.google.gerrit.common.data.SubmitTypeRecord;
-import com.google.gerrit.reviewdb.client.Account.Id;
-import com.google.gerrit.reviewdb.client.Change;
-import com.google.gerrit.reviewdb.client.PatchSet;
-import com.google.gerrit.reviewdb.client.PatchSetApproval;
-import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.CurrentUser;
-import com.google.gerrit.server.query.change.ChangeData;
-import com.google.gwtorm.server.OrmException;
-import com.googlecode.prolog_cafe.lang.Term;
-
-public class ChangeControlDelegate extends ChangeControl {
-
-  private final ChangeControl realChangeControl;
-  private final CurrentUser delegatedUser;
-
-  private ChangeControlDelegate(ChangeControl realChangeControl, CurrentUser delegatedUser) {
-    // All methods are delegated, super class constructor needs to be invoked anyway with dummy values
-    super(null, null, null, null);
-    this.realChangeControl = realChangeControl;
-    this.delegatedUser = delegatedUser;
-  }
-
-  public static ChangeControl wrap(ChangeControl realChangeControl, CurrentUser delegatedUser) {
-    return new ChangeControlDelegate(realChangeControl, delegatedUser);
-  }
-
-  public int hashCode() {
-    return realChangeControl.hashCode();
-  }
-
-  public boolean equals(Object obj) {
-    return realChangeControl.equals(obj);
-  }
-
-  public ChangeControl forUser(CurrentUser who) {
-    return realChangeControl.forUser(who);
-  }
-
-  public RefControl getRefControl() {
-    return realChangeControl.getRefControl();
-  }
-
-  public CurrentUser getCurrentUser() {
-    return delegatedUser;
-  }
-
-  public ProjectControl getProjectControl() {
-    return realChangeControl.getProjectControl();
-  }
-
-  public Project getProject() {
-    return realChangeControl.getProject();
-  }
-
-  public Change getChange() {
-    return realChangeControl.getChange();
-  }
-
-  public boolean isVisible(ReviewDb db) throws OrmException {
-    return realChangeControl.isVisible(db);
-  }
-
-  public boolean isRefVisible() {
-    return realChangeControl.isRefVisible();
-  }
-
-  public boolean isPatchVisible(PatchSet ps, ReviewDb db) throws OrmException {
-    return realChangeControl.isPatchVisible(ps, db);
-  }
-
-  public boolean canAbandon() {
-    return realChangeControl.canAbandon();
-  }
-
-  public boolean canPublish(ReviewDb db) throws OrmException {
-    return realChangeControl.canPublish(db);
-  }
-
-  public boolean canDeleteDraft(ReviewDb db) throws OrmException {
-    return realChangeControl.canDeleteDraft(db);
-  }
-
-  public boolean canRebase() {
-    return realChangeControl.canRebase();
-  }
-
-  public boolean canRestore() {
-    return realChangeControl.canRestore();
-  }
-
-  public LabelTypes getLabelTypes() {
-    return realChangeControl.getLabelTypes();
-  }
-
-  public String toString() {
-    return realChangeControl.toString();
-  }
-
-  public List<PermissionRange> getLabelRanges() {
-    return realChangeControl.getLabelRanges();
-  }
-
-  public PermissionRange getRange(String permission) {
-    return realChangeControl.getRange(permission);
-  }
-
-  public boolean canAddPatchSet() {
-    return realChangeControl.canAddPatchSet();
-  }
-
-  public boolean isOwner() {
-    return realChangeControl.isOwner();
-  }
-
-  public boolean isReviewer(ReviewDb db) throws OrmException {
-    return realChangeControl.isReviewer(db);
-  }
-
-  public boolean isReviewer(ReviewDb db, ChangeData cd) throws OrmException {
-    return realChangeControl.isReviewer(db, cd);
-  }
-
-  public boolean canRemoveReviewer(PatchSetApproval approval) {
-    return realChangeControl.canRemoveReviewer(approval);
-  }
-
-  public boolean canRemoveReviewer(Id reviewer, int value) {
-    return realChangeControl.canRemoveReviewer(reviewer, value);
-  }
-
-  public boolean canEditTopicName() {
-    return realChangeControl.canEditTopicName();
-  }
-
-  public List<SubmitRecord> getSubmitRecords(ReviewDb db, PatchSet patchSet) {
-    return realChangeControl.getSubmitRecords(db, patchSet);
-  }
-
-  public boolean canSubmit() {
-    return realChangeControl.canSubmit();
-  }
-
-  public List<SubmitRecord> canSubmit(ReviewDb db, PatchSet patchSet) {
-    return realChangeControl.canSubmit(db, patchSet);
-  }
-
-  public List<SubmitRecord> canSubmit(ReviewDb db, PatchSet patchSet,
-      ChangeData cd, boolean fastEvalLabels, boolean allowClosed,
-      boolean allowDraft) {
-    return realChangeControl.canSubmit(db, patchSet, cd, fastEvalLabels,
-        allowClosed, allowDraft);
-  }
-
-  public List<SubmitRecord> resultsToSubmitRecord(Term submitRule,
-      List<Term> results) {
-    return realChangeControl.resultsToSubmitRecord(submitRule, results);
-  }
-
-  public SubmitTypeRecord getSubmitTypeRecord(ReviewDb db, PatchSet patchSet) {
-    return realChangeControl.getSubmitTypeRecord(db, patchSet);
-  }
-
-  public SubmitTypeRecord getSubmitTypeRecord(ReviewDb db, PatchSet patchSet,
-      ChangeData cd) {
-    return realChangeControl.getSubmitTypeRecord(db, patchSet, cd);
-  }
-}
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
index e04c3d6..5d3b929 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
@@ -53,7 +53,6 @@
 import com.google.gerrit.server.git.validators.CommitValidationException;
 import com.google.gerrit.server.git.validators.CommitValidators;
 import com.google.gerrit.server.project.ChangeControl;
-import com.google.gerrit.server.project.ChangeControlDelegate;
 import com.google.gerrit.server.project.InvalidChangeOperationException;
 import com.google.gerrit.server.project.NoSuchChangeException;
 import com.google.gerrit.server.project.NoSuchProjectException;
@@ -164,7 +163,7 @@
 
           ChangeControl changeControl =
               projectControlFactory.controlFor(project.getNameKey()).controlFor(
-                  destChange);
+                  destChange).forUser(userFactory.create(pullRequestOwner));
 
           return insertPatchSet(git, revWalk, destChange, pullRequestCommit,
               changeControl, pullRequestOwner, pullRequestMesage, doValidation);
@@ -194,8 +193,7 @@
       boolean doValidation) throws InvalidChangeOperationException,
       IOException, OrmException, NoSuchChangeException {
     PatchSetInserter patchSetInserter =
-        patchSetInserterFactory.create(git, revWalk, ChangeControlDelegate.wrap(changeControl,
-            userFactory.create(pullRequestOwnerId)), cherryPickCommit);
+        patchSetInserterFactory.create(git, revWalk, changeControl, cherryPickCommit);
     // This apparently useless method call is made for triggering
     // the creation of patchSet inside PatchSetInserter and thus avoiding a NPE
     patchSetInserter.getPatchSetId();