2021-08-27 14:01:47 +02:00
|
|
|
<template>
|
2021-10-23 03:08:02 +02:00
|
|
|
<modals-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">
|
2021-09-06 01:20:29 +02:00
|
|
|
<div class="flex py-2 -mx-2">
|
|
|
|
<div class="w-1/2 px-2">
|
|
|
|
<ui-text-input-with-label v-model="newUser.username" label="Username" class="mx-2" />
|
|
|
|
</div>
|
|
|
|
<div class="w-1/2 px-2">
|
|
|
|
<ui-text-input-with-label v-if="!isEditingRoot" v-model="newUser.password" :label="isNew ? 'Password' : 'Change Password'" type="password" class="mx-2" />
|
|
|
|
</div>
|
2021-08-27 14:01:47 +02:00
|
|
|
</div>
|
|
|
|
<div class="flex py-2">
|
|
|
|
<div class="px-2">
|
2021-09-07 00:42:15 +02:00
|
|
|
<ui-input-dropdown v-model="newUser.type" label="Account Type" :disabled="isEditingRoot" :editable="false" :items="accountTypes" @input="userTypeUpdated" />
|
2021-08-27 14:01:47 +02:00
|
|
|
</div>
|
|
|
|
<div class="flex-grow" />
|
2021-09-06 01:20:29 +02:00
|
|
|
<div v-show="!isEditingRoot" class="flex items-center pt-4 px-2">
|
|
|
|
<p class="px-3 font-semibold" :class="isEditingRoot ? 'text-gray-300' : ''">Is Active</p>
|
|
|
|
<ui-toggle-switch v-model="newUser.isActive" :disabled="isEditingRoot" />
|
2021-08-27 14:01:47 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-09-07 00:42:15 +02:00
|
|
|
|
2021-09-18 19:45:34 +02:00
|
|
|
<div v-if="!isEditingRoot && newUser.permissions" class="w-full border-t border-b border-black-200 py-2 px-3 mt-4">
|
|
|
|
<p class="text-lg mb-2 font-semibold">Permissions</p>
|
|
|
|
<div class="flex items-center my-2 max-w-md">
|
2021-09-07 00:42:15 +02:00
|
|
|
<div class="w-1/2">
|
|
|
|
<p>Can Download</p>
|
|
|
|
</div>
|
|
|
|
<div class="w-1/2">
|
|
|
|
<ui-toggle-switch v-model="newUser.permissions.download" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2021-09-18 19:45:34 +02:00
|
|
|
<div class="flex items-center my-2 max-w-md">
|
2021-09-07 00:42:15 +02:00
|
|
|
<div class="w-1/2">
|
|
|
|
<p>Can Update</p>
|
|
|
|
</div>
|
|
|
|
<div class="w-1/2">
|
|
|
|
<ui-toggle-switch v-model="newUser.permissions.update" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2021-09-18 19:45:34 +02:00
|
|
|
<div class="flex items-center my-2 max-w-md">
|
2021-09-07 00:42:15 +02:00
|
|
|
<div class="w-1/2">
|
|
|
|
<p>Can Delete</p>
|
|
|
|
</div>
|
|
|
|
<div class="w-1/2">
|
|
|
|
<ui-toggle-switch v-model="newUser.permissions.delete" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-09-18 19:45:34 +02:00
|
|
|
|
|
|
|
<div class="flex items-center my-2 max-w-md">
|
|
|
|
<div class="w-1/2">
|
|
|
|
<p>Can Upload</p>
|
|
|
|
</div>
|
|
|
|
<div class="w-1/2">
|
|
|
|
<ui-toggle-switch v-model="newUser.permissions.upload" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-23 03:08:02 +02:00
|
|
|
|
|
|
|
<div class="flex items-center my-2 max-w-md">
|
|
|
|
<div class="w-1/2">
|
|
|
|
<p>Can Access All Libraries</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">
|
|
|
|
<ui-multi-select-dropdown v-model="newUser.librariesAccessible" :items="libraryItems" label="Libraries Accessible to User" />
|
|
|
|
</div>
|
2021-09-07 00:42:15 +02:00
|
|
|
</div>
|
|
|
|
|
2021-08-27 14:01:47 +02:00
|
|
|
<div class="flex pt-4">
|
|
|
|
<div class="flex-grow" />
|
|
|
|
<ui-btn color="success" type="submit">Submit</ui-btn>
|
|
|
|
</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,
|
|
|
|
accountTypes: ['guest', 'user', 'admin']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
show: {
|
|
|
|
handler(newVal) {
|
|
|
|
if (newVal) {
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
show: {
|
|
|
|
get() {
|
|
|
|
return this.value
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this.$emit('input', val)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
title() {
|
2021-09-06 01:20:29 +02:00
|
|
|
return this.isNew ? 'Add New Account' : `Update Account: ${(this.account || {}).username}`
|
|
|
|
},
|
|
|
|
isEditingRoot() {
|
|
|
|
return this.account && this.account.type === 'root'
|
2021-10-23 03:08:02 +02:00
|
|
|
},
|
|
|
|
libraries() {
|
|
|
|
return this.$store.state.libraries.libraries
|
|
|
|
},
|
|
|
|
libraryItems() {
|
|
|
|
return this.libraries.map((lib) => ({ text: lib.name, value: lib.id }))
|
2021-08-27 14:01:47 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2021-10-23 03:08:02 +02:00
|
|
|
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
|
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
if (!this.newUser.permissions.accessAllLibraries && !this.newUser.librariesAccessible.length) {
|
|
|
|
this.$toast.error('Must select at least one library')
|
|
|
|
return
|
|
|
|
}
|
2021-09-06 01:20:29 +02:00
|
|
|
|
|
|
|
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
|
2021-10-23 03:08:02 +02:00
|
|
|
console.log('Calling update', account)
|
2021-09-06 01:20:29 +02:00
|
|
|
this.$axios
|
|
|
|
.$patch(`/api/user/${this.account.id}`, account)
|
|
|
|
.then((data) => {
|
|
|
|
this.processing = false
|
|
|
|
if (data.error) {
|
|
|
|
this.$toast.error(`Failed to update account: ${data.error}`)
|
|
|
|
} else {
|
2021-10-23 03:08:02 +02:00
|
|
|
console.log('Account updated', data.user)
|
2021-09-06 01:20:29 +02:00
|
|
|
this.$toast.success('Account updated')
|
|
|
|
this.show = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.processing = false
|
2021-10-21 01:54:05 +02:00
|
|
|
console.error('Failed to update account', error)
|
|
|
|
var errMsg = error.response ? error.response.data || '' : ''
|
|
|
|
this.$toast.error(errMsg || 'Failed to update account')
|
2021-09-06 01:20:29 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
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
|
2021-09-06 01:20:29 +02:00
|
|
|
this.$axios
|
|
|
|
.$post('/api/user', 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')
|
2021-09-06 01:20:29 +02:00
|
|
|
this.show = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.processing = false
|
2021-10-21 01:54:05 +02:00
|
|
|
console.error('Failed to create account', error)
|
|
|
|
var errMsg = error.response ? error.response.data || '' : ''
|
|
|
|
this.$toast.error(errMsg || 'Failed to create account')
|
2021-09-06 01:20:29 +02:00
|
|
|
})
|
2021-08-27 14:01:47 +02:00
|
|
|
},
|
|
|
|
toggleActive() {
|
|
|
|
this.newUser.isActive = !this.newUser.isActive
|
|
|
|
},
|
2021-09-07 00:42:15 +02:00
|
|
|
userTypeUpdated(type) {
|
|
|
|
this.newUser.permissions = {
|
|
|
|
download: type !== 'guest',
|
|
|
|
update: type === 'admin',
|
2021-09-18 19:45:34 +02:00
|
|
|
delete: type === 'admin',
|
|
|
|
upload: type === 'admin'
|
2021-09-07 00:42:15 +02:00
|
|
|
}
|
|
|
|
},
|
2021-08-27 14:01:47 +02:00
|
|
|
init() {
|
|
|
|
this.isNew = !this.account
|
|
|
|
if (this.account) {
|
2021-10-23 03:08:02 +02:00
|
|
|
var librariesAccessible = this.account.librariesAccessible || []
|
2021-08-27 14:01:47 +02:00
|
|
|
this.newUser = {
|
|
|
|
username: this.account.username,
|
|
|
|
password: this.account.password,
|
|
|
|
type: this.account.type,
|
2021-09-07 00:42:15 +02:00
|
|
|
isActive: this.account.isActive,
|
2021-10-23 03:08:02 +02:00
|
|
|
permissions: { ...this.account.permissions },
|
|
|
|
librariesAccessible: [...librariesAccessible]
|
2021-08-27 14:01:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.newUser = {
|
|
|
|
username: null,
|
|
|
|
password: null,
|
|
|
|
type: 'user',
|
2021-09-07 00:42:15 +02:00
|
|
|
isActive: true,
|
|
|
|
permissions: {
|
|
|
|
download: true,
|
|
|
|
update: false,
|
2021-09-18 19:45:34 +02:00
|
|
|
delete: false,
|
2021-10-23 03:08:02 +02:00
|
|
|
upload: false,
|
|
|
|
accessAllLibraries: true
|
|
|
|
},
|
|
|
|
librariesAccessible: []
|
2021-08-27 14:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {}
|
|
|
|
}
|
|
|
|
</script>
|