admin-users-create.vue 6.73 KB
Newer Older
1
<template lang="pug">
Nick's avatar
Nick committed
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 8 9 10
        v-spacer
        v-btn.mx-0(color='white', outlined, disabled, dark)
          v-icon(left) mdi-database-import
          span Bulk Import
11
      v-card-text.pt-5
Nick's avatar
Nick committed
12
        v-select(
13
          :items='providers'
14
          item-text='displayName'
15
          item-value='key'
16 17
          outlined
          prepend-icon='mdi-domain'
18 19 20
          v-model='provider'
          label='Provider'
          )
Nick's avatar
Nick committed
21
        v-text-field(
22 23
          outlined
          prepend-icon='mdi-at'
24 25
          v-model='email'
          label='Email Address'
26 27
          key='newUserEmail'
          persistent-hint
28 29
          ref='emailInput'
          )
Nick's avatar
Nick committed
30
        v-text-field(
31
          v-if='provider === `local`'
32 33 34
          outlined
          prepend-icon='mdi-lock-outline'
          append-icon='mdi-dice-5'
35
          v-model='password'
Nick's avatar
Nick committed
36
          :label='mustChangePwd ? `Temporary Password` : `Password`'
37 38
          counter='255'
          @click:append='generatePwd'
39 40
          key='newUserPassword'
          persistent-hint
41
          )
Nick's avatar
Nick committed
42
        v-text-field(
43 44
          outlined
          prepend-icon='mdi-account-outline'
45 46
          v-model='name'
          label='Name'
47
          :hint='provider === `local` ? `Can be changed by the user.` : `May be overwritten by the provider during login.`'
48 49
          key='newUserName'
          persistent-hint
50
          )
51
        v-select.mt-2(
Nick's avatar
Nick committed
52 53
          :items='groups'
          item-text='name'
54
          item-value='id'
Nick's avatar
Nick committed
55
          item-disabled='isSystem'
56 57
          outlined
          prepend-icon='mdi-account-group'
Nick's avatar
Nick committed
58 59
          v-model='group'
          label='Assign to Group(s)...'
NGPixel's avatar
NGPixel committed
60 61
          hint='Note that you cannot assign users to the Administrators or Guests groups from this dialog.'
          persistent-hint
Nick's avatar
Nick committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
          clearable
          multiple
          )
        v-divider
        v-checkbox(
          color='primary'
          label='Require password change on first login'
          v-if='provider === `local`'
          v-model='mustChangePwd'
          hide-details
        )
        v-checkbox(
          color='primary'
          label='Send a welcome email'
          hide-details
          v-model='sendWelcomeEmail'
78
          disabled
Nick's avatar
Nick committed
79
        )
80 81
      v-card-chin
        v-spacer
82
        v-btn(text, @click='isShown = false') Cancel
83
        v-btn.px-3(depressed, color='primary', @click='newUser(false)')
84 85
          v-icon(left) mdi-chevron-right
          span Create
86
        v-btn.px-3(depressed, color='primary', @click='newUser(true)')
87 88
          v-icon(left) mdi-chevron-double-right
          span Create and Close
89 90 91
</template>

<script>
92
import _ from 'lodash'
93
import validate from 'validate.js'
94
import gql from 'graphql-tag'
95

96
import createUserMutation from 'gql/admin/users/users-mutation-create.gql'
Nick's avatar
Nick committed
97
import groupsQuery from 'gql/admin/users/users-query-groups.gql'
98

99 100 101 102 103 104 105 106 107
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
108 109
      providers: [],
      provider: 'local',
110 111
      email: '',
      password: '',
Nick's avatar
Nick committed
112 113
      name: '',
      groups: [],
114
      group: [],
Nick's avatar
Nick committed
115 116
      mustChangePwd: false,
      sendWelcomeEmail: false
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }
  },
  computed: {
    isShown: {
      get() { return this.value },
      set(val) { this.$emit('input', val) }
    }
  },
  watch: {
    value(newValue, oldValue) {
      if (newValue) {
        this.$nextTick(() => {
          this.$refs.emailInput.focus()
        })
      }
    }
  },
  methods: {
135
    async newUser(close = false) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      let rules = {
        email: {
          presence: {
            allowEmpty: false
          },
          email: true
        },
        name: {
          presence: {
            allowEmpty: false
          },
          length: {
            minimum: 2,
            maximum: 255
          }
        }
      }
      if (this.provider === `local`) {
        rules.password = {
          presence: {
            allowEmpty: false
          },
          length: {
            minimum: 6,
            maximum: 255
          }
        }
      }
      const validationResults = validate({
        email: this.email,
        password: this.password,
        name: this.name
      }, rules, { format: 'flat' })

      if (validationResults) {
        this.$store.commit('showNotification', {
          style: 'red',
          message: validationResults[0],
          icon: 'alert'
        })
176 177 178 179 180 181 182 183 184 185 186
        return
      }

      try {
        const resp = await this.$apollo.mutate({
          mutation: createUserMutation,
          variables: {
            providerKey: this.provider,
            email: this.email,
            passwordRaw: this.password,
            name: this.name,
187
            groups: this.group,
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
            mustChangePassword: this.mustChangePwd,
            sendWelcomeEmail: this.sendWelcomeEmail
          },
          watchLoading (isLoading) {
            this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-create')
          }
        })
        if (_.get(resp, 'data.users.create.responseResult.succeeded', false)) {
          this.$store.commit('showNotification', {
            style: 'success',
            message: 'New user created successfully.',
            icon: 'check'
          })

          this.email = ''
          this.password = ''
          this.name = ''

          if (close) {
            this.isShown = false
208
            this.$emit('refresh')
209 210 211 212 213 214
          } else {
            this.$refs.emailInput.focus()
          }
        } else {
          this.$store.commit('showNotification', {
            style: 'red',
215
            message: _.get(resp, 'data.users.create.responseResult.message', 'An unexpected error occurred.'),
216
            icon: 'alert'
217 218 219 220 221
          })
        }
      } catch (err) {
        this.$store.commit('pushGraphError', err)
      }
222 223
    },
    generatePwd() {
224 225
      const pwdChars = 'abcdefghkmnpqrstuvwxyzABCDEFHJKLMNPQRSTUVWXYZ23456789_*=?#!()+'
      this.password = _.sampleSize(pwdChars, 12).join('')
226
    }
227 228 229
  },
  apollo: {
    providers: {
230 231 232
      query: gql`
        query {
          authentication {
233
            activeStrategies {
234
              key
235
              displayName
236 237 238 239
            }
          }
        }
      `,
240
      fetchPolicy: 'network-only',
241
      update: (data) => data.authentication.activeStrategies,
242 243 244
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-users-strategies-refresh')
      }
Nick's avatar
Nick committed
245 246 247 248 249 250 251 252
    },
    groups: {
      query: groupsQuery,
      fetchPolicy: 'network-only',
      update: (data) => data.groups.list,
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
      }
253
    }
254 255 256
  }
}
</script>