actix-admin 0.9.0

An admin interface for actix-web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use crate::view_model::{
    ActixAdminFilterOperator, ActixAdminViewModelFilter, ActixAdminViewModelParams,
};
use crate::{ActixAdminError, ActixAdminErrorType, ActixAdminViewModelField};
use actix_multipart::Multipart;
use async_trait::async_trait;
use chrono::{NaiveDate, NaiveDateTime};
use futures_util::stream::StreamExt as _;
use sea_orm::{DatabaseConnection, EntityTrait};
use serde_derive::Serialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

/// Maximum total upload size (default 25MB). Individual deployments should
/// enforce their own limits via `actix_multipart::form::MultipartFormConfig`
/// but this provides a defensive per-field cap so that a single field cannot
/// exhaust memory in `create_from_payload`.
pub const DEFAULT_MAX_FIELD_SIZE_BYTES: usize = 25 * 1024 * 1024;

/// Sanitize a user-provided filename so that it can never traverse outside the
/// upload directory. Strips path separators, `..`, control chars and NULs, and
/// falls back to a timestamp-based name if the result would be empty.
pub fn sanitize_upload_filename(raw: &str) -> String {
    // Take just the last path component. Split manually on both '/' and '\\'
    // so we correctly reject Windows-style traversal even on unix hosts.
    let last = raw.rsplit(['/', '\\']).next().unwrap_or("");

    let cleaned: String = sanitize_filename::sanitize_with_options(
        last,
        sanitize_filename::Options {
            windows: true,
            truncate: true,
            replacement: "_",
        },
    )
    .chars()
    .filter(|c| !c.is_control() && *c != '\0')
    .collect();
    let cleaned = cleaned.trim_matches(|c: char| c == '.' || c.is_whitespace());
    if cleaned.is_empty() {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis())
            .unwrap_or(0);
        format!("upload_{now}")
    } else {
        cleaned.to_string()
    }
}

#[async_trait]
pub trait ActixAdminModelTrait {
    async fn list_model(
        db: &DatabaseConnection,
        params: &ActixAdminViewModelParams,
        filter_values: HashMap<String, Option<String>>,
    ) -> Result<(Option<u64>, Vec<ActixAdminModel>), ActixAdminError>;
    fn get_fields() -> &'static [ActixAdminViewModelField];
    fn validate_model(model: &mut ActixAdminModel);
    async fn load_foreign_keys(models: &mut [ActixAdminModel], db: &DatabaseConnection);
}

pub trait ActixAdminModelValidationTrait<T> {
    fn validate(_model: &T) -> HashMap<String, String> {
        HashMap::new()
    }
}

/// A single filter registered on an entity via `ActixAdminModelFilterTrait`.
///
/// The `filter` closure receives the current query, the user-provided
/// value (a plain `Option<String>`) and — when the filter opted into
/// operator selection — the operator picked in the UI.
pub struct ActixAdminModelFilter<E: EntityTrait> {
    pub name: String,
    pub filter_type: ActixAdminModelFilterType,
    /// Underlying filter callback (value-only or operator-aware).
    pub filter: FilterFn<E>,
    pub values: Option<Vec<(String, String)>>,
    pub foreign_key: Option<String>,
    /// Operators the user may pick from. When empty, no operator selector is
    /// rendered and operator-aware filters receive `None`.
    pub operators: Vec<ActixAdminFilterOperator>,
}

/// Storage for the filter callback. Two shapes are supported so that simple
/// value-only filters remain a one-liner, while operator-aware filters can
/// react to the user's chosen comparator.
#[allow(clippy::type_complexity)]
pub enum FilterFn<E: EntityTrait> {
    ValueOnly(fn(sea_orm::Select<E>, Option<String>) -> sea_orm::Select<E>),
    WithOp(
        fn(
            sea_orm::Select<E>,
            Option<String>,
            Option<ActixAdminFilterOperator>,
        ) -> sea_orm::Select<E>,
    ),
}

