Commands on settings

pull/144/head
Henrique Dias 2017-07-08 20:27:26 +01:00
parent 8c019921ba
commit 940505edec
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
4 changed files with 122 additions and 7 deletions

View File

@ -6,7 +6,7 @@
<li><router-link v-if="user.admin" to="/users">Go to User Management</router-link></li>
</ul>
<form @submit="saveHooks">
<form @submit="saveCommands">
<h2>Commands</h2>
<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>
<h3>Before Save</h3>
<textarea v-model="beforeSave"></textarea>
<textarea v-model.trim="beforeSave"></textarea>
<h3>After Save</h3>
<textarea v-model="afterSave"></textarea>
<textarea v-model.trim="afterSave"></textarea>
<p><input type="submit" value="Save"></p>
</form>
@ -27,6 +27,7 @@
<script>
import { mapState, mapMutations } from 'vuex'
import api from '@/utils/api'
export default {
name: 'settings',
@ -40,12 +41,29 @@ export default {
...mapState([ 'user' ])
},
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: {
...mapMutations([ 'showSuccess' ]),
saveHooks (event) {
...mapMutations([ 'showSuccess', 'showError' ]),
saveCommands (event) {
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) })
}
}
}

View File

@ -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 {
delete: rm,
fetch,
@ -331,5 +373,7 @@ export default {
updateUser,
getUsers,
updatePassword,
updateCSS
updateCSS,
getCommands,
updateCommands
}

View File

@ -127,6 +127,8 @@ func apiHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int,
return resourceHandler(c, w, r)
case "users":
return usersHandler(c, w, r)
case "commands":
return commandsHandler(c, w, r)
}
return http.StatusNotFound, nil

51
settings.go Normal file
View File

@ -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
}