@@ -7,6 +7,8 @@ use std::{
77 path:: { Path , PathBuf } ,
88} ;
99
10+ use tauri_utils:: acl:: manifest:: PermissionFile ;
11+
1012#[ path = "src/scope.rs" ]
1113#[ allow( dead_code) ]
1214mod scope;
@@ -75,31 +77,31 @@ const BASE_DIR_VARS: &[&str] = &[
7577 "APPCACHE" ,
7678 "APPLOG" ,
7779] ;
78- const COMMANDS : & [ & str ] = & [
79- "mkdir" ,
80- "create" ,
81- "copy_file" ,
82- "remove" ,
83- "rename" ,
84- "truncate" ,
85- "ftruncate" ,
86- "write" ,
87- "write_file" ,
88- "write_text_file" ,
89- "read_dir" ,
90- "read_file" ,
91- "read" ,
92- "open" ,
93- "read_text_file" ,
94- "read_text_file_lines" ,
95- "read_text_file_lines_next" ,
96- "seek" ,
97- "stat" ,
98- "lstat" ,
99- "fstat" ,
100- "exists" ,
101- "watch" ,
102- "unwatch" ,
80+ const COMMANDS : & [ ( & str , & [ & str ] ) ] = & [
81+ ( "mkdir" , & [ ] ) ,
82+ ( "create" , & [ ] ) ,
83+ ( "copy_file" , & [ ] ) ,
84+ ( "remove" , & [ ] ) ,
85+ ( "rename" , & [ ] ) ,
86+ ( "truncate" , & [ ] ) ,
87+ ( "ftruncate" , & [ ] ) ,
88+ ( "write" , & [ ] ) ,
89+ ( "write_file" , & [ "open" , "write" ] ) ,
90+ ( "write_text_file" , & [ ] ) ,
91+ ( "read_dir" , & [ ] ) ,
92+ ( "read_file" , & [ ] ) ,
93+ ( "read" , & [ ] ) ,
94+ ( "open" , & [ ] ) ,
95+ ( "read_text_file" , & [ ] ) ,
96+ ( "read_text_file_lines" , & [ "read_text_file_lines_next" ] ) ,
97+ ( "read_text_file_lines_next" , & [ ] ) ,
98+ ( "seek" , & [ ] ) ,
99+ ( "stat" , & [ ] ) ,
100+ ( "lstat" , & [ ] ) ,
101+ ( "fstat" , & [ ] ) ,
102+ ( "exists" , & [ ] ) ,
103+ ( "watch" , & [ ] ) ,
104+ ( "unwatch" , & [ ] ) ,
103105] ;
104106
105107fn main ( ) {
@@ -205,9 +207,47 @@ permissions = [
205207 }
206208 }
207209
208- tauri_plugin:: Builder :: new ( COMMANDS )
210+ tauri_plugin:: Builder :: new ( & COMMANDS . iter ( ) . map ( |c| c . 0 ) . collect :: < Vec < _ > > ( ) )
209211 . global_api_script_path ( "./api-iife.js" )
210212 . global_scope_schema ( schemars:: schema_for!( FsScopeEntry ) )
211213 . android_path ( "android" )
212214 . build ( ) ;
215+
216+ // workaround to include nested permissions as `tauri_plugin` doesn't support it
217+ let permissions_dir = autogenerated. join ( "commands" ) ;
218+ for ( command, nested_commands) in COMMANDS {
219+ if nested_commands. is_empty ( ) {
220+ continue ;
221+ }
222+
223+ let permission_path = permissions_dir. join ( format ! ( "{command}.toml" ) ) ;
224+
225+ let content = std:: fs:: read_to_string ( & permission_path)
226+ . unwrap_or_else ( |_| panic ! ( "failed to read {command}.toml" ) ) ;
227+
228+ let mut permission_file = toml:: from_str :: < PermissionFile > ( & content)
229+ . unwrap_or_else ( |_| panic ! ( "failed to deserialize {command}.toml" ) ) ;
230+
231+ for p in permission_file
232+ . permission
233+ . iter_mut ( )
234+ . filter ( |p| p. identifier . starts_with ( "allow" ) )
235+ {
236+ p. commands
237+ . allow
238+ . extend ( nested_commands. iter ( ) . map ( |s| s. to_string ( ) ) ) ;
239+ }
240+
241+ let out = toml:: to_string_pretty ( & permission_file)
242+ . unwrap_or_else ( |_| panic ! ( "failed to serialize {command}.toml" ) ) ;
243+ let out = format ! (
244+ r#"# Automatically generated - DO NOT EDIT!
245+
246+ "$schema" = "../../schemas/schema.json"
247+
248+ {out}"#
249+ ) ;
250+ std:: fs:: write ( permission_path, out)
251+ . unwrap_or_else ( |_| panic ! ( "failed to write {command}.toml" ) ) ;
252+ }
213253}
0 commit comments