Skip to content

Commit b21d86a

Browse files
fix(cli): permission add could add duplicated (#13981)
1 parent 33d0b3f commit b21d86a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": "patch:bug"
3+
"@tauri-apps/cli": "patch:bug"
4+
---
5+
6+
Fix `tauri permission add` could add duplicated permissions to the capability files

crates/tauri-cli/src/acl/permission/add.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ impl TomlOrJson {
7777
};
7878
}
7979

80+
fn has_permission(&self, identifier: &str) -> bool {
81+
(|| {
82+
Some(match self {
83+
TomlOrJson::Toml(t) => t
84+
.get("permissions")?
85+
.as_array()?
86+
.iter()
87+
.any(|value| value.as_str() == Some(identifier)),
88+
89+
TomlOrJson::Json(j) => j
90+
.as_object()?
91+
.get("permissions")?
92+
.as_array()?
93+
.iter()
94+
.any(|value| value.as_str() == Some(identifier)),
95+
})
96+
})()
97+
.unwrap_or_default()
98+
}
99+
80100
fn to_string(&self) -> Result<String> {
81101
Ok(match self {
82102
TomlOrJson::Toml(t) => t.to_string(),
@@ -236,9 +256,18 @@ pub fn command(options: Options) -> Result<()> {
236256
}
237257

238258
for (capability, path) in &mut capabilities {
239-
capability.insert_permission(options.identifier.clone());
240-
std::fs::write(&*path, capability.to_string()?)?;
241-
log::info!(action = "Added"; "permission `{}` to `{}` at {}", options.identifier, capability.identifier(), dunce::simplified(path).display());
259+
if capability.has_permission(&options.identifier) {
260+
log::info!(
261+
"Permission `{}` already found in `{}` at {}",
262+
options.identifier,
263+
capability.identifier(),
264+
dunce::simplified(path).display()
265+
);
266+
} else {
267+
capability.insert_permission(options.identifier.clone());
268+
std::fs::write(&*path, capability.to_string()?)?;
269+
log::info!(action = "Added"; "permission `{}` to `{}` at {}", options.identifier, capability.identifier(), dunce::simplified(path).display());
270+
}
242271
}
243272

244273
Ok(())

0 commit comments

Comments
 (0)