Eliminate usage of ProjectControl

Change-Id: I36e02a448ce5426b4608eaa2546cb529bbdb64b0
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserModel.java b/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserModel.java
index f66ecff..d33c48b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserModel.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserModel.java
@@ -20,14 +20,12 @@
 import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.StringUtils;
-import com.google.gerrit.extensions.client.ProjectState;
 import com.google.gerrit.reviewdb.client.Project.NameKey;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.permissions.PermissionBackend;
+import com.google.gerrit.server.permissions.PermissionBackend.ForProject;
 import com.google.gerrit.server.permissions.ProjectPermission;
-import com.google.gerrit.server.project.NoSuchProjectException;
-import com.google.gerrit.server.project.ProjectControl;
-import com.google.gerrit.server.project.ProjectControl.Factory;
+import com.google.gerrit.server.permissions.RefPermission;
 import com.google.inject.Provider;
 import java.util.HashSet;
 import java.util.Set;
@@ -49,7 +47,6 @@
   public final Set<String> repositories = new HashSet<String>();
   public final Set<TeamModel> teams = new HashSet<TeamModel>();
 
-  private final transient ProjectControl.Factory projectControlFactory;
   private final transient Provider<CurrentUser> userProvider;
   private final transient PermissionBackend permissionBackend;
 
@@ -57,41 +54,31 @@
   public boolean isAuthenticated;
 
   public GerritToGitBlitUserModel(String username) {
-    this(username, null, null, null);
+    this(username, null, null);
   }
 
   public GerritToGitBlitUserModel(
-      String username,
-      ProjectControl.Factory projectControlFactory,
-      Provider<CurrentUser> userProvider,
-      PermissionBackend persmissionBackend) {
+      String username, Provider<CurrentUser> userProvider, PermissionBackend permissionBackend) {
     super(username);
     this.username = username;
     this.isAuthenticated = true;
-    this.projectControlFactory = projectControlFactory;
     this.userProvider = userProvider;
-    this.permissionBackend = persmissionBackend;
+    this.permissionBackend = permissionBackend;
   }
 
