blob: ef13761f922b39a256020211af5c69059554127b [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>
13 <b-row>
14 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080015 <b-form-group
16 :label="$t('pageLocalUserManagement.modal.accountStatus')"
17 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080018 <b-form-radio
19 v-model="form.status"
20 name="user-status"
21 :value="true"
22 @input="$v.form.status.$touch()"
23 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080024 {{ $t('global.status.enabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080025 </b-form-radio>
26 <b-form-radio
27 v-model="form.status"
28 name="user-status"
29 :value="false"
30 @input="$v.form.status.$touch()"
31 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080032 {{ $t('global.status.disabled') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080033 </b-form-radio>
34 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080035 <b-form-group
36 :label="$t('pageLocalUserManagement.modal.username')"
37 label-for="username"
38 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080039 <b-form-text id="username-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080040 {{ $t('pageLocalUserManagement.modal.cannotStartWithANumber') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080041 <br />
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080042 {{
43 $t(
44 'pageLocalUserManagement.modal.noSpecialCharactersExceptUnderscore'
45 )
46 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080047 </b-form-text>
48 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -060049 id="username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080050 v-model="form.username"
51 type="text"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080052 aria-describedby="username-help-block"
53 :state="getValidationState($v.form.username)"
54 :disabled="!newUser && originalUsername === 'root'"
Yoshie Muranaka52b02232020-02-20 08:00:45 -080055 @input="$v.form.username.$touch()"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080056 />
57 <b-form-invalid-feedback role="alert">
58 <template v-if="!$v.form.username.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080059 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080060 </template>
61 <template v-else-if="!$v.form.username.maxLength">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080062 {{
63 $t('global.form.lengthMustBeBetween', { min: 1, max: 16 })
64 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080065 </template>
66 <template v-else-if="!$v.form.username.pattern">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080067 {{ $t('global.form.invalidFormat') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080068 </template>
69 </b-form-invalid-feedback>
70 </b-form-group>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080071 <b-form-group
72 :label="$t('pageLocalUserManagement.modal.privilege')"
73 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080074 <b-form-select
75 v-model="form.privilege"
76 :options="privilegeTypes"
77 :state="getValidationState($v.form.privilege)"
78 @input="$v.form.privilege.$touch()"
79 >
80 </b-form-select>
81 <b-form-invalid-feedback role="alert">
82 <template v-if="!$v.form.privilege.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080083 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080084 </template>
85 </b-form-invalid-feedback>
86 </b-form-group>
87 </b-col>
88 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080089 <b-form-group
90 :label="$t('pageLocalUserManagement.modal.userPassword')"
91 label-for="password"
92 >
Yoshie Muranaka52b02232020-02-20 08:00:45 -080093 <b-form-text id="password-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080094 {{
95 $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
96 min: passwordRequirements.minLength,
97 max: passwordRequirements.maxLength
98 })
99 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800100 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800101 <input-password-toggle>
102 <b-form-input
103 id="password"
104 v-model="form.password"
105 type="password"
106 aria-describedby="password-help-block"
107 :state="getValidationState($v.form.password)"
108 @input="$v.form.password.$touch()"
109 />
110 <b-form-invalid-feedback role="alert">
111 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800112 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800113 </template>
114 <template
115 v-if="
116 !$v.form.password.minLength || !$v.form.password.maxLength
117 "
118 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800119 {{
120 $t(
121 'pageLocalUserManagement.modal.passwordMustBeBetween',
122 {
123 min: passwordRequirements.minLength,
124 max: passwordRequirements.maxLength
125 }
126 )
127 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800128 </template>
129 </b-form-invalid-feedback>
130 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800131 </b-form-group>
132 <b-form-group
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800133 :label="$t('pageLocalUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800134 label-for="password-confirmation"
135 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800136 <input-password-toggle>
137 <b-form-input
138 id="password-confirmation"
139 v-model="form.passwordConfirmation"
140 type="password"
141 :state="getValidationState($v.form.passwordConfirmation)"
142 @input="$v.form.passwordConfirmation.$touch()"
143 />
144 <b-form-invalid-feedback role="alert">
145 <template v-if="!$v.form.passwordConfirmation.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800146 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800147 </template>
148 <template
149 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
150 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800151 {{
152 $t('pageLocalUserManagement.modal.passwordsDoNotMatch')
153 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800154 </template>
155 </b-form-invalid-feedback>
156 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800157 </b-form-group>
158 </b-col>
159 </b-row>
160 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800161 </b-form>
162 <template v-slot:modal-ok>
163 <template v-if="newUser">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800164 {{ $t('pageLocalUserManagement.addUser') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800165 </template>
166 <template v-else>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800167 {{ $t('global.action.save') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800168 </template>
169 </template>
170 </b-modal>
171</template>
172
173<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800174import {
175 required,
176 maxLength,
177 minLength,
178 sameAs,
179 helpers,
180 requiredIf
181} from 'vuelidate/lib/validators';
182import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800183import InputPasswordToggle from '../../../components/Global/InputPasswordToggle';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800184
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800185export default {
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800186 components: { InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800187 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600188 props: {
189 user: {
190 type: Object,
191 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800192 },
193 passwordRequirements: {
194 type: Object,
195 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600196 }
197 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800198 data() {
199 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800200 privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'],
201 originalUsername: '',
202 form: {
203 status: true,
204 username: '',
205 privilege: '',
206 password: '',
207 passwordConfirmation: ''
208 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800209 };
210 },
211 computed: {
212 newUser() {
213 return this.user ? false : true;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800214 }
215 },
216 watch: {
217 user: function(value) {
218 if (value === null) return;
219 this.originalUsername = value.username;
220 this.form.username = value.username;
221 this.form.status = value.Enabled;
222 this.form.privilege = value.privilege;
223 }
224 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800225 validations() {
226 return {
227 form: {
228 status: {
229 required
230 },
231 username: {
232 required,
233 maxLength: maxLength(16),
234 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
235 },
236 privilege: {
237 required
238 },
239 password: {
240 required: requiredIf(function() {
241 return this.requirePassword();
242 }),
243 minLength: minLength(this.passwordRequirements.minLength),
244 maxLength: maxLength(this.passwordRequirements.maxLength)
245 },
246 passwordConfirmation: {
247 required: requiredIf(function() {
248 return this.requirePassword();
249 }),
250 sameAsPassword: sameAs('password')
251 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800252 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800253 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800254 },
255 methods: {
256 handleSubmit() {
257 let userData = {};
258
259 if (this.newUser) {
260 this.$v.$touch();
261 if (this.$v.$invalid) return;
262 userData.username = this.form.username;
263 userData.status = this.form.status;
264 userData.privilege = this.form.privilege;
265 userData.password = this.form.password;
266 } else {
267 if (this.$v.$invalid) return;
268 userData.originalUsername = this.originalUsername;
269 if (this.$v.form.status.$dirty) {
270 userData.status = this.form.status;
271 }
272 if (this.$v.form.username.$dirty) {
273 userData.username = this.form.username;
274 }
275 if (this.$v.form.privilege.$dirty) {
276 userData.privilege = this.form.privilege;
277 }
278 if (this.$v.form.password.$dirty) {
279 userData.password = this.form.password;
280 }
281 if (Object.entries(userData).length === 1) {
282 this.closeModal();
283 return;
284 }
285 }
286
287 this.$emit('ok', { isNewUser: this.newUser, userData });
288 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800289 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800290 closeModal() {
291 this.$nextTick(() => {
292 this.$refs.modal.hide();
293 });
294 },
295 resetForm() {
296 this.form.originalUsername = '';
297 this.form.status = true;
298 this.form.username = '';
299 this.form.privilege = '';
300 this.form.password = '';
301 this.form.passwordConfirmation = '';
302 this.$v.$reset();
303 },
304 requirePassword() {
305 if (this.newUser) return true;
306 if (this.$v.form.password.$dirty) return true;
307 if (this.$v.form.passwordConfirmation.$dirty) return true;
308 return false;
309 },
310 onOk(bvModalEvt) {
311 // prevent modal close
312 bvModalEvt.preventDefault();
313 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800314 }
315 }
316};
317</script>