Add currentUser() to PermissionBackend

Passing in a Provider<CurrentUser> into PermissionBackend is
boiler-platy. This change adds a convenience method to PermissionBackend
and DefaultPermissionBackend to limit this boiler-plate. Future commits
will remove #user(Provider<CurrentUser>) from PermissionBackend, once
all callers were moved.

Change-Id: Ifcd07fe2c7d2673a66b2b4f9eff06ee8a3b6b943
This commit is contained in:
Patrick Hiesel
2018-03-26 10:44:52 +02:00
parent 548d22f29b
commit 659ea71969
2 changed files with 22 additions and 3 deletions

View File

@@ -32,6 +32,7 @@ import com.google.gerrit.server.project.NoSuchProjectException;
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;
import com.google.inject.Provider;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
@@ -44,12 +45,16 @@ import java.util.Set;
public class DefaultPermissionBackend extends PermissionBackend { public class DefaultPermissionBackend extends PermissionBackend {
private static final CurrentUser.PropertyKey<Boolean> IS_ADMIN = CurrentUser.PropertyKey.create(); private static final CurrentUser.PropertyKey<Boolean> IS_ADMIN = CurrentUser.PropertyKey.create();
private final Provider<CurrentUser> currentUser;
private final ProjectCache projectCache; private final ProjectCache projectCache;
private final ProjectControl.Factory projectControlFactory; private final ProjectControl.Factory projectControlFactory;
@Inject @Inject
DefaultPermissionBackend( DefaultPermissionBackend(
ProjectCache projectCache, ProjectControl.Factory projectControlFactory) { Provider<CurrentUser> currentUser,
ProjectCache projectCache,
ProjectControl.Factory projectControlFactory) {
this.currentUser = currentUser;
this.projectCache = projectCache; this.projectCache = projectCache;
this.projectControlFactory = projectControlFactory; this.projectControlFactory = projectControlFactory;
} }
@@ -58,6 +63,11 @@ public class DefaultPermissionBackend extends PermissionBackend {
return projectCache.getAllProjects().getCapabilityCollection(); return projectCache.getAllProjects().getCapabilityCollection();
} }
@Override
public WithUser currentUser() {
return new WithUserImpl(currentUser.get());
}
@Override @Override
public WithUser user(CurrentUser user) { public WithUser user(CurrentUser user) {
return new WithUserImpl(checkNotNull(user, "user")); return new WithUserImpl(checkNotNull(user, "user"));

View File

@@ -90,10 +90,19 @@ import org.slf4j.LoggerFactory;
public abstract class PermissionBackend { public abstract class PermissionBackend {
private static final Logger logger = LoggerFactory.getLogger(PermissionBackend.class); private static final Logger logger = LoggerFactory.getLogger(PermissionBackend.class);
/** @return lightweight factory scoped to answer for the specified user. */ /** @return lightweight factory scoped to answer for the current user. */
public abstract WithUser currentUser();
/**
* @return lightweight factory scoped to answer for the specified user. If an instance scoped to
* the current user is desired, use {@code currentUser()} instead.
*/
public abstract WithUser user(CurrentUser user); public abstract WithUser user(CurrentUser user);
/** @return lightweight factory scoped to answer for the specified user. */ /**
* @return lightweight factory scoped to answer for the specified user. If an instance scoped to
* the current user is desired, use {@code currentUser()} instead.
*/
public <U extends CurrentUser> WithUser user(Provider<U> user) { public <U extends CurrentUser> WithUser user(Provider<U> user) {
return user(checkNotNull(user, "Provider<CurrentUser>").get()); return user(checkNotNull(user, "Provider<CurrentUser>").get());
} }