ffplayout/frontend/components/ConfigUser.vue

273 lines
9.0 KiB
Vue
Raw Normal View History

2023-01-11 04:54:25 -05:00
<template>
2024-10-14 06:27:55 -04:00
<div class="w-full max-w-[800px] xs:pe-8">
<h2 class="pt-3 text-3xl">{{ t('user.title') }}</h2>
<div v-if="authStore.role === 'GlobalAdmin'" class="flex flex-col xs:flex-row gap-2 w-full mb-5 mt-10">
2024-04-05 12:51:50 -04:00
<div class="grow">
<select v-model="selected" class="select select-bordered w-full max-w-xs" @change="onChange($event)">
2024-06-20 11:08:50 -04:00
<option v-for="item in users" :key="item.username" :value="item.id">{{ item.username }}</option>
2024-04-04 17:28:25 -04:00
</select>
</div>
2024-04-05 12:51:50 -04:00
<div class="flex-none join">
<button class="join-item btn btn-primary" title="Add new User" @click="showUserModal = true">
{{ t('user.add') }}
2024-04-05 12:51:50 -04:00
</button>
<button class="join-item btn btn-primary" title="Delete selected user" @click="deleteUser()">
{{ t('user.delete') }}
2024-04-05 12:51:50 -04:00
</button>
2023-09-26 03:29:46 -04:00
</div>
2024-04-04 17:28:25 -04:00
</div>
<form v-if="configStore.configUser" class="mt-5" @submit.prevent="onSubmitUser">
2024-04-05 12:51:50 -04:00
<label class="form-control w-full max-w-md">
<div class="label">
<span class="label-text">{{ t('user.name') }}</span>
2023-01-11 04:54:25 -05:00
</div>
2024-04-05 12:51:50 -04:00
<input
v-model="configStore.configUser.username"
2024-04-05 12:51:50 -04:00
type="text"
class="input input-bordered w-full !bg-base-100"
disabled
/>
</label>
<label class="form-control w-full max-w-md mt-3">
<div class="label">
<span class="label-text">{{ t('user.mail') }}</span>
2023-01-11 04:54:25 -05:00
</div>
<input v-model="configStore.configUser.mail" type="email" class="input input-bordered w-full" />
2024-04-05 12:51:50 -04:00
</label>
<label class="form-control w-full max-w-md mt-3">
<div class="label">
<span class="label-text">{{ t('user.newPass') }}</span>
2023-01-11 04:54:25 -05:00
</div>
<input v-model="newPass" type="password" class="input input-bordered w-full" />
2024-04-05 12:51:50 -04:00
</label>
<label class="form-control w-full max-w-md mt-3">
<div class="label">
<span class="label-text">{{ t('user.confirmPass') }}</span>
2023-01-11 04:54:25 -05:00
</div>
<input v-model="confirmPass" type="password" class="input input-bordered w-full" />
2024-04-05 12:51:50 -04:00
</label>
<div v-if="authStore.role === 'GlobalAdmin'" class="form-control w-full max-w-md mt-5">
2024-06-20 11:08:50 -04:00
<Multiselect
v-model="configStore.configUser.channel_ids"
:options="configStore.channels"
2024-06-20 11:08:50 -04:00
mode="tags"
:close-on-select="true"
:can-clear="false"
label="name"
value-prop="id"
:classes="multiSelectClasses"
:disabled="configStore.configUser.role_id === 1"
/>
</div>
2024-04-05 12:51:50 -04:00
<div>
<button class="btn btn-primary mt-5" type="submit">{{ t('user.save') }}</button>
2024-04-04 17:28:25 -04:00
</div>
</form>
</div>
2023-09-26 03:29:46 -04:00
2024-04-16 12:13:28 -04:00
<GenericModal :show="showUserModal" title="Add user" :modal-action="addUser">
2024-06-18 02:56:12 -04:00
<div class="w-full max-w-[500px] h-[490px]">
2024-04-07 09:45:26 -04:00
<label class="form-control w-full">
<div class="label">
<span class="label-text">{{ t('user.name') }}</span>
2024-04-07 09:45:26 -04:00
</div>
2024-08-21 06:17:15 -04:00
<input v-model="user.username" name="username" type="text" class="input input-bordered w-full" />
2024-04-07 09:45:26 -04:00
</label>
2024-04-05 12:51:50 -04:00
2024-04-07 09:45:26 -04:00
<label class="form-control w-full mt-3">
<div class="label">
<span class="label-text">{{ t('user.mail') }}</span>
2024-04-07 09:45:26 -04:00
</div>
2024-08-21 06:17:15 -04:00
<input v-model="user.mail" name="mail" type="email" class="input input-bordered w-full" />
2024-04-07 09:45:26 -04:00
</label>
2024-04-05 12:51:50 -04:00
2024-04-07 09:45:26 -04:00
<label class="form-control w-full mt-3">
<div class="label">
<span class="label-text">{{ t('user.password') }}</span>
2024-04-05 12:51:50 -04:00
</div>
<input v-model="user.password" type="password" class="input input-bordered w-full" />
2024-04-07 09:45:26 -04:00
</label>
2024-04-05 12:51:50 -04:00
2024-04-07 09:45:26 -04:00
<label class="form-control w-full mt-3">
<div class="label">
<span class="label-text">{{ t('user.confirmPass') }}</span>
2024-04-05 12:51:50 -04:00
</div>
<input v-model="user.confirm" type="password" class="input input-bordered w-full" />
2024-04-07 09:45:26 -04:00
</label>
2024-06-18 02:56:12 -04:00
<div class="form-control mt-5">
2024-06-19 15:27:13 -04:00
<Multiselect
2024-06-20 11:08:50 -04:00
v-model="user.channel_ids"
:options="configStore.channels"
2024-06-20 11:08:50 -04:00
mode="tags"
:close-on-select="true"
:can-clear="false"
label="name"
value-prop="id"
:classes="multiSelectClasses"
/>
2024-06-18 02:56:12 -04:00
</div>
2024-04-07 09:45:26 -04:00
<div class="form-control mt-3">
2024-04-16 08:07:50 -04:00
<label class="label cursor-pointer w-1/2">
<span class="label-text">{{ t('user.admin') }}</span>
<input v-model.number="user.admin" type="checkbox" class="checkbox" />
2024-04-07 09:45:26 -04:00
</label>
</div>
2023-09-26 03:29:46 -04:00
</div>
2024-04-16 12:13:28 -04:00
</GenericModal>
2023-01-11 04:54:25 -05:00
</template>
<script setup lang="ts">
2024-04-16 08:07:50 -04:00
const { t } = useI18n()
2024-06-19 15:27:13 -04:00
const { multiSelectClasses } = useVariables()
2024-04-16 08:07:50 -04:00
2023-01-11 04:54:25 -05:00
const authStore = useAuth()
const configStore = useConfig()
const indexStore = useIndex()
2024-06-17 09:25:52 -04:00
const selected = ref(null as null | number)
2023-11-23 16:37:35 -05:00
const users = ref([] as User[])
2024-04-05 12:51:50 -04:00
const showUserModal = ref(false)
2023-01-11 04:54:25 -05:00
const newPass = ref('')
const confirmPass = ref('')
2023-09-26 03:29:46 -04:00
const user = ref({
2024-06-17 09:25:52 -04:00
id: 0,
2023-09-26 03:29:46 -04:00
username: '',
mail: '',
password: '',
confirm: '',
admin: false,
channel_ids: [configStore.channels[configStore.i]?.id ?? 1],
2024-06-18 02:56:12 -04:00
role_id: 3,
} as User)
2023-09-26 03:29:46 -04:00
2023-11-23 16:37:35 -05:00
onMounted(() => {
getUsers()
})
async function getUsers() {
fetch('/api/users', {
method: 'GET',
headers: authStore.authHeader,
})
.then((response) => response.json())
.then((data) => {
users.value = data
2024-04-16 08:07:50 -04:00
selected.value = configStore.currentUser
2023-11-23 16:37:35 -05:00
})
}
function onChange(event: any) {
selected.value = event.target.value
getUserConfig()
}
async function getUserConfig() {
2024-06-20 11:08:50 -04:00
await fetch(`/api/user/${selected.value}`, {
2023-11-23 16:37:35 -05:00
method: 'GET',
headers: authStore.authHeader,
})
.then((response) => response.json())
.then((data) => {
configStore.configUser = data
})
}
async function deleteUser() {
2024-06-17 09:25:52 -04:00
if (configStore.configUser.id === configStore.currentUser) {
2024-04-16 08:07:50 -04:00
indexStore.msgAlert('error', t('user.deleteNotPossible'), 2)
2023-11-23 16:37:35 -05:00
} else {
2024-08-21 06:17:15 -04:00
await fetch(`/api/user/${configStore.configUser.id}`, {
2023-11-23 16:37:35 -05:00
method: 'DELETE',
headers: authStore.authHeader,
})
2024-04-04 17:28:25 -04:00
.then(async () => {
2024-04-16 08:07:50 -04:00
indexStore.msgAlert('success', t('user.deleteSuccess'), 2)
2024-04-04 17:28:25 -04:00
await configStore.getUserConfig()
await getUsers()
})
.catch((e) => {
2024-04-16 08:07:50 -04:00
indexStore.msgAlert('error', `${t('user.deleteError')}: ${e}`, 2)
2024-04-04 17:28:25 -04:00
})
2023-11-23 16:37:35 -05:00
}
}
2024-04-07 09:45:26 -04:00
function clearUser() {
2024-06-17 09:25:52 -04:00
user.value.id = 0
2023-09-26 03:29:46 -04:00
user.value.username = ''
user.value.mail = ''
user.value.password = ''
user.value.confirm = ''
2023-11-23 16:37:35 -05:00
user.value.admin = false
2024-06-19 15:27:13 -04:00
user.value.channel_ids = [1]
2024-06-18 02:56:12 -04:00
user.value.role_id = 3
2023-09-26 03:29:46 -04:00
}
2024-04-07 09:45:26 -04:00
async function addUser(add: boolean) {
if (add) {
if (user.value.admin) {
user.value.role_id = 2
2024-06-18 02:56:12 -04:00
} else {
user.value.role_id = 3
2024-04-07 09:45:26 -04:00
}
2024-04-07 09:45:26 -04:00
delete user.value.admin
2024-06-18 02:56:12 -04:00
if (user.value.username && user.value.password && user.value.password === user.value.confirm) {
2024-04-07 09:45:26 -04:00
authStore.inspectToken()
const update = await configStore.addNewUser(user.value)
showUserModal.value = false
2023-09-26 03:29:46 -04:00
2024-04-07 09:45:26 -04:00
if (update.status === 200) {
2024-04-16 08:07:50 -04:00
indexStore.msgAlert('success', t('user.addSuccess'), 2)
2023-09-26 03:29:46 -04:00
2024-04-07 09:45:26 -04:00
await getUsers()
await getUserConfig()
} else {
2024-10-14 06:27:55 -04:00
indexStore.msgAlert('error', t('user.addFailed'), 3)
2024-04-07 09:45:26 -04:00
}
2023-11-23 16:37:35 -05:00
2024-04-07 09:45:26 -04:00
clearUser()
2023-09-26 03:29:46 -04:00
} else {
2024-10-14 06:27:55 -04:00
indexStore.msgAlert('error', t('user.mismatch'), 3)
2023-09-26 03:29:46 -04:00
}
} else {
2024-04-07 09:45:26 -04:00
showUserModal.value = false
clearUser()
2023-09-26 03:29:46 -04:00
}
}
2023-01-11 04:54:25 -05:00
async function onSubmitUser() {
2024-10-14 06:27:55 -04:00
if (newPass.value) {
if (newPass.value === confirmPass.value) {
configStore.configUser.password = newPass.value
} else {
indexStore.msgAlert('error', t('user.mismatch'), 3)
return
}
2023-01-11 04:54:25 -05:00
}
authStore.inspectToken()
const update = await configStore.setUserConfig(configStore.configUser)
if (update.status === 200) {
2024-04-16 08:07:50 -04:00
indexStore.msgAlert('success', t('user.updateSuccess'), 2)
2023-01-11 04:54:25 -05:00
} else {
2024-04-16 08:07:50 -04:00
indexStore.msgAlert('error', t('user.updateFailed'), 2)
2023-01-11 04:54:25 -05:00
}
newPass.value = ''
confirmPass.value = ''
}
</script>