mod targets;
use topcoat::{
context::Cx,
router::{
Body,
error::{bad_request, forbidden, not_found, see_other},
path_param_segment,
},
view::BoxView,
};
use self::targets::{load_targets, refuse};
use super::super::{
forms::{FormChrome, parse_form_body, render_form_page, truthy},
gate::{gate, landing_url},
write::commit_write,
};
use crate::{
db::{WriteTx, db},
form::{FieldErrors, FormValues},
notification::{Notification, set_notification},
panel::url::action_options_url,
policy::Ability,
resource::{
ActionEntry, ErasedInput, HeaderEntry, InputSpec, Mounted, Places, RESERVED_KEYS, Resource,
WriteError,
},
table::bulk_key,
topcoat_compat::async_page,
};
mod header;
use header::HeaderHooks;
pub(crate) use header::run_header;
#[derive(Clone, Copy)]
enum Target {
Record,
Bulk,
}
pub(crate) fn resource_delete<R: Resource>(cx: &Cx, body: Body) -> BoxView<'_> {
async_page(async move {
let resource = gate::<R>(cx)?;
mutate(
cx,
&resource,
body,
Target::Record,
Some(ActionEntry::delete()),
)
.await
})
}
pub(crate) fn resource_bulk_delete<R: Resource>(cx: &Cx, body: Body) -> BoxView<'_> {
async_page(async move {
let resource = gate::<R>(cx)?;
let action = Some(ActionEntry::bulk_delete());
mutate(cx, &resource, body, Target::Bulk, action).await
})
}
pub(crate) fn resource_record_action<R: Resource>(cx: &Cx, body: Body) -> BoxView<'_> {
async_page(async move {
let resource = gate::<R>(cx)?;
let action = custom_action(cx, &resource, Places::RECORD);
mutate(cx, &resource, body, Target::Record, action).await
})
}
pub(crate) fn resource_list_action<R: Resource>(cx: &Cx, body: Body) -> BoxView<'_> {
async_page(async move {
let resource = gate::<R>(cx)?;
let name = path_param_segment(cx, "action");
if let Some(header) = resource.header_actions.find(name).copied() {
if !header_allowed(cx, &resource, &header) {
return Err(forbidden().into());
}
let hooks = HeaderHooks::of::<R>();
return run_header(cx, body, header, &resource.url, Some(hooks)).await;
}
let action = custom_action(cx, &resource, Places::BULK);
mutate(cx, &resource, body, Target::Bulk, action).await
})
}
pub(super) fn header_allowed<R: Resource>(
cx: &Cx,
resource: &Mounted<R>,
header: &HeaderEntry,
) -> bool {
resource.can(cx, Ability::ViewAny)
&& resource.can(
cx,
Ability::RunHeader {
action: header.name,
},
)
&& (header.can_run)(cx)
}
fn custom_action<R: Resource>(
cx: &Cx,
resource: &Mounted<R>,
places: Places,
) -> Option<ActionEntry<R>> {
let name = path_param_segment(cx, "action");
resource
.actions
.find(name)
.filter(|a| a.places.intersects(places))
.copied()
}
struct Posted {
ids: Option<String>,
pending: Pending,
}
async fn read_post(
cx: &Cx,
body: Body,
name: &str,
confirm: bool,
input: &InputSpec,
) -> Result<Posted, topcoat::Error> {
let parts = parse_form_body(cx, body).await?;
crate::csrf::verify(cx, parts.last(crate::csrf::FIELD_NAME))?;
if confirm && !parts.last("confirm").is_some_and(truthy) {
return Err(bad_request(format!("{name} requires confirmation")).into());
}
let pending = read_input(cx, name, input, &parts.pairs).await?;
Ok(Posted {
ids: parts.last("ids").map(str::to_string),
pending,
})
}
async fn mutate<'a, R: Resource>(
cx: &'a Cx,
resource: &Mounted<R>,
body: Body,
target: Target,
action: Option<ActionEntry<R>>,
) -> Result<BoxView<'a>, topcoat::Error> {
let resource_wide = action.map_or(Ability::ViewAny, |action| action.resource_wide);
if !resource.can(cx, resource_wide) {
return Err(forbidden().into());
}
let Some(action) = action else {
return Err(not_found().into());
};
let Posted {
ids: posted_ids,
pending,
} = read_post(cx, body, action.name, action.confirm, &action.input).await?;
let ids = match target {
Target::Record => vec![path_param_segment(cx, "id").to_string()],
Target::Bulk => {
let ids = parse_bulk_ids(posted_ids.as_deref().unwrap_or_default(), MAX_BULK_IDS);
if ids.is_empty() {
set_notification(
cx,
Notification::error(crate::t!(cx, "tablo-list-select-first")),
);
return Err(see_other(landing_url(cx, &resource.url)).into());
}
if ids.len() > MAX_BULK_IDS {
return Err(bad_request(format!("too many ids (max {MAX_BULK_IDS})")).into());
}
ids
}
};
let mut db = db(cx)?;
let mut tx = WriteTx::begin(cx, &mut db)
.await
.map_err(crate::error::unavailable)?;
let rows = load_targets(cx, resource, &ids, target, action.trashed, &mut tx).await?;
if rows.iter().any(|row| !resource.can(cx, Ability::View(row))) {
return Err(forbidden().into());
}
let (rows, refused): (Vec<R::Model>, Vec<R::Model>) = rows
.into_iter()
.partition(|row| action.allows(resource, cx, row));
if rows.is_empty() {
return Err(refuse(cx, resource, &action, target, refused.len()));
}
let skipped = (!refused.is_empty()).then(|| {
let skipped = refused.len();
let selected = rows.len() + skipped;
format!(
" {}",
crate::t!(
cx,
"tablo-action-skipped",
skipped = skipped,
selected = selected
)
)
});
let label = (action.label)(cx);
let page = |page: InputPage, label: String| {
let title = match target {
Target::Record => crate::t!(
cx,
"tablo-action-on-record",
action = label.clone(),
record = resource.record_title(cx, &rows[0], &ids[0]),
),
Target::Bulk => {
crate::t!(
cx,
"tablo-action-on-records",
action = label.clone(),
count = rows.len(),
) + skipped.as_deref().unwrap_or_default()
}
};
let mut hidden = Vec::new();
if let (Target::Bulk, Some(ids)) = (target, &posted_ids) {
hidden.push(("ids".to_string(), ids.clone()));
}
let chrome = page.chrome(
action.name,
title,
label,
action.confirm,
hidden,
&resource.url,
);
(page, chrome)
};
let (input, posted) = match pending {
Pending::Ready(input) => (input, FormValues::new()),
Pending::Submitted {
input,
values: posted,
} => {
let errors = (action.input.schema)()
.recheck_relationships(cx, &posted, &FormValues::new(), &mut *tx)
.await;
if !errors.is_empty() {
let (page, chrome) = page(
InputPage {
values: posted,
errors,
},
label,
);
drop(tx);
return page.render(cx, &action.input, chrome).await;
}
(input, posted)
}
Pending::Page(input_page) => {
let (input_page, chrome) = page(input_page, label);
drop(tx);
return input_page.render(cx, &action.input, chrome).await;
}
};
let skipped_note = skipped.clone().unwrap_or_default();
let note = (action.success)(cx, rows.len()) + &skipped_note;
let written = match (action.run)(cx, &rows, input, &mut *tx).await {
Ok(outcome) => Ok(outcome.skipping(&skipped_note)),
Err(WriteError::Refused(errors)) => {
drop(tx);
let errors = refusal(action.name, &action.input, errors)?;
let (page, chrome) = page(
InputPage {
values: posted,
errors,
},
label,
);
return page.render(cx, &action.input, chrome).await;
}
Err(WriteError::Failed(error)) => Err(error),
};
let written = written.map(|outcome| (rows, outcome));
commit_write(
cx,
resource,
tx,
written,
action.change,
note,
action.failure,
)
.await
}
pub(super) fn refusal(
name: &str,
spec: &InputSpec,
errors: FieldErrors,
) -> Result<FieldErrors, topcoat::Error> {
if errors.is_empty() {
return Err(crate::error::declaration(format!(
"action '{name}' refused its input with no field error"
)));
}
if let Some(key) = (spec.schema)().unplaced_error(&errors) {
return Err(crate::error::declaration(format!(
"action '{name}' refused key `{key}`, which no input control renders"
)));
}
Ok(errors)
}
pub(super) struct InputPage {
pub(super) values: FormValues,
pub(super) errors: FieldErrors,
}
pub(super) enum Pending {
Ready(ErasedInput),
Submitted {
input: ErasedInput,
values: FormValues,
},
Page(InputPage),
}
pub(super) async fn read_input(
cx: &Cx,
name: &str,
spec: &InputSpec,
posted: &[(String, String)],
) -> Result<Pending, topcoat::Error> {
if !spec.takes_input {
let input = (spec.parse)(cx, &FormValues::new()).map_err(|_| {
crate::error::declaration(format!(
"action '{name}' declares no input field but refuses an empty input"
))
})?;
return Ok(Pending::Ready(input));
}
let schema = (spec.schema)();
let input = schema.read_input(cx, posted, &RESERVED_KEYS)?;
let mut errors = FieldErrors::new();
let parsed = match (spec.parse)(cx, &input) {
Ok(parsed) => Some(parsed),
Err(refused) => {
for error in refused {
errors.push(error);
}
None
}
};
schema
.check_controls(cx, &input, &FormValues::new(), &mut errors)
.await;
if let Some(key) = schema.unplaced_error(&errors) {
return Err(crate::error::declaration(format!(
"action '{name}' refused key `{key}`, which no input control renders"
)));
}
match parsed {
Some(parsed) if errors.is_empty() => Ok(Pending::Submitted {
input: parsed,
values: input,
}),
_ => Ok(Pending::Page(InputPage {
values: input,
errors,
})),
}
}
impl InputPage {
fn chrome(
&self,
name: &str,
title: String,
label: String,
confirm: bool,
hidden: Vec<(String, String)>,
home: &str,
) -> FormChrome<'static> {
let mut carried = Vec::new();
if confirm {
carried.push(("confirm".to_string(), "1".to_string()));
}
carried.extend(hidden);
FormChrome::action(
title,
label,
confirm,
carried,
home.to_string(),
action_options_url(home, name),
)
}
pub(super) async fn render<'a>(
self,
cx: &'a Cx,
spec: &InputSpec,
chrome: FormChrome<'a>,
) -> topcoat::Result<BoxView<'a>> {
render_form_page(
cx,
&(spec.schema)(),
chrome,
&self.values,
&self.errors,
&Default::default(),
)
.await
}
}
pub(super) const MAX_BULK_IDS: usize = 400;
pub(super) fn parse_bulk_ids(raw: &str, max: usize) -> Vec<String> {
let mut ids: Vec<String> = Vec::new();
let mut seen = std::collections::HashSet::new();
let parts = raw
.split(',')
.map(str::trim)
.filter(|part| !part.is_empty());
for part in parts {
let key = bulk_key(part);
if seen.insert(key.clone()) {
ids.push(key);
if ids.len() > max {
break;
}
}
}
ids
}
#[cfg(test)]
mod tests;