blob: a9c0f6c536e3d30ae68be68abbe0028f549ed4f4 [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 privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'],
229 originalUsername: '',
230 form: {
231 status: true,
232 username: '',
233 privilege: '',
234 password: '',
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700235 passwordConfirmation: '',
236 manualUnlock: false
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800237 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800238 };
239 },
240 computed: {
241 newUser() {
242 return this.user ? false : true;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700243 },
244 accountSettings() {
245 return this.$store.getters['localUsers/accountSettings'];
246 },
247 manualUnlockPolicy() {
248 return !this.accountSettings.accountLockoutDuration;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800249 }
250 },
251 watch: {
252 user: function(value) {
253 if (value === null) return;
254 this.originalUsername = value.username;
255 this.form.username = value.username;
256 this.form.status = value.Enabled;
257 this.form.privilege = value.privilege;
258 }
259 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800260 validations() {
261 return {
262 form: {
263 status: {
264 required
265 },
266 username: {
267 required,
268 maxLength: maxLength(16),
269 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
270 },
271 privilege: {
272 required
273 },
274 password: {
275 required: requiredIf(function() {
276 return this.requirePassword();
277 }),
278 minLength: minLength(this.passwordRequirements.minLength),
279 maxLength: maxLength(this.passwordRequirements.maxLength)
280 },
281 passwordConfirmation: {
282 required: requiredIf(function() {
283 return this.requirePassword();
284 }),
285 sameAsPassword: sameAs('password')
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700286 },
287 manualUnlock: {}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800288 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800289 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800290 },
291 methods: {
292 handleSubmit() {
293 let userData = {};
294
295 if (this.newUser) {
296 this.$v.$touch();
297 if (this.$v.$invalid) return;
298 userData.username = this.form.username;
299 userData.status = this.form.status;
300 userData.privilege = this.form.privilege;
301 userData.password = this.form.password;
302 } else {
303 if (this.$v.$invalid) return;
304 userData.originalUsername = this.originalUsername;
305 if (this.$v.form.status.$dirty) {
306 userData.status = this.form.status;
307 }
308 if (this.$v.form.username.$dirty) {
309 userData.username = this.form.username;
310 }
311 if (this.$v.form.privilege.$dirty) {
312 userData.privilege = this.form.privilege;
313 }
314 if (this.$v.form.password.$dirty) {
315 userData.password = this.form.password;
316 }
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700317 if (this.$v.form.manualUnlock.$dirty) {
318 // If form manualUnlock control $dirty then
319 // set user Locked property to false
320 userData.locked = false;
321 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800322 if (Object.entries(userData).length === 1) {
323 this.closeModal();
324 return;
325 }
326 }
327
328 this.$emit('ok', { isNewUser: this.newUser, userData });
329 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800330 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800331 closeModal() {
332 this.$nextTick(() => {
333 this.$refs.modal.hide();
334 });
335 },
336 resetForm() {
337 this.form.originalUsername = '';
338 this.form.status = true;
339 this.form.username = '';
340 this.form.privilege = '';
341 this.form.password = '';
342 this.form.passwordConfirmation = '';
343 this.$v.$reset();
344 },
345 requirePassword() {
346 if (this.newUser) return true;
347 if (this.$v.form.password.$dirty) return true;
348 if (this.$v.form.passwordConfirmation.$dirty) return true;
349 return false;
350 },
351 onOk(bvModalEvt) {
352 // prevent modal close
353 bvModalEvt.preventDefault();
354 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800355 }
356 }
357};
358</script>