blob: 0f8757ce2c9a648c3c936129d20b5639b4cf500a [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">
Sandeepa Singhb4406162021-07-26 15:05:39 +05305 {{ $t('pageUserManagement.addUser') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -08006 </template>
7 <template v-else>
Sandeepa Singhb4406162021-07-26 15:05:39 +05308 {{ $t('pageUserManagement.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">
Sandeepa Singhb4406162021-07-26 15:05:39 +053018 {{ $t('pageUserManagement.modal.accountLocked') }}
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070019 </template>
20 <template v-else>
Sandeepa Singhb4406162021-07-26 15:05:39 +053021 {{ $t('pageUserManagement.modal.clickSaveToUnlockAccount') }}
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070022 </template>
23 </alert>
24 </b-col>
25 <b-col sm="3">
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050026 <input
27 v-model="form.manualUnlock"
Sandeepa Singhb4406162021-07-26 15:05:39 +053028 data-test-id="userManagement-input-manualUnlock"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -050029 type="hidden"
30 value="false"
31 />
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070032 <b-button
33 variant="primary"
34 :disabled="$v.form.manualUnlock.$dirty"
Sandeepa Singhb4406162021-07-26 15:05:39 +053035 data-test-id="userManagement-button-manualUnlock"
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070036 @click="$v.form.manualUnlock.$touch()"
37 >
Sandeepa Singhb4406162021-07-26 15:05:39 +053038 {{ $t('pageUserManagement.modal.unlock') }}
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -070039 </b-button>
40 </b-col>
41 </b-row>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080042 <b-row>
43 <b-col>
Sandeepa Singhb4406162021-07-26 15:05:39 +053044 <b-form-group :label="$t('pageUserManagement.modal.accountStatus')">
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080045 <b-form-radio
46 v-model="form.status"
47 name="user-status"
48 :value="true"
Sandeepa Singhb4406162021-07-26 15:05:39 +053049 data-test-id="userManagement-radioButton-statusEnabled"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080050 @input="$v.form.status.$touch()"
51 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080052 {{ $t('global.status.enabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080053 </b-form-radio>
54 <b-form-radio
55 v-model="form.status"
56 name="user-status"
Sandeepa Singhb4406162021-07-26 15:05:39 +053057 data-test-id="userManagement-radioButton-statusDisabled"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080058 :value="false"
59 @input="$v.form.status.$touch()"
60 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080061 {{ $t('global.status.disabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080062 </b-form-radio>
63 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080064 <b-form-group
Sandeepa Singhb4406162021-07-26 15:05:39 +053065 :label="$t('pageUserManagement.modal.username')"
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080066 label-for="username"
67 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080068 <b-form-text id="username-help-block">
Sandeepa Singhb4406162021-07-26 15:05:39 +053069 {{ $t('pageUserManagement.modal.cannotStartWithANumber') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080070 <br />
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080071 {{
72 $t(
Sandeepa Singhb4406162021-07-26 15:05:39 +053073 'pageUserManagement.modal.noSpecialCharactersExceptUnderscore'
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080074 )
75 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080076 </b-form-text>
77 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -060078 id="username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080079 v-model="form.username"
80 type="text"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080081 aria-describedby="username-help-block"
Sandeepa Singhb4406162021-07-26 15:05:39 +053082 data-test-id="userManagement-input-username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080083 :state="getValidationState($v.form.username)"
84 :disabled="!newUser && originalUsername === 'root'"
Yoshie Muranaka52b02232020-02-20 08:00:45 -080085 @input="$v.form.username.$touch()"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080086 />
87 <b-form-invalid-feedback role="alert">
88 <template v-if="!$v.form.username.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080089 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080090 </template>
91 <template v-else-if="!$v.form.username.maxLength">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080092 {{
93 $t('global.form.lengthMustBeBetween', { min: 1, max: 16 })
94 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080095 </template>
96 <template v-else-if="!$v.form.username.pattern">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080097 {{ $t('global.form.invalidFormat') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080098 </template>
99 </b-form-invalid-feedback>
100 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800101 <b-form-group
Sandeepa Singhb4406162021-07-26 15:05:39 +0530102 :label="$t('pageUserManagement.modal.privilege')"
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -0800103 label-for="privilege"
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800104 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800105 <b-form-select
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -0800106 id="privilege"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800107 v-model="form.privilege"
108 :options="privilegeTypes"
Sandeepa Singhb4406162021-07-26 15:05:39 +0530109 data-test-id="userManagement-select-privilege"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800110 :state="getValidationState($v.form.privilege)"
111 @input="$v.form.privilege.$touch()"
112 >
Yoshie Muranakabc0d61b2020-11-02 14:36:27 -0800113 <template #first>
114 <b-form-select-option :value="null" disabled>
115 {{ $t('global.form.selectAnOption') }}
116 </b-form-select-option>
117 </template>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800118 </b-form-select>
119 <b-form-invalid-feedback role="alert">
120 <template v-if="!$v.form.privilege.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800121 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800122 </template>
123 </b-form-invalid-feedback>
124 </b-form-group>
125 </b-col>
126 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800127 <b-form-group
Sandeepa Singhb4406162021-07-26 15:05:39 +0530128 :label="$t('pageUserManagement.modal.userPassword')"
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800129 label-for="password"
130 >
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800131 <b-form-text id="password-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800132 {{
Sandeepa Singhb4406162021-07-26 15:05:39 +0530133 $t('pageUserManagement.modal.passwordMustBeBetween', {
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800134 min: passwordRequirements.minLength,
Derick Montague602e98a2020-10-21 16:20:00 -0500135 max: passwordRequirements.maxLength,
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800136 })
137 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800138 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800139 <input-password-toggle>
140 <b-form-input
141 id="password"
142 v-model="form.password"
143 type="password"
Sandeepa Singhb4406162021-07-26 15:05:39 +0530144 data-test-id="userManagement-input-password"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800145 aria-describedby="password-help-block"
146 :state="getValidationState($v.form.password)"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -0500147 class="form-control-with-button"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800148 @input="$v.form.password.$touch()"
149 />
150 <b-form-invalid-feedback role="alert">
151 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800152 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800153 </template>
154 <template
155 v-if="
156 !$v.form.password.minLength || !$v.form.password.maxLength
157 "
158 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800159 {{
Sandeepa Singhb4406162021-07-26 15:05:39 +0530160 $t('pageUserManagement.modal.passwordMustBeBetween', {
161 min: passwordRequirements.minLength,
162 max: passwordRequirements.maxLength,
163 })
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800164 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800165 </template>
166 </b-form-invalid-feedback>
167 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800168 </b-form-group>
169 <b-form-group
Sandeepa Singhb4406162021-07-26 15:05:39 +0530170 :label="$t('pageUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800171 label-for="password-confirmation"
172 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800173 <input-password-toggle>
174 <b-form-input
175 id="password-confirmation"
176 v-model="form.passwordConfirmation"
Sandeepa Singhb4406162021-07-26 15:05:39 +0530177 data-test-id="userManagement-input-passwordConfirmation"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800178 type="password"
179 :state="getValidationState($v.form.passwordConfirmation)"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -0500180 class="form-control-with-button"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800181 @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 >
Sandeepa Singhb4406162021-07-26 15:05:39 +0530190 {{ $t('pageUserManagement.modal.passwordsDoNotMatch') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800191 </template>
192 </b-form-invalid-feedback>
193 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800194 </b-form-group>
195 </b-col>
196 </b-row>
197 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800198 </b-form>
Sukanya Pandey18375032020-10-30 21:13:06 +0530199 <template #modal-footer="{ cancel }">
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500200 <b-button
201 variant="secondary"
Sandeepa Singhb4406162021-07-26 15:05:39 +0530202 data-test-id="userManagement-button-cancel"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500203 @click="cancel()"
204 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700205 {{ $t('global.action.cancel') }}
206 </b-button>
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500207 <b-button
208 form="form-user"
Sandeepa Singhb4406162021-07-26 15:05:39 +0530209 data-test-id="userManagement-button-submit"
Dixsie Wolmersfe1e6582020-07-15 11:18:12 -0500210 type="submit"
211 variant="primary"
212 @click="onOk"
213 >
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700214 <template v-if="newUser">
Sandeepa Singhb4406162021-07-26 15:05:39 +0530215 {{ $t('pageUserManagement.addUser') }}
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700216 </template>
217 <template v-else>
218 {{ $t('global.action.save') }}
219 </template>
220 </b-button>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800221 </template>
222 </b-modal>
223</template>
224
225<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800226import {
227 required,
228 maxLength,
229 minLength,
230 sameAs,
231 helpers,
Derick Montague602e98a2020-10-21 16:20:00 -0500232 requiredIf,
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800233} from 'vuelidate/lib/validators';
SurenNeware61859092020-10-01 09:37:32 +0530234import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
235import InputPasswordToggle from '@/components/Global/InputPasswordToggle';
236import Alert from '@/components/Global/Alert';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800237
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800238export default {
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700239 components: { Alert, InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800240 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600241 props: {
242 user: {
243 type: Object,
Derick Montague602e98a2020-10-21 16:20:00 -0500244 default: null,
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800245 },
246 passwordRequirements: {
247 type: Object,
Derick Montague602e98a2020-10-21 16:20:00 -0500248 required: true,
249 },
Derick Montague09e45cd2020-01-23 15:45:57 -0600250 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800251 data() {
252 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800253 originalUsername: '',
254 form: {
255 status: true,
256 username: '',
Yoshie Muranakabc0d61b2020-11-02 14:36:27 -0800257 privilege: null,
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800258 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700259 passwordConfirmation: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500260 manualUnlock: false,
261 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800262 };
263 },
264 computed: {
265 newUser() {
266 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700267 },
268 accountSettings() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530269 return this.$store.getters['userManagement/accountSettings'];
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700270 },
271 manualUnlockPolicy() {
272 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700273 },
274 privilegeTypes() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530275 return this.$store.getters['userManagement/accountRoles'];
Derick Montague602e98a2020-10-21 16:20:00 -0500276 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800277 },
278 watch: {
Derick Montague602e98a2020-10-21 16:20:00 -0500279 user: function (value) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800280 if (value === null) return;
281 this.originalUsername = value.username;
282 this.form.username = value.username;
283 this.form.status = value.Enabled;
284 this.form.privilege = value.privilege;
Derick Montague602e98a2020-10-21 16:20:00 -0500285 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800286 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800287 validations() {
288 return {
289 form: {
290 status: {
Derick Montague602e98a2020-10-21 16:20:00 -0500291 required,
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800292 },
293 username: {
294 required,
295 maxLength: maxLength(16),
Derick Montague602e98a2020-10-21 16:20:00 -0500296 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/),
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800297 },
298 privilege: {
Derick Montague602e98a2020-10-21 16:20:00 -0500299 required,
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800300 },
301 password: {
Derick Montague602e98a2020-10-21 16:20:00 -0500302 required: requiredIf(function () {
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800303 return this.requirePassword();
304 }),
305 minLength: minLength(this.passwordRequirements.minLength),
Derick Montague602e98a2020-10-21 16:20:00 -0500306 maxLength: maxLength(this.passwordRequirements.maxLength),
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800307 },
308 passwordConfirmation: {
Derick Montague602e98a2020-10-21 16:20:00 -0500309 required: requiredIf(function () {
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800310 return this.requirePassword();
311 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500312 sameAsPassword: sameAs('password'),
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700313 },
Derick Montague602e98a2020-10-21 16:20:00 -0500314 manualUnlock: {},
315 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800316 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800317 },
318 methods: {
319 handleSubmit() {
320 let userData = {};
321
322 if (this.newUser) {
323 this.$v.$touch();
324 if (this.$v.$invalid) return;
325 userData.username = this.form.username;
326 userData.status = this.form.status;
327 userData.privilege = this.form.privilege;
328 userData.password = this.form.password;
329 } else {
330 if (this.$v.$invalid) return;
331 userData.originalUsername = this.originalUsername;
332 if (this.$v.form.status.$dirty) {
333 userData.status = this.form.status;
334 }
335 if (this.$v.form.username.$dirty) {
336 userData.username = this.form.username;
337 }
338 if (this.$v.form.privilege.$dirty) {
339 userData.privilege = this.form.privilege;
340 }
341 if (this.$v.form.password.$dirty) {
342 userData.password = this.form.password;
343 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700344 if (this.$v.form.manualUnlock.$dirty) {
345 // If form manualUnlock control $dirty then
346 // set user Locked property to false
347 userData.locked = false;
348 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800349 if (Object.entries(userData).length === 1) {
350 this.closeModal();
351 return;
352 }
353 }
354
355 this.$emit('ok', { isNewUser: this.newUser, userData });
356 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800357 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800358 closeModal() {
359 this.$nextTick(() => {
360 this.$refs.modal.hide();
361 });
362 },
363 resetForm() {
364 this.form.originalUsername = '';
365 this.form.status = true;
366 this.form.username = '';
Yoshie Muranakabc0d61b2020-11-02 14:36:27 -0800367 this.form.privilege = null;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800368 this.form.password = '';
369 this.form.passwordConfirmation = '';
370 this.$v.$reset();
Yoshie Muranaka791622b2020-04-17 13:55:16 -0700371 this.$emit('hidden');
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800372 },
373 requirePassword() {
374 if (this.newUser) return true;
375 if (this.$v.form.password.$dirty) return true;
376 if (this.$v.form.passwordConfirmation.$dirty) return true;
377 return false;
378 },
379 onOk(bvModalEvt) {
380 // prevent modal close
381 bvModalEvt.preventDefault();
382 this.handleSubmit();
Derick Montague602e98a2020-10-21 16:20:00 -0500383 },
384 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800385};
386</script>