Remove PermissionBackend.*#user()

This commit removes the user() method from all PermissionBackend
subclasses. This method just encourages callers to pass around a
PermissionBackend when they really want to pass around a CurrentUser
and/or ChangeNotes.

Passing around heavy objects that represent a subsystem for the benefit
of getting smaller objects from it is an anti-pattern. We had this issue
at a larger scale with {Ref,Project,Change}Control that were often just
passed around to have a user and/or entity information. Removing the
user() methods should prevent that.

This refactoring also uncovered some wrongdoing that we already had in
the code as a direct result from passing PermissionBackends around (see
previous commit).

Removing user() comes at the (small) const that every PermissionBackend
has to implement #testCond by itsself, which seems fair and like a
better pattern in general.

Change-Id: Ibae1fc25e51228dca2acaf0faec76beee539da01
This commit is contained in:
Patrick Hiesel
2018-07-09 13:24:37 +02:00
parent 51b5df33af
commit b682dae920
11 changed files with 122 additions and 127 deletions

View File

@@ -658,10 +658,9 @@ public class ChangeJson {
// list permitted labels, since users can't vote on those patch sets. // list permitted labels, since users can't vote on those patch sets.
if (user.isIdentifiedUser() if (user.isIdentifiedUser()
&& (!limitToPsId.isPresent() || limitToPsId.get().equals(in.currentPatchSetId()))) { && (!limitToPsId.isPresent() || limitToPsId.get().equals(in.currentPatchSetId()))) {
PermissionBackend.ForChange perm = permissionBackendForChange(user, cd);
out.permittedLabels = out.permittedLabels =
cd.change().getStatus() != Change.Status.ABANDONED cd.change().getStatus() != Change.Status.ABANDONED
? permittedLabels(perm, cd) ? permittedLabels(user.getAccountId(), cd)
: ImmutableMap.of(); : ImmutableMap.of();
} }
@@ -889,7 +888,7 @@ public class ChangeJson {
LabelTypes labelTypes = cd.getLabelTypes(); LabelTypes labelTypes = cd.getLabelTypes();
for (Account.Id accountId : allUsers) { for (Account.Id accountId : allUsers) {
PermissionBackend.ForChange perm = permissionBackendForChange(accountId, cd); PermissionBackend.ForChange perm = permissionBackendForChange(accountId, cd);
Map<String, VotingRangeInfo> pvr = getPermittedVotingRanges(permittedLabels(perm, cd)); Map<String, VotingRangeInfo> pvr = getPermittedVotingRanges(permittedLabels(accountId, cd));
for (Map.Entry<String, LabelWithStatus> e : labels.entrySet()) { for (Map.Entry<String, LabelWithStatus> e : labels.entrySet()) {
LabelType lt = labelTypes.byLabel(e.getKey()); LabelType lt = labelTypes.byLabel(e.getKey());
if (lt == null) { if (lt == null) {
@@ -1030,8 +1029,7 @@ public class ChangeJson {
Map<String, ApprovalInfo> byLabel = Maps.newHashMapWithExpectedSize(labels.size()); Map<String, ApprovalInfo> byLabel = Maps.newHashMapWithExpectedSize(labels.size());
Map<String, VotingRangeInfo> pvr = Collections.emptyMap(); Map<String, VotingRangeInfo> pvr = Collections.emptyMap();
if (detailed) { if (detailed) {
PermissionBackend.ForChange perm = permissionBackendForChange(accountId, cd); pvr = getPermittedVotingRanges(permittedLabels(accountId, cd));
pvr = getPermittedVotingRanges(permittedLabels(perm, cd));
for (Map.Entry<String, LabelWithStatus> entry : labels.entrySet()) { for (Map.Entry<String, LabelWithStatus> entry : labels.entrySet()) {
ApprovalInfo ai = approvalInfo(accountId, 0, null, null, null); ApprovalInfo ai = approvalInfo(accountId, 0, null, null, null);
byLabel.put(entry.getKey(), ai); byLabel.put(entry.getKey(), ai);
@@ -1106,8 +1104,7 @@ public class ChangeJson {
} }
private Map<String, Collection<String>> permittedLabels( private Map<String, Collection<String>> permittedLabels(
PermissionBackend.ForChange perm, ChangeData cd) Account.Id filterApprovalsBy, ChangeData cd) throws OrmException, PermissionBackendException {
throws OrmException, PermissionBackendException {
boolean isMerged = cd.change().getStatus() == Change.Status.MERGED; boolean isMerged = cd.change().getStatus() == Change.Status.MERGED;
LabelTypes labelTypes = cd.getLabelTypes(); LabelTypes labelTypes = cd.getLabelTypes();
Map<String, LabelType> toCheck = new HashMap<>(); Map<String, LabelType> toCheck = new HashMap<>();
@@ -1123,7 +1120,8 @@ public class ChangeJson {
} }
Map<String, Short> labels = null; Map<String, Short> labels = null;
Set<LabelPermission.WithValue> can = perm.testLabels(toCheck.values()); Set<LabelPermission.WithValue> can =
permissionBackendForChange(filterApprovalsBy, cd).testLabels(toCheck.values());
SetMultimap<String, String> permitted = LinkedHashMultimap.create(); SetMultimap<String, String> permitted = LinkedHashMultimap.create();
for (SubmitRecord rec : submitRecords(cd)) { for (SubmitRecord rec : submitRecords(cd)) {
if (rec.labels == null) { if (rec.labels == null) {
@@ -1139,7 +1137,7 @@ public class ChangeJson {
boolean ok = can.contains(new LabelPermission.WithValue(type, v)); boolean ok = can.contains(new LabelPermission.WithValue(type, v));
if (isMerged) { if (isMerged) {
if (labels == null) { if (labels == null) {
labels = currentLabels(perm, cd); labels = currentLabels(filterApprovalsBy, cd);
} }
short prev = labels.getOrDefault(type.getName(), (short) 0); short prev = labels.getOrDefault(type.getName(), (short) 0);
ok &= v.getValue() >= prev; ok &= v.getValue() >= prev;
@@ -1163,16 +1161,15 @@ public class ChangeJson {
return permitted.asMap(); return permitted.asMap();
} }
private Map<String, Short> currentLabels(PermissionBackend.ForChange perm, ChangeData cd) private Map<String, Short> currentLabels(Account.Id accountId, ChangeData cd)
throws OrmException { throws OrmException {
IdentifiedUser user = perm.user().asIdentifiedUser();
Map<String, Short> result = new HashMap<>(); Map<String, Short> result = new HashMap<>();
for (PatchSetApproval psa : for (PatchSetApproval psa :
approvalsUtil.byPatchSetUser( approvalsUtil.byPatchSetUser(
db.get(), db.get(),
lazyLoad ? cd.notes() : notesFactory.createFromIndexedChange(cd.change()), lazyLoad ? cd.notes() : notesFactory.createFromIndexedChange(cd.change()),
cd.change().currentPatchSetId(), cd.change().currentPatchSetId(),
user.getAccountId(), accountId,
null, null,
null)) { null)) {
result.put(psa.getLabel(), psa.getValue()); result.put(psa.getLabel(), psa.getValue());

View File

@@ -23,6 +23,7 @@ import com.google.common.collect.Sets;
import com.google.gerrit.common.Nullable; import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.Permission; import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange; import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@@ -262,14 +263,9 @@ class ChangeControl {
return cd; return cd;
} }
@Override
public CurrentUser user() {
return getUser();
}
@Override @Override
public ForChange user(CurrentUser user) { public ForChange user(CurrentUser user) {
return user().equals(user) ? this : forUser(user).asForChange(cd, db); return getUser().equals(user) ? this : forUser(user).asForChange(cd, db);
} }
@Override @Override
@@ -308,6 +304,11 @@ class ChangeControl {
return ok; return ok;
} }
@Override
public BooleanCondition testCond(ChangePermissionOrLabel perm) {
return new PermissionBackendCondition.ForChange(this, perm, getUser());
}
private boolean can(ChangePermissionOrLabel perm) throws PermissionBackendException { private boolean can(ChangePermissionOrLabel perm) throws PermissionBackendException {
if (perm instanceof ChangePermission) { if (perm instanceof ChangePermission) {
return can((ChangePermission) perm); return can((ChangePermission) perm);

View File

@@ -23,6 +23,7 @@ import com.google.gerrit.common.data.PermissionRule;
import com.google.gerrit.common.data.PermissionRule.Action; import com.google.gerrit.common.data.PermissionRule.Action;
import com.google.gerrit.extensions.api.access.GlobalOrPluginPermission; import com.google.gerrit.extensions.api.access.GlobalOrPluginPermission;
import com.google.gerrit.extensions.api.access.PluginPermission; import com.google.gerrit.extensions.api.access.PluginPermission;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
@@ -32,6 +33,7 @@ import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.PeerDaemonUser; import com.google.gerrit.server.PeerDaemonUser;
import com.google.gerrit.server.account.CapabilityCollection; import com.google.gerrit.server.account.CapabilityCollection;
import com.google.gerrit.server.cache.PerThreadCache; import com.google.gerrit.server.cache.PerThreadCache;
import com.google.gerrit.server.permissions.PermissionBackendCondition.WithUser;
import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState; import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -97,11 +99,6 @@ public class DefaultPermissionBackend extends PermissionBackend {
this.user = checkNotNull(user, "user"); this.user = checkNotNull(user, "user");
} }
@Override
public CurrentUser user() {
return user;
}
@Override @Override
public ForProject project(Project.NameKey project) { public ForProject project(Project.NameKey project) {
try { try {
@@ -138,6 +135,11 @@ public class DefaultPermissionBackend extends PermissionBackend {
return ok; return ok;
} }
@Override
public BooleanCondition testCond(GlobalOrPluginPermission perm) {
return new PermissionBackendCondition.WithUser(this, perm, user);
}
private boolean can(GlobalOrPluginPermission perm) throws PermissionBackendException { private boolean can(GlobalOrPluginPermission perm) throws PermissionBackendException {
if (perm instanceof GlobalPermission) { if (perm instanceof GlobalPermission) {
return can((GlobalPermission) perm); return can((GlobalPermission) perm);

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.permissions; package com.google.gerrit.server.permissions;
import com.google.gerrit.extensions.api.access.GlobalOrPluginPermission; import com.google.gerrit.extensions.api.access.GlobalOrPluginPermission;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
@@ -83,11 +84,6 @@ public class FailedPermissionBackend {
this.cause = cause; this.cause = cause;
} }
@Override
public CurrentUser user() {
throw new UnsupportedOperationException("FailedPermissionBackend is not scoped to user");
}
@Override @Override
public ForProject project(Project.NameKey project) { public ForProject project(Project.NameKey project) {
return new FailedProject(message, cause); return new FailedProject(message, cause);
@@ -103,6 +99,12 @@ public class FailedPermissionBackend {
throws PermissionBackendException { throws PermissionBackendException {
throw new PermissionBackendException(message, cause); throw new PermissionBackendException(message, cause);
} }
@Override
public BooleanCondition testCond(GlobalOrPluginPermission perm) {
throw new UnsupportedOperationException(
"FailedPermissionBackend does not support conditions");
}
} }
private static class FailedProject extends ForProject { private static class FailedProject extends ForProject {
@@ -119,11 +121,6 @@ public class FailedPermissionBackend {
return this; return this;
} }
@Override
public CurrentUser user() {
throw new UnsupportedOperationException("FailedPermissionBackend is not scoped to user");
}
@Override @Override
public ForProject user(CurrentUser user) { public ForProject user(CurrentUser user) {
return this; return this;
@@ -156,6 +153,12 @@ public class FailedPermissionBackend {
throw new PermissionBackendException(message, cause); throw new PermissionBackendException(message, cause);
} }
@Override
public BooleanCondition testCond(ProjectPermission perm) {
throw new UnsupportedOperationException(
"FailedPermissionBackend does not support conditions");
}
@Override @Override
public Map<String, Ref> filter(Map<String, Ref> refs, Repository repo, RefFilterOptions opts) public Map<String, Ref> filter(Map<String, Ref> refs, Repository repo, RefFilterOptions opts)
throws PermissionBackendException { throws PermissionBackendException {
@@ -177,11 +180,6 @@ public class FailedPermissionBackend {
return this; return this;
} }
@Override
public CurrentUser user() {
throw new UnsupportedOperationException("FailedPermissionBackend is not scoped to user");
}
@Override @Override
public ForRef user(CurrentUser user) { public ForRef user(CurrentUser user) {
return this; return this;
@@ -223,6 +221,12 @@ public class FailedPermissionBackend {
throws PermissionBackendException { throws PermissionBackendException {
throw new PermissionBackendException(message, cause); throw new PermissionBackendException(message, cause);
} }
@Override
public BooleanCondition testCond(RefPermission perm) {
throw new UnsupportedOperationException(
"FailedPermissionBackend does not support conditions");
}
} }
private static class FailedChange extends ForChange { private static class FailedChange extends ForChange {
@@ -267,8 +271,9 @@ public class FailedPermissionBackend {
} }
@Override @Override
public CurrentUser user() { public BooleanCondition testCond(ChangePermissionOrLabel perm) {
throw new UnsupportedOperationException("FailedPermissionBackend is not scoped to user"); throw new UnsupportedOperationException(
"FailedPermissionBackend does not support conditions");
} }
} }
} }

View File

@@ -173,9 +173,6 @@ public abstract class PermissionBackend {
/** PermissionBackend scoped to a specific user. */ /** PermissionBackend scoped to a specific user. */
public abstract static class WithUser extends AcceptsReviewDb<WithUser> { public abstract static class WithUser extends AcceptsReviewDb<WithUser> {
/** Returns the user this instance is scoped to. */
public abstract CurrentUser user();
/** Returns an instance scoped for the specified project. */ /** Returns an instance scoped for the specified project. */
public abstract ForProject project(Project.NameKey project); public abstract ForProject project(Project.NameKey project);
@@ -257,9 +254,7 @@ public abstract class PermissionBackend {
} }
} }
public BooleanCondition testCond(GlobalOrPluginPermission perm) { public abstract BooleanCondition testCond(GlobalOrPluginPermission perm);
return new PermissionBackendCondition.WithUser(this, perm);
}
/** /**
* Filter a set of projects using {@code check(perm)}. * Filter a set of projects using {@code check(perm)}.
@@ -296,9 +291,6 @@ public abstract class PermissionBackend {
/** PermissionBackend scoped to a user and project. */ /** PermissionBackend scoped to a user and project. */
public abstract static class ForProject extends AcceptsReviewDb<ForProject> { public abstract static class ForProject extends AcceptsReviewDb<ForProject> {
/** Returns the user this instance is scoped to. */
public abstract CurrentUser user();
/** Returns the fully qualified resource path that this instance is scoped to. */ /** Returns the fully qualified resource path that this instance is scoped to. */
public abstract String resourcePath(); public abstract String resourcePath();
@@ -355,9 +347,7 @@ public abstract class PermissionBackend {
} }
} }
public BooleanCondition testCond(ProjectPermission perm) { public abstract BooleanCondition testCond(ProjectPermission perm);
return new PermissionBackendCondition.ForProject(this, perm);
}
/** /**
* Filter a map of references by visibility. * Filter a map of references by visibility.
@@ -407,9 +397,6 @@ public abstract class PermissionBackend {
/** PermissionBackend scoped to a user, project and reference. */ /** PermissionBackend scoped to a user, project and reference. */
public abstract static class ForRef extends AcceptsReviewDb<ForRef> { public abstract static class ForRef extends AcceptsReviewDb<ForRef> {
/** Returns the user this instance is scoped to. */
public abstract CurrentUser user();
/** Returns a fully qualified resource path that this instance is scoped to. */ /** Returns a fully qualified resource path that this instance is scoped to. */
public abstract String resourcePath(); public abstract String resourcePath();
@@ -461,16 +448,11 @@ public abstract class PermissionBackend {
} }
} }
public BooleanCondition testCond(RefPermission perm) { public abstract BooleanCondition testCond(RefPermission perm);
return new PermissionBackendCondition.ForRef(this, perm);
}
} }
/** PermissionBackend scoped to a user, project, reference and change. */ /** PermissionBackend scoped to a user, project, reference and change. */
public abstract static class ForChange extends AcceptsReviewDb<ForChange> { public abstract static class ForChange extends AcceptsReviewDb<ForChange> {
/** Returns the user this instance is scoped to. */
public abstract CurrentUser user();
/** Returns the fully qualified resource path that this instance is scoped to. */ /** Returns the fully qualified resource path that this instance is scoped to. */
public abstract String resourcePath(); public abstract String resourcePath();
@@ -511,9 +493,7 @@ public abstract class PermissionBackend {
} }
} }
public BooleanCondition testCond(ChangePermissionOrLabel perm) { public abstract BooleanCondition testCond(ChangePermissionOrLabel perm);
return new PermissionBackendCondition.ForChange(this, perm);
}
/** /**
* Test which values of a label the user may be able to set. * Test which values of a label the user may be able to set.

View File

@@ -56,10 +56,13 @@ public abstract class PermissionBackendCondition
public static class WithUser extends PermissionBackendCondition { public static class WithUser extends PermissionBackendCondition {
private final PermissionBackend.WithUser impl; private final PermissionBackend.WithUser impl;
private final GlobalOrPluginPermission perm; private final GlobalOrPluginPermission perm;
private final CurrentUser user;
WithUser(PermissionBackend.WithUser impl, GlobalOrPluginPermission perm) { public WithUser(
PermissionBackend.WithUser impl, GlobalOrPluginPermission perm, CurrentUser user) {
this.impl = impl; this.impl = impl;
this.perm = perm; this.perm = perm;
this.user = user;
} }
public PermissionBackend.WithUser withUser() { public PermissionBackend.WithUser withUser() {
@@ -82,7 +85,7 @@ public abstract class PermissionBackendCondition
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(perm, hashForUser(impl.user())); return Objects.hash(perm, hashForUser(user));
} }
@Override @Override
@@ -91,17 +94,19 @@ public abstract class PermissionBackendCondition
return false; return false;
} }
WithUser other = (WithUser) obj; WithUser other = (WithUser) obj;
return Objects.equals(perm, other.perm) && usersAreEqual(impl.user(), other.impl.user()); return Objects.equals(perm, other.perm) && usersAreEqual(user, other.user);
} }
} }
public static class ForProject extends PermissionBackendCondition { public static class ForProject extends PermissionBackendCondition {
private final PermissionBackend.ForProject impl; private final PermissionBackend.ForProject impl;
private final ProjectPermission perm; private final ProjectPermission perm;
private final CurrentUser user;
ForProject(PermissionBackend.ForProject impl, ProjectPermission perm) { public ForProject(PermissionBackend.ForProject impl, ProjectPermission perm, CurrentUser user) {
this.impl = impl; this.impl = impl;
this.perm = perm; this.perm = perm;
this.user = user;
} }
public PermissionBackend.ForProject project() { public PermissionBackend.ForProject project() {
@@ -124,7 +129,7 @@ public abstract class PermissionBackendCondition
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(perm, impl.resourcePath(), hashForUser(impl.user())); return Objects.hash(perm, impl.resourcePath(), hashForUser(user));
} }
@Override @Override
@@ -135,17 +140,19 @@ public abstract class PermissionBackendCondition
ForProject other = (ForProject) obj; ForProject other = (ForProject) obj;
return Objects.equals(perm, other.perm) return Objects.equals(perm, other.perm)
&& Objects.equals(impl.resourcePath(), other.impl.resourcePath()) && Objects.equals(impl.resourcePath(), other.impl.resourcePath())
&& usersAreEqual(impl.user(), other.impl.user()); && usersAreEqual(user, other.user);
} }
} }
public static class ForRef extends PermissionBackendCondition { public static class ForRef extends PermissionBackendCondition {
private final PermissionBackend.ForRef impl; private final PermissionBackend.ForRef impl;
private final RefPermission perm; private final RefPermission perm;
private final CurrentUser user;
ForRef(PermissionBackend.ForRef impl, RefPermission perm) { public ForRef(PermissionBackend.ForRef impl, RefPermission perm, CurrentUser user) {
this.impl = impl; this.impl = impl;
this.perm = perm; this.perm = perm;
this.user = user;
} }
public PermissionBackend.ForRef ref() { public PermissionBackend.ForRef ref() {
@@ -168,7 +175,7 @@ public abstract class PermissionBackendCondition
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(perm, impl.resourcePath(), hashForUser(impl.user())); return Objects.hash(perm, impl.resourcePath(), hashForUser(user));
} }
@Override @Override
@@ -179,17 +186,20 @@ public abstract class PermissionBackendCondition
ForRef other = (ForRef) obj; ForRef other = (ForRef) obj;
return Objects.equals(perm, other.perm) return Objects.equals(perm, other.perm)
&& Objects.equals(impl.resourcePath(), other.impl.resourcePath()) && Objects.equals(impl.resourcePath(), other.impl.resourcePath())
&& usersAreEqual(impl.user(), other.impl.user()); && usersAreEqual(user, other.user);
} }
} }
public static class ForChange extends PermissionBackendCondition { public static class ForChange extends PermissionBackendCondition {
private final PermissionBackend.ForChange impl; private final PermissionBackend.ForChange impl;
private final ChangePermissionOrLabel perm; private final ChangePermissionOrLabel perm;
private final CurrentUser user;
ForChange(PermissionBackend.ForChange impl, ChangePermissionOrLabel perm) { public ForChange(
PermissionBackend.ForChange impl, ChangePermissionOrLabel perm, CurrentUser user) {
this.impl = impl; this.impl = impl;
this.perm = perm; this.perm = perm;
this.user = user;
} }
public PermissionBackend.ForChange change() { public PermissionBackend.ForChange change() {
@@ -212,7 +222,7 @@ public abstract class PermissionBackendCondition
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(perm, impl.resourcePath(), hashForUser(impl.user())); return Objects.hash(perm, impl.resourcePath(), hashForUser(user));
} }
@Override @Override
@@ -223,7 +233,7 @@ public abstract class PermissionBackendCondition
ForChange other = (ForChange) obj; ForChange other = (ForChange) obj;
return Objects.equals(perm, other.perm) return Objects.equals(perm, other.perm)
&& Objects.equals(impl.resourcePath(), other.impl.resourcePath()) && Objects.equals(impl.resourcePath(), other.impl.resourcePath())
&& usersAreEqual(impl.user(), other.impl.user()); && usersAreEqual(user, other.user);
} }
} }

View File

@@ -19,6 +19,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import com.google.gerrit.common.data.AccessSection; import com.google.gerrit.common.data.AccessSection;
import com.google.gerrit.common.data.Permission; import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRule; import com.google.gerrit.common.data.PermissionRule;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
@@ -322,11 +323,6 @@ class ProjectControl {
private DefaultRefFilter refFilter; private DefaultRefFilter refFilter;
private String resourcePath; private String resourcePath;
@Override
public CurrentUser user() {
return getUser();
}
@Override @Override
public ForProject user(CurrentUser user) { public ForProject user(CurrentUser user) {
return forUser(user).asForProject().database(db); return forUser(user).asForProject().database(db);
@@ -394,6 +390,11 @@ class ProjectControl {
return ok; return ok;
} }
@Override
public BooleanCondition testCond(ProjectPermission perm) {
return new PermissionBackendCondition.ForProject(this, perm, getUser());
}
@Override @Override
public Map<String, Ref> filter(Map<String, Ref> refs, Repository repo, RefFilterOptions opts) public Map<String, Ref> filter(Map<String, Ref> refs, Repository repo, RefFilterOptions opts)
throws PermissionBackendException { throws PermissionBackendException {

View File

@@ -20,6 +20,7 @@ import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange; import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.PermissionRule; import com.google.gerrit.common.data.PermissionRule;
import com.google.gerrit.common.data.PermissionRule.Action; import com.google.gerrit.common.data.PermissionRule.Action;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@@ -407,11 +408,6 @@ class RefControl {
private class ForRefImpl extends ForRef { private class ForRefImpl extends ForRef {
private String resourcePath; private String resourcePath;
@Override
public CurrentUser user() {
return getUser();
}
@Override @Override
public ForRef user(CurrentUser user) { public ForRef user(CurrentUser user) {
return forUser(user).asForRef().database(db); return forUser(user).asForRef().database(db);
@@ -480,6 +476,11 @@ class RefControl {
return ok; return ok;
} }
@Override
public BooleanCondition testCond(RefPermission perm) {
return new PermissionBackendCondition.ForRef(this, perm, getUser());
}
private boolean can(RefPermission perm) throws PermissionBackendException { private boolean can(RefPermission perm) throws PermissionBackendException {
switch (perm) { switch (perm) {
case READ: case READ:

View File

@@ -452,17 +452,13 @@ public class PostReviewers
} }
ChangeData cd = changeDataFactory.create(dbProvider.get(), notes); ChangeData cd = changeDataFactory.create(dbProvider.get(), notes);
PermissionBackend.ForChange perm =
permissionBackend.user(caller).database(dbProvider).change(cd);
// Generate result details and fill AccountLoader. This occurs outside // Generate result details and fill AccountLoader. This occurs outside
// the Op because the accounts are in a different table. // the Op because the accounts are in a different table.
PostReviewersOp.Result opResult = op.getResult(); PostReviewersOp.Result opResult = op.getResult();
if (migration.readChanges() && state == CC) { if (migration.readChanges() && state == CC) {
result.ccs = Lists.newArrayListWithCapacity(opResult.addedCCs().size()); result.ccs = Lists.newArrayListWithCapacity(opResult.addedCCs().size());
for (Account.Id accountId : opResult.addedCCs()) { for (Account.Id accountId : opResult.addedCCs()) {
result.ccs.add( result.ccs.add(json.format(new ReviewerInfo(accountId.get()), accountId, cd));
json.format(new ReviewerInfo(accountId.get()), perm.absentUser(accountId), cd));
} }
accountLoaderFactory.create(true).fill(result.ccs); accountLoaderFactory.create(true).fill(result.ccs);
for (Address a : reviewersByEmail) { for (Address a : reviewersByEmail) {
@@ -475,7 +471,7 @@ public class PostReviewers
result.reviewers.add( result.reviewers.add(
json.format( json.format(
new ReviewerInfo(psa.getAccountId().get()), new ReviewerInfo(psa.getAccountId().get()),
perm.absentUser(psa.getAccountId()), psa.getAccountId(),
cd, cd,
ImmutableList.of(psa))); ImmutableList.of(psa)));
} }

View File

@@ -80,10 +80,7 @@ public class ReviewerJson {
ReviewerInfo info = ReviewerInfo info =
format( format(
new ReviewerInfo(rsrc.getReviewerUser().getAccountId().get()), new ReviewerInfo(rsrc.getReviewerUser().getAccountId().get()),
permissionBackend rsrc.getReviewerUser().getAccountId(),
.absentUser(rsrc.getReviewerUser().getAccountId())
.database(db)
.change(cd),
cd); cd);
loader.put(info); loader.put(info);
infos.add(info); infos.add(info);
@@ -97,22 +94,19 @@ public class ReviewerJson {
return format(ImmutableList.<ReviewerResource>of(rsrc)); return format(ImmutableList.<ReviewerResource>of(rsrc));
} }
public ReviewerInfo format(ReviewerInfo out, PermissionBackend.ForChange perm, ChangeData cd) public ReviewerInfo format(ReviewerInfo out, Account.Id reviewer, ChangeData cd)
throws OrmException, PermissionBackendException { throws OrmException, PermissionBackendException {
PatchSet.Id psId = cd.change().currentPatchSetId(); PatchSet.Id psId = cd.change().currentPatchSetId();
return format( return format(
out, out,
perm, reviewer,
cd, cd,
approvalsUtil.byPatchSetUser( approvalsUtil.byPatchSetUser(
db.get(), cd.notes(), psId, new Account.Id(out._accountId), null, null)); db.get(), cd.notes(), psId, new Account.Id(out._accountId), null, null));
} }
public ReviewerInfo format( public ReviewerInfo format(
ReviewerInfo out, ReviewerInfo out, Account.Id reviewer, ChangeData cd, Iterable<PatchSetApproval> approvals)
PermissionBackend.ForChange perm,
ChangeData cd,
Iterable<PatchSetApproval> approvals)
throws OrmException, PermissionBackendException { throws OrmException, PermissionBackendException {
LabelTypes labelTypes = cd.getLabelTypes(); LabelTypes labelTypes = cd.getLabelTypes();
@@ -128,6 +122,9 @@ public class ReviewerJson {
// do not exist in the DB. // do not exist in the DB.
PatchSet ps = cd.currentPatchSet(); PatchSet ps = cd.currentPatchSet();
if (ps != null) { if (ps != null) {
PermissionBackend.ForChange perm =
permissionBackend.absentUser(reviewer).database(db).change(cd);
for (SubmitRecord rec : submitRuleEvaluator.evaluate(cd)) { for (SubmitRecord rec : submitRuleEvaluator.evaluate(cd)) {
if (rec.labels == null) { if (rec.labels == null) {
continue; continue;

View File

@@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.CurrentUser; import com.google.gerrit.server.CurrentUser;
@@ -42,31 +43,6 @@ public class UiActionsTest {
private static class FakeForProject extends ForProject { private static class FakeForProject extends ForProject {
private boolean allowValueQueries = true; private boolean allowValueQueries = true;
@Override
public CurrentUser user() {
return new CurrentUser() {
@Override
public GroupMembership getEffectiveGroups() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public Object getCacheKey() {
return new Object();
}
@Override
public boolean isIdentifiedUser() {
return true;
}
@Override
public Account.Id getAccountId() {
return new Account.Id(1);
}
};
}
@Override @Override
public String resourcePath() { public String resourcePath() {
return "/projects/test-project"; return "/projects/test-project";
@@ -99,6 +75,11 @@ public class UiActionsTest {
return ImmutableSet.of(ProjectPermission.READ); return ImmutableSet.of(ProjectPermission.READ);
} }
@Override
public BooleanCondition testCond(ProjectPermission perm) {
return new PermissionBackendCondition.ForProject(this, perm, fakeUser());
}
@Override @Override
public Map<String, Ref> filter(Map<String, Ref> refs, Repository repo, RefFilterOptions opts) public Map<String, Ref> filter(Map<String, Ref> refs, Repository repo, RefFilterOptions opts)
throws PermissionBackendException { throws PermissionBackendException {
@@ -108,6 +89,30 @@ public class UiActionsTest {
private void disallowValueQueries() { private void disallowValueQueries() {
allowValueQueries = false; allowValueQueries = false;
} }
private static CurrentUser fakeUser() {
return new CurrentUser() {
@Override
public GroupMembership getEffectiveGroups() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public Object getCacheKey() {
return new Object();
}
@Override
public boolean isIdentifiedUser() {
return true;
}
@Override
public Account.Id getAccountId() {
return new Account.Id(1);
}
};
}
} }
@Test @Test