Adapt to PermissionBackend API

Change-Id: I1b982fc54f9aa53dedb2c403f2bebc50a1e56c76
diff --git a/pom.xml b/pom.xml
index 15a4f17..e2b7b28 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
   <artifactId>metrics-reporter-prometheus</artifactId>
   <name>metrics-reporter-prometheus</name>
   <packaging>jar</packaging>
-  <version>2.14</version>
+  <version>2.15</version>
   <properties>
     <Gerrit-ApiType>plugin</Gerrit-ApiType>
     <Gerrit-ApiVersion>${project.version}</Gerrit-ApiVersion>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java
index 32ca8f6..b574e40 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java
@@ -13,26 +13,43 @@
 // limitations under the License.
 package com.googlesource.gerrit.plugins.metricsreporters;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.api.access.PluginPermission;
+import com.google.gerrit.extensions.restapi.AuthException;
 import com.google.gerrit.server.CurrentUser;
-import com.google.gerrit.server.account.CapabilityControl;
+import com.google.gerrit.server.permissions.GlobalPermission;
+import com.google.gerrit.server.permissions.PermissionBackend;
+import com.google.gerrit.server.permissions.PermissionBackendException;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
+import com.google.inject.Singleton;
 
+@Singleton
 public class CapabilityChecker {
+  private final PermissionBackend permissionBackend;
   private final Provider<CurrentUser> userProvider;
-  private final String capabilityName;
-
+  private final String pluginName;
   @Inject
-  CapabilityChecker(Provider<CurrentUser> userProvider,
+  CapabilityChecker(
+      PermissionBackend permissionBackend,
+      Provider<CurrentUser> userProvider,
       @PluginName String pluginName) {
+    this.permissionBackend = permissionBackend;
     this.userProvider = userProvider;
-    this.capabilityName = String.format("%s-%s", pluginName,
-        ViewMetricsCapability.ID);
+    this.pluginName = pluginName;
   }
-
   public boolean canViewMetrics() {
-    CapabilityControl ctl = userProvider.get().getCapabilities();
-    return ctl.canAdministrateServer() || ctl.canPerform(capabilityName);
+    try {
+      permissionBackend
+          .user(userProvider)
+          .checkAny(
+              ImmutableSet.of(
+                  GlobalPermission.ADMINISTRATE_SERVER,
+                  new PluginPermission(pluginName, ViewMetricsCapability.ID)));
+      return true;
+    } catch (AuthException | PermissionBackendException e) {
+      return false;
+    }
   }
 }
\ No newline at end of file