Skip to content

Commit a851b65

Browse files
enhance: include permission group permissions in the generated schema (#13057)
* enhance: include permissions in default permission description * Only include in schema * Remove 'which includes` to tauri's build script * Also bump utils * Clippy My local clippy didn't report this, weird * Use `which enables all commands` for default permissions that enables everything * Extract description into a variable * Generate permissions with or without description * Tweak the docs and generate 'which includes' * Simplify to just `includes` * Docs change change file * Put the change in minor * Update .changes/include-permissions-in-schema.md Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com> * Remove unused change file --------- Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent be31675 commit a851b65

14 files changed

Lines changed: 112 additions & 29 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
tauri: minor:enhance
3+
tauri-utils: minor:enhance
4+
---
5+
6+
Enhanced the description of generated docs and schema for permission sets to include list of permissions within.

crates/tauri-utils/src/acl/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ pub fn generate_docs(
356356
default_permission.push_str(default.description.as_deref().unwrap_or_default());
357357
default_permission.push('\n');
358358
default_permission.push('\n');
359+
default_permission.push_str("#### This default permission set includes the following:\n");
360+
default_permission.push('\n');
359361
for permission in &default.permissions {
360-
default_permission.push_str(&format!("- `{permission}`"));
361-
default_permission.push('\n');
362+
default_permission.push_str(&format!("- `{permission}`\n"));
362363
}
363364
}
364365

crates/tauri-utils/src/acl/schema.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub trait PermissionSchemaGenerator<
4242
/// Default permission set description if any.
4343
fn default_set_description(&self) -> Option<&str>;
4444

45+
/// Default permission set's permissions if any.
46+
fn default_set_permissions(&self) -> Option<&Vec<String>>;
47+
4548
/// Permissions sets to generate schema for.
4649
fn permission_sets(&'a self) -> Ps;
4750

@@ -56,13 +59,26 @@ pub trait PermissionSchemaGenerator<
5659
_ => id.to_string(),
5760
};
5861

62+
let extensions = if let Some(description) = description {
63+
[(
64+
// This is non-standard, and only used by vscode right now,
65+
// but it does work really well
66+
"markdownDescription".to_string(),
67+
serde_json::Value::String(description.to_string()),
68+
)]
69+
.into()
70+
} else {
71+
Default::default()
72+
};
73+
5974
Schema::Object(SchemaObject {
6075
metadata: Some(Box::new(Metadata {
6176
description: description.map(ToString::to_string),
6277
..Default::default()
6378
})),
6479
instance_type: Some(InstanceType::String.into()),
6580
const_value: Some(serde_json::Value::String(command_name)),
81+
extensions,
6682
..Default::default()
6783
})
6884
}
@@ -73,13 +89,22 @@ pub trait PermissionSchemaGenerator<
7389

7490
// schema for default set
7591
if self.has_default_permission_set() {
76-
let default = Self::perm_id_schema(name, "default", self.default_set_description());
77-
permission_schemas.push(default);
92+
let description = self.default_set_description().unwrap_or_default();
93+
let description = if let Some(permissions) = self.default_set_permissions() {
94+
add_permissions_to_description(description, permissions, true)
95+
} else {
96+
description.to_string()
97+
};
98+
if !description.is_empty() {
99+
let default = Self::perm_id_schema(name, "default", Some(&description));
100+
permission_schemas.push(default);
101+
}
78102
}
79103

80104
// schema for each permission set
81105
for set in self.permission_sets() {
82-
let schema = Self::perm_id_schema(name, &set.identifier, Some(&set.description));
106+
let description = add_permissions_to_description(&set.description, &set.permissions, false);
107+
let schema = Self::perm_id_schema(name, &set.identifier, Some(&description));
83108
permission_schemas.push(schema);
84109
}
85110

@@ -93,6 +118,27 @@ pub trait PermissionSchemaGenerator<
93118
}
94119
}
95120

121+
fn add_permissions_to_description(
122+
description: &str,
123+
permissions: &[String],
124+
is_default: bool,
125+
) -> String {
126+
if permissions.is_empty() {
127+
return description.to_string();
128+
}
129+
let permissions_list = permissions
130+
.iter()
131+
.map(|permission| format!("- `{permission}`"))
132+
.collect::<Vec<_>>()
133+
.join("\n");
134+
let default_permission_set = if is_default {
135+
"default permission set"
136+
} else {
137+
"permission set"
138+
};
139+
format!("{description}\n#### This {default_permission_set} includes:\n\n{permissions_list}")
140+
}
141+
96142
impl<'a>
97143
PermissionSchemaGenerator<
98144
'a,
@@ -111,6 +157,10 @@ impl<'a>
111157
.map(|d| d.description.as_str())
112158
}
113159

