blob: 3c46d6660b0bbdedff3ef9a20ed825b2ffbe5083 [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">
28 <input v-model="form.manualUnlock" type="hidden" value="false" />
29 <b-button
30 variant="primary"
31 :disabled="$v.form.manualUnlock.$dirty"
32 @click="$v.form.manualUnlock.$touch()"
33 >
34 {{ $t('pageLocalUserManagement.modal.unlock') }}
35 </b-button>
36 </b-col>
37 </b-row>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080038 <b-row>
39 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080040 <b-form-group
41 :label="$t('pageLocalUserManagement.modal.accountStatus')"
42 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080043 <b-form-radio
44 v-model="form.status"
45 name="user-status"
46 :value="true"
47 @input="$v.form.status.$touch()"
48 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080049 {{ $t('global.status.enabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080050 </b-form-radio>
51 <b-form-radio
52 v-model="form.status"
53 name="user-status"
54 :value="false"
55 @input="$v.form.status.$touch()"
56 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080057 {{ $t('global.status.disabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080058 </b-form-radio>
59 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080060 <b-form-group
61 :label="$t('pageLocalUserManagement.modal.username')"
62 label-for="username"
63 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080064 <b-form-text id="username-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080065 {{ $t('pageLocalUserManagement.modal.cannotStartWithANumber') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080066 <br />
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080067 {{
68 $t(
69 'pageLocalUserManagement.modal.noSpecialCharactersExceptUnderscore'
70 )
71 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080072 </b-form-text>
73 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -060074 id="username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080075 v-model="form.username"
76 type="text"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080077 aria-describedby="username-help-block"
78 :state="getValidationState($v.form.username)"
79 :disabled="!newUser && originalUsername === 'root'"
Yoshie Muranaka52b02232020-02-20 08:00:45 -080080 @input="$v.form.username.$touch()"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080081 />
82 <b-form-invalid-feedback role="alert">
83 <template v-if="!$v.form.username.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080084 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080085 </template>
86 <template v-else-if="!$v.form.username.maxLength">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080087 {{
88 $t('global.form.lengthMustBeBetween', { min: 1, max: 16 })
89 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080090 </template>
91 <template v-else-if="!$v.form.username.pattern">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080092 {{ $t('global.form.invalidFormat') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080093 </template>
94 </b-form-invalid-feedback>
95 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080096 <b-form-group
97 :label="$t('pageLocalUserManagement.modal.privilege')"
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -080098 label-for="privilege"
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080099 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800100 <b-form-select
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -0800101 id="privilege"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800102 v-model="form.privilege"
103 :options="privilegeTypes"
104 :state="getValidationState($v.form.privilege)"
105 @input="$v.form.privilege.$touch()"
106 >
107 </b-form-select>
108 <b-form-invalid-feedback role="alert">
109 <template v-if="!$v.form.privilege.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800110 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800111 </template>
112 </b-form-invalid-feedback>
113 </b-form-group>
114 </b-col>
115 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800116 <b-form-group
117 :label="$t('pageLocalUserManagement.modal.userPassword')"
118 label-for="password"
119 >
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800120 <b-form-text id="password-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800121 {{
122 $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
123 min: passwordRequirements.minLength,
124 max: passwordRequirements.maxLength
125 })
126 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800127 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800128 <input-password-toggle>
129 <b-form-input
130 id="password"
131 v-model="form.password"
132 type="password"
133 aria-describedby="password-help-block"
134 :state="getValidationState($v.form.password)"
135 @input="$v.form.password.$touch()"
136 />
137 <b-form-invalid-feedback role="alert">
138 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800139 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800140 </template>
141 <template
142 v-if="
143 !$v.form.password.minLength || !$v.form.password.maxLength
144 "
145 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800146 {{
147 $t(
148 'pageLocalUserManagement.modal.passwordMustBeBetween',
149 {
150 min: passwordRequirements.minLength,
151 max: passwordRequirements.maxLength
152 }
153 )
154 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800155 </template>
156 </b-form-invalid-feedback>
157 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800158 </b-form-group>
159 <b-form-group
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800160 :label="$t('pageLocalUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800161 label-for="password-confirmation"
162 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800163 <input-password-toggle>
164 <b-form-input
165 id="password-confirmation"
166 v-model="form.passwordConfirmation"
167 type="password"
168 :state="getValidationState($v.form.passwordConfirmation)"
169 @input="$v.form.passwordConfirmation.$touch()"
170 />
171 <b-form-invalid-feedback role="alert">
172 <template v-if="!$v.form.passwordConfirmation.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800173 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800174 </template>
175 <template
176 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
177 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800178 {{
179 $t('pageLocalUserManagement.modal.passwordsDoNotMatch')
180 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800181 </template>
182 </b-form-invalid-feedback>
183 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800184 </b-form-group>
185 </b-col>
186 </b-row>
187 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800188 </b-form>
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700189 <template v-slot:modal-footer="{ ok, cancel }">
190 <b-button variant="secondary" @click="cancel()">
191 {{ $t('global.action.cancel') }}
192 </b-button>
Yoshie Muranaka0b8cffd2020-06-23 08:44:54 -0700193 <b-button form="form-user" type="submit" variant="primary" @click="onOk">
Yoshie Muranaka9a9092d2020-05-04 08:24:21 -0700194 <template v-if="newUser">
195 {{ $t('pageLocalUserManagement.addUser') }}
196 </template>
197 <template v-else>
198 {{ $t('global.action.save') }}
199 </template>
200 </b-button>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800201 </template>
202 </b-modal>
203</template>
204
205<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800206import {
207 required,
208 maxLength,
209 minLength,
210 sameAs,
211 helpers,
212 requiredIf
213} from 'vuelidate/lib/validators';
214import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800215import InputPasswordToggle from '../../../components/Global/InputPasswordToggle';
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700216import Alert from '../../../components/Global/Alert';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800217
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800218export default {
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700219 components: { Alert, InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800220 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600221 props: {
222 user: {
223 type: Object,
224 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800225 },
226 passwordRequirements: {
227 type: Object,
228 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600229 }
230 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800231 data() {
232 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800233 originalUsername: '',
234 form: {
235 status: true,
236 username: '',
237 privilege: '',
238 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700239 passwordConfirmation: '',
240 manualUnlock: false
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800241 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800242 };
243 },
244 computed: {
245 newUser() {
246 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700247 },
248 accountSettings() {
249 return this.$store.getters['localUsers/accountSettings'];
250 },
251 manualUnlockPolicy() {
252 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700253 },
254 privilegeTypes() {
255 return this.$store.getters['localUsers/accountRoles'];
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800256 }
257 },
258 watch: {
259 user: function(value) {
260 if (value === null) return;
261 this.originalUsername = value.username;
262 this.form.username = value.username;
263 this.form.status = value.Enabled;
264 this.form.privilege = value.privilege;
265 }
266 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800267 validations() {
268 return {
269 form: {
270 status: {
271 required
272 },
273 username: {
274 required,
275 maxLength: maxLength(16),
276 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
277 },
278 privilege: {
279 required
280 },
281 password: {
282 required: requiredIf(function() {
283 return this.requirePassword();
284 }),
285 minLength: minLength(this.passwordRequirements.minLength),
286 maxLength: maxLength(this.passwordRequirements.maxLength)
287 },
288 passwordConfirmation: {
289 required: requiredIf(function() {
290 return this.requirePassword();
291 }),
292 sameAsPassword: sameAs('password')
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700293 },
294 manualUnlock: {}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800295 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800296 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800297 },
298 methods: {
299 handleSubmit() {
300 let userData = {};
301
302 if (this.newUser) {
303 this.$v.$touch();
304 if (this.$v.$invalid) return;
305 userData.username = this.form.username;
306 userData.status = this.form.status;
307 userData.privilege = this.form.privilege;
308 userData.password = this.form.password;
309 } else {
310 if (this.$v.$invalid) return;
311 userData.originalUsername = this.originalUsername;
312 if (this.$v.form.status.$dirty) {
313 userData.status = this.form.status;
314 }
315 if (this.$v.form.username.$dirty) {
316 userData.username = this.form.username;
317 }
318 if (this.$v.form.privilege.$dirty) {
319 userData.privilege = this.form.privilege;
320 }
321 if (this.$v.form.password.$dirty) {
322 userData.password = this.form.password;
323 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700324 if (this.$v.form.manualUnlock.$dirty) {
325 // If form manualUnlock control $dirty then
326 // set user Locked property to false
327 userData.locked = false;
328 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800329 if (Object.entries(userData).length === 1) {
330 this.closeModal();
331 return;
332 }
333 }
334
335 this.$emit('ok', { isNewUser: this.newUser, userData });
336 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800337 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800338 closeModal() {
339 this.$nextTick(() => {
340 this.$refs.modal.hide();
341 });
342 },
343 resetForm() {
344 this.form.originalUsername = '';
345 this.form.status = true;
346 this.form.username = '';
347 this.form.privilege = '';
348 this.form.password = '';
349 this.form.passwordConfirmation = '';
350 this.$v.$reset();
Yoshie Muranaka791622b2020-04-17 13:55:16 -0700351 this.$emit('hidden');
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800352 },
353 requirePassword() {
354 if (this.newUser) return true;
355 if (this.$v.form.password.$dirty) return true;
356 if (this.$v.form.passwordConfirmation.$dirty) return true;
357 return false;
358 },
359 onOk(bvModalEvt) {
360 // prevent modal close
361 bvModalEvt.preventDefault();
362 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800363 }
364 }
365};
366</script>