Skip to content

Commit 3e472d0

Browse files
refactor(acl): permission and capability platforms are optional (#9115)
* refactor(acl): permission and capability platforms are optional * add iterator version * fix build --------- Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent 4ef17d0 commit 3e472d0

File tree

12 files changed

+78
-63
lines changed

12 files changed

+78
-63
lines changed

.changes/acl-platform-refactor.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri-utils": patch:enhance
3+
"tauri": patch:enhance
4+
"tauri-cli": patch:enhance
5+
"@tauri-apps/cli": patch:enhance
6+
---
7+
8+
Changed the permission and capability platforms to be optional.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:feat
3+
---
4+
5+
Added `CapabilityBuilder::platform` to link the runtime capability with a specific platform.

core/tauri-build/src/acl.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,12 @@ pub fn validate_capabilities(
452452
let target = tauri_utils::platform::Target::from_triple(&std::env::var("TARGET").unwrap());
453453

454454
for capability in capabilities.values() {
455-
if !capability.platforms.contains(&target) {
455+
if !capability
456+
.platforms
457+
.as_ref()
458+
.map(|platforms| platforms.contains(&target))
459+
.unwrap_or(true)
460+
{
456461
continue;
457462
}
458463

core/tauri-config-schema/schema.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,7 @@
10721072
"type": "object",
10731073
"required": [
10741074
"identifier",
1075-
"permissions",
1076-
"windows"
1075+
"permissions"
10771076
],
10781077
"properties": {
10791078
"identifier": {
@@ -1124,14 +1123,10 @@
11241123
},
11251124
"platforms": {
11261125
"description": "Target platforms this capability applies. By default all platforms are affected by this capability.",
1127-
"default": [
1128-
"linux",
1129-
"macOS",
1130-
"windows",
1131-
"android",
1132-
"iOS"
1126+
"type": [
1127+
"array",
1128+
"null"
11331129
],
1134-
"type": "array",
11351130
"items": {
11361131
"$ref": "#/definitions/Target"
11371132
}

core/tauri-utils/src/acl/capability.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub struct Capability {
6565
/// List of windows that uses this capability. Can be a glob pattern.
6666
///
6767
/// On multiwebview windows, prefer [`Self::webviews`] for a fine grained access control.
68+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
6869
pub windows: Vec<String>,
6970
/// List of webviews that uses this capability. Can be a glob pattern.
7071
///
@@ -75,24 +76,14 @@ pub struct Capability {
7576
/// List of permissions attached to this capability. Must include the plugin name as prefix in the form of `${plugin-name}:${permission-name}`.
7677
pub permissions: Vec<PermissionEntry>,
7778
/// Target platforms this capability applies. By default all platforms are affected by this capability.
78-
#[serde(default = "default_platforms", skip_serializing_if = "Vec::is_empty")]
79-
pub platforms: Vec<Target>,
79+
#[serde(skip_serializing_if = "Option::is_none")]
80+
pub platforms: Option<Vec<Target>>,
8081
}
8182

8283
fn default_capability_local() -> bool {
8384
true
8485
}
8586

86-
fn default_platforms() -> Vec<Target> {
87-
vec![
88-
Target::Linux,
89-
Target::MacOS,
90-
Target::Windows,
91-
Target::Android,
92-
Target::Ios,
93-
]
94-
}
95-
9687
/// Configuration for remote URLs that are associated with the capability.
9788
#[derive(Debug, Default, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Hash)]
9889
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
@@ -190,7 +181,7 @@ mod build {
190181
let local = self.local;
191182
let windows = vec_lit(&self.windows, str_lit);
192183
let permissions = vec_lit(&self.permissions, identity);
193-
let platforms = vec_lit(&self.platforms, identity);
184+
let platforms = opt_vec_lit(self.platforms.as_ref(), identity);
194185

195186
literal_struct!(
196187
tokens,

core/tauri-utils/src/acl/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,8 @@ pub struct Permission {
176176
pub scope: Scopes,
177177

178178
/// Target platforms this permission applies. By default all platforms are affected by this permission.
179-
#[serde(default = "default_platforms", skip_serializing_if = "Vec::is_empty")]
180-
pub platforms: Vec<Target>,
181-
}
182-
183-
fn default_platforms() -> Vec<Target> {
184-
vec![
185-
Target::Linux,
186-
Target::MacOS,
187-
Target::Windows,
188-
Target::Android,
189-
Target::Ios,
190-
]
179+
#[serde(skip_serializing_if = "Option::is_none")]
180+
pub platforms: Option<Vec<Target>>,
191181
}
192182

193183
/// A set of direct permissions grouped together under a new name.
@@ -313,7 +303,7 @@ mod build_ {
313303
let description = opt_str_lit(self.description.as_ref());
314304
let commands = &self.commands;
315305
let scope = &self.scope;
316-
let platforms = vec_lit(&self.platforms, identity);
306+
let platforms = opt_vec_lit(self.platforms.as_ref(), identity);
317307

318308
literal_struct!(
319309
tokens,

core/tauri-utils/src/acl/resolved.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ impl Resolved {
9292

9393
// resolve commands
9494
for capability in capabilities.values() {
95-
if !capability.platforms.contains(&target) {
95+
if !capability
96+
.platforms
97+
.as_ref()
98+
.map(|platforms| platforms.contains(&target))
99+
.unwrap_or(true)
100+
{
96101
continue;
97102
}
98103

@@ -222,7 +227,12 @@ fn with_resolved_permissions<F: FnMut(ResolvedPermission<'_>) -> Result<(), Erro
222227

223228
let permissions = get_permissions(key, permission_name, acl)?
224229
.into_iter()
225-
.filter(|p| p.platforms.contains(&target))
230+
.filter(|p| {
231+
p.platforms
232+
.as_ref()
233+
.map(|platforms| platforms.contains(&target))
234+
.unwrap_or(true)
235+
})
226236
.collect::<Vec<_>>();
227237

228238
let mut resolved_scope = Scopes::default();

core/tauri/src/ipc/authority.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use tauri_utils::acl::{
1919
resolved::{Resolved, ResolvedCommand, ResolvedScope, ScopeKey},
2020
ExecutionContext, Scopes,
2121
};
22+
use tauri_utils::platform::Target;
2223

2324
use url::Url;
2425

@@ -93,7 +94,7 @@ impl CapabilityBuilder {
9394
windows: Vec::new(),
9495
webviews: Vec::new(),
9596
permissions: Vec::new(),
96-
platforms: Vec::new(),
97+
platforms: None,
9798
})
9899
}
99100

@@ -193,6 +194,30 @@ impl CapabilityBuilder {
193194
.push(PermissionEntry::ExtendedPermission { identifier, scope });
194195
self
195196
}
197+
198+
/// Adds a target platform for this capability.
199+
///
200+
/// By default all platforms are applied.
201+
pub fn platform(mut self, platform: Target) -> Self {
202+
self
203+
.0
204+
.platforms
205+
.get_or_insert_with(Default::default)
206+
.push(platform);
207+
self
208+
}
209+
210+
/// Adds target platforms for this capability.
211+
///
212+
/// By default all platforms are applied.
213+
pub fn platforms(mut self, platforms: impl IntoIterator<Item = Target>) -> Self {
214+
self
215+
.0
216+
.platforms
217+
.get_or_insert_with(Default::default)
218+
.extend(platforms);
219+
self
220+
}
196221
}
197222

198223
impl RuntimeCapability for CapabilityBuilder {

tooling/cli/schema.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,7 @@
10721072
"type": "object",
10731073
"required": [
10741074
"identifier",
1075-
"permissions",
1076-
"windows"
1075+
"permissions"
10771076
],
10781077
"properties": {
10791078
"identifier": {
@@ -1124,14 +1123,10 @@
11241123
},
11251124
"platforms": {
11261125
"description": "Target platforms this capability applies. By default all platforms are affected by this capability.",
1127-
"default": [
1128-
"linux",
1129-
"macOS",
1130-
"windows",
1131-
"android",
1132-
"iOS"
1126+
"type": [
1127+
"array",
1128+
"null"
11331129
],
1134-
"type": "array",
11351130
"items": {
11361131
"$ref": "#/definitions/Target"
11371132
}

tooling/cli/src/acl/capability/new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn command(options: Options) -> Result<()> {
100100
)
101101
})
102102
.collect(),
103-
platforms: Vec::new(),
103+
platforms: None,
104104
};
105105

106106
let path = match options.out {

0 commit comments

Comments
 (0)