Add user ref operation validation example.
The following example blocks any update to any ref that starts with
'refs/heads/protected-' prefix if it is performed by user that has
no 'Administrate Server' capability.
Change-Id: If5373d238a4d30fc450a4114e6af51d6536ae91a
Signed-off-by: Jacek Centkowski <geminica.programs@gmail.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java
index 297e1e8..b17c871 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java
@@ -38,6 +38,7 @@
import com.google.gerrit.server.config.ProjectConfigEntry;
import com.google.gerrit.server.git.validators.CommitValidationListener;
import com.google.gerrit.server.git.validators.MergeValidationListener;
+import com.google.gerrit.server.git.validators.RefOperationValidationListener;
import com.google.gerrit.server.git.validators.UploadValidationListener;
import com.google.gerrit.server.plugins.ServerPluginProvider;
import com.google.gerrit.server.query.change.ChangeQueryBuilder.ChangeOperatorFactory;
@@ -78,6 +79,8 @@
.to(CommitValidator.class);
DynamicSet.bind(binder(), NewProjectCreatedListener.class)
.to(ProjectCreatedListener.class);
+ DynamicSet.bind(binder(), RefOperationValidationListener.class)
+ .to(RefOperationValidationExample.class);
configurePluginParameters();
DynamicSet.bind(binder(), ExternalIncludedIn.class)
.to(DeployedOnIncludedInExtension.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/RefOperationValidationExample.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/RefOperationValidationExample.java
new file mode 100644
index 0000000..2755902
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/RefOperationValidationExample.java
@@ -0,0 +1,45 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.cookbook;
+
+import com.google.common.collect.Lists;
+import com.google.gerrit.reviewdb.client.RefNames;
+import com.google.gerrit.server.events.RefReceivedEvent;
+import com.google.gerrit.server.git.validators.RefOperationValidationListener;
+import com.google.gerrit.server.git.validators.ValidationMessage;
+import com.google.gerrit.server.validators.ValidationException;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class RefOperationValidationExample implements
+ RefOperationValidationListener {
+
+ @Override
+ public List<ValidationMessage> onRefOperation(RefReceivedEvent event)
+ throws ValidationException {
+ ArrayList<ValidationMessage> messages = Lists.newArrayList();
+ if (event.command.getRefName()
+ .startsWith(RefNames.REFS_HEADS + "protected-")
+ && !event.user.getCapabilities().canAdministrateServer()) {
+ throw new ValidationException(String.format(
+ "Operation %s on %s branch in project %s is not valid!",
+ event.command.getType(),
+ event.command.getRefName(),
+ event.project.getName()));
+ }
+ return messages;
+ }
+}