Check canEditHashtags with PermissionBackend

Change-Id: Ief5473171d8e754b6d17104213d1613e4c974f8c
This commit is contained in:
Shawn Pearce
2017-02-18 16:00:05 -08:00
committed by David Pursehouse
parent 6b9563f3ce
commit e92ad1112e
6 changed files with 12 additions and 9 deletions

View File

@@ -476,7 +476,7 @@ class ChangeApiImpl implements ChangeApi {
public void setHashtags(HashtagsInput input) throws RestApiException {
try {
postHashtags.apply(change, input);
} catch (UpdateException e) {
} catch (UpdateException | PermissionBackendException e) {
throw new RestApiException("Cannot post hashtags", e);
}
}

View File

@@ -22,6 +22,8 @@ import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.webui.UiAction;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.permissions.ChangePermission;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.update.BatchUpdate;
import com.google.gerrit.server.update.UpdateException;
import com.google.inject.Inject;
@@ -47,7 +49,9 @@ public class PostHashtags
@Override
public Response<ImmutableSortedSet<String>> apply(ChangeResource req, HashtagsInput input)
throws RestApiException, UpdateException {
throws RestApiException, UpdateException, PermissionBackendException {
req.permissions().check(ChangePermission.EDIT_HASHTAGS);
try (BatchUpdate bu =
batchUpdateFactory.create(
db.get(), req.getChange().getProject(), req.getControl().getUser(), TimeUtil.nowTs())) {
@@ -59,9 +63,9 @@ public class PostHashtags
}
@Override
public UiAction.Description getDescription(ChangeResource resource) {
public UiAction.Description getDescription(ChangeResource rsrc) {
return new UiAction.Description()
.setLabel("Edit Hashtags")
.setVisible(resource.getControl().canEditHashtags());
.setVisible(rsrc.permissions().testOrFalse(ChangePermission.EDIT_HASHTAGS));
}
}

View File

@@ -94,9 +94,7 @@ public class SetHashtagsOp implements BatchUpdateOp {
updatedHashtags = ImmutableSortedSet.of();
return false;
}
if (!ctx.getControl().canEditHashtags()) {
throw new AuthException("Editing hashtags not permitted");
}
change = ctx.getChange();
ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
ChangeNotes notes = update.getNotes().load();

View File

@@ -2197,6 +2197,7 @@ public class ReceiveCommits {
.setUpdateRef(false)
.setPatchSetDescription(magicBranch.message));
if (!magicBranch.hashtags.isEmpty()) {
// Any change owner is allowed to add hashtags when creating a change.
bu.addOp(
changeId,
hashtagsFactory.create(new HashtagsInput(magicBranch.hashtags)).setFireEvent(false));

View File

@@ -464,7 +464,7 @@ public class ChangeControl {
}
/** Can this user edit the hashtag name? */
public boolean canEditHashtags() {
private boolean canEditHashtags() {
return isOwner() // owner (aka creator) of the change can edit hashtags
|| getRefControl().isOwner() // branch owner can edit hashtags
|| getProjectControl().isOwner() // project owner can edit hashtags

View File

@@ -447,7 +447,7 @@ public class RefControl {
}
/** @return true if this user can edit hashtag names. */
public boolean canEditHashtags() {
boolean canEditHashtags() {
return canPerform(Permission.EDIT_HASHTAGS);
}