tablo_core/panel/
impersonation.rs1use topcoat::{
5 context::Cx,
6 icon::icon,
7 view::{BoxView, ViewExt, attributes, view},
8};
9
10use crate::{Action, ActionDef, Outcome, PanelUser, Places, Resource, WriteError};
11
12#[derive(Debug, Clone, Copy)]
34pub struct Impersonate;
35
36impl<R: Resource> Action<R> for Impersonate
37where
38 R::Model: PanelUser,
39{
40 type Input = ();
41
42 const NAME: &'static str = "impersonate";
43
44 fn declare() -> ActionDef<R> {
45 ActionDef::new()
46 .places(Places::RECORD)
47 .icon(tablo_ui::icons::EYE)
48 .label(crate::text!("tablo-impersonation-start"))
49 .can_run(|cx, record: &R::Model| crate::auth::may_impersonate(cx, record))
50 .success(|_| crate::text!("tablo-impersonation-started"))
51 }
52
53 async fn run(
54 cx: &Cx,
55 records: &[R::Model],
56 (): (),
57 ex: &mut dyn toasty::Executor,
58 ) -> Result<Outcome, WriteError<()>> {
59 let [target] = records else {
60 return Err(WriteError::declined(crate::text!(
61 "tablo-impersonation-one"
62 )));
63 };
64 crate::auth::impersonate(cx, ex, target)
65 .await
66 .map_err(WriteError::Failed)?;
67 Ok(Outcome::done().redirect(crate::auth::panel_prefix(cx)))
68 }
69}
70
71pub(super) fn banner(cx: &Cx) -> BoxView<'_> {
74 match crate::auth::signed(cx) {
75 Some(signed) if signed.impersonator.is_some() => strip(cx, signed),
76 _ => view! {}.boxed(),
77 }
78}
79
80fn strip<'a>(cx: &'a Cx, signed: &'a crate::auth::SignedIn) -> BoxView<'a> {
81 let csrf = crate::csrf::ensure_token(cx);
82 let stop = crate::auth::stop_impersonating_url(cx);
83 let name = signed.user.display_name().to_owned();
84 view! {
85 <div
86 class="flex flex-wrap items-center justify-between gap-2 border-b border-warning/40 \
87 bg-warning/15 px-4 py-2 text-sm text-foreground"
88 role="status"
89 data-impersonating=(signed.user.user_id())
90 >
91 <span>(crate::t!(cx, "tablo-impersonation-banner", name = name))</span>
92 <form method="post" action=(stop)>
93 (crate::csrf::field(cx, &csrf))
94 tablo_ui::button(
95 variant: tablo_ui::ButtonVariant::Outline,
96 size: tablo_ui::ButtonSize::Sm,
97 attrs: attributes! { type="submit" },
98 icon(data: tablo_ui::icons::LOG_OUT)
99 (crate::t!(cx, "tablo-impersonation-stop"))
100 )
101 </form>
102 </div>
103 }
104 .boxed()
105}