github.com/topsteplocal/gophish@v0.6.0/static/js/src/app/users.js (about) 1 var groups = [] 2 3 // Save attempts to POST or PUT to /groups/ 4 function save(id) { 5 var targets = [] 6 $.each($("#targetsTable").DataTable().rows().data(), function (i, target) { 7 targets.push({ 8 first_name: unescapeHtml(target[0]), 9 last_name: unescapeHtml(target[1]), 10 email: unescapeHtml(target[2]), 11 position: unescapeHtml(target[3]) 12 }) 13 }) 14 var group = { 15 name: $("#name").val(), 16 targets: targets 17 } 18 // Submit the group 19 if (id != -1) { 20 // If we're just editing an existing group, 21 // we need to PUT /groups/:id 22 group.id = id 23 api.groupId.put(group) 24 .success(function (data) { 25 successFlash("Group updated successfully!") 26 load() 27 dismiss() 28 $("#modal").modal('hide') 29 }) 30 .error(function (data) { 31 modalError(data.responseJSON.message) 32 }) 33 } else { 34 // Else, if this is a new group, POST it 35 // to /groups 36 api.groups.post(group) 37 .success(function (data) { 38 successFlash("Group added successfully!") 39 load() 40 dismiss() 41 $("#modal").modal('hide') 42 }) 43 .error(function (data) { 44 modalError(data.responseJSON.message) 45 }) 46 } 47 } 48 49 function dismiss() { 50 $("#targetsTable").dataTable().DataTable().clear().draw() 51 $("#name").val("") 52 $("#modal\\.flashes").empty() 53 } 54 55 function edit(id) { 56 targets = $("#targetsTable").dataTable({ 57 destroy: true, // Destroy any other instantiated table - http://datatables.net/manual/tech-notes/3#destroy 58 columnDefs: [{ 59 orderable: false, 60 targets: "no-sort" 61 }] 62 }) 63 $("#modalSubmit").unbind('click').click(function () { 64 save(id) 65 }) 66 if (id == -1) { 67 var group = {} 68 } else { 69 api.groupId.get(id) 70 .success(function (group) { 71 $("#name").val(group.name) 72 $.each(group.targets, function (i, record) { 73 targets.DataTable() 74 .row.add([ 75 escapeHtml(record.first_name), 76 escapeHtml(record.last_name), 77 escapeHtml(record.email), 78 escapeHtml(record.position), 79 '<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>' 80 ]).draw() 81 }); 82 83 }) 84 .error(function () { 85 errorFlash("Error fetching group") 86 }) 87 } 88 // Handle file uploads 89 $("#csvupload").fileupload({ 90 url: "/api/import/group?api_key=" + user.api_key, 91 dataType: "json", 92 add: function (e, data) { 93 $("#modal\\.flashes").empty() 94 var acceptFileTypes = /(csv|txt)$/i; 95 var filename = data.originalFiles[0]['name'] 96 if (filename && !acceptFileTypes.test(filename.split(".").pop())) { 97 modalError("Unsupported file extension (use .csv or .txt)") 98 return false; 99 } 100 data.submit(); 101 }, 102 done: function (e, data) { 103 $.each(data.result, function (i, record) { 104 addTarget( 105 record.first_name, 106 record.last_name, 107 record.email, 108 record.position); 109 }); 110 targets.DataTable().draw(); 111 } 112 }) 113 } 114 115 function deleteGroup(id) { 116 var group = groups.find(function (x) { 117 return x.id === id 118 }) 119 if (!group) { 120 console.log('wat'); 121 return 122 } 123 if (confirm("Delete " + group.name + "?")) { 124 api.groupId.delete(id) 125 .success(function (data) { 126 successFlash(data.message) 127 load() 128 }) 129 } 130 } 131 132 function addTarget(firstNameInput, lastNameInput, emailInput, positionInput) { 133 // Create new data row. 134 var email = escapeHtml(emailInput).toLowerCase(); 135 var newRow = [ 136 escapeHtml(firstNameInput), 137 escapeHtml(lastNameInput), 138 email, 139 escapeHtml(positionInput), 140 '<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>' 141 ]; 142 143 // Check table to see if email already exists. 144 var targetsTable = targets.DataTable(); 145 var existingRowIndex = targetsTable 146 .column(2, { 147 order: "index" 148 }) // Email column has index of 2 149 .data() 150 .indexOf(email); 151 // Update or add new row as necessary. 152 if (existingRowIndex >= 0) { 153 targetsTable 154 .row(existingRowIndex, { 155 order: "index" 156 }) 157 .data(newRow); 158 } else { 159 targetsTable.row.add(newRow); 160 } 161 } 162 163 function load() { 164 $("#groupTable").hide() 165 $("#emptyMessage").hide() 166 $("#loading").show() 167 api.groups.summary() 168 .success(function (response) { 169 $("#loading").hide() 170 if (response.total > 0) { 171 groups = response.groups 172 $("#emptyMessage").hide() 173 $("#groupTable").show() 174 var groupTable = $("#groupTable").DataTable({ 175 destroy: true, 176 columnDefs: [{ 177 orderable: false, 178 targets: "no-sort" 179 }] 180 }); 181 groupTable.clear(); 182 $.each(groups, function (i, group) { 183 groupTable.row.add([ 184 escapeHtml(group.name), 185 escapeHtml(group.num_targets), 186 moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'), 187 "<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#modal' onclick='edit(" + group.id + ")'>\ 188 <i class='fa fa-pencil'></i>\ 189 </button>\ 190 <button class='btn btn-danger' onclick='deleteGroup(" + group.id + ")'>\ 191 <i class='fa fa-trash-o'></i>\ 192 </button></div>" 193 ]).draw() 194 }) 195 } else { 196 $("#emptyMessage").show() 197 } 198 }) 199 .error(function () { 200 errorFlash("Error fetching groups") 201 }) 202 } 203 204 $(document).ready(function () { 205 load() 206 // Setup the event listeners 207 // Handle manual additions 208 $("#targetForm").submit(function () { 209 addTarget( 210 $("#firstName").val(), 211 $("#lastName").val(), 212 $("#email").val(), 213 $("#position").val()); 214 targets.DataTable().draw(); 215 216 // Reset user input. 217 $("#targetForm>div>input").val(''); 218 $("#firstName").focus(); 219 return false; 220 }); 221 // Handle Deletion 222 $("#targetsTable").on("click", "span>i.fa-trash-o", function () { 223 targets.DataTable() 224 .row($(this).parents('tr')) 225 .remove() 226 .draw(); 227 }); 228 $("#modal").on("hide.bs.modal", function () { 229 dismiss(); 230 }); 231 });