admin-users-create.vue 6.5 KB

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