github.com/merlinepedra/gophish1@v0.9.0/static/js/src/app/webhooks.js (about)

     1  let webhooks = [];
     2  
     3  const dismiss = () => {
     4      $("#name").val("");
     5      $("#url").val("");
     6      $("#secret").val("");
     7      $("#is_active").prop("checked", false);
     8      $("#flashes").empty();
     9  };
    10  
    11  const saveWebhook = (id) => {
    12      let wh = {
    13          name: $("#name").val(),
    14          url: $("#url").val(),
    15          secret: $("#secret").val(),
    16          is_active: $("#is_active").is(":checked"),
    17      };
    18      if (id != -1) {
    19          wh.id = id;
    20          api.webhookId.put(wh)
    21              .success(function(data) {
    22                  dismiss();
    23                  load();
    24                  $("#modal").modal("hide");
    25                  successFlash(`Webhook "${escape(wh.name)}" has been updated successfully!`);
    26              })
    27              .error(function(data) {
    28                  modalError(data.responseJSON.message)
    29              })
    30      } else {
    31          api.webhooks.post(wh)
    32              .success(function(data) {
    33                  load();
    34                  dismiss();
    35                  $("#modal").modal("hide");
    36                  successFlash(`Webhook "${escape(wh.name)}" has been created successfully!`);
    37              })
    38              .error(function(data) {
    39                  modalError(data.responseJSON.message)
    40              })
    41      }
    42  };
    43  
    44  const load = () => {
    45      $("#webhookTable").hide();
    46      $("#loading").show();
    47      api.webhooks.get()
    48          .success((whs) => {
    49              webhooks = whs;
    50              $("#loading").hide()
    51              $("#webhookTable").show()
    52              let webhookTable = $("#webhookTable").DataTable({
    53                  destroy: true,
    54                  columnDefs: [{
    55                      orderable: false,
    56                      targets: "no-sort"
    57                  }]
    58              });
    59              webhookTable.clear();
    60              $.each(webhooks, (i, webhook) => {
    61                  webhookTable.row.add([
    62                      escapeHtml(webhook.name),
    63                      escapeHtml(webhook.url),
    64                      escapeHtml(webhook.is_active),
    65                      `
    66                        <div class="pull-right">
    67                          <button class="btn btn-primary ping_button" data-webhook-id="${webhook.id}">
    68                            Ping
    69                          </button>
    70                          <button class="btn btn-primary edit_button" data-toggle="modal" data-backdrop="static" data-target="#modal" data-webhook-id="${webhook.id}">
    71                            <i class="fa fa-pencil"></i>
    72                          </button>
    73                          <button class="btn btn-danger delete_button" data-webhook-id="${webhook.id}">
    74                            <i class="fa fa-trash-o"></i>
    75                          </button>
    76                        </div>
    77                      `
    78                  ]).draw()
    79              })
    80          })
    81          .error(() => {
    82              errorFlash("Error fetching webhooks")
    83          })
    84  };
    85  
    86  const editWebhook = (id) => {
    87      $("#modalSubmit").unbind("click").click(() => {
    88          saveWebhook(id);
    89      });
    90      if (id !== -1) {
    91          api.webhookId.get(id)
    92            .success(function(wh) {
    93                $("#name").val(wh.name);
    94                $("#url").val(wh.url);
    95                $("#secret").val(wh.secret);
    96                $("#is_active").prop("checked", wh.is_active);
    97            })
    98            .error(function () {
    99                errorFlash("Error fetching webhook")
   100            });
   101      }
   102  };
   103  
   104  const deleteWebhook = (id) => {
   105      var wh = webhooks.find(x => x.id == id);
   106      if (!wh) {
   107          return;
   108      }
   109      Swal.fire({
   110          title: "Are you sure?",
   111          text: `This will delete the webhook '${escape(wh.name)}'`,
   112          type: "warning",
   113          animation: false,
   114          showCancelButton: true,
   115          confirmButtonText: "Delete",
   116          confirmButtonColor: "#428bca",
   117          reverseButtons: true,
   118          allowOutsideClick: false,
   119          preConfirm: function () {
   120              return new Promise((resolve, reject) => {
   121                  api.webhookId.delete(id)
   122                      .success((msg) => {
   123                          resolve()
   124                      })
   125                      .error((data) => {
   126                          reject(data.responseJSON.message)
   127                      })
   128              })
   129              .catch(error => {
   130                  Swal.showValidationMessage(error)
   131                })
   132          }
   133      }).then(function(result) {
   134          if (result.value) {
   135              Swal.fire(
   136                  "Webhook Deleted!",
   137                  `The webhook has been deleted!`,
   138                  "success"
   139              );
   140          }
   141          $("button:contains('OK')").on("click", function() {
   142              location.reload();
   143          })
   144      })
   145  };
   146  
   147  const pingUrl = (btn, whId) => {
   148      dismiss();
   149      btn.disabled = true;
   150      api.webhookId.ping(whId)
   151          .success(function(wh) {
   152              btn.disabled = false;
   153              successFlash(`Ping of "${escape(wh.name)}" webhook succeeded.`);
   154          })
   155          .error(function(data) {
   156              btn.disabled = false;
   157              var wh = webhooks.find(x => x.id == whId);
   158              if (!wh) {
   159                  return
   160              }
   161              errorFlash(`Ping of "${escape(wh.name)}" webhook failed: "${data.responseJSON.message}"`)
   162          });
   163  };
   164  
   165  $(document).ready(function() {
   166      load();
   167      $("#modal").on("hide.bs.modal", function() {
   168          dismiss();
   169      });
   170      $("#new_button").on("click", function() {
   171          editWebhook(-1);
   172      });
   173      $("#webhookTable").on("click", ".edit_button", function(e) {
   174          editWebhook($(this).attr("data-webhook-id"));
   175      });
   176      $("#webhookTable").on("click", ".delete_button", function(e) {
   177          deleteWebhook($(this).attr("data-webhook-id"));
   178      });
   179      $("#webhookTable").on("click", ".ping_button", function(e) {
   180          pingUrl(e.currentTarget, e.currentTarget.dataset.webhookId);
   181      });
   182  });