This commit is contained in:
289
frontend/app/components/windows/AccountManagementWindow.vue
Normal file
289
frontend/app/components/windows/AccountManagementWindow.vue
Normal file
@@ -0,0 +1,289 @@
|
||||
<script setup>
|
||||
import { onMounted, ref } from 'vue';
|
||||
|
||||
import WindowHandle from './partials/WindowHandle.vue';
|
||||
import Server from '~/services/Server';
|
||||
import { DisplayToast } from '~/services/Toaster';
|
||||
import { GetUser } from '@/services/User';
|
||||
import { SetupHandle, SetSize, ResetPosition, Top, SetResizable, SetMinSize } from '@/services/Windows';
|
||||
|
||||
const handle = ref(null);
|
||||
const wrapper = ref(null);
|
||||
|
||||
const props = defineProps(['data']);
|
||||
const data = props.data;
|
||||
|
||||
const id = data.id;
|
||||
|
||||
// The admin standing in front of the window: their own row keeps the switches closed,
|
||||
// the server answers the same to anybody who still tries (routes/user.js).
|
||||
const currentUserId = GetUser()?._id;
|
||||
|
||||
const users = ref([]);
|
||||
const busy = ref("");
|
||||
|
||||
const creating = ref(false);
|
||||
const name = ref("");
|
||||
const username = ref("");
|
||||
const email = ref("");
|
||||
const password = ref("");
|
||||
|
||||
onMounted(() => {
|
||||
Top(wrapper);
|
||||
SetupHandle(id, handle);
|
||||
SetSize(id, {width: 620, height: 520});
|
||||
ResetPosition(id, "center");
|
||||
SetResizable(id, true);
|
||||
SetMinSize(id, {width: 460, height: 360});
|
||||
|
||||
LoadUsers();
|
||||
});
|
||||
|
||||
async function LoadUsers(){
|
||||
try {
|
||||
const response = await Server().get('/user/list');
|
||||
if(response.data.status !== 'ok') return;
|
||||
users.value = response.data.users || [];
|
||||
} catch {
|
||||
DisplayToast('red', $t('errors.internal'), 3000);
|
||||
}
|
||||
}
|
||||
|
||||
function ClearForm(){
|
||||
name.value = "";
|
||||
username.value = "";
|
||||
email.value = "";
|
||||
password.value = "";
|
||||
}
|
||||
|
||||
async function CreateAccount(){
|
||||
// The checks the register window makes before it asks the server for an account
|
||||
if(username.value.length < 3){
|
||||
DisplayToast('red', $t('settings.site-administration.accounts.errors.username-empty'), 3000);
|
||||
return;
|
||||
}
|
||||
if(email.value.length < 5 || !email.value.includes('@')){
|
||||
DisplayToast('red', $t('settings.site-administration.accounts.errors.email-empty'), 3000);
|
||||
return;
|
||||
}
|
||||
if(name.value.length == 0){
|
||||
DisplayToast('red', $t('settings.site-administration.accounts.errors.name-empty'), 3000);
|
||||
return;
|
||||
}
|
||||
if(password.value.length == 0){
|
||||
DisplayToast('red', $t('settings.site-administration.accounts.errors.password-empty'), 3000);
|
||||
return;
|
||||
}
|
||||
|
||||
creating.value = true;
|
||||
try {
|
||||
const response = await Server().post('/user/create', {
|
||||
name: name.value,
|
||||
username: username.value,
|
||||
email: email.value,
|
||||
password: password.value
|
||||
});
|
||||
if(response.data.status !== 'ok'){
|
||||
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
||||
return;
|
||||
}
|
||||
users.value.push(response.data.user);
|
||||
ClearForm();
|
||||
DisplayToast('green', $t('settings.site-administration.accounts.created'), 3000);
|
||||
} catch {
|
||||
DisplayToast('red', $t('errors.internal'), 3000);
|
||||
} finally {
|
||||
creating.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function ToggleAdmin(user){
|
||||
busy.value = user._id;
|
||||
try {
|
||||
const response = await Server().post('/user/set-admin', { id: user._id, admin: !user.admin });
|
||||
if(response.data.status !== 'ok'){
|
||||
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
||||
return;
|
||||
}
|
||||
user.admin = response.data.user.admin;
|
||||
DisplayToast('green', $t('settings.site-administration.accounts.updated'), 3000);
|
||||
} catch {
|
||||
DisplayToast('red', $t('errors.internal'), 3000);
|
||||
} finally {
|
||||
busy.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function DeleteAccount(user){
|
||||
busy.value = user._id;
|
||||
try {
|
||||
const response = await Server().post('/user/delete', { id: user._id });
|
||||
if(response.data.status !== 'ok'){
|
||||
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
||||
return;
|
||||
}
|
||||
users.value = users.value.filter((account) => account._id !== user._id);
|
||||
DisplayToast('green', $t('settings.site-administration.accounts.deleted'), 3000);
|
||||
} catch {
|
||||
DisplayToast('red', $t('errors.internal'), 3000);
|
||||
} finally {
|
||||
busy.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
function IsSelf(user){
|
||||
return user._id === currentUserId;
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
<template>
|
||||
<div class="window-wrapper" :id="'window-wrapper-' + id" ref="wrapper">
|
||||
<WindowHandle :window="id" ref="handle"></WindowHandle>
|
||||
|
||||
<!-- Body -->
|
||||
<div class="accounts-body">
|
||||
<div class="accounts-list">
|
||||
<div v-for="user in users" :key="user._id" class="account-row">
|
||||
<div class="account-info">
|
||||
<span class="account-name">{{ user.name }} <span v-if="IsSelf(user)" class="account-self">({{ $t('settings.site-administration.accounts.you') }})</span></span>
|
||||
<span class="account-line">@{{ user.username }}</span>
|
||||
<span class="account-line">{{ user.email }}</span>
|
||||
<span v-if="user.admin" class="account-admin">{{ $t('settings.site-administration.accounts.admin') }}</span>
|
||||
</div>
|
||||
<div class="account-actions" v-if="!IsSelf(user)">
|
||||
<button class="btn-primary button-small sound-click" type="button" :disabled="busy !== ''" v-on:click.prevent="ToggleAdmin(user)">
|
||||
{{ user.admin ? $t('settings.site-administration.accounts.remove-admin') : $t('settings.site-administration.accounts.make-admin') }}
|
||||
</button>
|
||||
<button class="btn-primary button-small danger sound-click" type="button" :disabled="busy !== ''" v-on:click.prevent="DeleteAccount(user)">
|
||||
{{ $t('settings.site-administration.accounts.delete') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form class="new-account" v-on:submit.prevent="CreateAccount">
|
||||
<h3>{{ $t('settings.site-administration.accounts.new-account') }}</h3>
|
||||
<div class="form-field">
|
||||
<label for="account-name-field">{{$t('settings.site-administration.accounts.name')}}</label>
|
||||
<input id="account-name-field" type="text" name="name" v-model="name" autocomplete="off">
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="account-username-field">{{$t('settings.site-administration.accounts.username')}}</label>
|
||||
<input id="account-username-field" type="text" name="username" v-model="username" autocomplete="off">
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="account-email-field">{{$t('settings.site-administration.accounts.email')}}</label>
|
||||
<input id="account-email-field" type="email" name="email" v-model="email" autocomplete="off">
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="account-password-field">{{$t('settings.site-administration.accounts.password')}}</label>
|
||||
<input id="account-password-field" type="password" name="password" v-model="password" autocomplete="off">
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<button class="btn-primary sound-click" type="submit" :disabled="creating">
|
||||
{{ $t('settings.site-administration.accounts.create') }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
||||
<style scoped>
|
||||
.window-wrapper {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.accounts-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.accounts-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.account-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 6px;
|
||||
background-color: var(--color-background-soft);
|
||||
}
|
||||
|
||||
.account-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.account-name {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.account-line {
|
||||
font-size: 13px;
|
||||
opacity: 0.75;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.account-self {
|
||||
font-weight: normal;
|
||||
opacity: 0.75;
|
||||
}
|
||||
|
||||
.account-admin {
|
||||
color: var(--color-green);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.account-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.danger {
|
||||
color: var(--color-red);
|
||||
}
|
||||
|
||||
.new-account {
|
||||
padding: 0 16px 16px 16px;
|
||||
border-top: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.new-account h3 {
|
||||
font-family: MrEavesRemake;
|
||||
}
|
||||
|
||||
.form-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.form-field input {
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
label {
|
||||
text-align: left;
|
||||
}
|
||||
</style>
|
||||
@@ -80,21 +80,15 @@ onMounted(() => {
|
||||
});
|
||||
|
||||
function OpenManageAccounts(){
|
||||
ClearWindow('settings');
|
||||
ClearWindow({ type: 'settings' });
|
||||
CreateWindow('account_management', {
|
||||
type: 'account_management',
|
||||
title: 'settings.site-administration.manage-accounts.title',
|
||||
id: 'account-management',
|
||||
// Back returns to the settings tab this button sits on; CreateWindow leaves
|
||||
// a window that is already open alone.
|
||||
back: () => {
|
||||
ClearWindow('account-management')
|
||||
CreateWindow('settings', {
|
||||
id: 'settings',
|
||||
type: 'settings',
|
||||
title: 'settings.title',
|
||||
back: () => { ClearWindow('settings'); CreateWindow('main_menu'); }
|
||||
});
|
||||
ClearWindow({ type: 'account_management' });
|
||||
CreateWindow('settings');
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
const getLocaleName = (locale) => {
|
||||
|
||||
@@ -34,6 +34,12 @@ const defWindows = {
|
||||
},
|
||||
movable: true
|
||||
},
|
||||
account_management: {
|
||||
title: 'windows.account-management',
|
||||
component: () => import('~/components/windows/AccountManagementWindow.vue'),
|
||||
close: () => ClearWindow({type: 'account_management'}),
|
||||
movable: true
|
||||
},
|
||||
create_campaign: {
|
||||
title: "windows.create-campaign",
|
||||
component: () => import('~/components/windows/CreateCampaignWindow.vue'),
|
||||
|
||||
@@ -8,6 +8,30 @@
|
||||
"language": "Idioma"
|
||||
},
|
||||
"site-administration": {
|
||||
"manage-accounts": "Gestiona els comptes"
|
||||
"manage-accounts": "Gestiona els comptes",
|
||||
"accounts": {
|
||||
"you": "tu",
|
||||
"admin": "Administrador",
|
||||
"make-admin": "Fes-lo administrador",
|
||||
"remove-admin": "Treu-li l'administrador",
|
||||
"delete": "Elimina",
|
||||
"new-account": "Nou compte",
|
||||
"name": "Nom",
|
||||
"username": "Nom d'usuari",
|
||||
"email": "Correu electrònic",
|
||||
"password": "Contrasenya",
|
||||
"create": "Crea el compte",
|
||||
"created": "Compte creat",
|
||||
"updated": "Compte actualitzat",
|
||||
"deleted": "Compte eliminat",
|
||||
"errors": {
|
||||
"name-empty": "Si us plau, introdueix un nom.",
|
||||
"username-empty": "Si us plau, introdueix un nom d'usuari.",
|
||||
"email-empty": "Si us plau, introdueix un correu electrònic vàlid.",
|
||||
"password-empty": "Si us plau, introdueix una contrasenya.",
|
||||
"self-admin": "No pots canviar els teus propis privilegis d'administrador.",
|
||||
"self-delete": "No pots eliminar el teu propi compte."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
"example": "Finestra d'exemple",
|
||||
"edit-profile": "Editar perfil",
|
||||
"settings": "Configuració",
|
||||
"account-management": "Gestió dels comptes",
|
||||
"create-campaign": "Crear campanya",
|
||||
"create-map": "Crear mapa",
|
||||
"map-viewer": "Mapa",
|
||||
|
||||
@@ -8,6 +8,30 @@
|
||||
"language": "Language"
|
||||
},
|
||||
"site-administration": {
|
||||
"manage-accounts": "Manage accounts"
|
||||
"manage-accounts": "Manage accounts",
|
||||
"accounts": {
|
||||
"you": "you",
|
||||
"admin": "Administrator",
|
||||
"make-admin": "Make administrator",
|
||||
"remove-admin": "Remove administrator",
|
||||
"delete": "Delete",
|
||||
"new-account": "New account",
|
||||
"name": "Name",
|
||||
"username": "Username",
|
||||
"email": "Email",
|
||||
"password": "Password",
|
||||
"create": "Create account",
|
||||
"created": "Account created",
|
||||
"updated": "Account updated",
|
||||
"deleted": "Account deleted",
|
||||
"errors": {
|
||||
"name-empty": "Please enter a name.",
|
||||
"username-empty": "Please enter a username.",
|
||||
"email-empty": "Please enter a valid email address.",
|
||||
"password-empty": "Please enter a password.",
|
||||
"self-admin": "You cannot change your own administrator rights.",
|
||||
"self-delete": "You cannot delete your own account."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
"example": "Example Window",
|
||||
"edit-profile": "Edit Profile",
|
||||
"settings": "Settings",
|
||||
"account-management": "Manage accounts",
|
||||
"create-campaign": "Create Campaign",
|
||||
"create-map": "Create Map",
|
||||
"map-viewer": "Map",
|
||||
|
||||
@@ -8,6 +8,30 @@
|
||||
"language": "Idioma"
|
||||
},
|
||||
"site-administration": {
|
||||
"manage-accounts": "Gestionar cuentas"
|
||||
"manage-accounts": "Gestionar cuentas",
|
||||
"accounts": {
|
||||
"you": "tú",
|
||||
"admin": "Administrador",
|
||||
"make-admin": "Nombrar administrador",
|
||||
"remove-admin": "Quitar administrador",
|
||||
"delete": "Eliminar",
|
||||
"new-account": "Nueva cuenta",
|
||||
"name": "Nombre",
|
||||
"username": "Nombre de usuario",
|
||||
"email": "Correo electrónico",
|
||||
"password": "Contraseña",
|
||||
"create": "Crear cuenta",
|
||||
"created": "Cuenta creada",
|
||||
"updated": "Cuenta actualizada",
|
||||
"deleted": "Cuenta eliminada",
|
||||
"errors": {
|
||||
"name-empty": "Por favor, introduce un nombre.",
|
||||
"username-empty": "Por favor, introduce un nombre de usuario.",
|
||||
"email-empty": "Por favor, introduce un correo electrónico válido.",
|
||||
"password-empty": "Por favor, introduce una contraseña.",
|
||||
"self-admin": "No puedes cambiar tus propios privilegios de administrador.",
|
||||
"self-delete": "No puedes eliminar tu propia cuenta."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
"example": "Ventana de ejemplo",
|
||||
"edit-profile": "Editar perfil",
|
||||
"settings": "Configuración",
|
||||
"account-management": "Gestionar cuentas",
|
||||
"create-campaign": "Crear campanya",
|
||||
"create-map": "Crear mapa",
|
||||
"map-viewer": "Mapa",
|
||||
|
||||
Reference in New Issue
Block a user