Mercurial > gemma
view client/src/store/usermanagement.js @ 5650:cb3c4ba0f052 DQR adjustments
Merged default branch
author | Sascha Wilde <wilde@sha-bang.de> |
---|---|
date | Thu, 29 Jun 2023 13:18:54 +0200 |
parents | 29af073c824d |
children |
line wrap: on
line source
/* This is Free Software under GNU Affero General Public License v >= 3.0 * without warranty, see README.md and license for details. * * SPDX-License-Identifier: AGPL-3.0-or-later * License-Filename: LICENSES/AGPL-3.0.txt * * Copyright (C) 2018 by via donau * – Österreichische Wasserstraßen-Gesellschaft mbH * Software engineering by Intevation GmbH * * Author(s): * Thomas Junk <thomas.junk@intevation.de> * Markus Kottländer <markus@intevation.de> */ import { HTTP } from "@/lib/http"; // initial state const init = () => { return { users: null, currentUser: null, userDetailsVisible: false }; }; const newUser = () => { return { user: "", email: "", country: null, role: null, isNew: true, password: "", roleLabel: "", active: true }; }; export default { init, namespaced: true, state: init(), getters: { userCountries: state => { if (!state.users) return []; return state.users.reduce((o, n) => { o[n.user] = n.role !== "sys_admin" ? n.country : "global"; return o; }, {}); }, isUserDetailsVisible: state => { return state.userDetailsVisible; }, currentUser: state => { return state.currentUser; }, users: state => { return state.users; }, getUserByName: state => name => { return state.users.find(user => { return user.user === name; }); } }, mutations: { setUserDetailsInvisible: state => { state.userDetailsVisible = false; }, setUserDetailsVisible: state => { state.userDetailsVisible = true; }, usersLoaded: (state, data) => { const resolveLabel = x => { const labels = { waterway_user: "Waterway User", waterway_admin: "Waterway Administrator", sys_admin: "System Admininistrator" }; return labels[x]; }; let users = data.users.map(u => { u["roleLabel"] = resolveLabel(u["role"]); return u; }); state.users = users; }, setCurrentUser: (state, data) => { state.currentUser = data; state.userDetailsVisible = true; }, clearCurrentUser: state => { state.currentUser = newUser(); } }, actions: { deleteUser({ commit }, data) { const { name } = data; return new Promise((resolve, reject) => { HTTP.delete("/users/" + encodeURIComponent(`${name}`), { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("clearCurrentUser"); commit("setUserDetailsInvisible"); resolve(response); }) .catch(error => { reject(error); }); }); }, saveCurrentUser({ commit }, data) { const { path, user } = data; if (user.isNew) { return new Promise((resolve, reject) => { HTTP.post("/users", user, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("setUserDetailsInvisible"); commit("clearCurrentUser"); resolve(response); }) .catch(error => { reject(error); }); }); } else { return new Promise((resolve, reject) => { // Reactivate User if (!user.active) { HTTP.patch( "/users/" + path, { active: true, email: user.email }, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } } ) .then(response => { commit("setUserDetailsInvisible"); commit("clearCurrentUser"); resolve(response); }) .catch(error => { reject(error); }); } else { HTTP.put("/users/" + path, user, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("setUserDetailsInvisible"); commit("clearCurrentUser"); resolve(response); }) .catch(error => { reject(error); }); } }); } }, loadUsers({ commit }) { return new Promise((resolve, reject) => { HTTP.get("/users", { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("usersLoaded", response.data); resolve(response); }) .catch(error => { reject(error); }); }); } } };