290 lines
8.8 KiB
Vue
290 lines
8.8 KiB
Vue
<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>
|