2020-07-17 13:11:14 +02:00
|
|
|
import Vue from 'vue'
|
2020-11-25 18:35:35 +01:00
|
|
|
|
2020-10-12 17:36:47 +02:00
|
|
|
import api from '@/api'
|
2021-04-16 22:10:43 +02:00
|
|
|
import { isEmptyValue } from '@/helpers/commons'
|
2022-02-01 17:21:35 +01:00
|
|
|
import { stratify } from '@/helpers/data/tree'
|
|
|
|
|
|
|
|
|
|
|
|
export function getParentDomain (domain, domains, highest = false) {
|
|
|
|
const method = highest ? 'lastIndexOf' : 'indexOf'
|
|
|
|
let i = domain[method]('.')
|
|
|
|
while (i !== -1) {
|
|
|
|
const dn = domain.slice(i + 1)
|
|
|
|
if (domains.includes(dn)) return dn
|
|
|
|
i = domain[method]('.', i + (highest ? -1 : 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
2020-07-16 16:07:54 +02:00
|
|
|
|
2020-11-25 18:35:35 +01:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
export default {
|
|
|
|
state: () => ({
|
2020-08-02 21:22:58 +02:00
|
|
|
main_domain: undefined,
|
2022-02-01 17:21:35 +01:00
|
|
|
domains: undefined, // Array
|
2022-02-01 22:17:32 +01:00
|
|
|
domains_details: {},
|
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: {
|
2021-04-16 22:10:43 +02:00
|
|
|
'SET_DOMAINS' (state, [domains]) {
|
2020-07-16 16:30:19 +02:00
|
|
|
state.domains = domains
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2022-02-01 22:17:32 +01:00
|
|
|
'SET_DOMAINS_DETAILS' (state, [name, { domains }]) {
|
|
|
|
Vue.set(state.domains_details, name, domains[name])
|
|
|
|
},
|
|
|
|
|
|
|
|
'UPDATE_DOMAINS_DETAILS' (state, payload) {
|
|
|
|
// FIXME use a common function to execute the same code ?
|
|
|
|
this.commit('SET_DOMAINS_DETAILS', payload)
|
|
|
|
},
|
|
|
|
|
|
|
|
'DEL_DOMAINS_DETAILS' (state, [name]) {
|
|
|
|
Vue.delete(state.domains_details, name)
|
|
|
|
if (state.domains) {
|
|
|
|
Vue.delete(state.domains, name)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'ADD_DOMAINS' (state, [{ domain }]) {
|
2020-08-04 22:11:30 +02:00
|
|
|
state.domains.push(domain)
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'DEL_DOMAINS' (state, [domain]) {
|
2020-08-05 19:09:48 +02:00
|
|
|
state.domains.splice(state.domains.indexOf(domain), 1)
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'SET_MAIN_DOMAIN' (state, [response]) {
|
2020-08-02 21:22:58 +02:00
|
|
|
state.main_domain = response.current_main_domain
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'UPDATE_MAIN_DOMAIN' (state, [domain]) {
|
2020-08-05 19:09:48 +02:00
|
|
|
state.main_domain = domain
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'SET_USERS' (state, [users]) {
|
2021-04-17 19:23:20 +02:00
|
|
|
state.users = users || null
|
2020-07-17 13:11:14 +02:00
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'ADD_USERS' (state, [user]) {
|
2020-11-25 18:35:35 +01:00
|
|
|
if (!state.users) 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)
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +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-11-25 18:35:35 +01:00
|
|
|
if (Object.keys(state.users).length === 0) {
|
|
|
|
state.users = null
|
|
|
|
}
|
2020-07-28 00:18:47 +02:00
|
|
|
}
|
2020-08-01 22:35:39 +02:00
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'SET_GROUPS' (state, [groups]) {
|
2020-08-01 22:35:39 +02:00
|
|
|
state.groups = groups
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'ADD_GROUPS' (state, [{ name }]) {
|
2020-08-02 14:23:25 +02:00
|
|
|
if (state.groups !== undefined) {
|
|
|
|
Vue.set(state.groups, name, { members: [], permissions: [] })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:11:49 +02:00
|
|
|
'UPDATE_GROUPS' (state, [data, { groupName }]) {
|
|
|
|
Vue.set(state.groups, groupName, data)
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'DEL_GROUPS' (state, [groupname]) {
|
2020-08-02 15:34:38 +02:00
|
|
|
Vue.delete(state.groups, groupname)
|
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'SET_PERMISSIONS' (state, [permissions]) {
|
2020-08-01 22:35:39 +02:00
|
|
|
state.permissions = permissions
|
2021-04-16 22:11:49 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'UPDATE_PERMISSIONS' (state, [_, { groupName, action, permId }]) {
|
|
|
|
// FIXME hacky way to update the store
|
|
|
|
const permissions = state.groups[groupName].permissions
|
|
|
|
if (action === 'add') {
|
|
|
|
permissions.push(permId)
|
|
|
|
} else if (action === 'remove') {
|
|
|
|
const index = permissions.indexOf(permId)
|
|
|
|
if (index > -1) permissions.splice(index, 1)
|
|
|
|
}
|
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: {
|
2021-04-16 22:10:43 +02:00
|
|
|
'GET' (
|
|
|
|
{ state, commit, rootState },
|
|
|
|
{ uri, param, storeKey = uri, humanKey, noCache, options, ...extraParams }
|
|
|
|
) {
|
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
|
2021-04-16 22:10:43 +02:00
|
|
|
const ignoreCache = !rootState.cache || noCache || false
|
|
|
|
if (currentState !== undefined && !ignoreCache) return currentState
|
2021-04-09 21:43:56 +02:00
|
|
|
return api.fetch('GET', param ? `${uri}/${param}` : uri, null, humanKey, options).then(responseData => {
|
2020-07-29 14:46:16 +02:00
|
|
|
const data = responseData[storeKey] ? responseData[storeKey] : responseData
|
2021-04-16 22:10:43 +02:00
|
|
|
commit(
|
|
|
|
'SET_' + storeKey.toUpperCase(),
|
|
|
|
[param, data, extraParams].filter(item => !isEmptyValue(item))
|
|
|
|
)
|
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
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'POST' ({ state, commit }, { uri, storeKey = uri, data, humanKey, options, ...extraParams }) {
|
2021-04-09 21:43:56 +02:00
|
|
|
return api.fetch('POST', uri, data, humanKey, options).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
|
2021-04-16 22:10:43 +02:00
|
|
|
commit('ADD_' + storeKey.toUpperCase(), [responseData, extraParams].filter(item => !isEmptyValue(item)))
|
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
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'PUT' ({ state, commit }, { uri, param, storeKey = uri, data, humanKey, options, ...extraParams }) {
|
2021-04-09 21:43:56 +02:00
|
|
|
return api.fetch('PUT', param ? `${uri}/${param}` : uri, data, humanKey, options).then(responseData => {
|
2020-07-29 14:46:16 +02:00
|
|
|
const data = responseData[storeKey] ? responseData[storeKey] : responseData
|
2021-04-16 22:10:43 +02:00
|
|
|
commit('UPDATE_' + storeKey.toUpperCase(), [param, data, extraParams].filter(item => !isEmptyValue(item)))
|
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
|
|
|
},
|
|
|
|
|
2021-04-16 22:10:43 +02:00
|
|
|
'DELETE' ({ commit }, { uri, param, storeKey = uri, data, humanKey, options, ...extraParams }) {
|
2021-04-09 21:43:56 +02:00
|
|
|
return api.fetch('DELETE', param ? `${uri}/${param}` : uri, data, humanKey, options).then(() => {
|
2021-04-16 22:10:43 +02:00
|
|
|
commit('DEL_' + storeKey.toUpperCase(), [param, extraParams].filter(item => !isEmptyValue(item)))
|
2020-07-28 00:18:47 +02:00
|
|
|
})
|
2021-01-31 16:16:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'RESET_CACHE_DATA' ({ state }, keys = Object.keys(state)) {
|
|
|
|
for (const key of keys) {
|
|
|
|
if (key === 'users_details') {
|
|
|
|
state[key] = {}
|
|
|
|
} else {
|
|
|
|
state[key] = 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
|
|
|
getters: {
|
2020-12-04 18:26:11 +01:00
|
|
|
users: state => {
|
|
|
|
if (state.users) return Object.values(state.users)
|
|
|
|
return state.users
|
|
|
|
},
|
2020-10-23 18:15:41 +02:00
|
|
|
|
|
|
|
userNames: state => {
|
2020-11-25 18:35:35 +01:00
|
|
|
if (state.users) return Object.keys(state.users)
|
|
|
|
return []
|
2020-10-23 18:15:41 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
user: state => name => state.users_details[name], // not cached
|
2020-10-30 13:58:44 +01:00
|
|
|
|
2020-10-23 18:15:41 +02:00
|
|
|
domains: state => state.domains,
|
|
|
|
|
2022-02-01 17:21:35 +01:00
|
|
|
orderedDomains: state => {
|
|
|
|
if (!state.domains) return
|
|
|
|
|
|
|
|
const splittedDomains = Object.fromEntries(state.domains.map(domain => {
|
|
|
|
// Keep the main part of the domain and the extension together
|
|
|
|
// eg: this.is.an.example.com -> ['example.com', 'an', 'is', 'this']
|
|
|
|
domain = domain.split('.')
|
|
|
|
domain.push(domain.pop() + domain.pop())
|
|
|
|
return [domain, domain.reverse()]
|
|
|
|
}))
|
|
|
|
|
|
|
|
return state.domains.sort((a, b) => splittedDomains[a] > splittedDomains[b])
|
|
|
|
},
|
|
|
|
|
|
|
|
domainsTree: (state, getters) => {
|
|
|
|
// This getter will not return any reactive data, make sure to assign its output
|
|
|
|
// to a component's `data`.
|
|
|
|
// FIXME manage to store the result in the store to allow reactive data (trigger an
|
|
|
|
// action when state.domain change)
|
|
|
|
const domains = getters.orderedDomains
|
|
|
|
if (!domains) return
|
|
|
|
const dataset = domains.map(name => ({
|
|
|
|
// data to build a hierarchy
|
|
|
|
name,
|
|
|
|
parent: getParentDomain(name, domains),
|
|
|
|
// utility data that will be used by `RecursiveListGroup` component
|
|
|
|
to: { name: 'domain-info', params: { name } },
|
|
|
|
opened: true
|
|
|
|
}))
|
|
|
|
return stratify(dataset)
|
|
|
|
},
|
|
|
|
|
|
|
|
domain: state => name => state.domains_details[name],
|
|
|
|
|
|
|
|
highestDomainParentName: (state, getters) => name => {
|
|
|
|
return getParentDomain(name, getters.orderedDomains, true)
|
|
|
|
},
|
|
|
|
|
2020-10-23 18:15:41 +02:00
|
|
|
mainDomain: state => state.main_domain,
|
|
|
|
|
|
|
|
domainsAsChoices: state => {
|
2020-10-06 23:49:52 +02:00
|
|
|
const mainDomain = state.main_domain
|
2020-10-23 18:15:41 +02:00
|
|
|
return state.domains.map(domain => {
|
2020-10-06 23:49:52 +02:00
|
|
|
return { value: domain, text: domain === mainDomain ? domain + ' ★' : domain }
|
|
|
|
})
|
|
|
|
}
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
}
|