跳到內容
Tauri

權限

權限是對命令明確特權的描述。

[[permission]]
identifier = "my-identifier"
description = "This describes the impact and more."
commands.allow = [
"read_file"
]
[[scope.allow]]
my-scope = "$HOME/*"
[[scope.deny]]
my-scope = "$HOME/secret"

它可以使命令在 Tauri 應用程式的前端可訪問。它可以將範圍映射到命令,並定義啟用哪些命令。權限可以啟用或拒絕特定命令、定義範圍或結合兩者。

權限可以分組為一個集合,並在一個新的識別符下。這稱為權限集。這允許您將範圍相關的權限與命令相關的權限結合起來。它還允許將特定於操作系統的權限分組或捆綁到更易於使用的集合中。

作為外掛程式開發人員,您可以為所有公開的命令發布多個預定義的、命名良好的權限。

作為應用程式開發人員,您可以擴展現有的外掛程式權限,或為您自己的命令定義權限。它們可以分組或擴展到一個集合中,以便重複使用或簡化後續的主要設定檔。

權限識別符

權限識別符用於確保權限可以重複使用並具有唯一名稱。

  • <name>:default 表示權限是外掛程式或應用程式的預設權限
  • <name>:<command-name> 表示權限適用於個別命令

外掛程式前綴 tauri-plugin- 將在編譯時自動添加到外掛程式的識別符中,無需手動指定。

識別符僅限於 ASCII 小寫字母字元 [a-z],且識別符的最大長度目前限制為 116,因為有以下常數

const IDENTIFIER_SEPARATOR: u8 = b':';
const PLUGIN_PREFIX: &str = "tauri-plugin-";
// https://doc.rust-lang.org/cargo/reference/manifest.html#the-name-field
const MAX_LEN_PREFIX: usize = 64 - PLUGIN_PREFIX.len();
const MAX_LEN_BASE: usize = 64;
const MAX_LEN_IDENTIFIER: usize = MAX_LEN_PREFIX + 1 + MAX_LEN_BASE;

設定檔

簡化的 Tauri 外掛程式 目錄結構範例

終端機視窗
tauri-plugin
├── README.md
├── src
└── lib.rs
├── build.rs
├── Cargo.toml
├── permissions
└── <identifier>.json/toml
└── default.json/toml

預設權限以特殊方式處理,因為只要使用 Tauri CLI 將外掛程式添加到 Tauri 應用程式,它就會自動添加到應用程式設定中。

對於應用程式開發人員,結構類似

終端機視窗
tauri-app
├── index.html
├── package.json
├── src
├── src-tauri
├── Cargo.toml
├── permissions
└── <identifier>.toml
| ├── capabilities
└── <identifier>.json/.toml
├── src
├── tauri.conf.json

範例

來自 檔案系統 外掛程式的權限範例。

plugins/fs/permissions/autogenerated/base-directories/home.toml
[[permission]]
identifier = "scope-home"
description = """This scope permits access to all files and
list content of top level directories in the `$HOME`folder."""
[[scope.allow]]
path = "$HOME/*"
plugins/fs/permissions/read-files.toml
[[permission]]
identifier = "read-files"
description = """This enables all file read related
commands without any pre-configured accessible paths."""
commands.allow = [
"read_file",
"read",
"open",
"read_text_file",
"read_text_file_lines",
"read_text_file_lines_next"
]
plugins/fs/permissions/autogenerated/commands/mkdir.toml
[[permission]]
identifier = "allow-mkdir"
description = "This enables the mkdir command."
commands.allow = [
"mkdir"
]

在您的應用程式中擴展上述外掛程式權限的範例實作

my-app/src-tauri/permissions/home-read-extends.toml
[[set]]
identifier = "allow-home-read-extended"
description = """ This allows non-recursive read access to files and to create directories
in the `$HOME` folder.
"""
permissions = [
"fs:read-files",
"fs:scope-home",
"fs:allow-mkdir"
]

© 2025 Tauri 貢獻者。CC-BY / MIT