🔒 Checks that the user has permission to write to disk before saving config

This commit is contained in:
Alicia Sykes 2021-06-21 13:09:20 +01:00
parent 6d30b54612
commit 46e6c23575
1 changed files with 20 additions and 7 deletions

View File

@ -11,12 +11,12 @@
<span class="save-option-title">Save Location:</span> <span class="save-option-title">Save Location:</span>
<div class="option"> <div class="option">
<input type="radio" id="local" value="local" <input type="radio" id="local" value="local"
v-model="saveMode" class="radio-option" :disabled="!isAdmin" /> v-model="saveMode" class="radio-option" :disabled="!allowWriteToDisk" />
<label for="local" class="save-option-label">Apply Locally</label> <label for="local" class="save-option-label">Apply Locally</label>
</div> </div>
<div class="option"> <div class="option">
<input type="radio" id="file" value="file" v-model="saveMode" class="radio-option" <input type="radio" id="file" value="file" v-model="saveMode" class="radio-option"
:disabled="!isAdmin" /> :disabled="!allowWriteToDisk" />
<label for="file" class="save-option-label">Write Changes to Config File</label> <label for="file" class="save-option-label">Write Changes to Config File</label>
</div> </div>
</div> </div>
@ -80,7 +80,7 @@ export default {
jsonParser: JsonToYaml, jsonParser: JsonToYaml,
responseText: '', responseText: '',
saveSuccess: undefined, saveSuccess: undefined,
isAdmin: isUserAdmin(this.config.appConfig.auth), allowWriteToDisk: this.shouldAllowWriteToDisk(),
}; };
}, },
computed: { computed: {
@ -89,11 +89,15 @@ export default {
}, },
}, },
mounted() { mounted() {
if (!this.isAdmin) this.saveMode = 'local'; if (!this.allowWriteToDisk) this.saveMode = 'local';
}, },
methods: { methods: {
shouldAllowWriteToDisk() {
const { appConfig } = this.config;
return appConfig.allowConfigEdit !== false && isUserAdmin(appConfig.auth);
},
save() { save() {
if (this.saveMode === 'local' || !this.isAdmin) { if (this.saveMode === 'local' || !this.allowWriteToDisk) {
this.saveConfigLocally(); this.saveConfigLocally();
} else if (this.saveMode === 'file') { } else if (this.saveMode === 'file') {
this.writeConfigToDisk(); this.writeConfigToDisk();
@ -114,11 +118,17 @@ export default {
request.then((response) => { request.then((response) => {
this.saveSuccess = response.data.success || false; this.saveSuccess = response.data.success || false;
this.responseText = response.data.message; this.responseText = response.data.message;
if (this.saveSuccess) this.carefullyClearLocalStorage(); if (this.saveSuccess) {
this.carefullyClearLocalStorage();
this.showToast('Config file written to disk succesfully', true);
} else {
this.showToast('An error occurred saving config', false);
}
}) })
.catch((error) => { .catch((error) => {
this.saveSuccess = false; this.saveSuccess = false;
this.responseText = error; this.responseText = error;
this.showToast(error, false);
}); });
}, },
saveConfigLocally() { saveConfigLocally() {
@ -135,7 +145,7 @@ export default {
if (data.appConfig.theme) { if (data.appConfig.theme) {
localStorage.setItem(localStorageKeys.THEME, data.appConfig.theme); localStorage.setItem(localStorageKeys.THEME, data.appConfig.theme);
} }
this.$toasted.show('Changes saved succesfully'); this.showToast('Changes saved succesfully', true);
}, },
carefullyClearLocalStorage() { carefullyClearLocalStorage() {
localStorage.removeItem(localStorageKeys.PAGE_INFO); localStorage.removeItem(localStorageKeys.PAGE_INFO);
@ -168,6 +178,9 @@ export default {
}); });
this.errorMessages = errorMessages; this.errorMessages = errorMessages;
}, },
showToast(message, success) {
this.$toasted.show(message, { className: `toast-${success ? 'success' : 'error'}` });
},
}, },
}; };
</script> </script>