Commands on settings
parent
8c019921ba
commit
940505edec
|
@ -6,7 +6,7 @@
|
||||||
<li><router-link v-if="user.admin" to="/users">Go to User Management</router-link></li>
|
<li><router-link v-if="user.admin" to="/users">Go to User Management</router-link></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<form @submit="saveHooks">
|
<form @submit="saveCommands">
|
||||||
<h2>Commands</h2>
|
<h2>Commands</h2>
|
||||||
|
|
||||||
<p class="small">Here you can set commands that are executed in the named events. You write one command
|
<p class="small">Here you can set commands that are executed in the named events. You write one command
|
||||||
|
@ -14,10 +14,10 @@
|
||||||
<code>file</code> will be available with the path of the file.</p>
|
<code>file</code> will be available with the path of the file.</p>
|
||||||
|
|
||||||
<h3>Before Save</h3>
|
<h3>Before Save</h3>
|
||||||
<textarea v-model="beforeSave"></textarea>
|
<textarea v-model.trim="beforeSave"></textarea>
|
||||||
|
|
||||||
<h3>After Save</h3>
|
<h3>After Save</h3>
|
||||||
<textarea v-model="afterSave"></textarea>
|
<textarea v-model.trim="afterSave"></textarea>
|
||||||
|
|
||||||
<p><input type="submit" value="Save"></p>
|
<p><input type="submit" value="Save"></p>
|
||||||
</form>
|
</form>
|
||||||
|
@ -27,6 +27,7 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { mapState, mapMutations } from 'vuex'
|
import { mapState, mapMutations } from 'vuex'
|
||||||
|
import api from '@/utils/api'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'settings',
|
name: 'settings',
|
||||||
|
@ -40,12 +41,29 @@ export default {
|
||||||
...mapState([ 'user' ])
|
...mapState([ 'user' ])
|
||||||
},
|
},
|
||||||
created () {
|
created () {
|
||||||
// TODO: fetch current settings here
|
api.getCommands()
|
||||||
|
.then(commands => {
|
||||||
|
this.beforeSave = commands['before_save'].join('\n')
|
||||||
|
this.afterSave = commands['after_save'].join('\n')
|
||||||
|
})
|
||||||
|
.catch(error => { this.showError(error) })
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapMutations([ 'showSuccess' ]),
|
...mapMutations([ 'showSuccess', 'showError' ]),
|
||||||
saveHooks (event) {
|
saveCommands (event) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
|
let commands = {
|
||||||
|
'before_save': this.beforeSave.split('\n'),
|
||||||
|
'after_save': this.afterSave.split('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commands['before_save'].length === 1 && commands['before_save'][0] === '') commands['before_save'] = []
|
||||||
|
if (commands['after_save'].length === 1 && commands['after_save'][0] === '') commands['after_save'] = []
|
||||||
|
|
||||||
|
api.updateCommands(commands)
|
||||||
|
.then(() => { this.showSuccess('Commands updated!') })
|
||||||
|
.catch(error => { this.showError(error) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -316,6 +316,48 @@ function updateCSS (css) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCommands () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let request = new window.XMLHttpRequest()
|
||||||
|
request.open('GET', `${store.state.baseURL}/api/commands/`, true)
|
||||||
|
request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`)
|
||||||
|
|
||||||
|
request.onload = () => {
|
||||||
|
switch (request.status) {
|
||||||
|
case 200:
|
||||||
|
resolve(JSON.parse(request.responseText))
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
reject(request.responseText)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
request.onerror = (error) => reject(error)
|
||||||
|
request.send()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCommands (commands) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let request = new window.XMLHttpRequest()
|
||||||
|
request.open('PUT', `${store.state.baseURL}/api/commands/`, true)
|
||||||
|
request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`)
|
||||||
|
|
||||||
|
request.onload = () => {
|
||||||
|
switch (request.status) {
|
||||||
|
case 200:
|
||||||
|
resolve()
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
reject(request.responseText)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
request.onerror = (error) => reject(error)
|
||||||
|
request.send(JSON.stringify(commands))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
delete: rm,
|
delete: rm,
|
||||||
fetch,
|
fetch,
|
||||||
|
@ -331,5 +373,7 @@ export default {
|
||||||
updateUser,
|
updateUser,
|
||||||
getUsers,
|
getUsers,
|
||||||
updatePassword,
|
updatePassword,
|
||||||
updateCSS
|
updateCSS,
|
||||||
|
getCommands,
|
||||||
|
updateCommands
|
||||||
}
|
}
|
||||||
|
|
2
http.go
2
http.go
|
@ -127,6 +127,8 @@ func apiHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int,
|
||||||
return resourceHandler(c, w, r)
|
return resourceHandler(c, w, r)
|
||||||
case "users":
|
case "users":
|
||||||
return usersHandler(c, w, r)
|
return usersHandler(c, w, r)
|
||||||
|
case "commands":
|
||||||
|
return commandsHandler(c, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.StatusNotFound, nil
|
return http.StatusNotFound, nil
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package filemanager
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func commandsHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
return commandsGetHandler(c, w, r)
|
||||||
|
case http.MethodPut:
|
||||||
|
return commandsPutHandler(c, w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.StatusMethodNotAllowed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func commandsGetHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
if !c.us.Admin {
|
||||||
|
return http.StatusForbidden, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return renderJSON(w, c.fm.Commands)
|
||||||
|
}
|
||||||
|
|
||||||
|
func commandsPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
if !c.us.Admin {
|
||||||
|
return http.StatusForbidden, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Body == nil {
|
||||||
|
return http.StatusBadGateway, errors.New("Empty request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
var commands map[string][]string
|
||||||
|
|
||||||
|
// Parses the user and checks for error.
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&commands)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest, errors.New("Invalid JSON")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.fm.db.Set("config", "commands", commands); err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.fm.Commands = commands
|
||||||
|
return http.StatusOK, nil
|
||||||
|
}
|
Loading…
Reference in New Issue