move per-user plugin grants to the user's page
Nightly Build / build (push) Successful in 7m16s

Granting was a checklist of every user on each plugin's page, so "what may
this person use?" meant opening every plugin in turn — and the answer lived
on N pages while the connector half of it already lived on one. Both grant
sections now sit together on #users/{id}: same row list, same disabled chip,
same replace-the-whole-set save. The plugin's own page keeps a read-only
roster of who holds it, linking back to each person.

- db: plugin_access::set_for_user, the per-user twin of set_for_user on
  mcp_catalog_access; set_access stays as the inverse read model
- PluginManager: list_grants_for_user / set_grants_for_user, which omit and
  reject manages_own_access plugins (a box that controls nothing is worse
  than no box)
- GET/PUT /api/users/{id}/plugins, mounted next to /users/{id}/connectors;
  PUT /api/plugins/{id}/access is gone, GET remains as the roster

No push after the write, unlike a connector grant: that one gates a runtime
snapshotted at login, while a plugin grant is re-read from plugin_access on
every request that depends on it (sidebar pages, /plugins/mine, and each
inbound channel message), so a revoke lands with no bus event.

Docs updated with where access is granted, and why mobile-connector is
absent from that list.
This commit is contained in:
2026-07-29 11:36:47 +01:00
parent 8bcf09a67e
commit da8a835d70
15 changed files with 327 additions and 93 deletions
+15
View File
@@ -1268,6 +1268,21 @@ mod tests {
assert!(!plugin_access::has_access(&pool, "telegram", "u1").await.unwrap());
assert_eq!(plugin_access::users_for_plugin(&pool, "telegram").await.unwrap(), vec!["u2"]);
// The Users-page write path: one user's grants across every plugin. A
// blanket replace, and scoped to that user — u2's telegram grant stands.
plugin_access::set_for_user(&pool, "u1", &["comfyui".to_string(), "honcho".to_string()])
.await.unwrap();
assert_eq!(
plugin_access::plugin_ids_for_user(&pool, "u1").await.unwrap(),
vec!["comfyui", "honcho"],
);
assert!(plugin_access::has_access(&pool, "telegram", "u2").await.unwrap());
plugin_access::set_for_user(&pool, "u1", &["honcho".to_string()]).await.unwrap();
assert_eq!(plugin_access::plugin_ids_for_user(&pool, "u1").await.unwrap(), vec!["honcho"]);
plugin_access::set_for_user(&pool, "u1", &[]).await.unwrap();
assert!(plugin_access::plugin_ids_for_user(&pool, "u1").await.unwrap().is_empty());
assert!(plugin_access::has_access(&pool, "telegram", "u2").await.unwrap());
plugin_user_configs::set(&pool, "telegram", "u2", &serde_json::json!({"linked": true})).await.unwrap();
assert_eq!(
plugin_user_configs::get(&pool, "telegram", "u2").await.unwrap(),
+33 -2
View File
@@ -83,8 +83,39 @@ pub async fn revoke(pool: &SqlitePool, plugin_id: &str, user_id: &str) -> Result
Ok(())
}
/// Replaces the full access list for a plugin in one shot (the admin UI's
/// "who can use this" checklist).
/// Replaces a user's full plugin-grant list in one shot the Users-page form
/// ("which plugins may this person use"), the per-user twin of
/// [`super::mcp_catalog_access::set_for_user`].
///
/// A blanket replace is correct because the form is fed the **complete** set of
/// grantable plugins: every registered plugin except the binding-managed ones
/// (`Plugin::manages_own_access`), and those never read this table — their
/// access is their own pairing — so clearing a stale row for one is a no-op.
///
/// Nothing has to be pushed after this write: unlike an MCP grant, which gates
/// a runtime snapshotted at login, a plugin grant is re-read from here on every
/// request and every inbound channel message, so a revoke takes effect at once.
pub async fn set_for_user(pool: &SqlitePool, user_id: &str, plugin_ids: &[String]) -> Result<()> {
let mut tx = pool.begin().await?;
sqlx::query("DELETE FROM plugin_access WHERE user_id = ?")
.bind(user_id)
.execute(&mut *tx)
.await?;
for plugin_id in plugin_ids {
sqlx::query("INSERT OR IGNORE INTO plugin_access (plugin_id, user_id) VALUES (?, ?)")
.bind(plugin_id)
.bind(user_id)
.execute(&mut *tx)
.await?;
}
tx.commit().await?;
Ok(())
}
/// Replaces the full access list for a plugin in one shot (the plugin-shaped
/// twin of [`set_for_user`]). No UI writes through this any more — "who may use
/// what" is edited on the user's page — but it is the honest inverse of the
/// read model and the cheapest way to set a plugin's audience from a test.
pub async fn set_access(pool: &SqlitePool, plugin_id: &str, user_ids: &[String]) -> Result<()> {
let mut tx = pool.begin().await?;
sqlx::query("DELETE FROM plugin_access WHERE plugin_id = ?")
+56 -4
View File
@@ -62,6 +62,22 @@ pub struct UserPluginView {
pub user_config: Value,
}
/// One plugin's grant state for one user — a row of the Users-page checklist
/// ("which plugins may this person use"), served by `GET /api/users/{id}/plugins`.
///
/// Deliberately shaped like the connector rows next to it on that page: enough
/// to render name + description + a "disabled" chip, and the `granted` flag the
/// checkbox binds to. A *disabled* plugin is still listed — the grant can be set
/// ahead of the admin enabling it, exactly like a disabled global connector.
#[derive(Debug, Clone, Serialize)]
pub struct PluginGrantView {
pub id: String,
pub name: String,
pub description: String,
pub enabled: bool,
pub granted: bool,
}
/// A plugin-contributed web page as seen by one user — served by
/// `GET /api/plugins/pages`. `entry_url` is already resolved against the
/// plugin's router mount, so the frontend can `import()` it directly.
@@ -522,15 +538,51 @@ impl PluginManager {
Ok(db::get(&self.db, id).await?.map(|r| r.enabled).unwrap_or(false))
}
/// The user ids granted access to a plugin (admin UI checklist).
/// The user ids granted access to a plugin — the plugin-detail page's
/// read-only "who has this" list. Writing is the *user's* page (see
/// [`Self::set_grants_for_user`]), so there is no plugin-shaped setter.
pub async fn list_grants(&self, id: &str) -> Result<Vec<String>> {
self.find(id)?;
plugin_access::users_for_plugin(&self.db, id).await
}
pub async fn set_grants(&self, id: &str, user_ids: &[String]) -> Result<()> {
self.find(id)?;
plugin_access::set_access(&self.db, id, user_ids).await
/// One user's grant state across every **grantable** plugin — the Users-page
/// checklist, the per-user twin of [`Self::list_grants`].
///
/// Binding-managed plugins (`Plugin::manages_own_access`) are omitted: their
/// access is their own pairing lifecycle, so a checkbox here would control
/// nothing. Disabled plugins are kept — a grant may be set before the admin
/// enables one, and the caller renders the state as a chip.
pub async fn list_grants_for_user(&self, user_id: &str) -> Result<Vec<PluginGrantView>> {
let granted: std::collections::HashSet<String> =
plugin_access::plugin_ids_for_user(&self.db, user_id).await?.into_iter().collect();
let mut out = Vec::new();
for plugin in &self.plugins {
if plugin.manages_own_access() {
continue;
}
out.push(PluginGrantView {
enabled: self.is_enabled(plugin.id()).await?,
granted: granted.contains(plugin.id()),
id: plugin.id().to_string(),
name: plugin.name().to_string(),
description: plugin.description().to_string(),
});
}
Ok(out)
}
/// Replaces one user's plugin grants (the Users-page save button). Every id
/// must name a registered, grantable plugin — a binding-managed one is
/// rejected rather than silently stored, since nothing would ever read it.
pub async fn set_grants_for_user(&self, user_id: &str, plugin_ids: &[String]) -> Result<()> {
for id in plugin_ids {
let plugin = self.find(id)?;
if plugin.manages_own_access() {
anyhow::bail!("plugin manages its own access: {id}");
}
}
plugin_access::set_for_user(&self.db, user_id, plugin_ids).await
}
/// Applies a user's per-plugin config submission, received from the