blob: 75961e34866608f8c3f6233847bdc5b2280e1c89 [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)"
146 @input="$v.form.password.$touch()"
147 />
148 <b-form-invalid-feedback role="alert">
149 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800150 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800151 </template>
152 <template
153 v-if="
154 !$v.form.password.minLength || !$v.form.password.maxLength
155 "
156 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800157 {{
158 $t(
159 'pageLocalUserManagement.modal.passwordMustBeBetween',
160 {
161 min: passwordRequirements.minLength,
162 max: passwordRequirements.maxLength
163 }
164 )
165 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800166 </template>
167 </b-form-invalid-feedback>
168 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800169 </b-form-group>
170 <b-form-group
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800171 :label="$t('pageLocalUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800172 label-for="password-confirmation"
173 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800174 <input-password-toggle>
175 <b-form-input
176 id="password-confirmation"
177 v-model="form.passwordConfirmation"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500178 data-test-id="localUserManagement-input-passwordConfirmation"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800179 type="password"
180 :state="getValidationState($v.form.passwordConfirmation)"
181 @input="$v.form.passwordConfirmation.$touch()"
182 />
183 <b-form-invalid-feedback role="alert">
184 <template v-if="!$v.form.passwordConfirmation.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800185 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800186 </template>
187 <template
188 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
189 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800190 {{
191 $t('pageLocalUserManagement.modal.passwordsDoNotMatch')
192 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800193 </template>
194 </b-form-invalid-feedback>
195 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800196 </b-form-group>
197 </b-col>
198 </b-row>
199 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800200 </b-form>
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700201 <template v-slot:modal-footer="{ ok, cancel }">
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500202 <b-button
203 variant="secondary"
204 data-test-id="localUserManagement-button-cancel"
205 @click="cancel()"
206 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700207 {{ $t('global.action.cancel') }}
208 </b-button>
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500209 <b-button
210 form="form-user"
211 data-test-id="localUserManagement-button-submit"
212 type="submit"
213 variant="primary"
214 @click="onOk"
215 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700216 <template v-if="newUser">
217 {{ $t('pageLocalUserManagement.addUser') }}
218 </template>
219 <template v-else>
220 {{ $t('global.action.save') }}
221 </template>
222 </b-button>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800223 </template>
224 </b-modal>
225</template>
226
227<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800228import {
229 required,
230 maxLength,
231 minLength,
232 sameAs,
233 helpers,
234 requiredIf
235} from 'vuelidate/lib/validators';
236import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800237import InputPasswordToggle from '../../../components/Global/InputPasswordToggle';
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700238import Alert from '../../../components/Global/Alert';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800239
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800240export default {
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700241 components: { Alert, InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800242 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600243 props: {
244 user: {
245 type: Object,
246 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800247 },
248 passwordRequirements: {
249 type: Object,
250 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600251 }
252 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800253 data() {
254 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800255 originalUsername: '',
256 form: {
257 status: true,
258 username: '',
259 privilege: '',
260 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700261 passwordConfirmation: '',
262 manualUnlock: false
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800263 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800264 };
265 },
266 computed: {
267 newUser() {
268 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700269 },
270 accountSettings() {
271 return this.$store.getters['localUsers/accountSettings'];
272 },
273 manualUnlockPolicy() {
274 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700275 },
276 privilegeTypes() {
277 return this.$store.getters['localUsers/accountRoles'];
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800278 }
279 },
280 watch: {
281 user: function(value) {
282 if (value === null) return;
283 this.originalUsername = value.username;
284 this.form.username = value.username;
285 this.form.status = value.Enabled;
286 this.form.privilege = value.privilege;
287 }
288 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800289 validations() {
290 return {
291 form: {
292 status: {
293 required
294 },
295 username: {
296 required,
297 maxLength: maxLength(16),
298 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
299 },
300 privilege: {
301 required
302 },
303 password: {
304 required: requiredIf(function() {
305 return this.requirePassword();
306 }),
307 minLength: minLength(this.passwordRequirements.minLength),
308 maxLength: maxLength(this.passwordRequirements.maxLength)
309 },
310 passwordConfirmation: {
311 required: requiredIf(function() {
312 return this.requirePassword();
313 }),
314 sameAsPassword: sameAs('password')
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700315 },
316 manualUnlock: {}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800317 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800318 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800319 },
320 methods: {
321 handleSubmit() {
322 let userData = {};
323
324 if (this.newUser) {
325 this.$v.$touch();
326 if (this.$v.$invalid) return;
327 userData.username = this.form.username;
328 userData.status = this.form.status;
329 userData.privilege = this.form.privilege;
330 userData.password = this.form.password;
331 } else {
332 if (this.$v.$invalid) return;
333 userData.originalUsername = this.originalUsername;
334 if (this.$v.form.status.$dirty) {
335 userData.status = this.form.status;
336 }
337 if (this.$v.form.username.$dirty) {
338 userData.username = this.form.username;
339 }
340 if (this.$v.form.privilege.$dirty) {
341 userData.privilege = this.form.privilege;
342 }
343 if (this.$v.form.password.$dirty) {
344 userData.password = this.form.password;
345 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700346 if (this.$v.form.manualUnlock.$dirty) {
347 // If form manualUnlock control $dirty then
348 // set user Locked property to false
349 userData.locked = false;
350 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800351 if (Object.entries(userData).length === 1) {
352 this.closeModal();
353 return;
354 }
355 }
356
357 this.$emit('ok', { isNewUser: this.newUser, userData });
358 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800359 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800360 closeModal() {
361 this.$nextTick(() => {
362 this.$refs.modal.hide();
363 });
364 },
365 resetForm() {
366 this.form.originalUsername = '';
367 this.form.status = true;
368 this.form.username = '';
369 this.form.privilege = '';
370 this.form.password = '';
371 this.form.passwordConfirmation = '';
372 this.$v.$reset();
Yoshie Muranaka791622b2020-04-17 13:55:16 -0700373 this.$emit('hidden');
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800374 },
375 requirePassword() {
376 if (this.newUser) return true;
377 if (this.$v.form.password.$dirty) return true;
378 if (this.$v.form.passwordConfirmation.$dirty) return true;
379 return false;
380 },
381 onOk(bvModalEvt) {
382 // prevent modal close
383 bvModalEvt.preventDefault();
384 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800385 }
386 }
387};
388</script>