mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-02-06 13:37:09 +00:00
fix(cli): permission add could add duplicated (#13981)
This commit is contained in:
parent
33d0b3f0c1
commit
b21d86a8a3
6
.changes/fix-permission-add-duplicates.md
Normal file
6
.changes/fix-permission-add-duplicates.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri-cli": "patch:bug"
|
||||
"@tauri-apps/cli": "patch:bug"
|
||||
---
|
||||
|
||||
Fix `tauri permission add` could add duplicated permissions to the capability files
|
||||
@ -77,6 +77,26 @@ impl TomlOrJson {
|
||||
};
|
||||
}
|
||||
|
||||
fn has_permission(&self, identifier: &str) -> bool {
|
||||
(|| {
|
||||
Some(match self {
|
||||
TomlOrJson::Toml(t) => t
|
||||
.get("permissions")?
|
||||
.as_array()?
|
||||
.iter()
|
||||
.any(|value| value.as_str() == Some(identifier)),
|
||||
|
||||
TomlOrJson::Json(j) => j
|
||||
.as_object()?
|
||||
.get("permissions")?
|
||||
.as_array()?
|
||||
.iter()
|
||||
.any(|value| value.as_str() == Some(identifier)),
|
||||
})
|
||||
})()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn to_string(&self) -> Result<String> {
|
||||
Ok(match self {
|
||||
TomlOrJson::Toml(t) => t.to_string(),
|
||||
@ -236,9 +256,18 @@ pub fn command(options: Options) -> Result<()> {
|
||||
}
|
||||
|
||||
for (capability, path) in &mut capabilities {
|
||||
capability.insert_permission(options.identifier.clone());
|
||||
std::fs::write(&*path, capability.to_string()?)?;
|
||||
log::info!(action = "Added"; "permission `{}` to `{}` at {}", options.identifier, capability.identifier(), dunce::simplified(path).display());
|
||||
if capability.has_permission(&options.identifier) {
|
||||
log::info!(
|
||||
"Permission `{}` already found in `{}` at {}",
|
||||
options.identifier,
|
||||
capability.identifier(),
|
||||
dunce::simplified(path).display()
|
||||
);
|
||||
} else {
|
||||
capability.insert_permission(options.identifier.clone());
|
||||
std::fs::write(&*path, capability.to_string()?)?;
|
||||
log::info!(action = "Added"; "permission `{}` to `{}` at {}", options.identifier, capability.identifier(), dunce::simplified(path).display());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user