mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Bind Replace Actions authorization to the selected source and held bytes
This commit is contained in:
1 parent
0e26b756fe
commit
243f14f6fe
2 files changed
+88
-17
No files matched your search
@@ -36,22 +36,32 @@ impl Kind {
|
||||
}
|
||||
}
|
||||
|
||||
/// The revision, authorization, and chosen payload a pending confirmation may use.
|
||||
pub(crate) struct Held {
|
||||
id: u64,
|
||||
kind: Kind,
|
||||
revision: u64,
|
||||
gate: Option<super::Gate>,
|
||||
automated: bool,
|
||||
replace_data: Option<(String, usize, String)>,
|
||||
replace_data: Option<Replacement>,
|
||||
append: bool,
|
||||
accept: bool,
|
||||
choice_append: bool,
|
||||
}
|
||||
|
||||
struct Replacement {
|
||||
name: String,
|
||||
count: usize,
|
||||
params: Value,
|
||||
path: Option<String>,
|
||||
}
|
||||
|
||||
/// Identify dialogs owned by the Classic Actions library controller.
|
||||
pub(crate) fn owns(fields: &Map<String, Value>) -> bool {
|
||||
fields.contains_key(MARK)
|
||||
}
|
||||
|
||||
/// Refuse library edits while its store, recording, or playback owns the definitions.
|
||||
pub(crate) fn idle(app: &PhotocraftApp, command: &str) -> Result<(), String> {
|
||||
super::suspended_guard(app, command)?;
|
||||
if app.services.preset_store.is_some() {
|
||||
@@ -67,8 +77,14 @@ pub(crate) fn idle(app: &PhotocraftApp, command: &str) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn effective_gate(app: &PhotocraftApp, captured: Option<super::Gate>, automated: bool) -> Result<Option<super::Gate>, String> {
|
||||
let service = if automated || app.automation_input { app.services.automation_authorize } else { None };
|
||||
let live = super::continuation_gate(app.session.authorize, service)?;
|
||||
super::continuation_gate(captured, live)
|
||||
}
|
||||
|
||||
fn authorize(app: &PhotocraftApp, command: &str, params: &Value, gate: Option<super::Gate>, automated: bool) -> Result<(), String> {
|
||||
let gate = super::continuation_gate(gate, app.session.authorize)?;
|
||||
let gate = effective_gate(app, gate, automated)?;
|
||||
if let Some(gate) = gate {
|
||||
gate(command, params).map_err(|error| error.to_string())?;
|
||||
}
|
||||
@@ -80,6 +96,7 @@ fn authorize(app: &PhotocraftApp, command: &str, params: &Value, gate: Option<su
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ask for the chosen library edit without reading or changing the library before consent.
|
||||
pub(super) fn open(app: &mut PhotocraftApp, kind: Kind) -> Result<Value, String> {
|
||||
let command = kind.command();
|
||||
idle(app, command)?;
|
||||
@@ -87,8 +104,8 @@ pub(super) fn open(app: &mut PhotocraftApp, kind: Kind) -> Result<Value, String>
|
||||
return Err(tl!("Confirm or cancel the pending Actions library change first.").into());
|
||||
}
|
||||
let revision = app.session.actions.rev;
|
||||
let gate = app.session.authorize.or(if app.automation_input { app.services.automation_authorize } else { None });
|
||||
let automated = app.automation_input || gate.is_some();
|
||||
let automated = app.automation_input;
|
||||
let gate = effective_gate(app, None, automated)?;
|
||||
let authorize = |params: &Value| self::authorize(app, command, params, gate, automated);
|
||||
let mut held = Held { id: 0, kind, revision, gate, automated, replace_data: None, append: false, accept: true, choice_append: false };
|
||||
if kind == Kind::Reset {
|
||||
@@ -98,7 +115,7 @@ pub(super) fn open(app: &mut PhotocraftApp, kind: Kind) -> Result<Value, String>
|
||||
if !held.append {
|
||||
replace?;
|
||||
}
|
||||
} else {
|
||||
} else if kind == Kind::Clear {
|
||||
authorize(&json!({"confirm":true}))?;
|
||||
}
|
||||
if kind != Kind::Replace {
|
||||
@@ -109,22 +126,23 @@ pub(super) fn open(app: &mut PhotocraftApp, kind: Kind) -> Result<Value, String>
|
||||
if app.session.actions.rev != revision || app.held_actions_library.is_some() {
|
||||
return Err(tl!("The actions changed. Request Replace Actions again.").into());
|
||||
}
|
||||
let (name, bytes) = match answer {
|
||||
let (name, bytes, path) = match answer {
|
||||
FileDialogAnswer::Paths(paths) => {
|
||||
let path = paths.into_iter().next().ok_or("cancelled")?;
|
||||
self::authorize(app, command, &json!({"confirm":true,"path":path}), gate, automated)?;
|
||||
let bytes = photocraft_format::read_file_limited(std::path::Path::new(&path), MAX_PICKED_BYTES).map_err(|error| format!("{path}: {error}"))?;
|
||||
(path, bytes)
|
||||
(crate::file_open::display_name(&path), bytes, Some(path))
|
||||
}
|
||||
FileDialogAnswer::Contents(name, bytes) => (name, bytes),
|
||||
FileDialogAnswer::Contents(name, bytes) => (crate::file_open::display_name(&name), bytes, None),
|
||||
_ => return Err(tl!("Choose one Actions file.").into()),
|
||||
};
|
||||
if bytes.len() as u64 > MAX_PICKED_BYTES {
|
||||
return Err(tl!("The Actions file is too large.").into());
|
||||
}
|
||||
let params = json!({"confirm":true,"data":photocraft_engine::paint::tile::b64_encode(&bytes)});
|
||||
self::authorize(app, command, ¶ms, gate, automated)?;
|
||||
let count = photocraft_engine::atn_cmds::validate_replacement(&bytes)?;
|
||||
let data = photocraft_engine::paint::tile::b64_encode(&bytes);
|
||||
held.replace_data = Some((crate::file_open::display_name(&name), count, data));
|
||||
held.replace_data = Some(Replacement { name, count, params, path });
|
||||
Ok(prompt(app, held))
|
||||
})
|
||||
}
|
||||
@@ -139,11 +157,15 @@ fn prompt(app: &mut PhotocraftApp, mut held: Held) -> Value {
|
||||
json!({"dialog":id})
|
||||
}
|
||||
|
||||
/// Draw the warning for the exact held library edit.
|
||||
pub(crate) fn body(app: &PhotocraftApp, ui: &mut egui::Ui, id: u64) {
|
||||
let Some(held) = app.held_actions_library.as_ref().filter(|held| held.id == id) else { return };
|
||||
match &held.replace_data {
|
||||
Some((name, count, _)) => {
|
||||
ui.label(crate::i18n::fmt(tl!("Replace all actions with {count} actions from {file}?"), &[("count", &count.to_string()), ("file", name)]));
|
||||
Some(replacement) => {
|
||||
ui.label(crate::i18n::fmt(
|
||||
tl!("Replace all actions with {count} actions from {file}?"),
|
||||
&[("count", &replacement.count.to_string()), ("file", &replacement.name)],
|
||||
));
|
||||
}
|
||||
None if held.kind == Kind::Reset => {
|
||||
ui.label(tl!("Replace the current actions?"));
|
||||
@@ -154,10 +176,12 @@ pub(crate) fn body(app: &PhotocraftApp, ui: &mut egui::Ui, id: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether OK is available for this still-owned dialog.
|
||||
pub(crate) fn can_confirm(app: &PhotocraftApp, id: u64) -> bool {
|
||||
app.held_actions_library.as_ref().is_some_and(|held| held.id == id && held.accept)
|
||||
}
|
||||
|
||||
/// Draw the destructive choices and retain which Reset button was pressed.
|
||||
pub(crate) fn buttons(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: u64) -> Option<ButtonRole> {
|
||||
let held = app.held_actions_library.as_mut().filter(|held| held.id == id)?;
|
||||
let ok = DialogButton::new(ButtonRole::Default, tl!("OK"), 84.0).enabled(held.accept);
|
||||
@@ -171,6 +195,7 @@ pub(crate) fn buttons(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: u64) -> Op
|
||||
clicked
|
||||
}
|
||||
|
||||
/// Recheck the held source and payload against the current authorization before dispatch.
|
||||
pub(crate) fn confirm(app: &mut PhotocraftApp, id: u64) -> Result<Value, String> {
|
||||
if !app.held_actions_library.as_ref().is_some_and(|held| held.id == id) {
|
||||
return Err(tl!("No Actions library change belongs to this dialog.").into());
|
||||
@@ -190,20 +215,22 @@ pub(crate) fn confirm(app: &mut PhotocraftApp, id: u64) -> Result<Value, String>
|
||||
json!({"confirm":true,"mode":if held.choice_append { "append" } else { "replace" }})
|
||||
}
|
||||
Kind::Replace => {
|
||||
let (_, _, data) = held.replace_data.ok_or("missing validated Actions file")?;
|
||||
let mut params = json!({"confirm":true});
|
||||
params["data"] = Value::String(data);
|
||||
params
|
||||
let replacement = held.replace_data.ok_or("missing validated Actions file")?;
|
||||
if let Some(path) = replacement.path {
|
||||
authorize(app, command, &json!({"confirm":true,"path":path}), held.gate, held.automated)?;
|
||||
}
|
||||
replacement.params
|
||||
}
|
||||
};
|
||||
authorize(app, command, ¶ms, held.gate, held.automated)?;
|
||||
let saved = app.session.authorize;
|
||||
app.session.authorize = super::continuation_gate(held.gate, saved)?;
|
||||
app.session.authorize = effective_gate(app, held.gate, held.automated)?;
|
||||
let result = app.run(command, params);
|
||||
app.session.authorize = saved;
|
||||
result
|
||||
}
|
||||
|
||||
/// Discard only the matching dialog's held edit.
|
||||
pub(crate) fn cancel(app: &mut PhotocraftApp, id: u64) {
|
||||
if app.held_actions_library.as_ref().is_some_and(|held| held.id == id) {
|
||||
app.held_actions_library = None;
|
||||
|
||||
@@ -119,6 +119,50 @@ fn stale_same_caption_selection_does_not_follow_a_compacted_slot() {
|
||||
assert!(duplicate_selection(&mut app).is_err());
|
||||
}
|
||||
|
||||
/// The selected path and held replacement bytes need current authorization.
|
||||
#[test]
|
||||
fn replace_picker_checks_selected_source_and_rechecks_payload_on_confirm() {
|
||||
use crate::file_dialog::{FileDialogAnswer, fake};
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
let (file_dialog, _) = fake(vec![
|
||||
Some(FileDialogAnswer::Paths(vec!["blocked-actions.atn".into()])),
|
||||
Some(FileDialogAnswer::Contents("replacement.atn".into(), action_set_bytes("Replacement", "Invert"))),
|
||||
]);
|
||||
let permit_data = Rc::new(Cell::new(true));
|
||||
let policy = Rc::clone(&permit_data);
|
||||
let automation_command = Box::new(move |id: &str, p: &Value| -> Result<(), String> {
|
||||
if id != "actions.replace" {
|
||||
return Ok(());
|
||||
}
|
||||
if p.get("path").is_some() {
|
||||
return Err("path denied by policy".into());
|
||||
}
|
||||
if p.get("data").is_some() && policy.get() { Ok(()) } else { Err("payload denied by policy".into()) }
|
||||
});
|
||||
let mut app = PhotocraftApp::new(
|
||||
photocraft_engine::Session::new(),
|
||||
crate::Services { file_dialog: Some(file_dialog), automation_command: Some(automation_command), ..Default::default() },
|
||||
);
|
||||
app.run("actions.newSet", json!({"name":"Keep"})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
for attempt in 0..2 {
|
||||
app.automation_input = true;
|
||||
assert!(library::open(&mut app, library::Kind::Replace).is_ok());
|
||||
app.automation_input = false;
|
||||
app.poll_file_dialog(&ctx, None);
|
||||
app.poll_file_dialog(&ctx, None);
|
||||
if attempt == 0 {
|
||||
assert!(app.ui.status.contains("path denied by policy"), "{}", app.ui.status);
|
||||
assert!(app.held_actions_library.is_none());
|
||||
}
|
||||
}
|
||||
let dialog = app.ui.dialogs.iter().find(|dialog| library::owns(&dialog.fields)).unwrap().id;
|
||||
permit_data.set(false);
|
||||
assert!(crate::dialogs::confirm(&mut app, dialog).is_err());
|
||||
assert_eq!(app.session.actions.sets, ["Keep"]);
|
||||
assert!(app.session.actions.set_rows.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_picker_holds_validated_bytes_until_confirmed() {
|
||||
use crate::file_dialog::{FileDialogAnswer, fake};
|
||||
|
||||
Reference in new issue
Block a user