admin-users-create.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template lang="pug">
  2. v-dialog(v-model='isShown', max-width='650', persistent)
  3. v-card.wiki-form
  4. .dialog-header.is-short
  5. v-icon.mr-3(color='white') mdi-plus
  6. span New User
  7. v-spacer
  8. v-btn.mx-0(color='white', outlined, disabled, dark)
  9. v-icon(left) mdi-database-import
  10. span Bulk Import
  11. v-card-text
  12. v-select(
  13. :items='providers'
  14. item-text='title'
  15. item-value='key'
  16. outlined
  17. prepend-icon='mdi-domain'
  18. v-model='provider'
  19. label='Provider'
  20. )
  21. v-text-field(
  22. outlined
  23. prepend-icon='mdi-at'
  24. v-model='email'
  25. label='Email Address'
  26. v-validate='{ required: true, email: true, min: 2, max: 255 }',
  27. data-vv-name='email',
  28. data-vv-as='Email Address',
  29. data-vv-scope='newUser',
  30. :error-messages='errors.collect(`newUser.email`)'
  31. key='newUserEmail'
  32. persistent-hint
  33. ref='emailInput'
  34. )
  35. v-text-field(
  36. v-if='provider === `local`'
  37. outlined
  38. prepend-icon='mdi-lock-outline'
  39. append-icon='mdi-dice-5'
  40. v-model='password'
  41. :label='mustChangePwd ? `Temporary Password` : `Password`'
  42. counter='255'
  43. @click:append='generatePwd'
  44. v-validate='{ required: true, min: 6, max: 255 }',
  45. data-vv-name='password',
  46. data-vv-as='Password',
  47. data-vv-scope='newUser',
  48. :error-messages='errors.collect(`newUser.password`)'
  49. key='newUserPassword'
  50. persistent-hint
  51. )
  52. v-text-field(
  53. outlined
  54. prepend-icon='mdi-account-outline'
  55. v-model='name'
  56. label='Name'
  57. v-validate='{ required: true, min: 2, max: 255 }',
  58. data-vv-name='name',
  59. data-vv-as='Name',
  60. data-vv-scope='newUser',
  61. :error-messages='errors.collect(`newUser.name`)'
  62. key='newUserName'
  63. persistent-hint
  64. )
  65. v-select(
  66. :items='groups'
  67. item-text='name'
  68. item-value='key'
  69. outlined
  70. prepend-icon='mdi-account-group'
  71. v-model='group'
  72. label='Assign to Group(s)...'
  73. clearable
  74. multiple
  75. )
  76. v-divider
  77. v-checkbox(
  78. color='primary'
  79. label='Require password change on first login'
  80. v-if='provider === `local`'
  81. v-model='mustChangePwd'
  82. hide-details
  83. )
  84. v-checkbox(
  85. color='primary'
  86. label='Send a welcome email'
  87. hide-details
  88. v-model='sendWelcomeEmail'
  89. )
  90. v-card-chin
  91. v-spacer
  92. v-btn(text, @click='isShown = false') Cancel
  93. v-btn(depressed, color='primary', @click='newUser(false)', :disabled='errors.any(`newUser`)') Create
  94. v-btn(depressed, color='primary', @click='newUser(true)', :disabled='errors.any(`newUser`)') Create and Close
  95. </template>
  96. <script>
  97. import _ from 'lodash'
  98. import createUserMutation from 'gql/admin/users/users-mutation-create.gql'
  99. import providersQuery from 'gql/admin/users/users-query-strategies.gql'
  100. import groupsQuery from 'gql/admin/auth/auth-query-groups.gql'
  101. export default {
  102. props: {
  103. value: {
  104. type: Boolean,
  105. default: false
  106. }
  107. },
  108. data() {
  109. return {
  110. providers: [],
  111. provider: 'local',
  112. email: '',
  113. password: '',
  114. name: '',
  115. groups: [],
  116. group: '',
  117. mustChangePwd: false,
  118. sendWelcomeEmail: false
  119. }
  120. },
  121. computed: {
  122. isShown: {
  123. get() { return this.value },
  124. set(val) { this.$emit('input', val) }
  125. }
  126. },
  127. watch: {
  128. value(newValue, oldValue) {
  129. if (newValue) {
  130. this.$validator.reset()
  131. this.$nextTick(() => {
  132. this.$refs.emailInput.focus()
  133. })
  134. }
  135. },
  136. provider(newValue, oldValue) {
  137. this.$validator.reset()
  138. }
  139. },
  140. methods: {
  141. async newUser(close = false) {
  142. const validationSuccess = await this.$validator.validateAll('newUser')
  143. if (!validationSuccess) {
  144. return
  145. }
  146. try {
  147. const resp = await this.$apollo.mutate({
  148. mutation: createUserMutation,
  149. variables: {
  150. providerKey: this.provider,
  151. email: this.email,
  152. passwordRaw: this.password,
  153. name: this.name,
  154. groups: this.groups,
  155. mustChangePassword: this.mustChangePwd,
  156. sendWelcomeEmail: this.sendWelcomeEmail
  157. },
  158. watchLoading (isLoading) {
  159. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-create')
  160. }
  161. })
  162. if (_.get(resp, 'data.users.create.responseResult.succeeded', false)) {
  163. this.$store.commit('showNotification', {
  164. style: 'success',
  165. message: 'New user created successfully.',
  166. icon: 'check'
  167. })
  168. this.email = ''
  169. this.password = ''
  170. this.name = ''
  171. if (close) {
  172. this.isShown = false
  173. } else {
  174. this.$refs.emailInput.focus()
  175. }
  176. } else {
  177. this.$store.commit('showNotification', {
  178. style: 'red',
  179. message: _.get(resp, 'data.users.create.responseResult.message', 'An unexpected error occured.'),
  180. icon: 'warning'
  181. })
  182. }
  183. } catch (err) {
  184. this.$store.commit('pushGraphError', err)
  185. }
  186. },
  187. generatePwd() {
  188. const pwdChars = 'abcdefghkmnpqrstuvwxyzABCDEFHJKLMNPQRSTUVWXYZ23456789_*=?#!()+'
  189. this.password = _.sampleSize(pwdChars, 12).join('')
  190. }
  191. },
  192. apollo: {
  193. providers: {
  194. query: providersQuery,
  195. fetchPolicy: 'network-only',
  196. update: (data) => data.authentication.strategies,
  197. watchLoading (isLoading) {
  198. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-strategies-refresh')
  199. }
  200. },
  201. groups: {
  202. query: groupsQuery,
  203. fetchPolicy: 'network-only',
  204. update: (data) => data.groups.list,
  205. watchLoading (isLoading) {
  206. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
  207. }
  208. }
  209. }
  210. }
  211. </script>