Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance multiple capabilities file format & fix mixed permissions schema #9079

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/build-schema-generation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-build": patch:bug
---

Fixed generation of capability schema for permissions field which previously disallowed mixed (strings and objects) permission definition.
5 changes: 5 additions & 0 deletions .changes/utils-named-capability-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-utils": major:breaking
---

Changed `CapabiltyFile::List` enum variant to be a tuple-struct and added `CapabiltyFile::NamedList`. This allows more flexibility when parsing capabilties from JSON files.
6 changes: 2 additions & 4 deletions core/tauri-build/src/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ fn capabilities_schema(acl_manifests: &BTreeMap<String, Manifest>) -> RootSchema
if let Some(Schema::Object(obj)) = schema.definitions.get_mut("PermissionEntry") {
let permission_entry_any_of_schemas = obj.subschemas().any_of.as_mut().unwrap();

if let Schema::Object(mut scope_extended_schema_obj) =
permission_entry_any_of_schemas.remove(permission_entry_any_of_schemas.len() - 1)
if let Schema::Object(scope_extended_schema_obj) =
permission_entry_any_of_schemas.last_mut().unwrap()
{
let mut global_scope_one_of = Vec::new();

Expand Down Expand Up @@ -246,8 +246,6 @@ fn capabilities_schema(acl_manifests: &BTreeMap<String, Manifest>) -> RootSchema
one_of: Some(global_scope_one_of),
..Default::default()
}));

permission_entry_any_of_schemas.push(scope_extended_schema_obj.into());
};
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
CapabilityFile::Capability(c) => {
capabilities.insert(c.identifier.clone(), c);
}
CapabilityFile::List {
CapabilityFile::List(capabilities_list)
| CapabilityFile::NamedList {
capabilities: capabilities_list,
} => {
capabilities.extend(
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-utils/src/acl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn parse_capabilities(
CapabilityFile::Capability(capability) => {
capabilities_map.insert(capability.identifier.clone(), capability);
}
CapabilityFile::List { capabilities } => {
CapabilityFile::List(capabilities) | CapabilityFile::NamedList { capabilities } => {
for capability in capabilities {
capabilities_map.insert(capability.identifier.clone(), capability);
}
Expand Down
12 changes: 6 additions & 6 deletions core/tauri-utils/src/acl/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ pub enum CapabilityFile {
/// A single capability.
Capability(Capability),
/// A list of capabilities.
List {
List(Vec<Capability>),
/// A list of capabilities.
NamedList {
/// The list of capabilities.
capabilities: Vec<Capability>,
},
Expand All @@ -135,11 +137,9 @@ impl FromStr for CapabilityFile {
type Err = super::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.chars().next() {
Some('[') => toml::from_str(s).map_err(Into::into),
Some('{') => serde_json::from_str(s).map_err(Into::into),
_ => Err(super::Error::UnknownCapabilityFormat(s.into())),
}
serde_json::from_str(s)
.or_else(|_| toml::from_str(s))
.map_err(Into::into)
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/tauri/src/ipc/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ impl RuntimeAuthority {
CapabilityFile::Capability(c) => {
capabilities.insert(c.identifier.clone(), c);
}
CapabilityFile::List {

CapabilityFile::List(capabilities_list)
| CapabilityFile::NamedList {
capabilities: capabilities_list,
} => {
capabilities.extend(
Expand Down
Loading