blob: 4a6a0bcfb14f814ef3145cecb73ecd00007b61db [file] [log] [blame]
Yoshie Muranaka463a5702019-12-04 09:09:36 -08001<template>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -08002 <b-modal id="modal-user" ref="modal" @ok="onOk" @hidden="resetForm">
Yoshie Muranaka463a5702019-12-04 09:09:36 -08003 <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 Muranaka4b0fc1d2020-01-06 07:36:16 -080011 <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 Montague09e45cd2020-01-23 15:45:57 -060040 id="username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080041 v-model="form.username"
42 type="text"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080043 aria-describedby="username-help-block"
44 :state="getValidationState($v.form.username)"
45 :disabled="!newUser && originalUsername === 'root'"
Yoshie Muranaka52b02232020-02-20 08:00:45 -080046 @input="$v.form.username.$touch()"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080047 />
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 Muranaka52b02232020-02-20 08:00:45 -080077 <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 Muranaka4b0fc1d2020-01-06 07:36:16 -080084 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080085 <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 Muranaka52b02232020-02-20 08:00:45 -0800103 Length must be between
104 <span class="text-nowrap">
105 {{ passwordRequirements.minLength }}
106 – {{ passwordRequirements.maxLength }}
107 </span>
108 characters
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800109 </template>
110 </b-form-invalid-feedback>
111 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800112 </b-form-group>
113 <b-form-group
114 label="Confirm user password"
115 label-for="password-confirmation"
116 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800117 <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 Muranaka4b0fc1d2020-01-06 07:36:16 -0800136 </b-form-group>
137 </b-col>
138 </b-row>
139 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800140 </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 Muranaka4b0fc1d2020-01-06 07:36:16 -0800153import {
154 required,
155 maxLength,
156 minLength,
157 sameAs,
158 helpers,
159 requiredIf
160} from 'vuelidate/lib/validators';
161import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800162import InputPasswordToggle from '../../../components/Global/InputPasswordToggle';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800163
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800164export default {
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800165 components: { InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800166 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600167 props: {
168 user: {
169 type: Object,
170 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800171 },
172 passwordRequirements: {
173 type: Object,
174 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600175 }
176 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800177 data() {
178 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800179 privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'],
180 originalUsername: '',
181 form: {
182 status: true,
183 username: '',
184 privilege: '',
185 password: '',
186 passwordConfirmation: ''
187 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800188 };
189 },
190 computed: {
191 newUser() {
192 return this.user ? false : true;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800193 }
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 Muranaka52b02232020-02-20 08:00:45 -0800204 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 Muranaka4b0fc1d2020-01-06 07:36:16 -0800231 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800232 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800233 },
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 Muranaka463a5702019-12-04 09:09:36 -0800268 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800269 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 Muranaka463a5702019-12-04 09:09:36 -0800293 }
294 }
295};
296</script>