From 0e22b669c9125bb26fe900db81e2f88938b33a87 Mon Sep 17 00:00:00 2001 From: Alex Crivion Date: Sun, 23 Feb 2025 17:03:52 +0000 Subject: [PATCH] Ensure correct permissions on user creation or file manager interaction - changed to an action as its used multiple places --- app/Actions/Filemanager/CreateFileAction.php | 11 +-- .../Filemanager/EnsurePermissionsAction.php | 19 ++++++ .../Filemanager/GetFileContentsAction.php | 27 +------- app/Actions/Filemanager/UploadFileAction.php | 6 +- app/Providers/AppServiceProvider.php | 5 ++ config/laranode.php | 40 +++++++++++ laranode-scripts/bin/laranode-user-manager.sh | 2 + .../Pages/Filemanager/Components/EditFile.jsx | 2 +- .../js/Pages/Filemanager/Filemanager.jsx | 67 ++++++++++--------- 9 files changed, 109 insertions(+), 70 deletions(-) create mode 100644 app/Actions/Filemanager/EnsurePermissionsAction.php diff --git a/app/Actions/Filemanager/CreateFileAction.php b/app/Actions/Filemanager/CreateFileAction.php index c640709..697d105 100644 --- a/app/Actions/Filemanager/CreateFileAction.php +++ b/app/Actions/Filemanager/CreateFileAction.php @@ -33,16 +33,7 @@ class CreateFileAction } // ensure file/directory permissions - $p = Process::run([ - 'sudo', - config('laranode.laranode_bin_path') . '/laranode-file-permissions.sh', - $r->path . '/' . $r->fileName, - auth()->user()->systemUsername, - ]); - - dump('sudo ' . config('laranode.laranode_bin_path') . '/laranode-file-permissions.sh', $r->path . '/' . $r->fileName); - - dump($p->output(), $p->errorOutput()); + (new EnsurePermissionsAction)->execute($r->path . '/' . $r->fileName, auth()->user()->systemUsername); return response()->json([ 'message' => $r->fileType . ' ' . $r->path . '/' . $r->fileName . ' created successfully!', diff --git a/app/Actions/Filemanager/EnsurePermissionsAction.php b/app/Actions/Filemanager/EnsurePermissionsAction.php new file mode 100644 index 0000000..bc89eb7 --- /dev/null +++ b/app/Actions/Filemanager/EnsurePermissionsAction.php @@ -0,0 +1,19 @@ +filesystem; - $editableMimeTypes = [ - 'text/plain', // .txt, .log, .ini, .env, .conf, .md, .sh, .bash, .zsh - 'text/html', // .html, .htm - 'text/css', // .css - 'text/x-php', // .php - 'application/x-empty', // for example empty php files - 'text/javascript', // .js - 'application/javascript', // .js - 'application/json', // .json - 'application/xml', // .xml - 'application/x-yaml', // .yaml, .yml - 'application/x-httpd-php', // .php - 'text/x-python', // .py - 'text/x-c', // .c - 'text/x-c++', // .cpp, .cc, .h - 'text/x-java-source', // .java - 'text/x-shellscript', // .sh, .bash, .zsh - 'text/x-sql', // .sql - 'text/markdown', // .md - 'text/x-typescript', // .ts, .tsx - 'text/x-jsx', // .jsx, .tsx - 'text/rtf', - 'application/x-sh', // .sh - 'application/x-sql', // .sql - ]; - + $editableMimeTypes = config('laranode.editable_mime_types'); try { diff --git a/app/Actions/Filemanager/UploadFileAction.php b/app/Actions/Filemanager/UploadFileAction.php index 9fdbdd3..ba8d1dc 100644 --- a/app/Actions/Filemanager/UploadFileAction.php +++ b/app/Actions/Filemanager/UploadFileAction.php @@ -20,10 +20,14 @@ class UploadFileAction ]); $file = $r->file('file'); - $path = Config::get('laranode.user_base_path') . '/' . $r->path; + + $path = '/home/' . $r->user()->systemUsername . '/' . $r->path; File::append($path . '/' . $r->originalName, $file->get()); + // ensure file permissions + (new EnsurePermissionsAction)->execute($r->path . '/' . $r->originalName, $r->user()->systemUsername); + return response()->json([ 'message' => 'Chunk uploaded', 'chunkIndex' => $r->chunkIndex, diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index b9f7829..6100c4a 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -61,5 +61,10 @@ class AppServiceProvider extends ServiceProvider { Vite::prefetch(concurrency: 3); URL::forceScheme('https'); + + if (Auth::check()) { + $user = Auth::user(); + Config::set('laranode.user_base_path', $user->homedir); + } } } diff --git a/config/laranode.php b/config/laranode.php index cffd818..b93fb43 100644 --- a/config/laranode.php +++ b/config/laranode.php @@ -34,4 +34,44 @@ return [ */ 'apache_vhost_template' => base_path('laranode-scripts/templates/apache-vhost.template'), + + /* + |-------------------------------------------------------------------------- + | Laranode File Manager - Editable Mime Types + |-------------------------------------------------------------------------- + | + | This option allows you to specify the mime types that can be edited + | in the file manager. + */ + 'editable_mime_types' => + [ + 'text/plain', // .txt, .log, .ini, .env, .conf, .md, .sh, .bash, .zsh + 'text/html', // .html, .htm + 'text/css', // .css + 'text/x-php', // .php + 'text/csv', // .csv + 'application/x-empty', // for example empty php files + 'text/javascript', // .js + 'application/javascript', // .js + 'application/x-javascript', // .js + 'application/java', // .java, .js (sometimes) + 'application/json', // .json + 'application/xml', // .xml + 'application/x-yaml', // .yaml, .yml + 'application/x-httpd-php', // .php + 'text/php', // .php + 'text/x-python', // .py + 'text/x-c', // .c + 'text/x-c++', // .cpp, .cc, .h + 'text/x-java-source', // .java + 'text/x-shellscript', // .sh, .bash, .zsh + 'text/x-sql', // .sql + 'text/markdown', // .md + 'text/x-typescript', // .ts, .tsx + 'text/x-jsx', // .jsx, .tsx + 'text/rtf', + 'application/x-sh', // .sh + 'application/x-sql', // .sql + ] + ]; diff --git a/laranode-scripts/bin/laranode-user-manager.sh b/laranode-scripts/bin/laranode-user-manager.sh index a1d390d..ff61415 100755 --- a/laranode-scripts/bin/laranode-user-manager.sh +++ b/laranode-scripts/bin/laranode-user-manager.sh @@ -52,6 +52,8 @@ create_user() { echo "User $USERNAME created successfully with no SSH login." fi + chmod 770 "/home/$USERNAME" + # add this user group to www-data group too usermod -aG "$USERNAME" www-data diff --git a/resources/js/Pages/Filemanager/Components/EditFile.jsx b/resources/js/Pages/Filemanager/Components/EditFile.jsx index e890dd5..c951e88 100644 --- a/resources/js/Pages/Filemanager/Components/EditFile.jsx +++ b/resources/js/Pages/Filemanager/Components/EditFile.jsx @@ -38,7 +38,7 @@ const EditFile = ({ editFile, setEditFile }) => { } const reader = response.body.getReader(); - const decoder = new TextDecoder(); + const decoder = new TextDecoder("utf-8"); // let buffer = ''; while (true) { diff --git a/resources/js/Pages/Filemanager/Filemanager.jsx b/resources/js/Pages/Filemanager/Filemanager.jsx index bd74985..ad30dc9 100644 --- a/resources/js/Pages/Filemanager/Filemanager.jsx +++ b/resources/js/Pages/Filemanager/Filemanager.jsx @@ -249,7 +249,7 @@ const Filemanager = () => { {copyFiles && ( )} @@ -287,42 +287,45 @@ const Filemanager = () => { )} - {files.sort((a, b) => { - if (a.type === 'dir' && b.type !== 'dir') return -1; - if (a.type !== 'dir' && b.type === 'dir') return 1; - return 0; - }).map((file, index) => ( -
handleDoubleClick(file)} - > -
- handleFileClick(file)} className="-mt-1 mr-1" /> -
- {file.type === "dir" ? ( + {files + .filter(file => !file.path.includes('laranode-scripts')) + .filter(file => ![".bash_logout", ".bashrc", ".profile"].includes(file.path)) + .sort((a, b) => { + if (a.type === 'dir' && b.type !== 'dir') return -1; + if (a.type !== 'dir' && b.type === 'dir') return 1; + return 0; + }).map((file, index) => ( +
handleDoubleClick(file)} + >
- + handleFileClick(file)} className="-mt-1 mr-1" />
- ) : ( -
- + {file.type === "dir" ? ( +
+ +
+ ) : ( +
+ +
+ )} + +
+ {file.path.split('/').pop()} + {selectedPaths.includes(file.path) && cutFiles && }
- )} -
- {file.path.split('/').pop()} - {selectedPaths.includes(file.path) && cutFiles && } +
+ {typeof file.file_size == "undefined" ? "--" : formatBytes(file.file_size)} +
- -
- {typeof file.file_size == "undefined" ? "--" : formatBytes(file.file_size)} -
-
- ))} + ))}