admin-users-create.vue 6.5 KB

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