-  public GerritToGitBlitUserModel(ProjectControl.Factory projectControlFactory) {
+  public GerritToGitBlitUserModel() {
     super(ANONYMOUS_USER);
-    this.projectControlFactory = projectControlFactory;
     this.userProvider = null;
     this.permissionBackend = null;
   }
 
-  @Deprecated
-  public boolean canAccessRepository(String repositoryName) {
-    boolean result = false;
-
-    try {
-      ProjectControl control = projectControlFactory.controlFor(new NameKey(repositoryName));
-      result = control != null;
-    } catch (NoSuchProjectException e) {
-      result = false;
-    }
-
-    return result;
+  @Override
+  public boolean canView(RepositoryModel repository, String ref) {
+    return permissionBackend
+        .user(userProvider)
+        .project(new NameKey(StringUtils.stripDotGit(repository.name)))
+        .ref(ref)
+        .testOrFalse(RefPermission.READ);
   }
 
   @Override
@@ -99,59 +86,28 @@
       RepositoryModel repository,
       AccessRestrictionType ifRestriction,
       AccessPermission requirePermission) {
-    boolean result = false;
-
-    try {
-      NameKey project = new NameKey(getRepositoryName(repository.name));
-      ProjectControl control = projectControlFactory.controlFor(project);
-
-      if (control == null) {
-        return false;
-      }
-
-      switch (ifRestriction) {
-        case VIEW:
-          return !control.getProject().getState().equals(ProjectState.HIDDEN);
-        case CLONE:
-          return permissionBackend
-              .user(userProvider)
-              .project(project)
-              .testOrFalse(ProjectPermission.RUN_UPLOAD_PACK);
-        case PUSH:
-          return permissionBackend
-              .user(userProvider)
-              .project(project)
-              .testOrFalse(ProjectPermission.RUN_RECEIVE_PACK);
-        default:
-          return true;
-      }
-    } catch (NoSuchProjectException e) {
-      result = false;
+    ForProject projectPermissions =
+        permissionBackend
+            .user(userProvider)
+            .project(new NameKey(StringUtils.stripDotGit(repository.name)));
+    switch (ifRestriction) {
+      case VIEW:
+        return projectPermissions.testOrFalse(ProjectPermission.ACCESS);
+      case CLONE:
+        return projectPermissions.testOrFalse(ProjectPermission.RUN_UPLOAD_PACK);
+      case PUSH:
+        return projectPermissions.testOrFalse(ProjectPermission.RUN_RECEIVE_PACK);
+      default:
+        return true;
     }
-
-    return result;
-  }
-
-  public String getRepositoryName(String name) {
-    if (name.endsWith(".git")) {
-      name = name.substring(0, name.length() - 4);
-    }
-    return name;
   }
 
   @Override
   public boolean hasRepositoryPermission(String name) {
-    boolean result = false;
-
-    try {
-      name = getRepositoryName(name);
-      ProjectControl control = projectControlFactory.controlFor(new NameKey(name));
-      result = control != null && !control.getProject().getState().equals(ProjectState.HIDDEN);
-    } catch (NoSuchProjectException e) {
-      result = false;
-    }
-
-    return result;
+    return permissionBackend
+        .user(userProvider)
+        .project(new NameKey(StringUtils.stripDotGit(name)))
+        .testOrFalse(ProjectPermission.ACCESS);
   }
 
   public boolean hasTeamAccess(String repositoryName) {
@@ -218,7 +174,7 @@
     return username.compareTo(o.username);
   }
 
-  public static UserModel getAnonymous(Factory projectControl) {
-    return new GerritToGitBlitUserModel(ANONYMOUS_USER, projectControl, null, null);
+  public static UserModel getAnonymous() {
+    return new GerritToGitBlitUserModel(ANONYMOUS_USER, null, null);
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserService.java b/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserService.java
index 21bd512..ed2ac6f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitblit/auth/GerritToGitBlitUserService.java
@@ -32,7 +32,6 @@
 import com.google.gerrit.server.account.AuthRequest;
 import com.google.gerrit.server.account.AuthResult;
 import com.google.gerrit.server.permissions.PermissionBackend;
-import com.google.gerrit.server.project.ProjectControl;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
@@ -50,7 +49,6 @@
 
   private final Provider<CurrentUser> userProvider;
   private final PermissionBackend permissionBackend;
-  private final ProjectControl.Factory projectControl;
   private final AccountManager accountManager;
   private final DynamicItem<WebSession> webSession;
 
@@ -60,12 +58,10 @@
   public GerritToGitBlitUserService(
       Provider<CurrentUser> userProvider,
       PermissionBackend permissionBackend,
-      ProjectControl.Factory projectControl,
       AccountManager accountManager,
       DynamicItem<WebSession> webSession) {
     this.userProvider = userProvider;
     this.permissionBackend = permissionBackend;
-    this.projectControl = projectControl;
     this.accountManager = accountManager;
     this.webSession = webSession;
   }
@@ -75,7 +71,7 @@
     String passwordString = new String(password);
 
     if (username.equals(GerritToGitBlitUserModel.ANONYMOUS_USER)) {
-      return GerritToGitBlitUserModel.getAnonymous(projectControl);
+      return GerritToGitBlitUserModel.getAnonymous();
     } else if (passwordString.startsWith(GerritToGitBlitUserService.SESSIONAUTH)) {
       return authenticateSSO(
           username, passwordString.substring(GerritToGitBlitUserService.SESSIONAUTH.length()));
@@ -102,7 +98,7 @@
       return null;
     }
 
-    return new GerritToGitBlitUserModel(username, projectControl, userProvider, permissionBackend);
+    return new GerritToGitBlitUserModel(username, userProvider, permissionBackend);
   }
 
   public UserModel authenticateBasicAuth(String username, String password) {
@@ -122,7 +118,7 @@
       return null;
     }
 
-    return new GerritToGitBlitUserModel(username, projectControl, userProvider, permissionBackend);
+    return new GerritToGitBlitUserModel(username, userProvider, permissionBackend);
   }
 
   @Override
@@ -142,7 +138,7 @@
     httpRequest.getSession().setAttribute(Constants.ATTRIB_AUTHTYPE, AuthenticationType.CONTAINER);
 
     if (Strings.isNullOrEmpty(gerritUsername) || Strings.isNullOrEmpty(gerritToken)) {
-      return GerritToGitBlitUserModel.getAnonymous(projectControl);
+      return GerritToGitBlitUserModel.getAnonymous();
     } else {
       return authenticateSSO(gerritUsername, gerritToken);
     }
@@ -220,7 +216,7 @@
 
   @Override
   public UserModel getUserModel(String username) {
-    return new GerritToGitBlitUserModel(username, projectControl, userProvider, permissionBackend);
+    return new GerritToGitBlitUserModel(username, userProvider, permissionBackend);
   }
 
   @Override