Check canRebase with PermissionBackend

Change-Id: I09b24e3473c8b45a583cbb7cab57c86ac1b99792
This commit is contained in:
Shawn Pearce
2017-02-18 15:52:35 -08:00
committed by David Pursehouse
parent df11d43964
commit 3008aac922
4 changed files with 29 additions and 23 deletions

View File

@@ -362,7 +362,11 @@ class ChangeApiImpl implements ChangeApi {
public void rebase(RebaseInput in) throws RestApiException { public void rebase(RebaseInput in) throws RestApiException {
try { try {
rebase.apply(change, in); rebase.apply(change, in);
} catch (EmailException | OrmException | UpdateException | IOException e) { } catch (EmailException
| OrmException
| UpdateException
| IOException
| PermissionBackendException e) {
throw new RestApiException("Cannot rebase change", e); throw new RestApiException("Cannot rebase change", e);
} }
} }

View File

@@ -268,7 +268,11 @@ class RevisionApiImpl implements RevisionApi {
public ChangeApi rebase(RebaseInput in) throws RestApiException { public ChangeApi rebase(RebaseInput in) throws RestApiException {
try { try {
return changes.id(rebase.apply(revision, in)._number); return changes.id(rebase.apply(revision, in)._number);
} catch (OrmException | EmailException | UpdateException | IOException e) { } catch (OrmException
| EmailException
| UpdateException
| IOException
| PermissionBackendException e) {
throw new RestApiException("Cannot rebase ps", e); throw new RestApiException("Cannot rebase ps", e);
} }
} }

View File

@@ -35,6 +35,8 @@ import com.google.gerrit.server.PatchSetUtil;
import com.google.gerrit.server.change.RebaseUtil.Base; import com.google.gerrit.server.change.RebaseUtil.Base;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.validators.CommitValidators; import com.google.gerrit.server.git.validators.CommitValidators;
import com.google.gerrit.server.permissions.ChangePermission;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.project.ChangeControl; import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException; import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.update.BatchUpdate; import com.google.gerrit.server.update.BatchUpdate;
@@ -85,7 +87,9 @@ public class Rebase
@Override @Override
public ChangeInfo apply(RevisionResource rsrc, RebaseInput input) public ChangeInfo apply(RevisionResource rsrc, RebaseInput input)
throws EmailException, OrmException, UpdateException, RestApiException, IOException, throws EmailException, OrmException, UpdateException, RestApiException, IOException,
NoSuchChangeException { NoSuchChangeException, PermissionBackendException {
rsrc.permissions().database(dbProvider).check(ChangePermission.REBASE);
ChangeControl control = rsrc.getControl(); ChangeControl control = rsrc.getControl();
Change change = rsrc.getChange(); Change change = rsrc.getChange();
try (Repository repo = repoManager.openRepository(change.getProject()); try (Repository repo = repoManager.openRepository(change.getProject());
@@ -94,9 +98,7 @@ public class Rebase
BatchUpdate bu = BatchUpdate bu =
updateFactory.create( updateFactory.create(
dbProvider.get(), change.getProject(), rsrc.getUser(), TimeUtil.nowTs())) { dbProvider.get(), change.getProject(), rsrc.getUser(), TimeUtil.nowTs())) {
if (!control.canRebase(dbProvider.get())) { if (!change.getStatus().isOpen()) {
throw new AuthException("rebase not permitted");
} else if (!change.getStatus().isOpen()) {
throw new ResourceConflictException("change is " + change.getStatus().name().toLowerCase()); throw new ResourceConflictException("change is " + change.getStatus().name().toLowerCase());
} else if (!hasOneParent(rw, rsrc.getPatchSet())) { } else if (!hasOneParent(rw, rsrc.getPatchSet())) {
throw new ResourceConflictException( throw new ResourceConflictException(
@@ -175,15 +177,12 @@ public class Rebase
@Override @Override
public UiAction.Description getDescription(RevisionResource resource) { public UiAction.Description getDescription(RevisionResource resource) {
PatchSet patchSet = resource.getPatchSet(); PatchSet patchSet = resource.getPatchSet();
Branch.NameKey dest = resource.getChange().getDest(); Change change = resource.getChange();
boolean canRebase = false; Branch.NameKey dest = change.getDest();
try {
canRebase = resource.getControl().canRebase(dbProvider.get());
} catch (OrmException e) {
log.error("Cannot check canRebase status. Assuming false.", e);
}
boolean visible = boolean visible =
resource.getChange().getStatus().isOpen() && resource.isCurrent() && canRebase; change.getStatus().isOpen()
&& resource.isCurrent()
&& resource.permissions().database(dbProvider).testOrFalse(ChangePermission.REBASE);
boolean enabled = true; boolean enabled = true;
if (visible) { if (visible) {
@@ -196,13 +195,11 @@ public class Rebase
visible = false; visible = false;
} }
} }
UiAction.Description descr = return new UiAction.Description()
new UiAction.Description()
.setLabel("Rebase") .setLabel("Rebase")
.setTitle("Rebase onto tip of branch or parent change") .setTitle("Rebase onto tip of branch or parent change")
.setVisible(visible) .setVisible(visible)
.setEnabled(enabled); .setEnabled(enabled);
return descr;
} }
public static class CurrentRevision implements RestModifyView<ChangeResource, RebaseInput> { public static class CurrentRevision implements RestModifyView<ChangeResource, RebaseInput> {
@@ -217,7 +214,8 @@ public class Rebase
@Override @Override
public ChangeInfo apply(ChangeResource rsrc, RebaseInput input) public ChangeInfo apply(ChangeResource rsrc, RebaseInput input)
throws EmailException, OrmException, UpdateException, RestApiException, IOException { throws EmailException, OrmException, UpdateException, RestApiException, IOException,
PermissionBackendException {
PatchSet ps = psUtil.current(rebase.dbProvider.get(), rsrc.getNotes()); PatchSet ps = psUtil.current(rebase.dbProvider.get(), rsrc.getNotes());
if (ps == null) { if (ps == null) {
throw new ResourceConflictException("current revision is missing"); throw new ResourceConflictException("current revision is missing");

View File

@@ -285,7 +285,7 @@ public class ChangeControl {
} }
/** Can this user rebase this change? */ /** Can this user rebase this change? */
public boolean canRebase(ReviewDb db) throws OrmException { private boolean canRebase(ReviewDb db) throws OrmException {
return (isOwner() || getRefControl().canSubmit(isOwner()) || getRefControl().canRebase()) return (isOwner() || getRefControl().canSubmit(isOwner()) || getRefControl().canRebase())
&& !isPatchSetLocked(db); && !isPatchSetLocked(db);
} }