blob: 202ae8e65043303c3a3e2102af7031f7bb78495b [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">
Derick Montague602e98a2020-10-21 16:20:00 -05003 <template #modal-title>
Yoshie Muranaka463a5702019-12-04 09:09:36 -08004 <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 >
Yoshie Muranakabc0d61b2020-11-02 14:36:27 -0800117 <template #first>
118 <b-form-select-option :value="null" disabled>
119 {{ $t('global.form.selectAnOption') }}
120 </b-form-select-option>
121 </template>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800122 </b-form-select>
123 <b-form-invalid-feedback role="alert">
124 <template v-if="!$v.form.privilege.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800125 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800126 </template>
127 </b-form-invalid-feedback>
128 </b-form-group>
129 </b-col>
130 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800131 <b-form-group
132 :label="$t('pageLocalUserManagement.modal.userPassword')"
133 label-for="password"
134 >
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800135 <b-form-text id="password-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800136 {{
137 $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
138 min: passwordRequirements.minLength,
Derick Montague602e98a2020-10-21 16:20:00 -0500139 max: passwordRequirements.maxLength,
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800140 })
141 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800142 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800143 <input-password-toggle>
144 <b-form-input
145 id="password"
146 v-model="form.password"
147 type="password"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500148 data-test-id="localUserManagement-input-password"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800149 aria-describedby="password-help-block"
150 :state="getValidationState($v.form.password)"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -0500151 class="form-control-with-button"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800152 @input="$v.form.password.$touch()"
153 />
154 <b-form-invalid-feedback role="alert">
155 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800156 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800157 </template>
158 <template
159 v-if="
160 !$v.form.password.minLength || !$v.form.password.maxLength
161 "
162 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800163 {{
164 $t(
165 'pageLocalUserManagement.modal.passwordMustBeBetween',
166 {
167 min: passwordRequirements.minLength,
Derick Montague602e98a2020-10-21 16:20:00 -0500168 max: passwordRequirements.maxLength,
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800169 }
170 )
171 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800172 </template>
173 </b-form-invalid-feedback>
174 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800175 </b-form-group>
176 <b-form-group
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800177 :label="$t('pageLocalUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800178 label-for="password-confirmation"
179 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800180 <input-password-toggle>
181 <b-form-input
182 id="password-confirmation"
183 v-model="form.passwordConfirmation"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500184 data-test-id="localUserManagement-input-passwordConfirmation"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800185 type="password"
186 :state="getValidationState($v.form.passwordConfirmation)"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -0500187 class="form-control-with-button"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800188 @input="$v.form.passwordConfirmation.$touch()"
189 />
190 <b-form-invalid-feedback role="alert">
191 <template v-if="!$v.form.passwordConfirmation.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800192 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800193 </template>
194 <template
195 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
196 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800197 {{
198 $t('pageLocalUserManagement.modal.passwordsDoNotMatch')
199 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800200 </template>
201 </b-form-invalid-feedback>
202 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800203 </b-form-group>
204 </b-col>
205 </b-row>
206 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800207 </b-form>
Sukanya Pandey18375032020-10-30 21:13:06 +0530208 <template #modal-footer="{ cancel }">
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500209 <b-button
210 variant="secondary"
211 data-test-id="localUserManagement-button-cancel"
212 @click="cancel()"
213 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700214 {{ $t('global.action.cancel') }}
215 </b-button>
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500216 <b-button
217 form="form-user"
218 data-test-id="localUserManagement-button-submit"
219 type="submit"
220 variant="primary"
221 @click="onOk"
222 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700223 <template v-if="newUser">
224 {{ $t('pageLocalUserManagement.addUser') }}
225 </template>
226 <template v-else>
227 {{ $t('global.action.save') }}
228 </template>
229 </b-button>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800230 </template>
231 </b-modal>
232</template>
233
234<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800235import {
236 required,
237 maxLength,
238 minLength,
239 sameAs,
240 helpers,
Derick Montague602e98a2020-10-21 16:20:00 -0500241 requiredIf,
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800242} from 'vuelidate/lib/validators';
SurenNeware61859092020-10-01 09:37:32 +0530243import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
244import InputPasswordToggle from '@/components/Global/InputPasswordToggle';
245import Alert from '@/components/Global/Alert';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800246
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800247export default {
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700248 components: { Alert, InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800249 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600250 props: {
251 user: {
252 type: Object,
Derick Montague602e98a2020-10-21 16:20:00 -0500253 default: null,
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800254 },
255 passwordRequirements: {
256 type: Object,
Derick Montague602e98a2020-10-21 16:20:00 -0500257 required: true,
258 },
Derick Montague09e45cd2020-01-23 15:45:57 -0600259 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800260 data() {
261 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800262 originalUsername: '',
263 form: {
264 status: true,
265 username: '',
Yoshie Muranakabc0d61b2020-11-02 14:36:27 -0800266 privilege: null,
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800267 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700268 passwordConfirmation: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500269 manualUnlock: false,
270 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800271 };
272 },
273 computed: {
274 newUser() {
275 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700276 },
277 accountSettings() {
278 return this.$store.getters['localUsers/accountSettings'];
279 },
280 manualUnlockPolicy() {
281 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700282 },
283 privilegeTypes() {
284 return this.$store.getters['localUsers/accountRoles'];
Derick Montague602e98a2020-10-21 16:20:00 -0500285 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800286 },
287 watch: {
Derick Montague602e98a2020-10-21 16:20:00 -0500288 user: function (value) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800289 if (value === null) return;
290 this.originalUsername = value.username;
291 this.form.username = value.username;
292 this.form.status = value.Enabled;
293 this.form.privilege = value.privilege;
Derick Montague602e98a2020-10-21 16:20:00 -0500294 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800295 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800296 validations() {
297 return {
298 form: {
299 status: {
Derick Montague602e98a2020-10-21 16:20:00 -0500300 required,
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800301 },
302 username: {
303 required,
304 maxLength: maxLength(16),
Derick Montague602e98a2020-10-21 16:20:00 -0500305 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/),
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800306 },
307 privilege: {
Derick Montague602e98a2020-10-21 16:20:00 -0500308 required,
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800309 },
310 password: {
Derick Montague602e98a2020-10-21 16:20:00 -0500311 required: requiredIf(function () {
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800312 return this.requirePassword();
313 }),
314 minLength: minLength(this.passwordRequirements.minLength),
Derick Montague602e98a2020-10-21 16:20:00 -0500315 maxLength: maxLength(this.passwordRequirements.maxLength),
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800316 },
317 passwordConfirmation: {
Derick Montague602e98a2020-10-21 16:20:00 -0500318 required: requiredIf(function () {
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800319 return this.requirePassword();
320 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500321 sameAsPassword: sameAs('password'),
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700322 },
Derick Montague602e98a2020-10-21 16:20:00 -0500323 manualUnlock: {},
324 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800325 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800326 },
327 methods: {
328 handleSubmit() {
329 let userData = {};
330
331 if (this.newUser) {
332 this.$v.$touch();
333 if (this.$v.$invalid) return;
334 userData.username = this.form.username;
335 userData.status = this.form.status;
336 userData.privilege = this.form.privilege;
337 userData.password = this.form.password;
338 } else {
339 if (this.$v.$invalid) return;
340 userData.originalUsername = this.originalUsername;
341 if (this.$v.form.status.$dirty) {
342 userData.status = this.form.status;
343 }
344 if (this.$v.form.username.$dirty) {
345 userData.username = this.form.username;
346 }
347 if (this.$v.form.privilege.$dirty) {
348 userData.privilege = this.form.privilege;
349 }
350 if (this.$v.form.password.$dirty) {
351 userData.password = this.form.password;
352 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700353 if (this.$v.form.manualUnlock.$dirty) {
354 // If form manualUnlock control $dirty then
355 // set user Locked property to false
356 userData.locked = false;
357 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800358 if (Object.entries(userData).length === 1) {
359 this.closeModal();
360 return;
361 }
362 }
363
364 this.$emit('ok', { isNewUser: this.newUser, userData });
365 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800366 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800367 closeModal() {
368 this.$nextTick(() => {
369 this.$refs.modal.hide();
370 });
371 },
372 resetForm() {
373 this.form.originalUsername = '';
374 this.form.status = true;
375 this.form.username = '';
Yoshie Muranakabc0d61b2020-11-02 14:36:27 -0800376 this.form.privilege = null;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800377 this.form.password = '';
378 this.form.passwordConfirmation = '';
379 this.$v.$reset();
Yoshie Muranaka791622b2020-04-17 13:55:16 -0700380 this.$emit('hidden');
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800381 },
382 requirePassword() {
383 if (this.newUser) return true;
384 if (this.$v.form.password.$dirty) return true;
385 if (this.$v.form.passwordConfirmation.$dirty) return true;
386 return false;
387 },
388 onOk(bvModalEvt) {
389 // prevent modal close
390 bvModalEvt.preventDefault();
391 this.handleSubmit();
Derick Montague602e98a2020-10-21 16:20:00 -0500392 },
393 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800394};
395</script>