160+
fn default_set_permissions(&self) -> Option<&Vec<String>> {
161+
self.default_permission.as_ref().map(|d| &d.permissions)
162+
}
163+
114164
fn permission_sets(&'a self) -> Values<'a, std::string::String, PermissionSet> {
115165
self.permission_sets.values()
116166
}
@@ -131,6 +181,10 @@ impl<'a> PermissionSchemaGenerator<'a, Iter<'a, PermissionSet>, Iter<'a, Permiss
131181
self.default.as_ref().and_then(|d| d.description.as_deref())
132182
}
133183

184+
fn default_set_permissions(&self) -> Option<&Vec<String>> {
185+
self.default.as_ref().map(|d| &d.permissions)
186+
}
187+
134188
fn permission_sets(&'a self) -> Iter<'a, PermissionSet> {
135189
self.set.iter()
136190
}

crates/tauri/build.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,28 @@ fn define_permissions(
373373
LICENSE_HEADER,
374374
false,
375375
);
376-
let default_permissions = commands
377-
.iter()
378-
.filter(|(_cmd, default)| *default)
376+
let default_permissions: Vec<_> = commands.iter().filter(|(_cmd, default)| *default).collect();
377+
let all_commands_enabled_by_default = commands.len() == default_permissions.len();
378+
let default_permissions = default_permissions
379+
.into_iter()
379380
.map(|(cmd, _)| {
380381
let slugified_command = cmd.replace('_', "-");
381382
format!("\"allow-{slugified_command}\"")
382383
})
383384
.collect::<Vec<_>>()
384385
.join(", ");
385386

387+
let all_enable_by_default = if all_commands_enabled_by_default {
388+
", which enables all commands"
389+
} else {
390+
""
391+
};
392+
386393
let default_toml = format!(
387394
r###"{LICENSE_HEADER}# Automatically generated - DO NOT EDIT!
388395
389396
[default]
390-
description = "Default permissions for the plugin."
397+
description = "Default permissions for the plugin{all_enable_by_default}."
391398
permissions = [{default_permissions}]
392399
"###,
393400
);
@@ -437,22 +444,15 @@ fn define_default_permission_set(
437444

438445
let default_toml = permissions_out_dir.join("default.toml");
439446
let toml_content = format!(
440-
r#"# {LICENSE_HEADER}
447+
r#"{LICENSE_HEADER}
441448
442449
[default]
443-
description = """Default core plugins set which includes:
444-
{}
445-
"""
450+
description = "Default core plugins set."
446451
permissions = [{}]
447452
"#,
448453
PLUGINS
449454
.iter()
450-
.map(|(k, _)| format!("- '{k}:default'"))
451-
.collect::<Vec<_>>()
452-
.join("\n"),
453-
PLUGINS
454-
.iter()
455-
.map(|(k, _)| format!("'{k}:default'"))
455+
.map(|(k, _)| format!("\"{k}:default\""))
456456
.collect::<Vec<_>>()
457457
.join(",")
458458
);

crates/tauri/permissions/app/autogenerated/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Default permissions for the plugin.
44

5+
#### This default permission set includes the following:
6+
57
- `allow-version`
68
- `allow-name`
79
- `allow-tauri-version`

crates/tauri/permissions/event/autogenerated/reference.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Default Permission
22

3-
Default permissions for the plugin.
3+
Default permissions for the plugin, which enables all commands.
4+
5+
#### This default permission set includes the following:
46

57
- `allow-listen`
68
- `allow-unlisten`

crates/tauri/permissions/image/autogenerated/reference.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Default Permission
22

3-
Default permissions for the plugin.
3+
Default permissions for the plugin, which enables all commands.
4+
5+
#### This default permission set includes the following:
46

57
- `allow-new`
68
- `allow-from-bytes`

crates/tauri/permissions/menu/autogenerated/reference.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Default Permission
22

3-
Default permissions for the plugin.
3+
Default permissions for the plugin, which enables all commands.
4+
5+
#### This default permission set includes the following:
46

57
- `allow-new`
68
- `allow-append`

crates/tauri/permissions/path/autogenerated/reference.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Default Permission
22

3-
Default permissions for the plugin.
3+
Default permissions for the plugin, which enables all commands.
4+
5+
#### This default permission set includes the following:
46

57
- `allow-resolve-directory`
68
- `allow-resolve`

crates/tauri/permissions/resources/autogenerated/reference.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Default Permission
22

3-
Default permissions for the plugin.
3+
Default permissions for the plugin, which enables all commands.
4+
5+
#### This default permission set includes the following:
46

57
- `allow-close`
68

0 commit comments

Comments
 (0)