impl<E: EntityTrait> FilterFn<E> {
    /// Apply the underlying callback to `query`. Operator-only closures
    /// receive `operator`; value-only closures ignore it.
    pub fn apply(
        &self,
        query: sea_orm::Select<E>,
        value: Option<String>,
        operator: Option<ActixAdminFilterOperator>,
    ) -> sea_orm::Select<E> {
        match self {
            FilterFn::ValueOnly(f) => f(query, value),
            FilterFn::WithOp(f) => f(query, value, operator),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub enum ActixAdminModelFilterType {
    Text,
    SelectList,
    Date,
    DateTime,
    Checkbox,
    TomSelectSearch,
}

impl<E: EntityTrait> ActixAdminModelFilter<E> {
    /// Build a value-only filter. The closure receives the current query
    /// plus the user-typed value; operator information is not surfaced.
    pub fn new(
        name: impl Into<String>,
        filter_type: ActixAdminModelFilterType,
        filter: fn(sea_orm::Select<E>, Option<String>) -> sea_orm::Select<E>,
    ) -> Self {
        Self {
            name: name.into(),
            filter_type,
            filter: FilterFn::ValueOnly(filter),
            values: None,
            foreign_key: None,
            operators: Vec::new(),
        }
    }

    /// Build an operator-aware filter. The closure additionally receives
    /// the operator the user picked in the UI (if any).
    pub fn with_op(
        name: impl Into<String>,
        filter_type: ActixAdminModelFilterType,
        filter: fn(
            sea_orm::Select<E>,
            Option<String>,
            Option<ActixAdminFilterOperator>,
        ) -> sea_orm::Select<E>,
    ) -> Self {
        Self {
            name: name.into(),
            filter_type,
            filter: FilterFn::WithOp(filter),
            values: None,
            foreign_key: None,
            operators: Vec::new(),
        }
    }

    pub fn with_operators(mut self, operators: Vec<ActixAdminFilterOperator>) -> Self {
        self.operators = operators;
        self
    }

    /// Replace the filter callback with an operator-aware one.
    ///
    /// Kept for backwards compatibility — new code should use
    /// [`Self::with_op`] directly.
    pub fn with_operator_filter(
        mut self,
        f: fn(
            sea_orm::Select<E>,
            Option<String>,
            Option<ActixAdminFilterOperator>,
        ) -> sea_orm::Select<E>,
    ) -> Self {
        self.filter = FilterFn::WithOp(f);
        self
    }

    pub fn with_foreign_key(mut self, fk: impl Into<String>) -> Self {
        self.foreign_key = Some(fk.into());
        self
    }

    pub fn with_values(mut self, values: Vec<(String, String)>) -> Self {
        self.values = Some(values);
        self
    }
}

#[async_trait]
pub trait ActixAdminModelFilterTrait<E: EntityTrait> {
    fn get_filter() -> Vec<ActixAdminModelFilter<E>> {
        Vec::new()
    }
    async fn get_filter_values(
        _filter: &ActixAdminModelFilter<E>,
        _db: &DatabaseConnection,
    ) -> Option<Vec<(String, String)>> {
        None
    }
}

impl<T: EntityTrait> From<ActixAdminModelFilter<T>> for ActixAdminViewModelFilter {
    fn from(filter: ActixAdminModelFilter<T>) -> Self {
        ActixAdminViewModelFilter {
            name: filter.name,
            value: None,
            values: None,
            filter_type: Some(filter.filter_type),
            foreign_key: None,
            operators: filter.operators,
            operator: None,
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct ActixAdminModel {
    pub primary_key: Option<String>,
    pub values: HashMap<String, String>,
    pub fk_values: HashMap<String, String>,
    pub errors: HashMap<String, String>,
    pub custom_errors: HashMap<String, String>,
    pub display_name: Option<String>,
}

impl ActixAdminModel {
    pub fn create_empty() -> ActixAdminModel {
        ActixAdminModel {
            primary_key: None,
            values: HashMap::new(),
            errors: HashMap::new(),
            custom_errors: HashMap::new(),
            fk_values: HashMap::new(),
            display_name: None,
        }
    }

    pub async fn create_from_payload(
        id: Option<String>,
        mut payload: Multipart,
        file_upload_folder: &str,
    ) -> Result<ActixAdminModel, ActixAdminError> {
        let mut hashmap = HashMap::<String, String>::new();

        while let Some(item) = payload.next().await {
            let mut field = item?;

            let mut binary_data: Vec<u8> = Vec::new();
            while let Some(chunk) = field.next().await {
                let chunk = chunk?;
                if binary_data.len().saturating_add(chunk.len()) > DEFAULT_MAX_FIELD_SIZE_BYTES {
                    return Err(ActixAdminError::new(
                        ActixAdminErrorType::UploadError,
                        "Uploaded field exceeds maximum size",
                    ));
                }
                binary_data.extend_from_slice(&chunk);
            }

            let content_disposition = match field.content_disposition() {
                Some(cd) => cd.clone(),
                None => continue,
            };
            let field_name = match content_disposition.get_name() {
                Some(name) => name.to_string(),
                None => continue,
            };

            if let Some(raw_filename) = content_disposition.get_filename() {
                // Skip empty file uploads silently (browsers submit empty file fields).
                if raw_filename.is_empty() && binary_data.is_empty() {
                    continue;
                }

                let mut filename = sanitize_upload_filename(raw_filename);

                let base = PathBuf::from(file_upload_folder);
                let mut file_path = base.join(&filename);

                // Avoid overwriting existing files by prefixing a timestamp.
                if file_path.exists() {
                    let ts = SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .map(|d| d.as_millis())
                        .unwrap_or(0);
                    filename = format!("{ts}_{filename}");
                    file_path = base.join(&filename);
                }

                // Defense in depth: reject any joined path that escapes the base.
                let canonical_base = base.canonicalize().unwrap_or_else(|_| base.clone());
                let parent = file_path.parent().unwrap_or(&base);
                let canonical_parent = parent
                    .canonicalize()
                    .unwrap_or_else(|_| parent.to_path_buf());
                if !canonical_parent.starts_with(&canonical_base) {
                    return Err(ActixAdminError::new(
                        ActixAdminErrorType::UploadError,
                        "Uploaded filename resolves outside the upload directory",
                    ));
                }

                let mut f = File::create(&file_path)?;
                f.write_all(&binary_data)?;

                hashmap.insert(field_name, filename);
            } else if let Ok(res_string) = String::from_utf8(binary_data) {
                hashmap.insert(field_name, res_string);
            }
        }

        Ok(ActixAdminModel {
            primary_key: id,
            values: hashmap,
            ..ActixAdminModel::create_empty()
        })
    }

    pub fn get_value<T: std::str::FromStr>(
        &self,
        key: &str,
        is_option_or_string: bool,
        is_allowed_to_be_empty: bool,
    ) -> Result<Option<T>, String> {
        self.get_value_by_closure(key, is_option_or_string, is_allowed_to_be_empty, |val| {
            val.parse::<T>()
        })
    }

    pub fn get_datetime(
        &self,
        key: &str,
        is_option_or_string: bool,
        is_allowed_to_be_empty: bool,
    ) -> Result<Option<NaiveDateTime>, String> {
        self.get_value_by_closure(key, is_option_or_string, is_allowed_to_be_empty, |val| {
            NaiveDateTime::parse_from_str(val, "%Y-%m-%dT%H:%M")
        })
    }

    pub fn get_date(
        &self,
        key: &str,
        is_option_or_string: bool,
        is_allowed_to_be_empty: bool,
    ) -> Result<Option<NaiveDate>, String> {
        self.get_value_by_closure(key, is_option_or_string, is_allowed_to_be_empty, |val| {
            NaiveDate::parse_from_str(val, "%Y-%m-%d")
        })
    }

    pub fn get_bool(
        &self,
        key: &str,
        is_option_or_string: bool,
        is_allowed_to_be_empty: bool,
    ) -> Result<Option<bool>, String> {
        // A missing/invalid bool from a checkbox means "unchecked".
        let val =
            self.get_value_by_closure(key, is_option_or_string, is_allowed_to_be_empty, |val| {
                Ok::<bool, std::str::ParseBoolError>(matches!(val.as_str(), "true" | "yes"))
            });
        Ok(val.unwrap_or(Some(false)))
    }

    fn get_value_by_closure<T: std::str::FromStr>(
        &self,
        key: &str,
        is_option_or_string: bool,
        is_allowed_to_be_empty: bool,
        f: impl Fn(&String) -> Result<T, <T as std::str::FromStr>::Err>,
    ) -> Result<Option<T>, String> {
        match self.values.get(key) {
            Some(val) => {
                if val.is_empty() && is_option_or_string {
                    return if is_allowed_to_be_empty {
                        Ok(None)
                    } else {
                        Err("Cannot be empty".to_string())
                    };
                }
                f(val).map(Some).map_err(|_| "Invalid Value".to_string())
            }
            None => match (is_option_or_string, is_allowed_to_be_empty) {
                (true, true) => Ok(None),
                (true, false) => Err("Cannot be empty".to_string()),
                (false, _) => Err("Invalid Value".to_string()),
            },
        }
    }

    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty() || !self.custom_errors.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn model_with(key: &str, value: &str) -> ActixAdminModel {
        let mut m = ActixAdminModel::create_empty();
        m.values.insert(key.to_string(), value.to_string());
        m
    }

    // ---- sanitize_upload_filename ----

    #[test]
    fn sanitize_strips_traversal() {
        assert_eq!(sanitize_upload_filename("../../etc/passwd"), "passwd");
        assert_eq!(sanitize_upload_filename("..\\..\\evil.exe"), "evil.exe");
        assert_eq!(sanitize_upload_filename("/absolute/path/x.txt"), "x.txt");
    }

    #[test]
    fn sanitize_removes_control_chars_and_nul() {
        let out = sanitize_upload_filename("foo\0bar\nbaz.txt");
        assert!(!out.contains('\0'));
        assert!(!out.contains('\n'));
        assert!(out.ends_with(".txt"));
    }

    #[test]
    fn sanitize_empty_or_dotfile_falls_back() {
        let out = sanitize_upload_filename("");
        assert!(out.starts_with("upload_"));
        let out = sanitize_upload_filename("...");
        // Result must not resolve to a traversal or hidden dotfile.
        assert!(!out.contains(".."));
        assert!(!out.starts_with('.'));
        assert!(!out.is_empty());
    }

    // ---- get_value matrix ----

    #[test]
    fn get_value_present_parses() {
        let m = model_with("n", "42");
        let v: Option<i32> = m.get_value("n", false, false).unwrap();
        assert_eq!(v, Some(42));
    }

    #[test]
    fn get_value_invalid_returns_err() {
        let m = model_with("n", "abc");
        let r: Result<Option<i32>, _> = m.get_value("n", false, false);
        assert!(r.is_err());
    }

    #[test]
    fn get_value_empty_string_option_allowed_returns_none() {
        let m = model_with("s", "");
        let r: Result<Option<String>, _> = m.get_value("s", true, true);
        assert_eq!(r.unwrap(), None);
    }

    #[test]
    fn get_value_empty_string_option_not_allowed_returns_err() {
        let m = model_with("s", "");
        let r: Result<Option<String>, _> = m.get_value("s", true, false);
        assert!(r.is_err());
    }

    #[test]
    fn get_value_missing_option_allowed_returns_none() {
        let m = ActixAdminModel::create_empty();
        let r: Result<Option<String>, _> = m.get_value("missing", true, true);
        assert_eq!(r.unwrap(), None);
    }

    #[test]
    fn get_value_missing_option_not_allowed_returns_err() {
        let m = ActixAdminModel::create_empty();
        let r: Result<Option<String>, _> = m.get_value("missing", true, false);
        assert!(r.is_err());
    }

    #[test]
    fn get_value_missing_non_option_returns_err() {
        let m = ActixAdminModel::create_empty();
        let r: Result<Option<i32>, _> = m.get_value("missing", false, true);
        assert!(r.is_err());
    }

    // ---- get_bool ----

    #[test]
    fn get_bool_true_yes() {
        let m = model_with("b", "true");
        assert_eq!(m.get_bool("b", false, true).unwrap(), Some(true));
        let m = model_with("b", "yes");
        assert_eq!(m.get_bool("b", false, true).unwrap(), Some(true));
    }

    #[test]
    fn get_bool_other_values_are_false() {
        let m = model_with("b", "off");
        assert_eq!(m.get_bool("b", false, true).unwrap(), Some(false));
    }

    #[test]
    fn get_bool_missing_falls_back_to_false() {
        let m = ActixAdminModel::create_empty();
        assert_eq!(m.get_bool("b", false, false).unwrap(), Some(false));
    }

    // ---- get_date / get_datetime ----

    #[test]
    fn get_date_valid() {
        let m = model_with("d", "2024-01-02");
        let d = m.get_date("d", false, false).unwrap().unwrap();
        assert_eq!(d, chrono::NaiveDate::from_ymd_opt(2024, 1, 2).unwrap());
    }

    #[test]
    fn get_date_invalid() {
        let m = model_with("d", "nope");
        assert!(m.get_date("d", false, false).is_err());
    }

    #[test]
    fn get_datetime_valid_local_form() {
        let m = model_with("d", "2024-01-02T03:04");
        let dt = m.get_datetime("d", false, false).unwrap().unwrap();
        assert_eq!(
            dt,
            chrono::NaiveDate::from_ymd_opt(2024, 1, 2)
                .unwrap()
                .and_hms_opt(3, 4, 0)
                .unwrap()
        );
    }

    // ---- has_errors ----

    #[test]
    fn has_errors_reports_both_maps() {
        let mut m = ActixAdminModel::create_empty();
        assert!(!m.has_errors());
        m.errors.insert("a".into(), "b".into());
        assert!(m.has_errors());
        m.errors.clear();
        m.custom_errors.insert("a".into(), "b".into());
        assert!(m.has_errors());
    }
}