CredentialFormModal.vue 4.7 KB

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