admin-users-create.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template lang="pug">
  2. v-dialog(v-model='isShown', max-width='650', persistent)
  3. v-card
  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.pt-5
  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. :hint='provider === `local` ? `Can be changed by the user.` : `May be overwritten by the provider during login.`'
  63. key='newUserName'
  64. persistent-hint
  65. )
  66. v-select.mt-2(
  67. :items='groups'
  68. item-text='name'
  69. item-value='id'
  70. item-disabled='isSystem'
  71. outlined
  72. prepend-icon='mdi-account-group'
  73. v-model='group'
  74. label='Assign to Group(s)...'
  75. dense
  76. clearable
  77. multiple
  78. )
  79. v-divider
  80. v-checkbox(
  81. color='primary'
  82. label='Require password change on first login'
  83. v-if='provider === `local`'
  84. v-model='mustChangePwd'
  85. hide-details
  86. )
  87. v-checkbox(
  88. color='primary'
  89. label='Send a welcome email'
  90. hide-details
  91. v-model='sendWelcomeEmail'
  92. disabled
  93. )
  94. v-card-chin
  95. v-spacer
  96. v-btn(text, @click='isShown = false') Cancel
  97. v-btn.px-3(depressed, color='primary', @click='newUser(false)', :disabled='errors.any(`newUser`)')
  98. v-icon(left) mdi-chevron-right
  99. span Create
  100. v-btn.px-3(depressed, color='primary', @click='newUser(true)', :disabled='errors.any(`newUser`)')
  101. v-icon(left) mdi-chevron-double-right
  102. span Create and Close
  103. </template>
  104. <script>
  105. import _ from 'lodash'
  106. import createUserMutation from 'gql/admin/users/users-mutation-create.gql'
  107. import providersQuery from 'gql/admin/users/users-query-strategies.gql'
  108. import groupsQuery from 'gql/admin/users/users-query-groups.gql'
  109. export default {
  110. props: {
  111. value: {
  112. type: Boolean,
  113. default: false
  114. }
  115. },
  116. data() {
  117. return {
  118. providers: [],
  119. provider: 'local',
  120. email: '',
  121. password: '',
  122. name: '',
  123. groups: [],
  124. group: [],
  125. mustChangePwd: false,
  126. sendWelcomeEmail: false
  127. }
  128. },
  129. computed: {
  130. isShown: {
  131. get() { return this.value },
  132. set(val) { this.$emit('input', val) }
  133. }
  134. },
  135. watch: {
  136. value(newValue, oldValue) {
  137. if (newValue) {
  138. this.$validator.reset()
  139. this.$nextTick(() => {
  140. this.$refs.emailInput.focus()
  141. })
  142. }
  143. },
  144. provider(newValue, oldValue) {
  145. this.$validator.reset()
  146. }
  147. },
  148. methods: {
  149. async newUser(close = false) {
  150. const validationSuccess = await this.$validator.validateAll('newUser')
  151. if (!validationSuccess) {
  152. return
  153. }
  154. try {
  155. const resp = await this.$apollo.mutate({
  156. mutation: createUserMutation,
  157. variables: {
  158. providerKey: this.provider,
  159. email: this.email,
  160. passwordRaw: this.password,
  161. name: this.name,
  162. groups: this.group,
  163. mustChangePassword: this.mustChangePwd,
  164. sendWelcomeEmail: this.sendWelcomeEmail
  165. },
  166. watchLoading (isLoading) {
  167. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-create')
  168. }
  169. })
  170. if (_.get(resp, 'data.users.create.responseResult.succeeded', false)) {
  171. this.$store.commit('showNotification', {
  172. style: 'success',
  173. message: 'New user created successfully.',
  174. icon: 'check'
  175. })
  176. this.email = ''
  177. this.password = ''
  178. this.name = ''
  179. if (close) {
  180. this.isShown = false
  181. this.$emit('refresh')
  182. } else {
  183. this.$refs.emailInput.focus()
  184. }
  185. } else {
  186. this.$store.commit('showNotification', {
  187. style: 'red',
  188. message: _.get(resp, 'data.users.create.responseResult.message', 'An unexpected error occured.'),
  189. icon: 'warning'
  190. })
  191. }
  192. } catch (err) {
  193. this.$store.commit('pushGraphError', err)
  194. }
  195. },
  196. generatePwd() {
  197. const pwdChars = 'abcdefghkmnpqrstuvwxyzABCDEFHJKLMNPQRSTUVWXYZ23456789_*=?#!()+'
  198. this.password = _.sampleSize(pwdChars, 12).join('')
  199. }
  200. },
  201. apollo: {
  202. providers: {
  203. query: providersQuery,
  204. fetchPolicy: 'network-only',
  205. update: (data) => data.authentication.strategies,
  206. watchLoading (isLoading) {
  207. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-strategies-refresh')
  208. }
  209. },
  210. groups: {
  211. query: groupsQuery,
  212. fetchPolicy: 'network-only',
  213. update: (data) => data.groups.list,
  214. watchLoading (isLoading) {
  215. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
  216. }
  217. }
  218. }
  219. }
  220. </script>