blob: aacda11b4f860afce7c73109344448238f621e6a [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')"
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -080073 label-for="privilege"
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080074 >
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080075 <b-form-select
Yoshie Muranakaa4b9e402020-03-06 14:59:46 -080076 id="privilege"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080077 v-model="form.privilege"
78 :options="privilegeTypes"
79 :state="getValidationState($v.form.privilege)"
80 @input="$v.form.privilege.$touch()"
81 >
82 </b-form-select>
83 <b-form-invalid-feedback role="alert">
84 <template v-if="!$v.form.privilege.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080085 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080086 </template>
87 </b-form-invalid-feedback>
88 </b-form-group>
89 </b-col>
90 <b-col>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080091 <b-form-group
92 :label="$t('pageLocalUserManagement.modal.userPassword')"
93 label-for="password"
94 >
Yoshie Muranaka52b02232020-02-20 08:00:45 -080095 <b-form-text id="password-help-block">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -080096 {{
97 $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
98 min: passwordRequirements.minLength,
99 max: passwordRequirements.maxLength
100 })
101 }}
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800102 </b-form-text>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800103 <input-password-toggle>
104 <b-form-input
105 id="password"
106 v-model="form.password"
107 type="password"
108 aria-describedby="password-help-block"
109 :state="getValidationState($v.form.password)"
110 @input="$v.form.password.$touch()"
111 />
112 <b-form-invalid-feedback role="alert">
113 <template v-if="!$v.form.password.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800114 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800115 </template>
116 <template
117 v-if="
118 !$v.form.password.minLength || !$v.form.password.maxLength
119 "
120 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800121 {{
122 $t(
123 'pageLocalUserManagement.modal.passwordMustBeBetween',
124 {
125 min: passwordRequirements.minLength,
126 max: passwordRequirements.maxLength
127 }
128 )
129 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800130 </template>
131 </b-form-invalid-feedback>
132 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800133 </b-form-group>
134 <b-form-group
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800135 :label="$t('pageLocalUserManagement.modal.confirmUserPassword')"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800136 label-for="password-confirmation"
137 >
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800138 <input-password-toggle>
139 <b-form-input
140 id="password-confirmation"
141 v-model="form.passwordConfirmation"
142 type="password"
143 :state="getValidationState($v.form.passwordConfirmation)"
144 @input="$v.form.passwordConfirmation.$touch()"
145 />
146 <b-form-invalid-feedback role="alert">
147 <template v-if="!$v.form.passwordConfirmation.required">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800148 {{ $t('global.form.fieldRequired') }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800149 </template>
150 <template
151 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
152 >
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800153 {{
154 $t('pageLocalUserManagement.modal.passwordsDoNotMatch')
155 }}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800156 </template>
157 </b-form-invalid-feedback>
158 </input-password-toggle>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800159 </b-form-group>
160 </b-col>
161 </b-row>
162 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800163 </b-form>
164 <template v-slot:modal-ok>
165 <template v-if="newUser">
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800166 {{ $t('pageLocalUserManagement.addUser') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800167 </template>
168 <template v-else>
Yoshie Muranakaeaa04802020-02-28 13:21:27 -0800169 {{ $t('global.action.save') }}
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800170 </template>
171 </template>
172 </b-modal>
173</template>
174
175<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800176import {
177 required,
178 maxLength,
179 minLength,
180 sameAs,
181 helpers,
182 requiredIf
183} from 'vuelidate/lib/validators';
184import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800185import InputPasswordToggle from '../../../components/Global/InputPasswordToggle';
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800186
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800187export default {
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -0800188 components: { InputPasswordToggle },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800189 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600190 props: {
191 user: {
192 type: Object,
193 default: null
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800194 },
195 passwordRequirements: {
196 type: Object,
197 required: true
Derick Montague09e45cd2020-01-23 15:45:57 -0600198 }
199 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800200 data() {
201 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800202 privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'],
203 originalUsername: '',
204 form: {
205 status: true,
206 username: '',
207 privilege: '',
208 password: '',
209 passwordConfirmation: ''
210 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800211 };
212 },
213 computed: {
214 newUser() {
215 return this.user ? false : true;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800216 }
217 },
218 watch: {
219 user: function(value) {
220 if (value === null) return;
221 this.originalUsername = value.username;
222 this.form.username = value.username;
223 this.form.status = value.Enabled;
224 this.form.privilege = value.privilege;
225 }
226 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800227 validations() {
228 return {
229 form: {
230 status: {
231 required
232 },
233 username: {
234 required,
235 maxLength: maxLength(16),
236 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
237 },
238 privilege: {
239 required
240 },
241 password: {
242 required: requiredIf(function() {
243 return this.requirePassword();
244 }),
245 minLength: minLength(this.passwordRequirements.minLength),
246 maxLength: maxLength(this.passwordRequirements.maxLength)
247 },
248 passwordConfirmation: {
249 required: requiredIf(function() {
250 return this.requirePassword();
251 }),
252 sameAsPassword: sameAs('password')
253 }
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800254 }
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800255 };
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800256 },
257 methods: {
258 handleSubmit() {
259 let userData = {};
260
261 if (this.newUser) {
262 this.$v.$touch();
263 if (this.$v.$invalid) return;
264 userData.username = this.form.username;
265 userData.status = this.form.status;
266 userData.privilege = this.form.privilege;
267 userData.password = this.form.password;
268 } else {
269 if (this.$v.$invalid) return;
270 userData.originalUsername = this.originalUsername;
271 if (this.$v.form.status.$dirty) {
272 userData.status = this.form.status;
273 }
274 if (this.$v.form.username.$dirty) {
275 userData.username = this.form.username;
276 }
277 if (this.$v.form.privilege.$dirty) {
278 userData.privilege = this.form.privilege;
279 }
280 if (this.$v.form.password.$dirty) {
281 userData.password = this.form.password;
282 }
283 if (Object.entries(userData).length === 1) {
284 this.closeModal();
285 return;
286 }
287 }
288
289 this.$emit('ok', { isNewUser: this.newUser, userData });
290 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800291 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800292 closeModal() {
293 this.$nextTick(() => {
294 this.$refs.modal.hide();
295 });
296 },
297 resetForm() {
298 this.form.originalUsername = '';
299 this.form.status = true;
300 this.form.username = '';
301 this.form.privilege = '';
302 this.form.password = '';
303 this.form.passwordConfirmation = '';
304 this.$v.$reset();
305 },
306 requirePassword() {
307 if (this.newUser) return true;
308 if (this.$v.form.password.$dirty) return true;
309 if (this.$v.form.passwordConfirmation.$dirty) return true;
310 return false;
311 },
312 onOk(bvModalEvt) {
313 // prevent modal close
314 bvModalEvt.preventDefault();
315 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800316 }
317 }
318};
319</script>