UsersManagement.vue 14.3 KB
Newer Older
vstanojevic's avatar
vstanojevic committed
1
<template>
vstanojevic's avatar
vstanojevic committed
2
    <div class="kn-page kn-width-full-with-menu">
vstanojevic's avatar
vstanojevic committed
3
4
5
        <div class="kn-page-content p-grid p-m-0">
            <div class="kn-list--column p-col-4 p-sm-4 p-md-3 p-p-0">
                <Toolbar class="kn-toolbar kn-toolbar--primary">
6
                    <template #start>
vstanojevic's avatar
vstanojevic committed
7
8
                        {{ $t('managers.usersManagement.title') }}
                    </template>
9
                    <template #end>
vstanojevic's avatar
vstanojevic committed
10
11
12
13
14
15
16
17
18
                        <KnFabButton icon="fas fa-plus" @click="showForm()" data-test="open-form-button"></KnFabButton>
                    </template>
                </Toolbar>
                <ProgressBar mode="indeterminate" class="kn-progress-bar" v-if="loading" data-test="progress-bar" />
                <div v-if="!loading">
                    <UsersListBox :users="users" :loading="loading" @selectedUser="onUserSelect" @deleteUser="onUserDelete" data-test="users-list"></UsersListBox>
                </div>
            </div>

19
            <KnHint :title="'managers.usersManagement.title'" :hint="'managers.usersManagement.hint'" v-if="hiddenForm"></KnHint>
dbulatovicx32's avatar
dbulatovicx32 committed
20
            <div v-show="!hiddenForm" class="p-col-8 p-sm-8 p-md-9 p-p-0 p-m-0 kn-page">
vstanojevic's avatar
vstanojevic committed
21
                <Toolbar class="kn-toolbar kn-toolbar--secondary">
22
                    <template #start>
vstanojevic's avatar
vstanojevic committed
23
24
                        {{ userDetailsForm.userId }}
                    </template>
25
                    <template #end>
26
27
                        <Button icon="pi pi-save" class="p-button-text p-button-rounded p-button-plain" :disabled="v$.userDetailsForm.$invalid" @click="saveUser" />
                        <Button class="p-button-text p-button-rounded p-button-plain" icon="pi pi-times" @click="closeForm" />
vstanojevic's avatar
vstanojevic committed
28
29
30
                    </template>
                </Toolbar>
                <ProgressBar mode="indeterminate" class="kn-progress-bar" v-if="loading" data-test="progress-bar" />
31
                <div class="kn-page-content">
32
                    <TabView class="tabview-custom kn-tab" ref="usersFormTab">
vstanojevic's avatar
vstanojevic committed
33
34
                        <TabPanel>
                            <template #header>
35
                                <span>{{ $t('managers.usersManagement.detail') }}</span>
vstanojevic's avatar
vstanojevic committed
36
                            </template>
37
                            <DetailFormTab :formInsert="formInsert" :formValues="userDetailsForm" :vobj="v$" :disabledUID="disableUsername" @dataChanged="onDataChange" @unlock="unlockUser($event)"></DetailFormTab>
vstanojevic's avatar
vstanojevic committed
38
39
40
41
                        </TabPanel>

                        <TabPanel>
                            <template #header>
42
                                <span>{{ $t('managers.usersManagement.roles') }}</span>
vstanojevic's avatar
vstanojevic committed
43
44
45
46
47
48
                            </template>
                            <RolesTab :defRole="defaultRole" :rolesList="roles" :selected="selectedRoles" @changed="setSelectedRoles($event)" @setDefaultRole="setDefaultRoleValue($event)"></RolesTab>
                        </TabPanel>

                        <TabPanel>
                            <template #header>
49
                                <span>{{ $t('managers.usersManagement.attributes') }}</span>
vstanojevic's avatar
vstanojevic committed
50
                            </template>
51
                            <UserAttributesForm :attributes="attributes" v-model="attributesForm" @formDirty="onFormDirty"></UserAttributesForm>
vstanojevic's avatar
vstanojevic committed
52
53
54
55
56
57
58
59
60
                        </TabPanel>
                    </TabView>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts">
