admin-users-create.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. key='newUserEmail'
  27. persistent-hint
  28. ref='emailInput'
  29. )
  30. v-text-field(
  31. v-if='provider === `local`'
  32. outlined
  33. prepend-icon='mdi-lock-outline'
  34. append-icon='mdi-dice-5'
  35. v-model='password'
  36. :label='mustChangePwd ? `Temporary Password` : `Password`'
  37. counter='255'
  38. @click:append='generatePwd'
  39. key='newUserPassword'
  40. persistent-hint
  41. )
  42. v-text-field(
  43. outlined
  44. prepend-icon='mdi-account-outline'
  45. v-model='name'
  46. label='Name'
  47. :hint='provider === `local` ? `Can be changed by the user.` : `May be overwritten by the provider during login.`'
  48. key='newUserName'
  49. persistent-hint
  50. )
  51. v-select.mt-2(
  52. :items='groups'
  53. item-text='name'
  54. item-value='id'
  55. item-disabled='isSystem'
  56. outlined
  57. prepend-icon='mdi-account-group'
  58. v-model='group'
  59. label='Assign to Group(s)...'
  60. dense
  61. clearable
  62. multiple
  63. )
  64. v-divider
  65. v-checkbox(
  66. color='primary'
  67. label='Require password change on first login'
  68. v-if='provider === `local`'
  69. v-model='mustChangePwd'
  70. hide-details
  71. )
  72. v-checkbox(
  73. color='primary'
  74. label='Send a welcome email'
  75. hide-details
  76. v-model='sendWelcomeEmail'
  77. disabled
  78. )
  79. v-card-chin
  80. v-spacer
  81. v-btn(text, @click='isShown = false') Cancel
  82. v-btn.px-3(depressed, color='primary', @click='newUser(false)')
  83. v-icon(left) mdi-chevron-right
  84. span Create
  85. v-btn.px-3(depressed, color='primary', @click='newUser(true)')
  86. v-icon(left) mdi-chevron-double-right
  87. span Create and Close
  88. </template>
  89. <script>
  90. import _ from 'lodash'
  91. import validate from 'validate.js'
  92. import createUserMutation from 'gql/admin/users/users-mutation-create.gql'
  93. import providersQuery from 'gql/admin/users/users-query-strategies.gql'
  94. import groupsQuery from 'gql/admin/users/users-query-groups.gql'
  95. export default {
  96. props: {
  97. value: {
  98. type: Boolean,
  99. default: false
  100. }
  101. },
  102. data() {
  103. return {
  104. providers: [],
  105. provider: 'local',
  106. email: '',
  107. password: '',
  108. name: '',
  109. groups: [],
  110. group: [],
  111. mustChangePwd: false,
  112. sendWelcomeEmail: false
  113. }
  114. },
  115. computed: {
  116. isShown: {
  117. get() { return this.value },
  118. set(val) { this.$emit('input', val) }
  119. }
  120. },
  121. watch: {
  122. value(newValue, oldValue) {
  123. if (newValue) {
  124. this.$nextTick(() => {
  125. this.$refs.emailInput.focus()
  126. })
  127. }
  128. }
  129. },
  130. methods: {
  131. async newUser(close = false) {
  132. let rules = {
  133. email: {
  134. presence: {
  135. allowEmpty: false
  136. },
  137. email: true
  138. },
  139. name: {
  140. presence: {
  141. allowEmpty: false
  142. },
  143. length: {
  144. minimum: 2,
  145. maximum: 255
  146. }
  147. }
  148. }
  149. if (this.provider === `local`) {
  150. rules.password = {
  151. presence: {
  152. allowEmpty: false
  153. },
  154. length: {
  155. minimum: 6,
  156. maximum: 255
  157. }
  158. }
  159. }
  160. const validationResults = validate({
  161. email: this.email,
  162. password: this.password,
  163. name: this.name
  164. }, rules, { format: 'flat' })
  165. if (validationResults) {
  166. this.$store.commit('showNotification', {
  167. style: 'red',
  168. message: validationResults[0],
  169. icon: 'alert'
  170. })
  171. return
  172. }
  173. try {
  174. const resp = await this.$apollo.mutate({
  175. mutation: createUserMutation,
  176. variables: {
  177. providerKey: this.provider,
  178. email: this.email,
  179. passwordRaw: this.password,
  180. name: this.name,
  181. groups: this.group,
  182. mustChangePassword: this.mustChangePwd,
  183. sendWelcomeEmail: this.sendWelcomeEmail
  184. },
  185. watchLoading (isLoading) {
  186. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-create')
  187. }
  188. })
  189. if (_.get(resp, 'data.users.create.responseResult.succeeded', false)) {
  190. this.$store.commit('showNotification', {
  191. style: 'success',
  192. message: 'New user created successfully.',
  193. icon: 'check'
  194. })
  195. this.email = ''
  196. this.password = ''
  197. this.name = ''
  198. if (close) {
  199. this.isShown = false
  200. this.$emit('refresh')
  201. } else {
  202. this.$refs.emailInput.focus()
  203. }
  204. } else {
  205. this.$store.commit('showNotification', {
  206. style: 'red',
  207. message: _.get(resp, 'data.users.create.responseResult.message', 'An unexpected error occured.'),
  208. icon: 'alert'
  209. })
  210. }
  211. } catch (err) {
  212. this.$store.commit('pushGraphError', err)
  213. }
  214. },
  215. generatePwd() {
  216. const pwdChars = 'abcdefghkmnpqrstuvwxyzABCDEFHJKLMNPQRSTUVWXYZ23456789_*=?#!()+'
  217. this.password = _.sampleSize(pwdChars, 12).join('')
  218. }
  219. },
  220. apollo: {
  221. providers: {
  222. query: providersQuery,
  223. fetchPolicy: 'network-only',
  224. update: (data) => data.authentication.strategies,
  225. watchLoading (isLoading) {
  226. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-strategies-refresh')
  227. }
  228. },
  229. groups: {
  230. query: groupsQuery,
  231. fetchPolicy: 'network-only',
  232. update: (data) => data.groups.list,
  233. watchLoading (isLoading) {
  234. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
  235. }
  236. }
  237. }
  238. }
  239. </script>