Merge "Show configurable message on successful service user creation"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetMessages.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetMessages.java
new file mode 100644
index 0000000..ef885ff
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetMessages.java
@@ -0,0 +1,45 @@
+// Copyright (C) 2013 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.serviceuser;
+
+import com.google.common.base.Strings;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.google.gerrit.server.config.ConfigResource;
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.inject.Inject;
+
+public class GetMessages implements RestReadView<ConfigResource> {
+
+  private final PluginConfig cfg;
+
+  @Inject
+  public GetMessages(PluginConfigFactory cfgFactory,
+      @PluginName String pluginName) {
+    this.cfg = cfgFactory.getFromGerritConfig(pluginName);
+  }
+
+  @Override
+  public MessagesInfo apply(ConfigResource rsrc) {
+    MessagesInfo info = new MessagesInfo();
+    info.onSuccess = Strings.emptyToNull(cfg.getString("onSuccessMessage"));
+    return info;
+  }
+
+  public class MessagesInfo {
+    String onSuccess;
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java
index 697b573..4a45a89 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java
@@ -38,6 +38,7 @@
         bind(ServiceUserCollection.class);
         child(CONFIG_KIND, "serviceusers").to(ServiceUserCollection.class);
         install(new FactoryModuleBuilder().build(CreateServiceUser.Factory.class));
+        get(CONFIG_KIND, "messages").to(GetMessages.class);
       }
     });
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/CreateServiceUserForm.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/CreateServiceUserForm.java
index 2dfbe18..c2a300a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/CreateServiceUserForm.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/CreateServiceUserForm.java
@@ -43,6 +43,7 @@
   private DialogBox dialogBox;
   private TextBox usernameTxt;
   private TextArea sshKeyTxt;
+  private String onSuccessMessage;
 
   @Override
   public void onModuleLoad() {
@@ -154,6 +155,19 @@
           createButton.setEnabled(false);
         }
     }, ClickEvent.getType());
+
+    new RestApi("config").id("server").view("serviceuser", "messages")
+        .get(new AsyncCallback<MessagesInfo>() {
+          @Override
+          public void onSuccess(MessagesInfo info) {
+            onSuccessMessage = info.getOnSuccessMessage();
+          }
+
+          @Override
+          public void onFailure(Throwable caught) {
+            // never invoked
+          }
+    });
   }
 
   private void doCreate() {
@@ -185,6 +199,11 @@
             successDialog.hide();
           }
         });
+
+        if (onSuccessMessage != null && !"".equals(onSuccessMessage)) {
+          p.add(new HTML(onSuccessMessage));
+        }
+
         p.add(okButton);
         successDialog.add(p);
 
@@ -204,6 +223,13 @@
     sshKeyTxt.setValue("");
   }
 
+  private static class MessagesInfo extends JavaScriptObject {
+    public final native String getOnSuccessMessage() /*-{ return this.on_success }-*/;
+
+    protected MessagesInfo() {
+    }
+  }
+
   private static class ServiceUserInput extends JavaScriptObject {
     final native void ssh_key(String s) /*-{ this.ssh_key = s; }-*/;
 
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index ebf5b37..4938613 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -21,3 +21,8 @@
 :	A group to which newly created service users should be
     automatically added. Multiple groups can be specified by having
     multiple `plugin.@PLUGIN@.group` entries.
+
+<a id="onSuccessMessage">
+`plugin.@PLUGIN@.onSuccessMessage`
+:	HTML formatted message that should be displayed after a service
+	user was successfully created.
diff --git a/src/main/resources/Documentation/rest-api-config.md b/src/main/resources/Documentation/rest-api-config.md
index d2b2a2f..5f35b61 100644
--- a/src/main/resources/Documentation/rest-api-config.md
+++ b/src/main/resources/Documentation/rest-api-config.md
@@ -51,10 +51,46 @@
   }
 ```
 
+### <a id="get-messages"> Get Messages
+_GET /config/server/@PLUGIN@~messages_
+
+Gets help messages to be displayed for the service user creation in the
+Web UI.
+
+#### Request
+
+```
+  GET /config/server/@PLUGIN@~messages HTTP/1.0
+```
+
+As response a [MessagesInfo](#messages-info) entity is returned that
+contains the messages.
+
+#### Response
+
+```
+  HTTP/1.1 200 OK
+  Content-Disposition: attachment
+  Content-Type: application/json;charset=UTF-8
+
+  )]}'
+  {
+    "on_success": "Don't forget to assign \u003ca href\u003d\"Documentation/access-control.html\"\u003eaccess rights\u003c/a\u003e to the service user."
+  }
+```
+
 
 <a id="json-entities">JSON Entities
 -----------------------------------
 
+### <a id="messages-info"></a>MessagesInfo
+
+The `MessagesInfo` entity contains help messages that should be
+displayed for the service user creation in the Web UI.
+
+* _on\_success_: HTML formatted message that should be displayed after
+  a service user was successfully created.
+
 ### <a id="service-user-input"></a>ServiceUserInput
 
 The `ServiceUserInput` entity contains options for creating a service