vstanojevic's avatar
vstanojevic committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { defineComponent } from 'vue'
import { createValidations, ICustomValidatorMap } from '@/helpers/commons/validationHelper'
import { iUser, iRole, iAttribute } from './UsersManagement'
import useValidate from '@vuelidate/core'
import { AxiosResponse } from 'axios'
import TabView from 'primevue/tabview'
import TabPanel from 'primevue/tabpanel'
import KnFabButton from '@/components/UI/KnFabButton.vue'
import KnHint from '@/components/UI/KnHint.vue'
import RolesTab from './UserRolesTab/RolesTab.vue'
import DetailFormTab from './UserDetailTab/DetailFormTab.vue'
import UsersListBox from './UsersListBox.vue'
import UserAttributesForm from './UserAttributesTab/UserAttributesForm.vue'
import detailFormTabValidationDescriptor from './UserDetailTab/DetailFormTabValidationDescriptor.json'
import { sameAs } from '@vuelidate/validators'
vstanojevic's avatar
vstanojevic committed
76

vstanojevic's avatar
vstanojevic committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
export default defineComponent({
    name: 'user-management',
    components: { UsersListBox, TabView, TabPanel, KnFabButton, KnHint, RolesTab, DetailFormTab, UserAttributesForm },
    data() {
        return {
            v$: useValidate() as any,
            apiUrl: process.env.VUE_APP_RESTFUL_SERVICES_PATH + '2.0/',
            users: [] as iUser[],
            roles: [] as iRole[],
            detailFormTabValidationDescriptor: detailFormTabValidationDescriptor,
            attributes: [],
            userDetailsForm: {} as any,
            dirty: false,
            formInsert: true,
            attributesForm: {},
            tempAttributes: {},
            defaultRole: null,
            hiddenForm: true,
            disableUsername: true,
            loading: false,
            selectedRoles: [] as iRole[]
        }
    },
    validations() {
        const customValidators: ICustomValidatorMap = {
            'custom-required': (value) => {
                return !this.formInsert || value
            },
            'custom-sameAs': sameAs(this.userDetailsForm.password)
        }
        const validationObject = {
            userDetailsForm: createValidations('userDetailsForm', detailFormTabValidationDescriptor.validations.userDetailsForm, customValidators)
        }
110

vstanojevic's avatar
vstanojevic committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        return validationObject
    },
    async created() {
        await this.loadAllUsers()
        await this.loadAllRoles()
        await this.loadAllAttributes()
    },
    methods: {
        async loadAllUsers() {
            this.loading = true
            await this.$http
                .get(this.apiUrl + 'users')
                .then((response: AxiosResponse<any>) => {
                    this.users = response.data
                })
                .finally(() => (this.loading = false))
vstanojevic's avatar
vstanojevic committed
127
        },
vstanojevic's avatar
vstanojevic committed
128
129
130
131
132
133
134
135
        async loadAllRoles() {
            this.loading = true
            await this.$http
                .get(this.apiUrl + 'roles')
                .then((response: AxiosResponse<any>) => {
                    this.roles = response.data
                })
                .finally(() => (this.loading = false))
vstanojevic's avatar
vstanojevic committed
136
        },
vstanojevic's avatar
vstanojevic committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        async loadAllAttributes() {
            this.loading = true
            await this.$http
                .get(this.apiUrl + 'attributes')
                .then((response: AxiosResponse<any>) => {
                    this.attributes = response.data
                })
                .finally(() => (this.loading = false))
        },
        setDefaultRoleValue(defaultRole: any) {
            this.defaultRole = defaultRole
            this.dirty = true
        },
        setSelectedRoles(roles: iRole[]) {
            this.selectedRoles = roles
            this.dirty = true
        },
        async showForm() {
            this.tempAttributes = {}
            this.attributesForm = {}
            this.disableUsername = false
            this.hiddenForm = false
            this.selectedRoles = []
            this.userDetailsForm.id = null
            this.userDetailsForm.userId = ''
            this.userDetailsForm.fullName = ''
            this.userDetailsForm.failedLoginAttempts = 0
            this.userDetailsForm.sbiExtUserRoleses = []
            this.userDetailsForm.sbiUserAttributeses = {}
vstanojevic's avatar
vstanojevic committed
166

vstanojevic's avatar
vstanojevic committed
167
168
169
            this.formInsert = true
            this.dirty = false
            this.v$.$reset()
vstanojevic's avatar
vstanojevic committed
170

vstanojevic's avatar
vstanojevic committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
            this.populateForms(this.userDetailsForm)
        },
        formatUserObject() {
            const userToSave = { ...this.userDetailsForm }
            delete userToSave.passwordConfirm
            userToSave['defaultRoleId'] = this.defaultRole
            userToSave['sbiUserAttributeses'] = { ...this.attributesForm }
            userToSave['sbiExtUserRoleses'] = this.selectedRoles ? [...this.selectedRoles.map((selRole) => selRole.id)] : []
            return userToSave
        },
        onFormDirty() {
            this.dirty = true
        },
        saveOrUpdateUser(user: iUser) {
            const endpointPath = `${process.env.VUE_APP_RESTFUL_SERVICES_PATH}2.0/users`
            return this.userDetailsForm.id ? this.$http.put<any>(`${endpointPath}/${user.id}`, user) : this.$http.post<any>(endpointPath, user)
        },
        async saveUser() {
            this.loading = true
            if (!this.selectedRoles || this.selectedRoles.length == 0) {
                this.$store.commit('setError', {
192
                    title: this.userDetailsForm.id ? this.$t('common.toast.updateTitle') : this.$t('managers.usersManagement.info.createTitle'),
vstanojevic's avatar
vstanojevic committed
193
                    msg: this.$t('managers.usersManagement.error.noRolesSelected')
vstanojevic's avatar
vstanojevic committed
194
                })
vstanojevic's avatar
vstanojevic committed
195
196
197
198
199
200
                this.loading = false
            } else {
                const userToSave = this.formatUserObject()
                this.saveOrUpdateUser(userToSave)
                    .then((response: AxiosResponse<any>) => {
                        this.afterSaveOrUpdate(response)
201
202
                    })
                    .catch((error) => {
203
204
205
206
                        this.$store.commit('setError', {
                            title: error.title,
                            msg: error.msg
                        })
207
208
209
210
                    })
                    .finally(() => {
                        this.loading = false
                    })
vstanojevic's avatar
vstanojevic committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
            }
        },
        async afterSaveOrUpdate(response: AxiosResponse<any>) {
            this.dirty = false
            await this.loadAllUsers()
            this.formInsert = false
            const id: number | null = response.data
            const selectedUser = this.users.find((user) => {
                return user.id === id
            })
            if (selectedUser) {
                this.onUserSelect(selectedUser)
            }
            this.$store.commit('setInfo', {
                title: this.userDetailsForm.id ? this.$t('common.toast.updateTitle') : this.$t('managers.usersManagement.info.createTitle'),
                msg: this.userDetailsForm.id ? this.$t('common.toast.updateSuccess') : this.$t('managers.usersManagement.info.createMessage')
            })
        },
        onUserDelete(id: number) {
            this.loading = true
            this.$http
                .delete(`${process.env.VUE_APP_RESTFUL_SERVICES_PATH}2.0/users/${id}`)
                .then(() => {
                    this.loadAllUsers()
                    this.$store.commit('setInfo', {
                        title: this.$t('managers.usersManagement.info.deleteTitle'),
                        msg: this.$t('managers.usersManagement.info.deleteMessage')
vstanojevic's avatar
vstanojevic committed
238
239
                    })
                })
vstanojevic's avatar
vstanojevic committed
240
241
242
243
                .catch((error) => {
                    this.$store.commit('setError', {
                        title: error.title,
                        msg: error.msg
244
                    })
vstanojevic's avatar
vstanojevic committed
245
246
                })
                .finally(() => {
vstanojevic's avatar
vstanojevic committed
247
                    this.hiddenForm = true
vstanojevic's avatar
vstanojevic committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
                    this.loading = false
                })
        },
        async unlockUser() {
            this.userDetailsForm.failedLoginAttempts = 0
            await this.saveUser()
        },
        async onUserSelect(userSelected: any) {
            this.formInsert = false
            if (this.dirty) {
                this.$confirm.require({
                    message: this.$t('common.toast.unsavedChangesMessage'),
                    header: this.$t('common.toast.unsavedChangesHeader'),
                    icon: 'pi pi-exclamation-triangle',
                    accept: () => {
                        this.populateForms(userSelected)
                        this.dirty = false
                    },
                    reject: () => {}
                })
            } else {
                this.populateForms(userSelected)
            }
        },
        populateForms(userObj: any) {
            this.dirty = false
            this.attributesForm = {}
            this.hiddenForm = false
            this.disableUsername = true
            this.defaultRole = userObj.defaultRoleId
            this.selectedRoles = this.getSelectedUserRoles(userObj.sbiExtUserRoleses)
            this.userDetailsForm = { ...userObj }
            this.populateAttributesForm(userObj.sbiUserAttributeses)
        },
        populateAttributesForm(userAttributeValues: any) {
            const tmp = {}
            this.attributes.forEach((attribute: iAttribute) => {
                let obj = {}
                obj[attribute.attributeName] = userAttributeValues && userAttributeValues[attribute.attributeId] ? userAttributeValues[attribute.attributeId][attribute.attributeName] : null
                tmp[attribute.attributeId] = obj
            })
            this.attributesForm = { ...tmp }
        },
        getSelectedUserRoles(userRoles: number[]) {
            return this.roles ? [...this.roles.filter((role) => userRoles && userRoles.find((userRoleId) => role.id === userRoleId))] : []
        },
        closeForm() {
            if (this.dirty) {
                this.$confirm.require({
                    message: this.$t('common.toast.unsavedChangesMessage'),
                    header: this.$t('common.toast.unsavedChangesHeader'),
                    icon: 'pi pi-exclamation-triangle',
                    accept: () => {
                        this.hiddenForm = true
                        this.dirty = false
                    },
                    reject: () => {}
                })
            } else {
                this.hiddenForm = true
vstanojevic's avatar
vstanojevic committed
308
            }
309
310
311
312
        },
        onDataChange() {
            console.log('onDataChange ---------------------')
            this.dirty = true
vstanojevic's avatar
vstanojevic committed
313
        }
vstanojevic's avatar
vstanojevic committed
314
315
    }
})
vstanojevic's avatar
vstanojevic committed
316
317
318
</script>

