credential-form-modal.vue 3.9 KB

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