github.com/merlinepedra/gophish1@v0.9.0/static/js/src/app/users.js (about) 1 let users = [] 2 3 // Save attempts to POST or PUT to /users/ 4 const save = (id) => { 5 // Validate that the passwords match 6 if ($("#password").val() !== $("#confirm_password").val()) { 7 modalError("Passwords must match.") 8 return 9 } 10 let user = { 11 username: $("#username").val(), 12 password: $("#password").val(), 13 role: $("#role").val() 14 } 15 // Submit the user 16 if (id != -1) { 17 // If we're just editing an existing user, 18 // we need to PUT /user/:id 19 user.id = id 20 api.userId.put(user) 21 .success(function (data) { 22 successFlash("User " + escapeHtml(user.username) + " updated successfully!") 23 load() 24 dismiss() 25 $("#modal").modal('hide') 26 }) 27 .error(function (data) { 28 modalError(data.responseJSON.message) 29 }) 30 } else { 31 // Else, if this is a new user, POST it 32 // to /user 33 api.users.post(user) 34 .success(function (data) { 35 successFlash("User " + escapeHtml(user.username) + " registered successfully!") 36 load() 37 dismiss() 38 $("#modal").modal('hide') 39 }) 40 .error(function (data) { 41 modalError(data.responseJSON.message) 42 }) 43 } 44 } 45 46 const dismiss = () => { 47 $("#username").val("") 48 $("#password").val("") 49 $("#confirm_password").val("") 50 $("#role").val("") 51 $("#modal\\.flashes").empty() 52 } 53 54 const edit = (id) => { 55 $("#modalSubmit").unbind('click').click(() => { 56 save(id) 57 }) 58 $("#role").select2() 59 if (id == -1) { 60 $("#role").val("user") 61 $("#role").trigger("change") 62 } else { 63 api.userId.get(id) 64 .success(function (user) { 65 $("#username").val(user.username) 66 $("#role").val(user.role.slug) 67 $("#role").trigger("change") 68 }) 69 .error(function () { 70 errorFlash("Error fetching user") 71 }) 72 } 73 } 74 75 const deleteUser = (id) => { 76 var user = users.find(x => x.id == id) 77 if (!user) { 78 return 79 } 80 Swal.fire({ 81 title: "Are you sure?", 82 text: "This will delete the account for " + escapeHtml(user.username) + " as well as all of the objects they have created.\n\nThis can't be undone!", 83 type: "warning", 84 animation: false, 85 showCancelButton: true, 86 confirmButtonText: "Delete", 87 confirmButtonColor: "#428bca", 88 reverseButtons: true, 89 allowOutsideClick: false, 90 preConfirm: function () { 91 return new Promise((resolve, reject) => { 92 api.userId.delete(id) 93 .success((msg) => { 94 resolve() 95 }) 96 .error((data) => { 97 reject(data.responseJSON.message) 98 }) 99 }) 100 .catch(error => { 101 Swal.showValidationMessage(error) 102 }) 103 } 104 }).then(function (result) { 105 if (result.value){ 106 Swal.fire( 107 'User Deleted!', 108 "The user account for " + escapeHtml(user.username) + " and all associated objects have been deleted!", 109 'success' 110 ); 111 } 112 $('button:contains("OK")').on('click', function () { 113 location.reload() 114 }) 115 }) 116 } 117 118 119 const load = () => { 120 $("#userTable").hide() 121 $("#loading").show() 122 api.users.get() 123 .success((us) => { 124 users = us 125 $("#loading").hide() 126 $("#userTable").show() 127 let userTable = $("#userTable").DataTable({ 128 destroy: true, 129 columnDefs: [{ 130 orderable: false, 131 targets: "no-sort" 132 }] 133 }); 134 userTable.clear(); 135 $.each(users, (i, user) => { 136 userTable.row.add([ 137 escapeHtml(user.username), 138 escapeHtml(user.role.name), 139 "<div class='pull-right'><button class='btn btn-primary edit_button' data-toggle='modal' data-backdrop='static' data-target='#modal' data-user-id='" + user.id + "'>\ 140 <i class='fa fa-pencil'></i>\ 141 </button>\ 142 <button class='btn btn-danger delete_button' data-user-id='" + user.id + "'>\ 143 <i class='fa fa-trash-o'></i>\ 144 </button></div>" 145 ]).draw() 146 }) 147 }) 148 .error(() => { 149 errorFlash("Error fetching users") 150 }) 151 } 152 153 $(document).ready(function () { 154 load() 155 // Setup the event listeners 156 $("#modal").on("hide.bs.modal", function () { 157 dismiss(); 158 }); 159 // Select2 Defaults 160 $.fn.select2.defaults.set("width", "100%"); 161 $.fn.select2.defaults.set("dropdownParent", $("#role-select")); 162 $.fn.select2.defaults.set("theme", "bootstrap"); 163 $.fn.select2.defaults.set("sorter", function (data) { 164 return data.sort(function (a, b) { 165 if (a.text.toLowerCase() > b.text.toLowerCase()) { 166 return 1; 167 } 168 if (a.text.toLowerCase() < b.text.toLowerCase()) { 169 return -1; 170 } 171 return 0; 172 }); 173 }) 174 $("#new_button").on("click", function () { 175 edit(-1) 176 }) 177 $("#userTable").on('click', '.edit_button', function (e) { 178 edit($(this).attr('data-user-id')) 179 }) 180 $("#userTable").on('click', '.delete_button', function (e) { 181 deleteUser($(this).attr('data-user-id')) 182 }) 183 });