blob: 0fec89445cd44fbbe0874fa18aa3ba3146f8af17 [file] [log] [blame]
Yoshie Muranaka463a5702019-12-04 09:09:36 -08001<template>
Yoshie Muranaka0b8cffd2020-06-23 08:44:54 -07002 <b-modal id="modal-user" ref="modal" @hidden="resetForm">
Yoshie Muranaka463a5702019-12-04 09:09:36 -08003 <template v-slot:modal-title>
4 <template v-if="newUser">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -08005 {{ $t('pageLocalUserManagement.addUser') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -08006 </template>
7 <template v-else>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -08008 {{ $t('pageLocalUserManagement.editUser') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -08009 </template>
10 </template>
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -070011 <b-form id="form-user" novalidate @submit.prevent="handleSubmit">
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080012 <b-container>
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070013 <!-- Manual unlock form control -->
14 <b-row v-if="!newUser && manualUnlockPolicy && user.Locked">
15 <b-col sm="9">
16 <alert :show="true" variant="warning" small>
17 <template v-if="!$v.form.manualUnlock.$dirty">
18 {{ $t('pageLocalUserManagement.modal.accountLocked') }}
19 </template>
20 <template v-else>
21 {{
22 $t('pageLocalUserManagement.modal.clickSaveToUnlockAccount')
23 }}
24 </template>
25 </alert>
26 </b-col>
27 <b-col sm="3">
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050028 <input
29 v-model="form.manualUnlock"
30 data-test-id="localUserManagement-input-manualUnlock"
31 type="hidden"
32 value="false"
33 />
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070034 <b-button
35 variant="primary"
36 :disabled="$v.form.manualUnlock.$dirty"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050037 data-test-id="localUserManagement-button-manualUnlock"
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070038 @click="$v.form.manualUnlock.$touch()"
39 >
40 {{ $t('pageLocalUserManagement.modal.unlock') }}
41 </b-button>
42 </b-col>
43 </b-row>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080044 <b-row>
45 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080046 <b-form-group
47 :label="$t('pageLocalUserManagement.modal.accountStatus')"
48 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080049 <b-form-radio
50 v-model="form.status"
51 name="user-status"
52 :value="true"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050053 data-test-id="localUserManagement-radioButton-statusEnabled"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080054 @input="$v.form.status.$touch()"
55 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080056 {{ $t('global.status.enabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080057 </b-form-radio>
58 <b-form-radio
59 v-model="form.status"
60 name="user-status"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050061 data-test-id="localUserManagement-radioButton-statusDisabled"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080062 :value="false"
63 @input="$v.form.status.$touch()"
64 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080065 {{ $t('global.status.disabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080066 </b-form-radio>
67 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080068 <b-form-group
69 :label="$t('pageLocalUserManagement.modal.username')"
70 label-for="username"
71 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080072 <b-form-text id="username-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080073 {{ $t('pageLocalUserManagement.modal.cannotStartWithANumber') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080074 <br />
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080075 {{
76 $t(
77 'pageLocalUserManagement.modal.noSpecialCharactersExceptUnderscore'
78 )
79 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080080 </b-form-text>
81 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -060082 id="username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080083 v-model="form.username"
84 type="text"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080085 aria-describedby="username-help-block"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050086 data-test-id="localUserManagement-input-username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080087 :state="getValidationState($v.form.username)"
88 :disabled="!newUser && originalUsername === 'root'"
Yoshie Muranaka52b02232020-02-20 08:00:45 -080089 @input="$v.form.username.$touch()"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080090 />
91 <b-form-invalid-feedback role="alert">
92 <template v-if="!$v.form.username.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080093 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080094 </template>
95 <template v-else-if="!$v.form.username.maxLength">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080096 {{
97 $t('global.form.lengthMustBeBetween', { min: 1, max: 16 })
98 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080099 </template>
100 <template v-else-if="!$v.form.username.pattern">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800101 {{ $t('global.form.invalidFormat') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800102 </template>
103 </b-form-invalid-feedback>
104 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800105 <b-form-group
106 :label="$t('pageLocalUserManagement.modal.privilege')"
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -0800107 label-for="privilege"
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800108 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800109 <b-form-select
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -0800110 id="privilege"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800111 v-model="form.privilege"
112 :options="privilegeTypes"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500113 data-test-id="localUserManagement-select-privilege"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800114 :state="getValidationState($v.form.privilege)"
115 @input="$v.form.privilege.$touch()"
116 >
117 </b-form-select>
118 <b-form-invalid-feedback role="alert">
119 <template v-if="!$v.form.privilege.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800120 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800121 </template>
122 </b-form-invalid-feedback>
123 </b-form-group>
124 </b-col>
125 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800126 <b-form-group
127 :label="$t('pageLocalUserManagement.modal.userPassword')"
128 label-for="password"
129 >
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800130 <b-form-text id="password-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800131 {{
132 $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
133 min: passwordRequirements.minLength,
134 max: passwordRequirements.maxLength
135 })
136 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800137 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800138 <input-password-toggle>
139 <b-form-input
140 id="password"
141 v-model="form.password"
142 type="password"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500143 data-test-id="localUserManagement-input-password"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800144 aria-describedby="password-help-block"
145 :state="getValidationState($v.form.password)"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -0500146 class="form-control-with-button"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800147 @input="$v.form.password.$touch()"
148 />
149 <b-form-invalid-feedback role="alert">
150 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800151 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800152 </template>
153 <template
154 v-if="
155 !$v.form.password.minLength || !$v.form.password.maxLength
156 "
157 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800158 {{
159 $t(
160 'pageLocalUserManagement.modal.passwordMustBeBetween',
161 {
162 min: passwordRequirements.minLength,
163 max: passwordRequirements.maxLength
164 }
165 )
166 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800167 </template>
168 </b-form-invalid-feedback>
169 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800170 </b-form-group>
171 <b-form-group
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800172 :label="$t('pageLocalUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800173 label-for="password-confirmation"
174 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800175 <input-password-toggle>
176 <b-form-input
177 id="password-confirmation"
178 v-model="form.passwordConfirmation"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500179 data-test-id="localUserManagement-input-passwordConfirmation"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800180 type="password"
181 :state="getValidationState($v.form.passwordConfirmation)"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -0500182 class="form-control-with-button"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800183 @input="$v.form.passwordConfirmation.$touch()"
184 />
185 <b-form-invalid-feedback role="alert">
186 <template v-if="!$v.form.passwordConfirmation.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800187 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800188 </template>
189 <template
190 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
191 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800192 {{
193 $t('pageLocalUserManagement.modal.passwordsDoNotMatch')
194 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800195 </template>
196 </b-form-invalid-feedback>
197 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800198 </b-form-group>
199 </b-col>
200 </b-row>
201 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800202 </b-form>
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700203 <template v-slot:modal-footer="{ ok, cancel }">
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500204 <b-button
205 variant="secondary"
206 data-test-id="localUserManagement-button-cancel"
207 @click="cancel()"
208 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700209 {{ $t('global.action.cancel') }}
210 </b-button>
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500211 <b-button
212 form="form-user"
213 data-test-id="localUserManagement-button-submit"
214 type="submit"
215 variant="primary"
216 @click="onOk"
217 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700218 <template v-if="newUser">
219 {{ $t('pageLocalUserManagement.addUser') }}
220 </template>
221 <template v-else>
222 {{ $t('global.action.save') }}
223 </template>
224 </b-button>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800225 </template>
226 </b-modal>
227</template>
228
229<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800230import {
231 required,
232 maxLength,
233 minLength,
234 sameAs,
235 helpers,
236 requiredIf
237} from 'vuelidate/lib/validators';
SurenNeware61859092020-10-01 09:37:32 +0530238import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
239import InputPasswordToggle from '@/components/Global/InputPasswordToggle';
240import Alert from '@/components/Global/Alert';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800241
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800242export default {
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700243 components: { Alert, InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800244 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600245 props: {
246 user: {
247 type: Object,
248 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800249 },
250 passwordRequirements: {
251 type: Object,
252 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600253 }
254 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800255 data() {
256 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800257 originalUsername: '',
258 form: {
259 status: true,
260 username: '',
261 privilege: '',
262 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700263 passwordConfirmation: '',
264 manualUnlock: false
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800265 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800266 };
267 },
268 computed: {
269 newUser() {
270 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700271 },
272 accountSettings() {
273 return this.$store.getters['localUsers/accountSettings'];
274 },
275 manualUnlockPolicy() {
276 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700277 },
278 privilegeTypes() {
279 return this.$store.getters['localUsers/accountRoles'];
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800280 }
281 },
282 watch: {
283 user: function(value) {
284 if (value === null) return;
285 this.originalUsername = value.username;
286 this.form.username = value.username;
287 this.form.status = value.Enabled;
288 this.form.privilege = value.privilege;
289 }
290 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800291 validations() {
292 return {
293 form: {
294 status: {
295 required
296 },
297 username: {
298 required,
299 maxLength: maxLength(16),
300 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
301 },
302 privilege: {
303 required
304 },
305 password: {
306 required: requiredIf(function() {
307 return this.requirePassword();
308 }),
309 minLength: minLength(this.passwordRequirements.minLength),
310 maxLength: maxLength(this.passwordRequirements.maxLength)
311 },
312 passwordConfirmation: {
313 required: requiredIf(function() {
314 return this.requirePassword();
315 }),
316 sameAsPassword: sameAs('password')
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700317 },
318 manualUnlock: {}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800319 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800320 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800321 },
322 methods: {
323 handleSubmit() {
324 let userData = {};
325
326 if (this.newUser) {
327 this.$v.$touch();
328 if (this.$v.$invalid) return;
329 userData.username = this.form.username;
330 userData.status = this.form.status;
331 userData.privilege = this.form.privilege;
332 userData.password = this.form.password;
333 } else {
334 if (this.$v.$invalid) return;
335 userData.originalUsername = this.originalUsername;
336 if (this.$v.form.status.$dirty) {
337 userData.status = this.form.status;
338 }
339 if (this.$v.form.username.$dirty) {
340 userData.username = this.form.username;
341 }
342 if (this.$v.form.privilege.$dirty) {
343 userData.privilege = this.form.privilege;
344 }
345 if (this.$v.form.password.$dirty) {
346 userData.password = this.form.password;
347 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700348 if (this.$v.form.manualUnlock.$dirty) {
349 // If form manualUnlock control $dirty then
350 // set user Locked property to false
351 userData.locked = false;
352 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800353 if (Object.entries(userData).length === 1) {
354 this.closeModal();
355 return;
356 }
357 }
358
359 this.$emit('ok', { isNewUser: this.newUser, userData });
360 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800361 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800362 closeModal() {
363 this.$nextTick(() => {
364 this.$refs.modal.hide();
365 });
366 },
367 resetForm() {
368 this.form.originalUsername = '';
369 this.form.status = true;
370 this.form.username = '';
371 this.form.privilege = '';
372 this.form.password = '';
373 this.form.passwordConfirmation = '';
374 this.$v.$reset();
Yoshie Muranaka791622b2020-04-17 13:55:16 -0700375 this.$emit('hidden');
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800376 },
377 requirePassword() {
378 if (this.newUser) return true;
379 if (this.$v.form.password.$dirty) return true;
380 if (this.$v.form.passwordConfirmation.$dirty) return true;
381 return false;
382 },
383 onOk(bvModalEvt) {
384 // prevent modal close
385 bvModalEvt.preventDefault();
386 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800387 }
388 }
389};
390</script>