blob: baf49e916c2bb0168ce13492d5367a15c9c57844 [file] [log] [blame]
Yoshie Muranaka463a5702019-12-04 09:09:36 -08001<template>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -08002 <b-modal id="modal-user" ref="modal" @ok="onOk" @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 Muranaka4b0fc1d2020-01-06 07:36:16 -080011 <b-form novalidate @submit="handleSubmit">
12 <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>
189 <template v-slot:modal-ok>
190 <template v-if="newUser">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800191 {{ $t('pageLocalUserManagement.addUser') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800192 </template>
193 <template v-else>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800194 {{ $t('global.action.save') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800195 </template>
196 </template>
197 </b-modal>
198</template>
199
200<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800201import {
202 required,
203 maxLength,
204 minLength,
205 sameAs,
206 helpers,
207 requiredIf
208} from 'vuelidate/lib/validators';
209import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800210import InputPasswordToggle from '../../../components/Global/InputPasswordToggle';
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700211import Alert from '../../../components/Global/Alert';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800212
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800213export default {
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700214 components: { Alert, InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800215 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600216 props: {
217 user: {
218 type: Object,
219 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800220 },
221 passwordRequirements: {
222 type: Object,
223 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600224 }
225 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800226 data() {
227 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800228 originalUsername: '',
229 form: {
230 status: true,
231 username: '',
232 privilege: '',
233 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700234 passwordConfirmation: '',
235 manualUnlock: false
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800236 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800237 };
238 },
239 computed: {
240 newUser() {
241 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700242 },
243 accountSettings() {
244 return this.$store.getters['localUsers/accountSettings'];
245 },
246 manualUnlockPolicy() {
247 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700248 },
249 privilegeTypes() {
250 return this.$store.getters['localUsers/accountRoles'];
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800251 }
252 },
253 watch: {
254 user: function(value) {
255 if (value === null) return;
256 this.originalUsername = value.username;
257 this.form.username = value.username;
258 this.form.status = value.Enabled;
259 this.form.privilege = value.privilege;
260 }
261 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800262 validations() {
263 return {
264 form: {
265 status: {
266 required
267 },
268 username: {
269 required,
270 maxLength: maxLength(16),
271 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
272 },
273 privilege: {
274 required
275 },
276 password: {
277 required: requiredIf(function() {
278 return this.requirePassword();
279 }),
280 minLength: minLength(this.passwordRequirements.minLength),
281 maxLength: maxLength(this.passwordRequirements.maxLength)
282 },
283 passwordConfirmation: {
284 required: requiredIf(function() {
285 return this.requirePassword();
286 }),
287 sameAsPassword: sameAs('password')
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700288 },
289 manualUnlock: {}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800290 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800291 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800292 },
293 methods: {
294 handleSubmit() {
295 let userData = {};
296
297 if (this.newUser) {
298 this.$v.$touch();
299 if (this.$v.$invalid) return;
300 userData.username = this.form.username;
301 userData.status = this.form.status;
302 userData.privilege = this.form.privilege;
303 userData.password = this.form.password;
304 } else {
305 if (this.$v.$invalid) return;
306 userData.originalUsername = this.originalUsername;
307 if (this.$v.form.status.$dirty) {
308 userData.status = this.form.status;
309 }
310 if (this.$v.form.username.$dirty) {
311 userData.username = this.form.username;
312 }
313 if (this.$v.form.privilege.$dirty) {
314 userData.privilege = this.form.privilege;
315 }
316 if (this.$v.form.password.$dirty) {
317 userData.password = this.form.password;
318 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700319 if (this.$v.form.manualUnlock.$dirty) {
320 // If form manualUnlock control $dirty then
321 // set user Locked property to false
322 userData.locked = false;
323 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800324 if (Object.entries(userData).length === 1) {
325 this.closeModal();
326 return;
327 }
328 }
329
330 this.$emit('ok', { isNewUser: this.newUser, userData });
331 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800332 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800333 closeModal() {
334 this.$nextTick(() => {
335 this.$refs.modal.hide();
336 });
337 },
338 resetForm() {
339 this.form.originalUsername = '';
340 this.form.status = true;
341 this.form.username = '';
342 this.form.privilege = '';
343 this.form.password = '';
344 this.form.passwordConfirmation = '';
345 this.$v.$reset();
Yoshie Muranaka791622b2020-04-17 13:55:16 -0700346 this.$emit('hidden');
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800347 },
348 requirePassword() {
349 if (this.newUser) return true;
350 if (this.$v.form.password.$dirty) return true;
351 if (this.$v.form.passwordConfirmation.$dirty) return true;
352 return false;
353 },
354 onOk(bvModalEvt) {
355 // prevent modal close
356 bvModalEvt.preventDefault();
357 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800358 }
359 }
360};
361</script>