2023-09-22 15:07:47 +02:00
|
|
|
import { defineStore } from 'pinia'
|
2023-09-27 10:44:54 +02:00
|
|
|
import authService from '@/services/authService'
|
2024-02-18 14:58:21 +01:00
|
|
|
import userService from '@/services/userService'
|
2023-09-27 10:44:54 +02:00
|
|
|
import router from '@/router'
|
2023-10-04 09:30:05 +02:00
|
|
|
import { useColorMode } from '@vueuse/core'
|
2023-10-27 15:42:58 +02:00
|
|
|
import { useTwofaccounts } from '@/stores/twofaccounts'
|
|
|
|
import { useGroups } from '@/stores/groups'
|
2023-11-15 17:01:51 +01:00
|
|
|
import { useNotifyStore } from '@/stores/notify'
|
2023-09-22 15:07:47 +02:00
|
|
|
|
|
|
|
export const useUserStore = defineStore({
|
2023-09-27 10:44:54 +02:00
|
|
|
id: 'user',
|
2023-09-22 15:07:47 +02:00
|
|
|
|
2023-09-27 10:44:54 +02:00
|
|
|
state: () => {
|
2023-09-22 15:07:47 +02:00
|
|
|
return {
|
2024-01-29 08:55:56 +01:00
|
|
|
id: undefined,
|
2023-09-27 10:44:54 +02:00
|
|
|
name: undefined,
|
2023-10-05 16:59:53 +02:00
|
|
|
email: undefined,
|
2023-12-09 17:22:24 +01:00
|
|
|
oauth_provider: undefined,
|
2024-09-20 10:37:33 +02:00
|
|
|
authenticated_by_proxy: undefined,
|
2023-09-27 10:44:54 +02:00
|
|
|
preferences: window.defaultPreferences,
|
2023-09-22 15:07:47 +02:00
|
|
|
isAdmin: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-27 10:44:54 +02:00
|
|
|
getters: {
|
|
|
|
isAuthenticated() {
|
|
|
|
return this.name != undefined
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2023-10-27 15:42:58 +02:00
|
|
|
/**
|
|
|
|
* Initializes the store for the given user
|
|
|
|
*
|
|
|
|
* @param {object} user
|
|
|
|
*/
|
2023-11-02 08:13:31 +01:00
|
|
|
async loginAs(user) {
|
2023-10-27 15:42:58 +02:00
|
|
|
this.$patch(user)
|
2023-11-02 08:13:31 +01:00
|
|
|
await this.initDataStores()
|
2023-10-27 15:42:58 +02:00
|
|
|
this.applyUserPrefs()
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the user's data stores
|
|
|
|
*/
|
2023-11-02 08:13:31 +01:00
|
|
|
async initDataStores() {
|
2023-10-27 15:42:58 +02:00
|
|
|
const accounts = useTwofaccounts()
|
|
|
|
const groups = useGroups()
|
|
|
|
|
|
|
|
if (this.isAuthenticated) {
|
2023-11-02 08:13:31 +01:00
|
|
|
await accounts.fetch()
|
2023-11-02 15:10:57 +01:00
|
|
|
groups.fetch()
|
2023-10-27 15:42:58 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
accounts.$reset()
|
|
|
|
groups.$reset()
|
|
|
|
}
|
|
|
|
},
|
2023-09-27 10:44:54 +02:00
|
|
|
|
2023-10-27 15:42:58 +02:00
|
|
|
/**
|
|
|
|
* Logs the user out or moves to proxy logout url
|
|
|
|
*/
|
2023-11-15 17:01:51 +01:00
|
|
|
logout(options = {}) {
|
|
|
|
const { kicked } = options
|
2023-11-30 13:18:35 +01:00
|
|
|
const notify = useNotifyStore()
|
2023-11-15 17:01:51 +01:00
|
|
|
|
2023-09-27 10:44:54 +02:00
|
|
|
// async appLogout(evt) {
|
|
|
|
if (this.$2fauth.config.proxyAuth) {
|
|
|
|
if (this.$2fauth.config.proxyLogoutUrl) {
|
|
|
|
location.assign(this.$2fauth.config.proxyLogoutUrl)
|
|
|
|
}
|
|
|
|
else return false
|
|
|
|
}
|
|
|
|
else {
|
2023-11-30 13:18:35 +01:00
|
|
|
authService.logout({ returnError: true }).then(() => {
|
2023-11-15 17:01:51 +01:00
|
|
|
if (kicked) {
|
|
|
|
notify.clear()
|
|
|
|
notify.warn({ text: trans('auth.autolock_triggered_punchline'), duration:-1 })
|
|
|
|
}
|
2023-11-30 13:18:35 +01:00
|
|
|
this.tossOut()
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
// The logout request will receive a 401 response when the
|
|
|
|
// backend has already detect inactivity on its side. In this case we
|
|
|
|
// don't want any error to be displayed.
|
|
|
|
if (error.response.status !== 401) {
|
|
|
|
notify.error(error)
|
|
|
|
}
|
|
|
|
else this.tossOut()
|
2023-09-27 10:44:54 +02:00
|
|
|
})
|
|
|
|
}
|
2023-09-28 13:23:58 +02:00
|
|
|
},
|
|
|
|
|
2023-10-27 15:42:58 +02:00
|
|
|
/**
|
2023-11-30 13:18:35 +01:00
|
|
|
* Resets all user data and push out
|
2023-10-27 15:42:58 +02:00
|
|
|
*/
|
2023-11-30 13:18:35 +01:00
|
|
|
tossOut() {
|
2023-09-28 13:23:58 +02:00
|
|
|
this.$reset()
|
2023-10-27 15:42:58 +02:00
|
|
|
this.initDataStores()
|
2023-10-04 09:30:05 +02:00
|
|
|
this.applyUserPrefs()
|
2023-09-28 13:23:58 +02:00
|
|
|
router.push({ name: 'login' })
|
2023-10-04 09:30:05 +02:00
|
|
|
},
|
|
|
|
|
2023-10-27 15:42:58 +02:00
|
|
|
/**
|
|
|
|
* Applies user theme
|
|
|
|
*/
|
2023-10-04 09:30:05 +02:00
|
|
|
applyTheme() {
|
|
|
|
const mode = useColorMode({
|
|
|
|
attribute: 'data-theme',
|
|
|
|
})
|
|
|
|
mode.value = this.preferences.theme == 'system' ? 'auto' : this.preferences.theme
|
|
|
|
},
|
|
|
|
|
2023-10-27 15:42:58 +02:00
|
|
|
/**
|
|
|
|
* Applies user language
|
|
|
|
*/
|
2023-10-04 09:30:05 +02:00
|
|
|
applyLanguage() {
|
|
|
|
const { isSupported, language } = useNavigatorLanguage()
|
|
|
|
|
|
|
|
if (isSupported) {
|
|
|
|
loadLanguageAsync(this.preferences.lang == 'browser' ? language.value.slice(0, 2) : this.preferences.lang)
|
|
|
|
}
|
|
|
|
else loadLanguageAsync('en')
|
|
|
|
},
|
|
|
|
|
2023-10-27 15:42:58 +02:00
|
|
|
/**
|
|
|
|
* Applies both user theme & language
|
|
|
|
*/
|
2023-10-04 09:30:05 +02:00
|
|
|
applyUserPrefs() {
|
|
|
|
this.applyTheme()
|
|
|
|
this.applyLanguage()
|
2024-02-18 14:58:21 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh user preferences with backend state
|
|
|
|
*/
|
|
|
|
refreshPreferences() {
|
|
|
|
userService.getPreferences({returnError: true})
|
|
|
|
.then(response => {
|
|
|
|
response.data.forEach(preference => {
|
|
|
|
this.preferences[preference.key] = preference.value
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2024-03-06 08:41:28 +01:00
|
|
|
const notify = useNotifyStore()
|
2024-02-18 14:58:21 +01:00
|
|
|
notify.alert({ text: trans('errors.data_cannot_be_refreshed_from_server') })
|
|
|
|
})
|
2023-09-27 10:44:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2023-09-22 15:07:47 +02:00
|
|
|
})
|