<style lang="scss" scoped>
vstanojevic's avatar
vstanojevic committed
319
320
321
322
.table-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
vstanojevic's avatar
vstanojevic committed
323

vstanojevic's avatar
vstanojevic committed
324
325
    @media screen and (max-width: 960px) {
        align-items: start;
vstanojevic's avatar
vstanojevic committed
326
    }
vstanojevic's avatar
vstanojevic committed
327
}
vstanojevic's avatar
vstanojevic committed
328

vstanojevic's avatar
vstanojevic committed
329
330
331
332
.record-image {
    width: 50px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}
vstanojevic's avatar
vstanojevic committed
333

vstanojevic's avatar
vstanojevic committed
334
335
336
337
338
.p-dialog .record-image {
    width: 50px;
    margin: 0 auto 2rem auto;
    display: block;
}
vstanojevic's avatar
vstanojevic committed
339

vstanojevic's avatar
vstanojevic committed
340
341
342
343
344
345
346
347
.confirmation-content {
    display: flex;
    align-items: center;
    justify-content: center;
}
@media screen and (max-width: 960px) {
    ::v-deep(.p-toolbar) {
        flex-wrap: wrap;
vstanojevic's avatar
vstanojevic committed
348

vstanojevic's avatar
vstanojevic committed
349
350
        .p-button {
            margin-bottom: 0.25rem;
vstanojevic's avatar
vstanojevic committed
351
352
        }
    }
vstanojevic's avatar
vstanojevic committed
353
}
vstanojevic's avatar
vstanojevic committed
354
</style>