Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 1 | <template> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 2 | <b-modal id="modal-user" ref="modal" @ok="onOk" @hidden="resetForm"> |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 3 | <template v-slot:modal-title> |
| 4 | <template v-if="newUser"> |
| 5 | Add user |
| 6 | </template> |
| 7 | <template v-else> |
| 8 | Edit user |
| 9 | </template> |
| 10 | </template> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 11 | <b-form novalidate @submit="handleSubmit"> |
| 12 | <b-container> |
| 13 | <b-row> |
| 14 | <b-col> |
| 15 | <b-form-group label="Account status"> |
| 16 | <b-form-radio |
| 17 | v-model="form.status" |
| 18 | name="user-status" |
| 19 | :value="true" |
| 20 | @input="$v.form.status.$touch()" |
| 21 | > |
| 22 | Enabled |
| 23 | </b-form-radio> |
| 24 | <b-form-radio |
| 25 | v-model="form.status" |
| 26 | name="user-status" |
| 27 | :value="false" |
| 28 | @input="$v.form.status.$touch()" |
| 29 | > |
| 30 | Disabled |
| 31 | </b-form-radio> |
| 32 | </b-form-group> |
| 33 | <b-form-group label="Username" label-for="username"> |
| 34 | <b-form-text id="username-help-block"> |
| 35 | Cannot start with a number |
| 36 | <br /> |
| 37 | No special characters except underscore |
| 38 | </b-form-text> |
| 39 | <b-form-input |
Derick Montague | 09e45cd | 2020-01-23 15:45:57 -0600 | [diff] [blame] | 40 | id="username" |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 41 | v-model="form.username" |
| 42 | type="text" |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 43 | aria-describedby="username-help-block" |
| 44 | :state="getValidationState($v.form.username)" |
| 45 | :disabled="!newUser && originalUsername === 'root'" |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 46 | @input="$v.form.username.$touch()" |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 47 | /> |
| 48 | <b-form-invalid-feedback role="alert"> |
| 49 | <template v-if="!$v.form.username.required"> |
| 50 | Field required |
| 51 | </template> |
| 52 | <template v-else-if="!$v.form.username.maxLength"> |
| 53 | Length must be between 1 – 16 characters |
| 54 | </template> |
| 55 | <template v-else-if="!$v.form.username.pattern"> |
| 56 | Invalid format |
| 57 | </template> |
| 58 | </b-form-invalid-feedback> |
| 59 | </b-form-group> |
| 60 | <b-form-group label="Privilege"> |
| 61 | <b-form-select |
| 62 | v-model="form.privilege" |
| 63 | :options="privilegeTypes" |
| 64 | :state="getValidationState($v.form.privilege)" |
| 65 | @input="$v.form.privilege.$touch()" |
| 66 | > |
| 67 | </b-form-select> |
| 68 | <b-form-invalid-feedback role="alert"> |
| 69 | <template v-if="!$v.form.privilege.required"> |
| 70 | Field required |
| 71 | </template> |
| 72 | </b-form-invalid-feedback> |
| 73 | </b-form-group> |
| 74 | </b-col> |
| 75 | <b-col> |
| 76 | <b-form-group label="User password" label-for="password"> |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 77 | <b-form-text id="password-help-block"> |
| 78 | Password must between |
| 79 | <span class="text-nowrap"> |
| 80 | {{ passwordRequirements.minLength }} |
| 81 | – {{ passwordRequirements.maxLength }} |
| 82 | </span> |
| 83 | characters |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 84 | </b-form-text> |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 85 | <input-password-toggle> |
| 86 | <b-form-input |
| 87 | id="password" |
| 88 | v-model="form.password" |
| 89 | type="password" |
| 90 | aria-describedby="password-help-block" |
| 91 | :state="getValidationState($v.form.password)" |
| 92 | @input="$v.form.password.$touch()" |
| 93 | /> |
| 94 | <b-form-invalid-feedback role="alert"> |
| 95 | <template v-if="!$v.form.password.required"> |
| 96 | Field required |
| 97 | </template> |
| 98 | <template |
| 99 | v-if=" |
| 100 | !$v.form.password.minLength || !$v.form.password.maxLength |
| 101 | " |
| 102 | > |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 103 | Length must be between |
| 104 | <span class="text-nowrap"> |
| 105 | {{ passwordRequirements.minLength }} |
| 106 | – {{ passwordRequirements.maxLength }} |
| 107 | </span> |
| 108 | characters |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 109 | </template> |
| 110 | </b-form-invalid-feedback> |
| 111 | </input-password-toggle> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 112 | </b-form-group> |
| 113 | <b-form-group |
| 114 | label="Confirm user password" |
| 115 | label-for="password-confirmation" |
| 116 | > |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 117 | <input-password-toggle> |
| 118 | <b-form-input |
| 119 | id="password-confirmation" |
| 120 | v-model="form.passwordConfirmation" |
| 121 | type="password" |
| 122 | :state="getValidationState($v.form.passwordConfirmation)" |
| 123 | @input="$v.form.passwordConfirmation.$touch()" |
| 124 | /> |
| 125 | <b-form-invalid-feedback role="alert"> |
| 126 | <template v-if="!$v.form.passwordConfirmation.required"> |
| 127 | Field required |
| 128 | </template> |
| 129 | <template |
| 130 | v-else-if="!$v.form.passwordConfirmation.sameAsPassword" |
| 131 | > |
| 132 | Passwords do not match |
| 133 | </template> |
| 134 | </b-form-invalid-feedback> |
| 135 | </input-password-toggle> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 136 | </b-form-group> |
| 137 | </b-col> |
| 138 | </b-row> |
| 139 | </b-container> |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 140 | </b-form> |
| 141 | <template v-slot:modal-ok> |
| 142 | <template v-if="newUser"> |
| 143 | Add user |
| 144 | </template> |
| 145 | <template v-else> |
| 146 | Save |
| 147 | </template> |
| 148 | </template> |
| 149 | </b-modal> |
| 150 | </template> |
| 151 | |
| 152 | <script> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 153 | import { |
| 154 | required, |
| 155 | maxLength, |
| 156 | minLength, |
| 157 | sameAs, |
| 158 | helpers, |
| 159 | requiredIf |
| 160 | } from 'vuelidate/lib/validators'; |
| 161 | import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js'; |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 162 | import InputPasswordToggle from '../../../components/Global/InputPasswordToggle'; |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 163 | |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 164 | export default { |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 165 | components: { InputPasswordToggle }, |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 166 | mixins: [VuelidateMixin], |
Derick Montague | 09e45cd | 2020-01-23 15:45:57 -0600 | [diff] [blame] | 167 | props: { |
| 168 | user: { |
| 169 | type: Object, |
| 170 | default: null |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 171 | }, |
| 172 | passwordRequirements: { |
| 173 | type: Object, |
| 174 | required: true |
Derick Montague | 09e45cd | 2020-01-23 15:45:57 -0600 | [diff] [blame] | 175 | } |
| 176 | }, |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 177 | data() { |
| 178 | return { |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 179 | privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'], |
| 180 | originalUsername: '', |
| 181 | form: { |
| 182 | status: true, |
| 183 | username: '', |
| 184 | privilege: '', |
| 185 | password: '', |
| 186 | passwordConfirmation: '' |
| 187 | } |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 188 | }; |
| 189 | }, |
| 190 | computed: { |
| 191 | newUser() { |
| 192 | return this.user ? false : true; |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 193 | } |
| 194 | }, |
| 195 | watch: { |
| 196 | user: function(value) { |
| 197 | if (value === null) return; |
| 198 | this.originalUsername = value.username; |
| 199 | this.form.username = value.username; |
| 200 | this.form.status = value.Enabled; |
| 201 | this.form.privilege = value.privilege; |
| 202 | } |
| 203 | }, |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 204 | validations() { |
| 205 | return { |
| 206 | form: { |
| 207 | status: { |
| 208 | required |
| 209 | }, |
| 210 | username: { |
| 211 | required, |
| 212 | maxLength: maxLength(16), |
| 213 | pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/) |
| 214 | }, |
| 215 | privilege: { |
| 216 | required |
| 217 | }, |
| 218 | password: { |
| 219 | required: requiredIf(function() { |
| 220 | return this.requirePassword(); |
| 221 | }), |
| 222 | minLength: minLength(this.passwordRequirements.minLength), |
| 223 | maxLength: maxLength(this.passwordRequirements.maxLength) |
| 224 | }, |
| 225 | passwordConfirmation: { |
| 226 | required: requiredIf(function() { |
| 227 | return this.requirePassword(); |
| 228 | }), |
| 229 | sameAsPassword: sameAs('password') |
| 230 | } |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 231 | } |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 232 | }; |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 233 | }, |
| 234 | methods: { |
| 235 | handleSubmit() { |
| 236 | let userData = {}; |
| 237 | |
| 238 | if (this.newUser) { |
| 239 | this.$v.$touch(); |
| 240 | if (this.$v.$invalid) return; |
| 241 | userData.username = this.form.username; |
| 242 | userData.status = this.form.status; |
| 243 | userData.privilege = this.form.privilege; |
| 244 | userData.password = this.form.password; |
| 245 | } else { |
| 246 | if (this.$v.$invalid) return; |
| 247 | userData.originalUsername = this.originalUsername; |
| 248 | if (this.$v.form.status.$dirty) { |
| 249 | userData.status = this.form.status; |
| 250 | } |
| 251 | if (this.$v.form.username.$dirty) { |
| 252 | userData.username = this.form.username; |
| 253 | } |
| 254 | if (this.$v.form.privilege.$dirty) { |
| 255 | userData.privilege = this.form.privilege; |
| 256 | } |
| 257 | if (this.$v.form.password.$dirty) { |
| 258 | userData.password = this.form.password; |
| 259 | } |
| 260 | if (Object.entries(userData).length === 1) { |
| 261 | this.closeModal(); |
| 262 | return; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | this.$emit('ok', { isNewUser: this.newUser, userData }); |
| 267 | this.closeModal(); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 268 | }, |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 269 | closeModal() { |
| 270 | this.$nextTick(() => { |
| 271 | this.$refs.modal.hide(); |
| 272 | }); |
| 273 | }, |
| 274 | resetForm() { |
| 275 | this.form.originalUsername = ''; |
| 276 | this.form.status = true; |
| 277 | this.form.username = ''; |
| 278 | this.form.privilege = ''; |
| 279 | this.form.password = ''; |
| 280 | this.form.passwordConfirmation = ''; |
| 281 | this.$v.$reset(); |
| 282 | }, |
| 283 | requirePassword() { |
| 284 | if (this.newUser) return true; |
| 285 | if (this.$v.form.password.$dirty) return true; |
| 286 | if (this.$v.form.passwordConfirmation.$dirty) return true; |
| 287 | return false; |
| 288 | }, |
| 289 | onOk(bvModalEvt) { |
| 290 | // prevent modal close |
| 291 | bvModalEvt.preventDefault(); |
| 292 | this.handleSubmit(); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | }; |
| 296 | </script> |