From 8ed9f33dc0e0026b0a9733433d5633d476f85cc8 Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sat, 30 Oct 2021 23:12:08 +0200 Subject: [PATCH] feat: add moonraker file_manager permissions to store and config files Signed-off-by: Stefan Dej --- src/components/TheEditor.vue | 6 +- .../panels/Machine/ConfigFilesPanel.vue | 109 ++++++++---------- src/plugins/helpers.ts | 1 - src/store/editor/actions.ts | 1 + src/store/editor/index.ts | 1 + src/store/editor/mutations.ts | 11 +- src/store/editor/types.ts | 1 + src/store/farm/printer/index.ts | 3 +- src/store/files/actions.ts | 8 +- src/store/files/getters.ts | 19 +++ src/store/files/mutations.ts | 7 +- src/store/files/types.ts | 3 + 12 files changed, 98 insertions(+), 72 deletions(-) diff --git a/src/components/TheEditor.vue b/src/components/TheEditor.vue index 0960a7b8..73e6d5c1 100644 --- a/src/components/TheEditor.vue +++ b/src/components/TheEditor.vue @@ -122,8 +122,12 @@ export default class TheEditor extends Mixins(BaseMixin) { return this.$store.state.editor.fileroot ?? 'gcodes' } + get permissions(): string { + return this.$store.state.editor.permissions ?? 'r' + } + get isWriteable() { - return ['config', 'gcodes'].includes(this.fileroot) + return this.permissions.includes('w') } get sourcecode() { diff --git a/src/components/panels/Machine/ConfigFilesPanel.vue b/src/components/panels/Machine/ConfigFilesPanel.vue index 36a8efde..c7127c04 100644 --- a/src/components/panels/Machine/ConfigFilesPanel.vue +++ b/src/components/panels/Machine/ConfigFilesPanel.vue @@ -136,21 +136,21 @@ - mdi-file-document-edit-outline {{ isDirWriteable ? $t('Machine.ConfigFilesPanel.EditFile') : $t('Machine.ConfigFilesPanel.ShowFile') }} + mdi-file-document-edit-outline {{ contextMenu.item.permissions.includes('w') ? $t('Machine.ConfigFilesPanel.EditFile') : $t('Machine.ConfigFilesPanel.ShowFile') }} mdi-cloud-download {{ $t('Machine.ConfigFilesPanel.Download') }} - + mdi-rename-box {{ $t('Machine.ConfigFilesPanel.Rename') }} - + mdi-rename-box {{ $t('Machine.ConfigFilesPanel.Rename') }} - + mdi-delete {{ $t('Machine.ConfigFilesPanel.Delete') }} - + mdi-delete {{ $t('Machine.ConfigFilesPanel.Delete') }} @@ -249,10 +249,9 @@ diff --git a/src/plugins/helpers.ts b/src/plugins/helpers.ts index 74e172f7..4f2178f7 100644 --- a/src/plugins/helpers.ts +++ b/src/plugins/helpers.ts @@ -1,4 +1,3 @@ -import { ServerStateEvent } from '@/store/server/types' import {FileStateFile} from '@/store/files/types' import {PrinterStateMacroParams} from '@/store/printer/types' diff --git a/src/store/editor/actions.ts b/src/store/editor/actions.ts index 393052cb..0d744975 100644 --- a/src/store/editor/actions.ts +++ b/src/store/editor/actions.ts @@ -23,6 +23,7 @@ export const actions: ActionTree = { commit('updateLoaderState', true) commit('setFilename', payload.filename) + commit('setPermissions', payload.permissions) axios.get(url, { cancelToken: source.token, diff --git a/src/store/editor/index.ts b/src/store/editor/index.ts index 5dff1e16..67da41a8 100644 --- a/src/store/editor/index.ts +++ b/src/store/editor/index.ts @@ -8,6 +8,7 @@ export const getDefaultState = (): EditorState => { return { bool: false, filename: '', + permissions: '', fileroot: '', filepath: '', sourcecode: '', diff --git a/src/store/editor/mutations.ts b/src/store/editor/mutations.ts index 874fb38a..d4687677 100644 --- a/src/store/editor/mutations.ts +++ b/src/store/editor/mutations.ts @@ -45,6 +45,10 @@ export const mutations: MutationTree = { Vue.set(state, 'filename', filename) }, + setPermissions(state, filename) { + Vue.set(state, 'permissions', filename) + }, + hideEditor(state) { Vue.set(state, 'bool', false) }, @@ -62,11 +66,8 @@ export const mutations: MutationTree = { // the hash took 2 seconds per run, the editor itself is pretty laggy even without hash // calculations. Hash calculations with typical config file sizes (50KB) only take 1 or 2ms // on my machine, so I guess this is acceptable for most use cases. - - if (sha256(payload) != state.loadedHash) - state.changed = true - else - state.changed = false + + state.changed = (sha256(payload) != state.loadedHash) } } diff --git a/src/store/editor/types.ts b/src/store/editor/types.ts index fa2d6f3d..7dedaaf3 100644 --- a/src/store/editor/types.ts +++ b/src/store/editor/types.ts @@ -2,6 +2,7 @@ export interface EditorState { bool: boolean filename: string fileroot: string + permissions: string filepath: string sourcecode: string loaderBool: boolean diff --git a/src/store/farm/printer/index.ts b/src/store/farm/printer/index.ts index 013c766d..8aed3f02 100644 --- a/src/store/farm/printer/index.ts +++ b/src/store/farm/printer/index.ts @@ -31,7 +31,8 @@ export const getDefaultState = (): FarmPrinterState => { current_file: { isDirectory: false, filename: '', - modified: new Date() + modified: new Date(), + permissions: '' }, theme_files: [] } diff --git a/src/store/files/actions.ts b/src/store/files/actions.ts index 5dd76521..684881fa 100644 --- a/src/store/files/actions.ts +++ b/src/store/files/actions.ts @@ -10,6 +10,7 @@ import { import {findDirectory} from '@/plugins/helpers' import {RootState} from '@/store/types' import i18n from '@/plugins/i18n' +import {readOnlyRoots} from '@/store/variables' export const actions: ActionTree = { reset({ commit }) { @@ -19,7 +20,10 @@ export const actions: ActionTree = { initRootDirs({ state, commit }, dirs) { dirs.forEach((dirname: string) => { if (state.filetree.findIndex((tmp: FileStateFile) => tmp.filename === dirname) === -1) { - commit('createRootDir', dirname) + commit('createRootDir', { + name: dirname, + permissions: readOnlyRoots.includes(dirname) ? 'r' : 'rw' + }) Vue.$socket.emit('server.files.get_directory', { path: dirname }, { action: 'files/getDirectory' }) } }) @@ -65,6 +69,7 @@ export const actions: ActionTree = { item: { path: path.length ? path+'/'+dir.dirname : dir.dirname, root: root, + permissions: dir.permissions, modified: dir.modified * 1000 } }) @@ -95,6 +100,7 @@ export const actions: ActionTree = { item: { path: path.length ? path+'/'+file.filename : file.filename, root: root, + permissions: file.permissions, modified: file.modified, size: file.size, } diff --git a/src/store/files/getters.ts b/src/store/files/getters.ts index 0312090e..73b2f6ed 100644 --- a/src/store/files/getters.ts +++ b/src/store/files/getters.ts @@ -6,6 +6,25 @@ import {FileState, FileStateFile} from '@/store/files/types' // eslint-disable-next-line export const getters: GetterTree = { + getDirectory: (state) => (requestedPath) => { + if (requestedPath.startsWith('/')) requestedPath = requestedPath.substr(1) + + const findDirectory = function(filetree: FileStateFile[], pathArray: string[]): FileStateFile | null { + if (pathArray.length) { + const newFiletree = filetree?.childrens?.find((element: FileStateFile) => (element.isDirectory && element.filename === pathArray[0])) + if (newFiletree) { + pathArray.shift() + + return findDirectory(newFiletree, pathArray) + } else return null + } + + return filetree + } + + return findDirectory({ childrens: state.filetree }, requestedPath.split('/')) + }, + getThemeFileUrl: (state, getters, rootState, rootGetters) => (acceptName: string, acceptExtensions: string[]) => { const directory = findDirectory(state.filetree, ['config', themeDir]) diff --git a/src/store/files/mutations.ts b/src/store/files/mutations.ts index 60f8c83f..adc828d2 100644 --- a/src/store/files/mutations.ts +++ b/src/store/files/mutations.ts @@ -13,11 +13,12 @@ export const mutations: MutationTree = { Object.assign(state, getDefaultState()) }, - createRootDir(state, dirname) { + createRootDir(state, payload) { state.filetree.push({ isDirectory: true, - filename: dirname, + filename: payload.name, modified: new Date(), + permissions: payload.permissions, childrens: [], disk_usage: { free: 0, @@ -62,6 +63,7 @@ export const mutations: MutationTree = { isDirectory: false, filename: filename, modified: modified, + permissions: payload.item.permissions, size: payload.item.size, metadataPulled: false, }) @@ -199,6 +201,7 @@ export const mutations: MutationTree = { isDirectory: true, filename: dirname, modified: payload.item.modified ?? new Date(), + permissions: payload.item.permissions, childrens: [], }) } diff --git a/src/store/files/types.ts b/src/store/files/types.ts index e9b969ff..c1d7047d 100644 --- a/src/store/files/types.ts +++ b/src/store/files/types.ts @@ -6,6 +6,7 @@ export interface FileStateFile { isDirectory: boolean filename: string modified: Date + permissions: string childrens?: FileStateFile[] disk_usage?: FileStateDiskUsage print_start_time?: Date | null @@ -52,10 +53,12 @@ export interface ApiGetDirectoryReturnDir { modified: number size: number dirname: string + permissions: string } export interface ApiGetDirectoryReturnFile { modified: number size: number filename: string + permissions: string } \ No newline at end of file