2020-07-17 13:11:14 +02:00
|
|
|
import Vue from 'vue'
|
2020-08-27 18:30:45 +02:00
|
|
|
import api from '@/helpers/api'
|
2020-07-16 16:07:54 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
state: () => ({
|
2020-07-17 13:11:14 +02:00
|
|
|
domains: undefined, // Array
|
2020-08-02 21:22:58 +02:00
|
|
|
main_domain: undefined,
|
2020-07-17 13:11:14 +02:00
|
|
|
users: undefined, // basic user data: Object {username: {data}}
|
2020-08-01 22:35:39 +02:00
|
|
|
users_details: {}, // precise user data: Object {username: {data}}
|
|
|
|
groups: undefined,
|
|
|
|
permissions: undefined
|
2020-07-16 16:07:54 +02:00
|
|
|
}),
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
mutations: {
|
2020-07-16 16:30:19 +02:00
|
|
|
'SET_DOMAINS' (state, domains) {
|
|
|
|
state.domains = domains
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-08-04 22:11:30 +02:00
|
|
|
'ADD_DOMAINS' (state, { domain }) {
|
|
|
|
state.domains.push(domain)
|
|
|
|
},
|
|
|
|
|
2020-08-05 19:09:48 +02:00
|
|
|
'DEL_DOMAINS' (state, domain) {
|
|
|
|
state.domains.splice(state.domains.indexOf(domain), 1)
|
|
|
|
},
|
|
|
|
|
2020-08-02 21:22:58 +02:00
|
|
|
'SET_MAIN_DOMAIN' (state, response) {
|
|
|
|
state.main_domain = response.current_main_domain
|
|
|
|
},
|
|
|
|
|
2020-08-05 19:09:48 +02:00
|
|
|
'UPDATE_MAIN_DOMAIN' (state, domain) {
|
|
|
|
state.main_domain = domain
|
|
|
|
},
|
|
|
|
|
2020-07-16 16:30:19 +02:00
|
|
|
'SET_USERS' (state, users) {
|
2020-07-17 13:11:14 +02:00
|
|
|
state.users = Object.keys(users).length === 0 ? null : users
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-29 14:46:16 +02:00
|
|
|
'ADD_USERS' (state, user) {
|
2020-08-02 14:23:25 +02:00
|
|
|
if (state.users === undefined) state.users = {}
|
2020-07-26 19:42:00 +02:00
|
|
|
Vue.set(state.users, user.username, user)
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-29 14:46:16 +02:00
|
|
|
'SET_USERS_DETAILS' (state, [username, userData]) {
|
2020-07-17 13:11:14 +02:00
|
|
|
Vue.set(state.users_details, username, userData)
|
2020-07-27 20:46:27 +02:00
|
|
|
if (!state.users) return
|
|
|
|
const user = state.users[username]
|
|
|
|
for (const key of ['firstname', 'lastname', 'mail']) {
|
|
|
|
if (user[key] !== userData[key]) {
|
|
|
|
Vue.set(user, key, userData[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Vue.set(user, 'fullname', `${userData.firstname} ${userData.lastname}`)
|
2020-07-28 00:18:47 +02:00
|
|
|
},
|
|
|
|
|
2020-08-05 19:09:48 +02:00
|
|
|
'UPDATE_USERS_DETAILS' (state, payload) {
|
|
|
|
// FIXME use a common function to execute the same code ?
|
|
|
|
this.commit('SET_USERS_DETAILS', payload)
|
|
|
|
},
|
|
|
|
|
2020-07-29 14:46:16 +02:00
|
|
|
'DEL_USERS_DETAILS' (state, username) {
|
2020-07-28 00:18:47 +02:00
|
|
|
Vue.delete(state.users_details, username)
|
|
|
|
if (state.users) {
|
|
|
|
Vue.delete(state.users, username)
|
|
|
|
}
|
2020-08-01 22:35:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'SET_GROUPS' (state, groups) {
|
|
|
|
state.groups = groups
|
|
|
|
},
|
|
|
|
|
2020-08-02 14:23:25 +02:00
|
|
|
'ADD_GROUPS' (state, { name }) {
|
|
|
|
if (state.groups !== undefined) {
|
|
|
|
Vue.set(state.groups, name, { members: [], permissions: [] })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-02 15:34:38 +02:00
|
|
|
'DEL_GROUPS' (state, groupname) {
|
|
|
|
Vue.delete(state.groups, groupname)
|
|
|
|
},
|
|
|
|
|
2020-08-01 22:35:39 +02:00
|
|
|
'SET_PERMISSIONS' (state, permissions) {
|
|
|
|
state.permissions = permissions
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
actions: {
|
2020-09-13 17:35:32 +02:00
|
|
|
'FETCH' ({ state, commit, rootState }, { uri, param, storeKey = uri, cache = rootState.cache }) {
|
2020-07-17 13:11:14 +02:00
|
|
|
const currentState = param ? state[storeKey][param] : state[storeKey]
|
2020-07-16 19:18:01 +02:00
|
|
|
// if data has already been queried, simply return
|
2020-09-13 17:35:32 +02:00
|
|
|
if (currentState !== undefined && cache) return currentState
|
2020-07-26 19:42:00 +02:00
|
|
|
|
2020-07-17 13:11:14 +02:00
|
|
|
return api.get(param ? `${uri}/${param}` : uri).then(responseData => {
|
2020-07-29 14:46:16 +02:00
|
|
|
const data = responseData[storeKey] ? responseData[storeKey] : responseData
|
|
|
|
commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data)
|
2020-08-01 22:35:39 +02:00
|
|
|
return param ? state[storeKey][param] : state[storeKey]
|
2020-07-16 16:07:54 +02:00
|
|
|
})
|
2020-07-26 19:42:00 +02:00
|
|
|
},
|
|
|
|
|
2020-09-13 17:35:32 +02:00
|
|
|
'FETCH_ALL' ({ state, commit, rootState }, queries) {
|
|
|
|
return Promise.all(queries.map(({ uri, param, storeKey = uri, cache = rootState.cache }) => {
|
2020-08-01 22:35:39 +02:00
|
|
|
const currentState = param ? state[storeKey][param] : state[storeKey]
|
2020-08-02 14:23:25 +02:00
|
|
|
// if data has already been queried, simply return the state as cached
|
2020-09-13 17:35:32 +02:00
|
|
|
if (currentState !== undefined && cache) {
|
2020-09-10 19:10:37 +02:00
|
|
|
return { cached: currentState }
|
2020-08-01 22:35:39 +02:00
|
|
|
}
|
|
|
|
return api.get(param ? `${uri}/${param}` : uri).then(responseData => {
|
|
|
|
return { storeKey, param, responseData }
|
|
|
|
})
|
2020-07-29 14:46:16 +02:00
|
|
|
})).then(responsesData => {
|
2020-09-10 19:10:37 +02:00
|
|
|
return responsesData.map(({ storeKey, param, responseData, cached = null }) => {
|
|
|
|
if (cached) return cached
|
2020-07-29 14:46:16 +02:00
|
|
|
const data = responseData[storeKey] ? responseData[storeKey] : responseData
|
|
|
|
commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data)
|
2020-08-01 22:35:39 +02:00
|
|
|
return param ? state[storeKey][param] : state[storeKey]
|
2020-07-29 14:46:16 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
'POST' ({ state, commit }, { uri, data, storeKey = uri }) {
|
|
|
|
return api.post(uri, data).then(responseData => {
|
2020-08-04 22:11:30 +02:00
|
|
|
// FIXME api/domains returns null
|
|
|
|
if (responseData === null) responseData = data
|
|
|
|
responseData = responseData[storeKey] ? responseData[storeKey] : responseData
|
|
|
|
commit('ADD_' + storeKey.toUpperCase(), responseData)
|
2020-08-01 22:35:39 +02:00
|
|
|
return state[storeKey]
|
2020-07-29 14:46:16 +02:00
|
|
|
})
|
2020-07-27 20:46:27 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'PUT' ({ state, commit }, { uri, param, data, storeKey = uri }) {
|
2020-07-28 00:18:47 +02:00
|
|
|
return api.put(param ? `${uri}/${param}` : uri, data).then(responseData => {
|
2020-07-29 14:46:16 +02:00
|
|
|
const data = responseData[storeKey] ? responseData[storeKey] : responseData
|
2020-08-05 19:09:48 +02:00
|
|
|
commit('UPDATE_' + storeKey.toUpperCase(), param ? [param, data] : data)
|
2020-08-01 22:35:39 +02:00
|
|
|
return param ? state[storeKey][param] : state[storeKey]
|
2020-07-27 20:46:27 +02:00
|
|
|
})
|
2020-07-28 00:18:47 +02:00
|
|
|
},
|
|
|
|
|
2020-09-10 19:10:37 +02:00
|
|
|
'DELETE' ({ commit }, { uri, param, data = {}, storeKey = uri }) {
|
2020-07-28 00:18:47 +02:00
|
|
|
return api.delete(param ? `${uri}/${param}` : uri, data).then(() => {
|
2020-07-29 14:46:16 +02:00
|
|
|
commit('DEL_' + storeKey.toUpperCase(), param)
|
2020-07-28 00:18:47 +02:00
|
|
|
})
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
getters: {
|
2020-10-06 23:49:52 +02:00
|
|
|
usersAsOptions: state => {
|
|
|
|
const choices = Object.values(state.users).map(({ username, fullname, mail }) => {
|
|
|
|
return { text: `${fullname} (${mail})`, value: username }
|
|
|
|
})
|
|
|
|
return { choices, value: choices[0].value }
|
|
|
|
},
|
2020-07-16 16:07:54 +02:00
|
|
|
|
2020-10-06 23:49:52 +02:00
|
|
|
domainsAsOptions: state => {
|
|
|
|
const mainDomain = state.main_domain
|
|
|
|
const choices = state.domains.map(domain => {
|
|
|
|
return { value: domain, text: domain === mainDomain ? domain + ' ★' : domain }
|
|
|
|
})
|
|
|
|
return { choices, value: mainDomain }
|
|
|
|
}
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
}
|