audiobookshelf/client/components/modals/AccountModal.vue

349 lines
11 KiB
Vue
Raw Normal View History

2021-08-27 14:01:47 +02:00
<template>
<modals-modal ref="modal" v-model="show" name="account" :width="800" :height="'unset'" :processing="processing">
2021-08-27 14:01:47 +02:00
<template #outer>
<div class="absolute top-0 left-0 p-5 w-2/3 overflow-hidden">
<p class="font-book text-3xl text-white truncate">{{ title }}</p>
</div>
</template>
<form @submit.prevent="submitForm">
<div class="px-4 w-full text-sm py-6 rounded-lg bg-bg shadow-lg border border-black-300">
<div class="w-full p-8">
<div class="flex py-2">
<div class="w-1/2 px-2">
2022-11-08 01:27:17 +01:00
<ui-text-input-with-label v-model="newUser.username" :label="$strings.LabelUsername" />
</div>
<div class="w-1/2 px-2">
2022-11-08 01:27:17 +01:00
<ui-text-input-with-label v-if="!isEditingRoot" v-model="newUser.password" :label="isNew ? $strings.LabelPassword : $strings.LabelChangePassword" type="password" />
</div>
2021-08-27 14:01:47 +02:00
</div>
<div v-show="!isEditingRoot" class="flex py-2">
<div class="px-2 w-52">
2022-11-08 01:27:17 +01:00
<ui-dropdown v-model="newUser.type" :label="$strings.LabelAccountType" :disabled="isEditingRoot" :items="accountTypes" @input="userTypeUpdated" />
2021-08-27 14:01:47 +02:00
</div>
<div class="flex-grow" />
<div class="flex items-center pt-4 px-2">
2022-11-08 01:27:17 +01:00
<p class="px-3 font-semibold" :class="isEditingRoot ? 'text-gray-300' : ''">{{ $strings.LabelEnable }}</p>
<ui-toggle-switch v-model="newUser.isActive" :disabled="isEditingRoot" />
2021-08-27 14:01:47 +02:00
</div>
</div>
<div v-if="!isEditingRoot && newUser.permissions" class="w-full border-t border-b border-black-200 py-2 px-3 mt-4">
2022-11-08 01:27:17 +01:00
<p class="text-lg mb-2 font-semibold">{{ $strings.HeaderPermissions }}</p>
<div class="flex items-center my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsDownload }}</p>
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.download" />
</div>
</div>
<div class="flex items-center my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsUpdate }}</p>
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.update" />
</div>
</div>
<div class="flex items-center my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsDelete }}</p>
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.delete" />
</div>
</div>
<div class="flex items-center my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsUpload }}</p>
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.upload" />
</div>
</div>
<div class="flex items-center my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsAccessExplicitContent }}</p>
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.accessExplicitContent" />
</div>
</div>
<div class="flex items-center my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsAccessAllLibraries }}</p>
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.accessAllLibraries" @input="accessAllLibrariesToggled" />
</div>
</div>
<div v-if="!newUser.permissions.accessAllLibraries" class="my-4">
2022-11-08 01:27:17 +01:00
<ui-multi-select-dropdown v-model="newUser.librariesAccessible" :items="libraryItems" :label="$strings.LabelLibrariesAccessibleToUser" />
</div>
2022-03-20 12:29:08 +01:00
<div class="flex items-cen~ter my-2 max-w-md">
<div class="w-1/2">
2022-11-08 01:27:17 +01:00
<p>{{ $strings.LabelPermissionsAccessAllTags }}</p>
2022-03-20 12:29:08 +01:00
</div>
<div class="w-1/2">
<ui-toggle-switch v-model="newUser.permissions.accessAllTags" @input="accessAllTagsToggled" />
</div>
</div>
<div v-if="!newUser.permissions.accessAllTags" class="my-4">
2022-11-08 01:27:17 +01:00
<ui-multi-select-dropdown v-model="newUser.itemTagsAccessible" :items="itemTags" :label="$strings.LabelTagsAccessibleToUser" />
2022-03-20 12:29:08 +01:00
</div>
</div>
<div class="flex pt-4 px-2">
2022-11-08 01:27:17 +01:00
<ui-btn v-if="isEditingRoot" to="/account">{{ $strings.ButtonChangeRootPassword }}</ui-btn>
2021-08-27 14:01:47 +02:00
<div class="flex-grow" />
2022-11-08 01:27:17 +01:00
<ui-btn color="success" type="submit">{{ $strings.ButtonSubmit }}</ui-btn>
2021-08-27 14:01:47 +02:00
</div>
</div>
</div>
</form>
</modals-modal>
</template>
<script>
export default {
props: {
value: Boolean,
account: {
type: Object,
default: () => null
}
},
data() {
return {
processing: false,
newUser: {},
isNew: true,
2022-03-20 12:29:08 +01:00
tags: [],
loadingTags: false
2021-08-27 14:01:47 +02:00
}
},
watch: {
show: {
handler(newVal) {
if (newVal) {
this.init()
}
}
}
},
computed: {
show: {
get() {
return this.value
},
set(val) {
this.$emit('input', val)
}
},
2022-11-09 00:10:08 +01:00
accountTypes() {
return [
{
text: this.$strings.LabelAccountTypeGuest,
value: 'guest'
},
{
text: this.$strings.LabelAccountTypeUser,
value: 'user'
},
{
text: this.$strings.LabelAccountTypeAdmin,
value: 'admin'
}
]
},
2022-07-19 00:19:16 +02:00
user() {
return this.$store.state.user.user
},
2021-08-27 14:01:47 +02:00
title() {
2022-11-08 01:27:17 +01:00
return this.isNew ? this.$strings.HeaderNewAccount : this.$strings.HeaderUpdateAccount
},
isEditingRoot() {
return this.account && this.account.type === 'root'
},
libraries() {
return this.$store.state.libraries.libraries
},
libraryItems() {
return this.libraries.map((lib) => ({ text: lib.name, value: lib.id }))
2022-03-20 12:29:08 +01:00
},
itemTags() {
return this.tags.map((t) => {
return {
text: t,
value: t
}
})
2021-08-27 14:01:47 +02:00
}
},
methods: {
close() {
// Force close when navigating - used in UsersTable
if (this.$refs.modal) this.$refs.modal.setHide()
},
2022-03-20 12:29:08 +01:00
accessAllTagsToggled(val) {
2022-05-01 22:12:21 +02:00
if (val && this.newUser.itemTagsAccessible.length) {
2022-03-20 12:29:08 +01:00
this.newUser.itemTagsAccessible = []
}
},
fetchAllTags() {
this.loadingTags = true
this.$axios
.$get(`/api/tags`)
.then((res) => {
this.tags = res.tags
2022-03-20 12:29:08 +01:00
this.loadingTags = false
})
.catch((error) => {
console.error('Failed to load tags', error)
this.loadingTags = false
})
},
accessAllLibrariesToggled(val) {
if (!val && !this.newUser.librariesAccessible.length) {
this.newUser.librariesAccessible = this.libraries.map((l) => l.id)
} else if (val && this.newUser.librariesAccessible.length) {
this.newUser.librariesAccessible = []
}
},
2021-08-27 14:01:47 +02:00
submitForm() {
if (!this.newUser.username) {
this.$toast.error('Enter a username')
return
}
if (!this.newUser.permissions.accessAllLibraries && !this.newUser.librariesAccessible.length) {
this.$toast.error('Must select at least one library')
return
}
2022-05-01 22:12:21 +02:00
if (!this.newUser.permissions.accessAllTags && !this.newUser.itemTagsAccessible.length) {
this.$toast.error('Must select at least one tag')
return
}
if (this.isNew) {
this.submitCreateAccount()
} else {
this.submitUpdateAccount()
}
},
submitUpdateAccount() {
var account = { ...this.newUser }
if (!account.password || account.type === 'root') {
delete account.password
}
if (account.type === 'root' && !account.isActive) return
this.processing = true
console.log('Calling update', account)
this.$axios
.$patch(`/api/users/${this.account.id}`, account)
.then((data) => {
this.processing = false
if (data.error) {
2022-11-09 00:10:08 +01:00
this.$toast.error(`${this.$strings.ToastAccountUpdateFailed}: ${data.error}`)
} else {
console.log('Account updated', data.user)
2022-07-19 00:19:16 +02:00
if (data.user.id === this.user.id && data.user.token !== this.user.token) {
console.log('Current user token was updated')
this.$store.commit('user/setUserToken', data.user.token)
}
2022-11-09 00:10:08 +01:00
this.$toast.success(this.$strings.ToastAccountUpdateSuccess)
this.show = false
}
})
.catch((error) => {
this.processing = false
console.error('Failed to update account', error)
var errMsg = error.response ? error.response.data || '' : ''
this.$toast.error(errMsg || 'Failed to update account')
})
},
submitCreateAccount() {
2021-08-27 14:01:47 +02:00
if (!this.newUser.password) {
this.$toast.error('Must have a password, only root user can have an empty password')
return
}
var account = { ...this.newUser }
this.processing = true
this.$axios
.$post('/api/users', account)
.then((data) => {
this.processing = false
if (data.error) {
this.$toast.error(`Failed to create account: ${data.error}`)
} else {
2021-08-27 14:01:47 +02:00
this.$toast.success('New account created')
this.show = false
}
})
.catch((error) => {
this.processing = false
console.error('Failed to create account', error)
var errMsg = error.response ? error.response.data || '' : ''
this.$toast.error(errMsg || 'Failed to create account')
})
2021-08-27 14:01:47 +02:00
},
toggleActive() {
this.newUser.isActive = !this.newUser.isActive
},
userTypeUpdated(type) {
this.newUser.permissions = {
download: type !== 'guest',
update: type === 'admin',
delete: type === 'admin',
2022-03-20 12:29:08 +01:00
upload: type === 'admin',
accessAllLibraries: true,
accessAllTags: true
}
},
2021-08-27 14:01:47 +02:00
init() {
this.fetchAllTags()
2021-08-27 14:01:47 +02:00
this.isNew = !this.account
if (this.account) {
this.newUser = {
username: this.account.username,
password: this.account.password,
type: this.account.type,
isActive: this.account.isActive,
permissions: { ...this.account.permissions },
2022-03-20 12:29:08 +01:00
librariesAccessible: [...(this.account.librariesAccessible || [])],
itemTagsAccessible: [...(this.account.itemTagsAccessible || [])]
2021-08-27 14:01:47 +02:00
}
} else {
this.newUser = {
username: null,
password: null,
type: 'user',
isActive: true,
permissions: {
download: true,
update: false,
delete: false,
upload: false,
2022-03-20 12:29:08 +01:00
accessAllLibraries: true,
accessAllTags: true
},
librariesAccessible: []
2021-08-27 14:01:47 +02:00
}
}
}
},
mounted() {}
}
</script>