CredentialFormModal.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import Modal from '../components/Modal.vue'
  29. import AutocompleteCustomDropdown from '../components/AutocompleteCustomDropdown.vue'
  30. export default {
  31. components: {
  32. AutocompleteCustomDropdown,
  33. Modal,
  34. },
  35. props: {
  36. populateWith: {
  37. type: Object,
  38. default () {
  39. return {
  40. creddesc: null,
  41. creduser: null,
  42. credpass: null,
  43. groupid: null,
  44. pcid: null,
  45. credtype: null,
  46. }
  47. }
  48. },
  49. modalId: {
  50. type: String,
  51. required: true
  52. },
  53. descriptions: {
  54. type: Object,
  55. required: true
  56. },
  57. create: {
  58. type: Boolean
  59. },
  60. pcid: {
  61. type: Number
  62. },
  63. groupid: {
  64. type: Number
  65. }
  66. },
  67. data() {
  68. return {
  69. data: JSON.parse(JSON.stringify(this.populateWith)),
  70. creddesc: {},
  71. id: this.modalId,
  72. errors: []
  73. }
  74. },
  75. mounted () {
  76. this.creddesc = {
  77. 'id': this.data.credtype,
  78. 'name': this.data.creddesc
  79. }
  80. },
  81. methods: {
  82. rndStr(len) {
  83. let text = ""
  84. let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%"
  85. for( let i=0; i < len; i++ ) {
  86. text += chars.charAt(Math.floor(Math.random() * chars.length))
  87. }
  88. return text
  89. },
  90. getRandomPassword(id) {
  91. this.data.credpass = this.rndStr(16);
  92. },
  93. updateCredential() {
  94. if (this.creddesc.id === "custom") {
  95. this.data.credtype = 1
  96. this.data.creddesc = this.creddesc.name
  97. }
  98. else {
  99. this.data.credtype = this.creddesc.id
  100. this.data.creddesc = this.creddesc.name
  101. }
  102. if (this.create) {
  103. if(this.pcid) {
  104. this.data.pcid = this.pcid
  105. }
  106. if(this.groupid) {
  107. this.data.groupid = this.groupid
  108. }
  109. axios.post('/api/credentials/', this.data)
  110. .then(response => {
  111. $('#'+this.id).modal('hide');
  112. })
  113. } else {
  114. axios.put('/api/credentials/' + this.data.credid, this.data)
  115. .then(response => {
  116. $('#'+this.id).modal('hide');
  117. }).catch(error =>{console.error(error)});
  118. }
  119. }
  120. }
  121. }
  122. </script>