blob: d156c3dafc69ad765e42953138e14db28b40bf31 [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">
5 Add user
6 </template>
7 <template v-else>
8 Edit user
9 </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>
15 <b-form-group label="Account status">
16 <b-form-radio
17 v-model="form.status"
18 name="user-status"
19 :value="true"
20 @input="$v.form.status.$touch()"
21 >
22 Enabled
23 </b-form-radio>
24 <b-form-radio
25 v-model="form.status"
26 name="user-status"
27 :value="false"
28 @input="$v.form.status.$touch()"
29 >
30 Disabled
31 </b-form-radio>
32 </b-form-group>
33 <b-form-group label="Username" label-for="username">
34 <b-form-text id="username-help-block">
35 Cannot start with a number
36 <br />
37 No special characters except underscore
38 </b-form-text>
39 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -060040 id="username"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080041 v-model="form.username"
42 type="text"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080043 aria-describedby="username-help-block"
44 :state="getValidationState($v.form.username)"
45 :disabled="!newUser && originalUsername === 'root'"
46 />
47 <b-form-invalid-feedback role="alert">
48 <template v-if="!$v.form.username.required">
49 Field required
50 </template>
51 <template v-else-if="!$v.form.username.maxLength">
52 Length must be between 1 – 16 characters
53 </template>
54 <template v-else-if="!$v.form.username.pattern">
55 Invalid format
56 </template>
57 </b-form-invalid-feedback>
58 </b-form-group>
59 <b-form-group label="Privilege">
60 <b-form-select
61 v-model="form.privilege"
62 :options="privilegeTypes"
63 :state="getValidationState($v.form.privilege)"
64 @input="$v.form.privilege.$touch()"
65 >
66 </b-form-select>
67 <b-form-invalid-feedback role="alert">
68 <template v-if="!$v.form.privilege.required">
69 Field required
70 </template>
71 </b-form-invalid-feedback>
72 </b-form-group>
73 </b-col>
74 <b-col>
75 <b-form-group label="User password" label-for="password">
76 <b-form-text id="password-help-block" text-variant="black">
77 <!-- TODO: Should be dynamic values -->
78 Password must between 8 – 20 characters
79 </b-form-text>
80 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -060081 id="password"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080082 v-model="form.password"
83 type="password"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080084 aria-describedby="password-help-block"
85 :state="getValidationState($v.form.password)"
86 @input="$v.form.password.$touch()"
87 />
88 <b-form-invalid-feedback role="alert">
89 <template v-if="!$v.form.password.required">
90 Field required
91 </template>
92 <template
93 v-if="
94 !$v.form.password.minLength || !$v.form.password.maxLength
95 "
96 >
97 Length must be between 8 – 20 characters
98 </template>
99 </b-form-invalid-feedback>
100 </b-form-group>
101 <b-form-group
102 label="Confirm user password"
103 label-for="password-confirmation"
104 >
105 <b-form-input
Derick Montague09e45cd2020-01-23 15:45:57 -0600106 id="password-confirmation"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800107 v-model="form.passwordConfirmation"
108 type="password"
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800109 :state="getValidationState($v.form.passwordConfirmation)"
110 @input="$v.form.passwordConfirmation.$touch()"
111 />
112 <b-form-invalid-feedback role="alert">
113 <template v-if="!$v.form.passwordConfirmation.required">
114 Field required
115 </template>
116 <template
117 v-else-if="!$v.form.passwordConfirmation.sameAsPassword"
118 >
119 Passwords do not match
120 </template>
121 </b-form-invalid-feedback>
122 </b-form-group>
123 </b-col>
124 </b-row>
125 </b-container>
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800126 </b-form>
127 <template v-slot:modal-ok>
128 <template v-if="newUser">
129 Add user
130 </template>
131 <template v-else>
132 Save
133 </template>
134 </template>
135 </b-modal>
136</template>
137
138<script>
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800139import {
140 required,
141 maxLength,
142 minLength,
143 sameAs,
144 helpers,
145 requiredIf
146} from 'vuelidate/lib/validators';
147import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
148
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800149export default {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800150 mixins: [VuelidateMixin],
Derick Montague09e45cd2020-01-23 15:45:57 -0600151 props: {
152 user: {
153 type: Object,
154 default: null
155 }
156 },
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800157 data() {
158 return {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800159 privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'],
160 originalUsername: '',
161 form: {
162 status: true,
163 username: '',
164 privilege: '',
165 password: '',
166 passwordConfirmation: ''
167 }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800168 };
169 },
170 computed: {
171 newUser() {
172 return this.user ? false : true;
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800173 }
174 },
175 watch: {
176 user: function(value) {
177 if (value === null) return;
178 this.originalUsername = value.username;
179 this.form.username = value.username;
180 this.form.status = value.Enabled;
181 this.form.privilege = value.privilege;
182 }
183 },
184 validations: {
185 form: {
186 status: {
187 required
188 },
189 username: {
190 required,
191 maxLength: maxLength(16),
192 pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/)
193 },
194 privilege: {
195 required
196 },
197 password: {
198 required: requiredIf(function() {
199 return this.requirePassword();
200 }),
201 minLength: minLength(8), //TODO: Update to dynamic backend values
202 maxLength: maxLength(20) //TODO: UPdate to dynamic backend values
203 },
204 passwordConfirmation: {
205 required: requiredIf(function() {
206 return this.requirePassword();
207 }),
208 sameAsPassword: sameAs('password')
209 }
210 }
211 },
212 methods: {
213 handleSubmit() {
214 let userData = {};
215
216 if (this.newUser) {
217 this.$v.$touch();
218 if (this.$v.$invalid) return;
219 userData.username = this.form.username;
220 userData.status = this.form.status;
221 userData.privilege = this.form.privilege;
222 userData.password = this.form.password;
223 } else {
224 if (this.$v.$invalid) return;
225 userData.originalUsername = this.originalUsername;
226 if (this.$v.form.status.$dirty) {
227 userData.status = this.form.status;
228 }
229 if (this.$v.form.username.$dirty) {
230 userData.username = this.form.username;
231 }
232 if (this.$v.form.privilege.$dirty) {
233 userData.privilege = this.form.privilege;
234 }
235 if (this.$v.form.password.$dirty) {
236 userData.password = this.form.password;
237 }
238 if (Object.entries(userData).length === 1) {
239 this.closeModal();
240 return;
241 }
242 }
243
244 this.$emit('ok', { isNewUser: this.newUser, userData });
245 this.closeModal();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800246 },
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -0800247 closeModal() {
248 this.$nextTick(() => {
249 this.$refs.modal.hide();
250 });
251 },
252 resetForm() {
253 this.form.originalUsername = '';
254 this.form.status = true;
255 this.form.username = '';
256 this.form.privilege = '';
257 this.form.password = '';
258 this.form.passwordConfirmation = '';
259 this.$v.$reset();
260 },
261 requirePassword() {
262 if (this.newUser) return true;
263 if (this.$v.form.password.$dirty) return true;
264 if (this.$v.form.passwordConfirmation.$dirty) return true;
265 return false;
266 },
267 onOk(bvModalEvt) {
268 // prevent modal close
269 bvModalEvt.preventDefault();
270 this.handleSubmit();
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800271 }
272 }
273};
274</script>