credential-form-modal.vue 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <modal :id="id" tabindex="-1" role="dialog" :aria-labelledby="id+'Label'">
  3. <h5 slot="header" class="modal-title" :id="id+'Label'">
  4. Edit Credential
  5. </h5>
  6. <div slot="body">
  7. <div class="form">
  8. <autocomplete-custom-dropdown :options="descriptions" :value="creddesc" v-model="creddesc" :allow-custom="true"></autocomplete-custom-dropdown>
  9. </div>
  10. <div class="form-group">
  11. <label for="newusername">Username</label>
  12. <input type="text" :name="'newusername'+data.credid" :id="'newusername'+data.credid" class="form-control credential" v-model="data.creduser">
  13. </div>
  14. <div class="form-group">
  15. <label for="newpassword">Password</label>
  16. <input type="text" :name="'newpassword'+data.credid" :id="'newpassword'+data.credid" class="form-control credential" v-model="data.credpass">
  17. <button type="button" class="btn btn-secondary" @click="getRandomPassword(data.credid)">Generate Random</button>
  18. </div>
  19. <div class="form-group"></div>
  20. </div>
  21. <div slot="footer">
  22. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  23. <button type="button" class="btn btn-primary" @click="updateCredential()">Save</button>
  24. </div>
  25. </modal>
  26. </template>
  27. <script>
  28. export default {
  29. props: ['populateWith', 'modalId', 'descriptions'],
  30. data() {
  31. return {
  32. data: JSON.parse(JSON.stringify(this.populateWith)),
  33. creddesc: {},
  34. id: this.modalId,
  35. errors: []
  36. }
  37. },
  38. mounted () {
  39. // Definitely needs to be refactored into something that runs once
  40. // per page instead of once per credential. Only putting this here now
  41. // to test something else.
  42. this.creddesc = {
  43. 'id': this.data.credtype,
  44. 'name': this.data.creddesc
  45. }
  46. },
  47. methods: {
  48. rndStr(len) {
  49. let text = ""
  50. let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%"
  51. for( let i=0; i < len; i++ ) {
  52. text += chars.charAt(Math.floor(Math.random() * chars.length))
  53. }
  54. return text
  55. },
  56. getRandomPassword(id) {
  57. this.data.credpass = this.rndStr(16);
  58. },
  59. updateCredential() {
  60. console.log(this.creddesc)
  61. if (this.creddesc.id === "custom") {
  62. this.data.credtype = 1
  63. this.data.creddesc = this.creddesc.name
  64. }
  65. else {
  66. this.data.credtype = this.creddesc.id
  67. this.data.creddesc = this.creddesc.name
  68. }
  69. axios.put('/api/credentials/' + this.data.credid, this.data).then(response => {}).catch(error =>{console.error(error)});
  70. $('#credential'+this.data.credid+'editModal').modal('hide');
  71. }
  72. }
  73. }
  74. </script>