feat: code cleanup, new features and v2.0

We're merging this to continue https://github.com/filebrowser/filebrowser/pull/575 and setup translations auto-updating.
This commit is contained in:
Henrique Dias authored and GitHub committed 2019-01-05 16:12:09 +00:00
1 parent 2642333928
commit 39be89780e
155 files changed
+16498 -6582

No files matched your search

+37 -35
View File
@@ -33,9 +33,13 @@ import InternalError from './errors/500'
import Preview from '@/components/files/Preview'
import Listing from '@/components/files/Listing'
import Editor from '@/components/files/Editor'
import * as api from '@/utils/api'
import { files as api } from '@/api'
import { mapGetters, mapState, mapMutations } from 'vuex'
function clean (path) {
return path.endsWith('/') ? path.slice(0, -1) : path
}
export default {
name: 'files',
components: {
@@ -48,7 +52,10 @@ export default {
},
computed: {
...mapGetters([
'selectedCount'
'selectedCount',
'isListing',
'isEditor',
'isFiles'
]),
...mapState([
'req',
@@ -57,14 +64,8 @@ export default {
'multiple',
'loading'
]),
isListing () {
return this.req.kind === 'listing' && !this.loading
},
isPreview () {
return this.req.kind === 'preview' && !this.loading
},
isEditor () {
return this.req.kind === 'editor' && !this.loading
return !this.loading && !this.isListing && !this.isEditor
},
breadcrumbs () {
let parts = this.$route.path.split('/')
@@ -127,7 +128,7 @@ export default {
},
methods: {
...mapMutations([ 'setLoading' ]),
fetchData () {
async fetchData () {
// Reset view information.
this.$store.commit('setReload', false)
this.$store.commit('resetSelected')
@@ -142,21 +143,20 @@ export default {
if (url === '') url = '/'
if (url[0] !== '/') url = '/' + url
api.fetch(url)
.then((req) => {
if (this.$store.state.baseURL + req.url !== window.location.pathname) return
if (!url.endsWith('/') && req.url.endsWith('/')) {
window.history.replaceState(window.history.state, document.title, window.location.pathname + '/')
}
try {
const res = await api.fetch(url)
this.$store.commit('updateRequest', req)
document.title = req.name
this.setLoading(false)
})
.catch(error => {
this.setLoading(false)
this.error = error
})
if (clean(res.path) !== clean(`/${this.$route.params.pathMatch}`)) {
return
}
this.$store.commit('updateRequest', res)
document.title = res.name
} catch (e) {
this.error = e
} finally {
this.setLoading(false)
}
},
keyEvent (event) {
// Esc!
@@ -165,18 +165,18 @@ export default {
// If we're on a listing, unselect all
// files and folders.
if (this.req.kind === 'listing') {
if (this.isListing) {
this.$store.commit('resetSelected')
}
}
// Del!
if (event.keyCode === 46) {
if (this.req.kind === 'editor' ||
this.$route.name !== 'Files' ||
if (this.isEditor ||
!this.isFiles ||
this.loading ||
!this.user.allowEdit ||
(this.req.kind === 'listing' && this.selectedCount === 0)) return
!this.user.perm.delete ||
(this.isListing && this.selectedCount === 0)) return
this.$store.commit('showHover', 'delete')
}
@@ -189,18 +189,20 @@ export default {
// F2!
if (event.keyCode === 113) {
if (this.req.kind === 'editor' ||
this.$route.name !== 'Files' ||
if (this.isEditor ||
!this.isFiles ||
this.loading ||
!this.user.allowEdit ||
(this.req.kind === 'listing' && this.selectedCount === 0) ||
(this.req.kind === 'listing' && this.selectedCount > 1)) return
!this.user.perm.rename ||
(this.isListing && this.selectedCount === 0) ||
(this.isListing && this.selectedCount > 1)) return
this.$store.commit('showHover', 'rename')
}
// CTRL + S
if (event.ctrlKey || event.metaKey) {
if (this.isEditor) return
if (String.fromCharCode(event.which).toLowerCase() === 's') {
event.preventDefault()
@@ -210,7 +212,7 @@ export default {
}
}
},
scroll (event) {
scroll () {
if (this.req.kind !== 'listing' || this.$store.state.user.viewMode === 'mosaic') return
let top = 112 - window.scrollY
+10 -10
View File
@@ -6,25 +6,31 @@
<site-header></site-header>
<sidebar></sidebar>
<main>
<router-view @css="$emit('update:css')"></router-view>
<router-view></router-view>
<shell v-if="isLogged && user.perm.execute" />
</main>
<prompts></prompts>
</div>
</template>
<script>
import Search from '@/components/Search'
import { mapState, mapGetters } from 'vuex'
import Sidebar from '@/components/Sidebar'
import Prompts from '@/components/prompts/Prompts'
import SiteHeader from '@/components/Header'
import Shell from '@/components/Shell'
export default {
name: 'layout',
components: {
Search,
Sidebar,
SiteHeader,
Prompts
Prompts,
Shell
},
computed: {
...mapGetters([ 'isLogged' ]),
...mapState([ 'user' ])
},
watch: {
'$route': function () {
@@ -32,12 +38,6 @@ export default {
this.$store.commit('multiple', false)
if (this.$store.state.show !== 'success') this.$store.commit('closeHovers')
}
},
mounted () {
this.$emit('update:css')
},
beforeDestroy () {
this.$emit('clean:css')
}
}
</script>
+56 -32
View File
@@ -1,49 +1,55 @@
<template>
<div id="login" :class="{ recaptcha: recaptcha.length > 0 }">
<div id="login" :class="{ recaptcha: recaptcha }">
<form @submit="submit">
<img src="../assets/logo.svg" alt="File Browser">
<h1>File Browser</h1>
<div v-if="wrong" class="wrong">{{ $t("login.wrongCredentials") }}</div>
<input type="text" v-model="username" :placeholder="$t('login.username')">
<input type="password" v-model="password" :placeholder="$t('login.password')">
<div v-if="recaptcha.length" id="recaptcha"></div>
<input type="submit" :value="$t('login.submit')">
<img :src="logoURL" alt="File Browser">
<h1>{{ name }}</h1>
<div v-if="error !== ''" class="wrong">{{ error }}</div>
<input class="input input--block" type="text" v-model="username" :placeholder="$t('login.username')">
<input class="input input--block" type="password" v-model="password" :placeholder="$t('login.password')">
<input class="input input--block" v-if="createMode" type="password" v-model="passwordConfirm" :placeholder="$t('login.passwordConfirm')" />
<div v-if="recaptcha" id="recaptcha"></div>
<input class="button button--block" type="submit" :value="createMode ? $t('login.signup') : $t('login.submit')">
<p @click="toggleMode" v-if="signup">{{ createMode ? $t('login.loginInstead') : $t('login.createAnAccount') }}</p>
</form>
</div>
</template>
<script>
import auth from '@/utils/auth'
import { mapState } from 'vuex'
import * as auth from '@/utils/auth'
import { name, logoURL, recaptcha, recaptchaKey, signup } from '@/utils/constants'
export default {
name: 'login',
props: ['dependencies'],
computed: mapState(['recaptcha']),
computed: {
signup: () => signup,
name: () => name,
logoURL: () => logoURL
},
data: function () {
return {
wrong: false,
createMode: false,
error: '',
username: '',
password: ''
password: '',
recaptcha: recaptcha,
passwordConfirm: ''
}
},
mounted () {
if (this.dependencies) this.setup()
},
watch: {
dependencies: function (val) {
if (val) this.setup()
}
if (!recaptcha) return
window.grecaptcha.render('recaptcha', {
sitekey: recaptchaKey
})
},
methods: {
setup () {
if (this.recaptcha.length === 0) return
window.grecaptcha.render('recaptcha', {
sitekey: this.recaptcha
})
toggleMode () {
this.createMode = !this.createMode
},
submit (event) {
async submit (event) {
event.preventDefault()
event.stopPropagation()
@@ -53,18 +59,36 @@ export default {
}
let captcha = ''
if (this.recaptcha.length > 0) {
if (recaptcha) {
captcha = window.grecaptcha.getResponse()
if (captcha === '') {
this.wrong = true
this.error = this.$t('login.wrongCredentials')
return
}
}
auth.login(this.username, this.password, captcha)
.then(() => { this.$router.push({ path: redirect }) })
.catch(() => { this.wrong = true })
if (this.createMode) {
if (this.password !== this.passwordConfirm) {
this.error = this.$t('login.passwordsDontMatch')
return
}
}
try {
if (this.createMode) {
await auth.signup(this.username, this.password)
}
await auth.login(this.username, this.password, captcha)
this.$router.push({ path: redirect })
} catch (e) {
if (e.message == 409) {
this.error = this.$t('login.usernameTaken')
} else {
this.error = this.$t('login.wrongCredentials')
}
}
}
}
}
+2 -2
View File
@@ -1,12 +1,12 @@
<template>
<div class="dashboard">
<ul id="nav" v-if="user.admin">
<ul id="nav" v-if="user.perm.admin">
<li :class="{ active: $route.path === '/settings/profile' }"><router-link to="/settings/profile">{{ $t('settings.profileSettings') }}</router-link></li>
<li :class="{ active: $route.path === '/settings/global' }"><router-link to="/settings/global">{{ $t('settings.globalSettings') }}</router-link></li>
<li :class="{ active: $route.path === '/settings/users' }"><router-link to="/settings/users">{{ $t('settings.userManagement') }}</router-link></li>
</ul>
<router-view @css="$emit('css')"></router-view>
<router-view></router-view>
</div>
</template>
+59
View File
@@ -0,0 +1,59 @@
<template>
<div class="share" v-if="loaded">
<a target="_blank" :href="link">
<div class="share__box">
<div class="share__box__download" v-if="file.isDir">{{ $t('download.downloadFolder') }}</div>
<div class="share__box__download" v-else>{{ $t('download.downloadFile') }}</div>
<div class="share__box__info">
<svg v-if="file.isDir" fill="#40c4ff" height="150" viewBox="0 0 24 24" width="150" xmlns="http://www.w3.org/2000/svg">
<path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>
<svg v-else fill="#40c4ff" height="150" viewBox="0 0 24 24" width="150" xmlns="http://www.w3.org/2000/svg">
<path d="M6 2c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6H6zm7 7V3.5L18.5 9H13z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>
<h1 class="share__box__title">{{ file.name }}</h1>
</div>
</div>
</a>
</div>
</template>
<script>
import { share as api } from '@/api'
import { baseURL } from '@/utils/constants'
export default {
name: 'share',
data: () => ({
loaded: false,
notFound: false,
file: null
}),
watch: {
'$route': 'fetchData'
},
created: function () {
this.fetchData()
},
computed: {
hash: function () {
return this.$route.params.pathMatch
},
link: function () {
return `${baseURL}/api/public/dl/${this.hash}`
},
},
methods: {
fetchData: async function () {
try {
this.file = await api.getHash(this.hash)
this.loaded = true
} catch (e) {
this.notFound = true
}
}
}
}
</script>
+88 -108
View File
@@ -1,99 +1,136 @@
<template>
<div class="dashboard">
<form class="card" v-if="staticGen.length" @submit.prevent="saveStaticGen">
<div class="dashboard" v-if="settings !== null">
<form class="card" @submit.prevent="save">
<div class="card-title">
<h2>{{ capitalize($store.state.staticGen) }}</h2>
<h2>{{ $t('settings.globalSettings') }}</h2>
</div>
<div class="card-content">
<p v-for="field in staticGen" :key="field.variable">
<label v-if="field.type !== 'checkbox'">{{ field.name }}</label>
<input v-if="field.type === 'text'" type="text" v-model.trim="field.value">
<input v-else-if="field.type === 'checkbox'" type="checkbox" v-model.trim="field.value">
<template v-if="field.type === 'checkbox'">{{ capitalize(field.name, 'caps') }}</template>
<p><input type="checkbox" v-model="settings.signup"> {{ $t('settings.allowSignup') }}</p>
<h3>{{ $t('settings.rules') }}</h3>
<p class="small">{{ $t('settings.globalRules') }}</p>
<rules :rules.sync="settings.rules" />
<h3>{{ $t('settings.executeOnShell') }}</h3>
<p class="small">{{ $t('settings.executeOnShellDescription') }}</p>
<input class="input input--block" type="text" placeholder="bash -c, cmd /c, ..." v-model="settings.shell" />
<h3>{{ $t('settings.branding') }}</h3>
<i18n path="settings.brandingHelp" tag="p" class="small">
<a class="link" target="_blank" href="https://docs.filebrowser.xyz/configuration/custom-branding">{{ $t('settings.documentation') }}</a>
</i18n>
<p>
<input type="checkbox" v-model="settings.branding.disableExternal" id="branding-links" />
{{ $t('settings.disableExternalLinks') }}
</p>
<p>
<label for="branding-name">{{ $t('settings.instanceName') }}</label>
<input class="input input--block" type="text" v-model="settings.branding.name" id="branding-name" />
</p>
<p>
<label for="branding-files">{{ $t('settings.brandingDirectoryPath') }}</label>
<input class="input input--block" type="text" v-model="settings.branding.files" id="branding-files" />
</p>
</div>
<div class="card-action">
<input class="flat" type="submit" :value="$t('buttons.update')">
<input class="button button--flat" type="submit" :value="$t('buttons.update')">
</div>
</form>
<form class="card" @submit.prevent="saveCSS">
<form class="card" @submit.prevent="save">
<div class="card-title">
<h2>{{ $t('settings.customStylesheet') }}</h2>
<h2>{{ $t('settings.userDefaults') }}</h2>
</div>
<div class="card-content">
<textarea v-model="css"></textarea>
<p class="small">{{ $t('settings.defaultUserDescription') }}</p>
<user-form :isNew="false" :isDefault="true" :user.sync="settings.defaults" />
</div>
<div class="card-action">
<input class="flat" type="submit" :value="$t('buttons.update')">
<input class="button button--flat" type="submit" :value="$t('buttons.update')">
</div>
</form>
<form class="card" @submit.prevent="saveCommands">
<form class="card" @submit.prevent="save">
<div class="card-title">
<h2>{{ $t('settings.commands') }}</h2>
<h2>{{ $t('settings.commandRunner') }}</h2>
</div>
<div class="card-content">
<p class="small">{{ $t('settings.commandsHelp') }}</p>
<i18n path="settings.commandRunnerHelp" tag="p" class="small">
<code>FILE</code>
<code>SCOPE</code>
<a class="link" target="_blank" href="https://docs.filebrowser.xyz/configuration/command-runner">{{ $t('settings.documentation') }}</a>
</i18n>
<div v-for="command in commands" :key="command.name" class="collapsible">
<div v-for="command in settings.commands" :key="command.name" class="collapsible">
<input :id="command.name" type="checkbox">
<label :for="command.name">
<p>{{ capitalize(command.name) }}</p>
<i class="material-icons">arrow_drop_down</i>
</label>
<div class="collapse">
<textarea v-model.trim="command.value"></textarea>
<textarea class="input input--block input--textarea" v-model.trim="command.value"></textarea>
</div>
</div>
</div>
<div class="card-action">
<input class="flat" type="submit" :value="$t('buttons.update')">
<input class="button button--flat" type="submit" :value="$t('buttons.update')">
</div>
</form>
</div>
</template>
<script>
import { mapState } from 'vuex'
import { getSettings, updateSettings } from '@/utils/api'
import { settings as api } from '@/api'
import UserForm from '@/components/settings/UserForm'
import Rules from '@/components/settings/Rules'
export default {
name: 'settings',
components: {
UserForm,
Rules
},
data: function () {
return {
commands: [],
staticGen: [],
css: ''
originalSettings: null,
settings: null
}
},
computed: {
...mapState([ 'user' ])
},
created () {
getSettings()
.then(settings => {
if (this.$store.state.staticGen.length > 0) {
this.parseStaticGen(settings.staticGen)
}
async created () {
try {
const original = await api.get()
let settings = { ...original, commands: [] }
for (let key in settings.commands) {
this.commands.push({
name: key,
value: settings.commands[key].join('\n')
})
}
for (const key in original.commands) {
settings.commands.push({
name: key,
value: original.commands[key].join('\n')
})
}
this.css = settings.css
})
.catch(this.$showError)
settings.shell = settings.shell.join(' ')
this.originalSettings = original
this.settings = settings
} catch (e) {
this.$showError(e)
}
},
methods: {
capitalize (name, where = '_') {
@@ -107,79 +144,22 @@ export default {
return name.slice(0, -1)
},
saveCommands (event) {
let commands = {}
for (let command of this.commands) {
let value = command.value.split('\n')
if (value.length === 1 && value[0] === '') {
value = []
}
commands[command.name] = value
async save () {
let settings = {
...this.settings,
shell: this.settings.shell.trim().split(' ').filter(s => s !== ''),
commands: {}
}
updateSettings(commands, 'commands')
.then(() => { this.$showSuccess(this.$t('settings.commandsUpdated')) })
.catch(this.$showError)
},
saveCSS (event) {
updateSettings(this.css, 'css')
.then(() => {
this.$showSuccess(this.$t('settings.settingsUpdated'))
this.$store.commit('setCSS', this.css)
this.$emit('css')
})
.catch(this.$showError)
},
saveStaticGen (event) {
let staticGen = {}
for (let field of this.staticGen) {
staticGen[field.variable] = field.value
if (field.original === 'array') {
let val = field.value.split(' ')
if (val[0] === '') {
val.shift()
}
staticGen[field.variable] = val
}
for (const { name, value } of this.settings.commands) {
settings.commands[name] = value.split('\n').filter(cmd => cmd !== '')
}
updateSettings(staticGen, 'staticGen')
.then(() => { this.$showSuccess(this.$t('settings.settingsUpdated')) })
.catch(this.$showError)
},
parseStaticGen (staticgen) {
for (let option of staticgen) {
let value = option.value
let field = {
name: option.name,
variable: option.variable,
type: 'text',
original: 'text',
value: value
}
if (Array.isArray(value)) {
field.original = 'array'
field.value = value.join(' ')
this.staticGen.push(field)
continue
}
switch (typeof value) {
case 'boolean':
field.type = 'checkbox'
field.original = 'boolean'
break
}
this.staticGen.push(field)
try {
await api.update(settings)
this.$showSuccess(this.$t('settings.settingsUpdated'))
} catch (e) {
this.$showError(e)
}
}
}
+29 -35
View File
@@ -7,13 +7,11 @@
<div class="card-content">
<h3>{{ $t('settings.language') }}</h3>
<p><languages id="locale" :selected.sync="locale"></languages></p>
<h3>{{ $t('settings.customStylesheet') }}</h3>
<textarea v-model="css" name="css"></textarea>
<languages class="input input--block" :locale.sync="locale"></languages>
</div>
<div class="card-action">
<input class="flat" type="submit" :value="$t('buttons.update')">
<input class="button button--flat" type="submit" :value="$t('buttons.update')">
</div>
</form>
@@ -23,21 +21,21 @@
</div>
<div class="card-content">
<p><input :class="passwordClass" type="password" :placeholder="$t('settings.newPassword')" v-model="password" name="password"></p>
<p><input :class="passwordClass" type="password" :placeholder="$t('settings.newPasswordConfirm')" v-model="passwordConf" name="password"></p>
<input :class="passwordClass" type="password" :placeholder="$t('settings.newPassword')" v-model="password" name="password">
<input :class="passwordClass" type="password" :placeholder="$t('settings.newPasswordConfirm')" v-model="passwordConf" name="password">
</div>
<div class="card-action">
<input class="flat" type="submit" :value="$t('buttons.update')">
<input class="button button--flat" type="submit" :value="$t('buttons.update')">
</div>
</form>
</div>
</template>
<script>
import { mapState } from 'vuex'
import { updateUser } from '@/utils/api'
import Languages from '@/components/Languages'
import { mapState, mapMutations } from 'vuex'
import { users as api } from '@/api'
import Languages from '@/components/settings/Languages'
export default {
name: 'settings',
@@ -48,61 +46,57 @@ export default {
return {
password: '',
passwordConf: '',
css: '',
locale: ''
}
},
computed: {
...mapState([ 'user' ]),
passwordClass () {
const baseClass = 'input input--block'
if (this.password === '' && this.passwordConf === '') {
return ''
return baseClass
}
if (this.password === this.passwordConf) {
return 'green'
return `${baseClass} input--green`
}
return 'red'
return `${baseClass} input--red`
}
},
created () {
this.css = this.user.css
this.locale = this.user.locale
},
methods: {
updatePassword (event) {
...mapMutations([ 'updateUser' ]),
async updatePassword (event) {
event.preventDefault()
if (this.password !== this.passwordConf) {
if (this.password !== this.passwordConf || this.password === '') {
return
}
let user = {
ID: this.$store.state.user.ID,
password: this.password
}
updateUser(user, 'password').then(location => {
try {
const data = { id: this.user.id, password: this.password }
await api.update(data, ['password'])
this.updateUser(data)
this.$showSuccess(this.$t('settings.passwordUpdated'))
}).catch(e => {
} catch (e) {
this.$showError(e)
})
}
},
updateSettings (event) {
async updateSettings (event) {
event.preventDefault()
let user = {...this.$store.state.user}
user.css = this.css
user.locale = this.locale
updateUser(user, 'partial').then(location => {
this.$store.commit('setUser', user)
this.$emit('css')
try {
const data = { id: this.user.id, locale: this.locale }
await api.update(data, ['locale'])
this.updateUser(data)
this.$showSuccess(this.$t('settings.settingsUpdated'))
}).catch(e => {
} catch (e) {
this.$showError(e)
})
}
}
}
}
+76 -247
View File
@@ -1,72 +1,27 @@
<template>
<div>
<form @submit="save" class="card">
<form v-if="loaded" @submit="save" class="card">
<div class="card-title">
<h2 v-if="id === 0">{{ $t('settings.newUser') }}</h2>
<h2 v-else>{{ $t('settings.user') }} {{ username }}</h2>
<h2 v-if="user.id === 0">{{ $t('settings.newUser') }}</h2>
<h2 v-else>{{ $t('settings.user') }} {{ user.username }}</h2>
</div>
<div class="card-content">
<p>
<label for="username">{{ $t('settings.username') }}</label>
<input type="text" v-model="username" id="username">
</p>
<p>
<label for="password">{{ $t('settings.password') }}</label>
<input type="password" :placeholder="passwordPlaceholder" v-model="password" id="password">
</p>
<p>
<label for="scope">{{ $t('settings.scope') }}</label>
<input type="text" v-model="filesystem" id="scope">
</p>
<p>
<label for="locale">{{ $t('settings.language') }}</label>
<languages id="locale" :selected.sync="locale"></languages>
</p>
<p><input type="checkbox" :disabled="admin" v-model="lockPassword"> {{ $t('settings.lockPassword') }}</p>
<h3>{{ $t('settings.permissions') }}</h3>
<p class="small">{{ $t('settings.permissionsHelp') }}</p>
<p><input type="checkbox" v-model="admin"> {{ $t('settings.administrator') }}</p>
<p><input type="checkbox" :disabled="admin" v-model="allowNew"> {{ $t('settings.allowNew') }}</p>
<p><input type="checkbox" :disabled="admin" v-model="allowEdit"> {{ $t('settings.allowEdit') }}</p>
<p><input type="checkbox" :disabled="admin" v-model="allowCommands"> {{ $t('settings.allowCommands') }}</p>
<p v-show="$store.state.staticGen.length"><input type="checkbox" :disabled="admin" v-model="allowPublish"> {{ $t('settings.allowPublish') }}</p>
<h3>{{ $t('settings.userCommands') }}</h3>
<p class="small">{{ $t('settings.userCommandsHelp') }} <i>git svn hg</i>.</p>
<input type="text" v-model.trim="commands">
<h3>{{ $t('settings.rules') }}</h3>
<p class="small">{{ $t('settings.rulesHelp1') }}</p>
<i18n path="settings.rulesHelp2" tag="p" class="small">
<code>allow</code><code>disallow</code><code>regex</code>
</i18n>
<p class="small"><strong>{{ $t('settings.examples') }}</strong></p>
<ul class="small">
<li><code>disallow regex [\\\/]\..+</code> - {{ $t('settings.ruleExample1') }}</li>
<li><code>disallow /Caddyfile</code> - {{ $t('settings.ruleExample2') }}</li>
</ul>
<textarea v-model.trim="rules"></textarea>
<h3>{{ $t('settings.customStylesheet') }}</h3>
<textarea name="css"></textarea>
<user-form :user.sync="user" :isDefault="false" :isNew="isNew" />
</div>
<div class="card-action">
<button v-if="id !== 0" @click.prevent="deletePrompt" type="button" class="flat delete" :aria-label="$t('buttons.delete')" :title="$t('buttons.delete')">{{ $t('buttons.delete') }}</button>
<input class="flat" type="submit" :value="$t('buttons.save')">
<button
v-if="!isNew"
@click.prevent="deletePrompt"
type="button"
class="button button--flat button--red"
:aria-label="$t('buttons.delete')"
:title="$t('buttons.delete')">{{ $t('buttons.delete') }}</button>
<input
class="button button--flat"
type="submit"
:value="$t('buttons.save')">
</div>
</form>
@@ -76,14 +31,14 @@
</div>
<div class="card-action">
<button class="cancel flat"
<button class="button button--flat button--grey"
@click="closeHovers"
v-focus
:aria-label="$t('buttons.cancel')"
:title="$t('buttons.cancel')">
{{ $t('buttons.cancel') }}
</button>
<button class="flat"
<button class="button button--flat"
@click="deleteUser">
{{ $t('buttons.delete') }}
</button>
@@ -94,225 +49,99 @@
<script>
import { mapMutations } from 'vuex'
import { getUser, newUser, updateUser, deleteUser } from '@/utils/api'
import Languages from '@/components/Languages'
import { users as api, settings } from '@/api'
import UserForm from '@/components/settings/UserForm'
import deepClone from 'lodash.clonedeep'
export default {
name: 'user',
components: { Languages },
components: {
UserForm
},
data: () => {
return {
originalUser: null,
id: 0,
admin: false,
allowNew: false,
allowEdit: false,
allowCommands: false,
allowPublish: false,
lockPassword: false,
permissions: {},
password: '',
username: '',
filesystem: '',
rules: '',
locale: '',
css: '',
commands: ''
}
},
computed: {
passwordPlaceholder () {
if (this.$route.path === '/settings/users/new') return ''
return this.$t('settings.avoidChanges')
user: {},
loaded: false
}
},
created () {
this.fetchData()
},
computed: {
isNew () {
return this.$route.path === '/settings/users/new'
}
},
watch: {
'$route': 'fetchData',
admin: function () {
if (!this.admin) return
this.allowCommands = true
this.allowEdit = true
this.allowNew = true
this.allowPublish = true
this.lockPassword = false
for (let key in this.permissions) {
this.permissions[key] = true
}
'user.perm.admin': function () {
if (!this.user.perm.admin) return
this.user.lockPassword = false
}
},
methods: {
...mapMutations(['closeHovers']),
fetchData () {
let user = this.$route.params[0]
if (this.$route.path === '/settings/users/new') {
user = 'base'
}
getUser(user).then(user => {
this.originalUser = user
this.id = user.ID
this.admin = user.admin
this.allowCommands = user.allowCommands
this.allowNew = user.allowNew
this.allowEdit = user.allowEdit
this.allowPublish = user.allowPublish
this.lockPassword = user.lockPassword
this.filesystem = user.filesystem
this.username = user.username
this.css = user.css
this.permissions = user.permissions
this.locale = user.locale
if (user.commands) {
this.commands = user.commands.join(' ')
...mapMutations([ 'closeHovers', 'showHover', 'setUser' ]),
async fetchData () {
try {
if (this.isNew) {
let { defaults } = await settings.get()
this.user = {
...defaults,
username: '',
passsword: '',
rules: [],
lockPassword: false,
id: 0
}
} else {
const id = this.$route.params.pathMatch
this.user = { ...await api.get(id) }
}
for (let rule of user.rules) {
if (rule.allow) {
this.rules += 'allow '
} else {
this.rules += 'disallow '
}
if (rule.regex) {
this.rules += 'regex ' + rule.regexp.raw
} else {
this.rules += rule.path
}
this.rules += '\n'
}
this.rules = this.rules.trim()
}).catch(() => {
this.loaded = true
} catch (e) {
this.$router.push({ path: '/settings/users/new' })
})
},
capitalize (name) {
let splitted = name.split(/(?=[A-Z])/)
name = ''
for (let i = 0; i < splitted.length; i++) {
name += splitted[i].charAt(0).toUpperCase() + splitted[i].slice(1) + ' '
}
return name.slice(0, -1)
},
reset () {
this.id = 0
this.admin = false
this.allowNew = false
this.allowEdit = false
this.allowPublish = false
this.permissins = {}
this.allowCommands = false
this.lockPassword = false
this.password = ''
this.username = ''
this.filesystem = ''
this.rules = ''
this.locale = ''
this.css = ''
this.commands = ''
deletePrompt () {
this.showHover('deleteUser')
},
deletePrompt (event) {
this.$store.commit('showHover', 'deleteUser')
},
deleteUser (event) {
async deleteUser (event) {
event.preventDefault()
deleteUser(this.id).then(location => {
try {
await api.remove(this.user.id)
this.$router.push({ path: '/settings/users' })
this.$showSuccess(this.$t('settings.userDeleted'))
}).catch(e => {
} catch (e) {
this.$showError(e)
})
}
},
save (event) {
async save (event) {
event.preventDefault()
let user = this.parseForm()
let user = {
...this.originalUser,
...this.user
}
if (this.$route.path === '/settings/users/new') {
newUser(user).then(location => {
this.$router.push({ path: location })
try {
if (this.isNew) {
const loc = await api.create(user)
this.$router.push({ path: loc })
this.$showSuccess(this.$t('settings.userCreated'))
}).catch(e => {
this.$showError(e)
})
return
}
updateUser(user).then(location => {
if (user.ID === this.$store.state.user.ID) {
this.$store.commit('setUser', user)
}
this.$showSuccess(this.$t('settings.userUpdated'))
}).catch(e => {
this.$showError(e)
})
},
parseForm () {
let user = this.originalUser
user.username = this.username
user.password = this.password
user.lockPassword = this.lockPassword
user.filesystem = this.filesystem
user.admin = this.admin
user.allowCommands = this.allowCommands
user.allowNew = this.allowNew
user.allowEdit = this.allowEdit
user.allowPublish = this.allowPublish
user.permissions = this.permissions
user.css = this.css
user.locale = this.locale
user.commands = this.commands.split(' ')
user.rules = []
let rules = this.rules.split('\n')
for (let rawRule of rules) {
let rule = {
allow: true,
path: '',
regex: false,
regexp: {
raw: ''
}
}
rawRule = rawRule.split(' ')
// Skip a malformed rule
if (rawRule.length < 2) {
continue
}
// Skip a malformed rule
if (rawRule[0] !== 'allow' && rawRule[0] !== 'disallow') {
continue
}
rule.allow = (rawRule[0] === 'allow')
rawRule.shift()
if (rawRule[0] === 'regex') {
rule.regex = true
rawRule.shift()
rule.regexp.raw = rawRule.join(' ')
} else {
rule.path = rawRule.join(' ')
await api.update(user)
if (user.id === this.$store.state.user.id) {
this.setUser({ ...deepClone(user) })
}
this.$showSuccess(this.$t('settings.userUpdated'))
}
user.rules.push(rule)
} catch (e) {
this.$showError(e)
}
return user
}
}
}
+11 -11
View File
@@ -2,7 +2,7 @@
<div class="card">
<div class="card-title">
<h2>{{ $t('settings.users') }}</h2>
<router-link to="/settings/users/new"><button class="flat">{{ $t('buttons.new') }}</button></router-link>
<router-link to="/settings/users/new"><button class="button">{{ $t('buttons.new') }}</button></router-link>
</div>
<div class="card-content full">
@@ -16,10 +16,10 @@
<tr v-for="user in users" :key="user.id">
<td>{{ user.username }}</td>
<td><i v-if="user.admin" class="material-icons">done</i><i v-else class="material-icons">close</i></td>
<td>{{ user.filesystem }}</td>
<td><i v-if="user.perm.admin" class="material-icons">done</i><i v-else class="material-icons">close</i></td>
<td>{{ user.scope }}</td>
<td class="small">
<router-link :to="'/settings/users/' + user.ID"><i class="material-icons">mode_edit</i></router-link>
<router-link :to="'/settings/users/' + user.id"><i class="material-icons">mode_edit</i></router-link>
</td>
</tr>
</table>
@@ -28,7 +28,7 @@
</template>
<script>
import * as api from '@/utils/api'
import { users as api } from '@/api'
export default {
name: 'users',
@@ -37,12 +37,12 @@ export default {
users: []
}
},
created () {
api.getUsers().then(users => {
this.users = users
}).catch(error => {
this.$showError(error)
})
async created () {
try {
this.users = await api.getAll()
} catch (e) {
this.$showError(e)
}
}
}
</script>