fastauth-plugin-admin 0.3.0

Admin plugin for fastauth: roles and permissions, bans, impersonation and user management.
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
//! The `/admin/*` endpoints, the ban check, and the helpers they share.

use fastauth_core::events::{PasswordChanged, UserDeleted, UserUpdated};
use fastauth_core::plugin_prelude::*;
use fastauth_core::routes::Status;
use fastauth_core::schema::ColumnDef;
use serde_json::{Map, Value as Json};
use time::format_description::well_known::Rfc3339;

use crate::{Admin, AdminError, AdminSession, AdminUser, Permissions};

const ADMIN_SESSION_COOKIE: &str = "admin_session";
const SESSION_TOKEN_COOKIE: &str = "session_token";
const DONT_REMEMBER_COOKIE: &str = "dont_remember";
const SESSION_CACHE_COOKIE: &str = "session_data";
const BAN_COLUMNS: [&str; 3] = ["banned", "banReason", "banExpires"];

/// Who an admin endpoint runs for: an HTTP request, whose session needs the right
/// permissions, or the server API, which is trusted.
#[derive(Clone, Debug)]
pub struct Caller {
    plugin: Arc<Admin>,
    server: bool,
}

impl Caller {
    pub(crate) fn http(plugin: Arc<Admin>) -> Self {
        Self {
            plugin,
            server: false,
        }
    }

    pub(crate) fn server(plugin: Arc<Admin>) -> Self {
        Self {
            plugin,
            server: true,
        }
    }

    /// The caller's session, after checking it may do `action` on `resource`. `None` for
    /// server calls; 401 without a session; `denied` without the permission.
    fn check<'c, D: Adapter>(
        &self,
        ctx: &'c Ctx<D, MaybeAuthenticated>,
        resource: &str,
        action: &str,
        denied: AdminError,
    ) -> Result<Option<&'c SessionView>, AuthError> {
        if self.server {
            return Ok(None);
        }
        let session = ctx.session().ok_or_else(AuthError::unauthorized)?;
        if !may(&self.plugin, Some(session), resource, action)? {
            return Err(denied.into());
        }
        Ok(Some(session))
    }

    /// Like `check` for a second permission the request needs (e.g. `set-role` on create).
    fn also(
        &self,
        session: Option<&SessionView>,
        resource: &str,
        action: &str,
        denied: AdminError,
    ) -> Result<(), AuthError> {
        if may(&self.plugin, session, resource, action)? {
            Ok(())
        } else {
            Err(denied.into())
        }
    }
}

/// Whether the session may do `action` on `resource`; server calls (`None`) may do anything.
fn may(
    plugin: &Admin,
    session: Option<&SessionView>,
    resource: &str,
    action: &str,
) -> Result<bool, AuthError> {
    let Some(session) = session else {
        return Ok(true);
    };
    let fields = admin_fields(&session.user)?;
    Ok(plugin.has_permission(
        &session.user.id,
        fields.role.as_deref(),
        &Permissions::new().allow(resource, [action]),
    ))
}

fn admin_fields(user: &UserView) -> Result<AdminUser, AuthError> {
    user.fields().map_err(AuthError::internal)
}

/// The error a banned user's sign-in gets, or `None`. Lifts the ban when it has expired.
pub(crate) async fn ban_error<D: Adapter, A>(
    plugin: &Admin,
    ctx: &Ctx<D, A>,
    user: &UserView,
) -> Result<Option<AuthError>, AuthError> {
    let fields = admin_fields(user)?;
    if !fields.banned {
        return Ok(None);
    }
    if fields.ban_expires.is_some_and(|t| t <= ctx.now()) {
        lift_ban(ctx, &user.id).await?;
        return Ok(None);
    }
    Ok(Some(plugin.banned_error(fields.ban_reason.as_deref())))
}

async fn lift_ban<D: Adapter, A>(ctx: &Ctx<D, A>, user_id: &Id) -> Result<(), AuthError> {
    ctx.db()
        .update::<User>(User::ID.eq(user_id))
        .set(AdminUser::BANNED, false)
        .set(AdminUser::BAN_REASON, None::<String>)
        .set(AdminUser::BAN_EXPIRES, None::<OffsetDateTime>)
        .exec()
        .await?;
    Ok(())
}

async fn find_user<D: Adapter, A>(ctx: &Ctx<D, A>, id: &Id) -> Result<UserView, AuthError> {
    ctx.internal()
        .find_user_by_id(id)
        .await?
        .ok_or_else(user_not_found)
}

fn user_not_found() -> AuthError {
    AuthError::new(
        StatusCode::NOT_FOUND,
        codes::USER_NOT_FOUND,
        "User not found",
    )
}

fn invalid_field(message: String) -> AuthError {
    AuthError::bad_request(codes::INVALID_FIELD, message)
}

fn camel_case(snake: &str) -> String {
    let mut out = String::with_capacity(snake.len());
    let mut upper = false;
    for ch in snake.chars() {
        if ch == '_' {
            upper = true;
        } else if upper {
            out.extend(ch.to_uppercase());
            upper = false;
        } else {
            out.push(ch);
        }
    }
    out
}

/// The `user` column a client names in camelCase. Hidden columns don't exist for clients.
fn user_column<D: Adapter, A>(ctx: &Ctx<D, A>, key: &str) -> Result<ColumnDef, AuthError> {
    ctx.schema()
        .expect(User::TABLE)
        .columns
        .iter()
        .find(|c| c.returned && camel_case(c.name) == key)
        .copied()
        .ok_or_else(|| invalid_field(format!("unknown field `{key}`")))
}

fn column_value(column: &ColumnDef, key: &str, json: &Json) -> Result<Value, AuthError> {
    Value::from_json(column.ty, json)
        .filter(|v| !v.is_null() || column.nullable)
        .ok_or_else(|| invalid_field(format!("field `{key}` has the wrong type")))
}

fn normalize_email(email: &str) -> Result<String, AuthError> {
    let email = email.trim().to_lowercase();
    let valid = email.len() <= 254
        && email.split_once('@').is_some_and(|(local, domain)| {
            !local.is_empty() && domain.contains('.') && !domain.starts_with('.')
        });
    if !valid {
        return Err(AuthError::bad_request(
            codes::INVALID_EMAIL,
            "Invalid email",
        ));
    }
    Ok(email)
}

fn email_taken() -> AuthError {
    AuthError::bad_request(
        codes::USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL,
        "User already exists. Use another email.",
    )
}

/// One role or several: `"admin"` or `["admin", "support"]`. Stored comma-separated.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Roles {
    One(String),
    Many(Vec<String>),
}

impl Roles {
    fn names(&self) -> Vec<&str> {
        let parts: Vec<&str> = match self {
            Roles::One(role) => vec![role.as_str()],
            Roles::Many(roles) => roles.iter().map(String::as_str).collect(),
        };
        parts
            .into_iter()
            .flat_map(|r| r.split(','))
            .map(str::trim)
            .filter(|r| !r.is_empty())
            .collect()
    }

    /// The `user.role` value, after checking every role is defined.
    fn stored(&self, plugin: &Admin) -> Result<String, AuthError> {
        let names = self.names();
        if names.is_empty() {
            return Err(AdminError::InvalidRoleType.into());
        }
        if names.iter().any(|r| !plugin.has_role_defined(r)) {
            return Err(AdminError::YouAreNotAllowedToSetNonExistentValue.into());
        }
        Ok(names.join(","))
    }

    fn from_json(json: &Json) -> Result<Self, AuthError> {
        serde_json::from_value(json.clone()).map_err(|_| AdminError::InvalidRoleType.into())
    }
}

impl From<&str> for Roles {
    fn from(role: &str) -> Self {
        Roles::One(role.to_owned())
    }
}

impl From<String> for Roles {
    fn from(role: String) -> Self {
        Roles::One(role)
    }
}

impl<const N: usize> From<[&str; N]> for Roles {
    fn from(roles: [&str; N]) -> Self {
        Roles::Many(roles.iter().map(|r| (*r).to_owned()).collect())
    }
}

/// `{ "user": {..} }`.
#[derive(Debug, Serialize)]
pub struct UserOutput {
    pub user: UserView,
}

/// `{ "success": true }`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub struct Success {
    pub success: bool,
}

/// Body of the endpoints that name one user.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserIdInput {
    pub user_id: Id,
}

// ---------------------------------------------------------------------------- create-user

/// Body of `/admin/create-user`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateUserInput {
    pub email: String,
    /// Creates a credential account. Without it the user signs in some other way.
    #[serde(default)]
    pub password: Option<String>,
    pub name: String,
    /// The user's roles; the default role when absent.
    #[serde(default)]
    pub role: Option<Roles>,
    /// Any other user columns by camelCase name, including app and plugin columns.
    #[serde(default)]
    pub data: Map<String, Json>,
}

/// `POST /admin/create-user`: needs `user:create`, plus `user:set-role` to pick a role
/// and `user:ban` to set ban columns.
pub struct CreateUser(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for CreateUser {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/create-user";
    type Access = MaybeAuthenticated;
    type Input = CreateUserInput;
    type Output = UserOutput;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        mut input: CreateUserInput,
    ) -> Result<UserOutput, AuthError> {
        let caller = &self.0;
        let plugin = &caller.plugin;
        let session = caller.check(
            &ctx,
            "user",
            "create",
            AdminError::YouAreNotAllowedToCreateUsers,
        )?;
        let data_role = input.data.remove("role");
        let role = match (input.role, data_role) {
            (Some(role), _) => Some(role),
            (None, Some(json)) => Some(Roles::from_json(&json)?),
            (None, None) => None,
        };
        let role = match role {
            Some(role) => {
                caller.also(
                    session,
                    "user",
                    "set-role",
                    AdminError::YouAreNotAllowedToChangeUsersRole,
                )?;
                role.stored(plugin)?
            }
            None => plugin.default_role.clone(),
        };
        if BAN_COLUMNS.iter().any(|k| input.data.contains_key(*k)) {
            caller.also(
                session,
                "user",
                "ban",
                AdminError::YouAreNotAllowedToBanUsers,
            )?;
        }
        let email = normalize_email(&input.email)?;
        let internal = ctx.internal();
        if let Some(password) = &input.password {
            internal.validate_new_password(password)?;
        }
        if internal.find_user_by_email(&email).await?.is_some() {
            return Err(email_taken());
        }
        let mut extra = vec![("role", Value::Text(role))];
        for (key, json) in &input.data {
            let column = user_column(&ctx, key)?;
            if matches!(
                column.name,
                "id" | "email" | "name" | "created_at" | "updated_at"
            ) {
                return Err(invalid_field(format!("field `{key}` can't be set")));
            }
            extra.push((column.name, column_value(&column, key, json)?));
        }
        let missing = ctx.schema().expect(User::TABLE).columns.iter().find(|c| {
            c.owner != "core"
                && !c.nullable
                && c.default.is_none()
                && !extra.iter().any(|(name, _)| *name == c.name)
        });
        if let Some(column) = missing {
            return Err(invalid_field(format!(
                "field `{}` is required",
                camel_case(column.name)
            )));
        }
        let account = match &input.password {
            Some(password) => Some(NewAccount::credential(
                ctx.passwords()
                    .hash(password)
                    .await
                    .map_err(AuthError::internal)?,
            )),
            None => None,
        };
        let user = NewUser {
            email,
            name: input.name,
            extra,
            ..NewUser::default()
        };
        let user = internal.create_user(user, account).await?;
        Ok(UserOutput { user })
    }
}

// ---------------------------------------------------------------------------- list-users

/// Which column `searchValue` matches.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SearchField {
    #[default]
    Email,
    Name,
}

/// How `searchValue` matches. Case-sensitive.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SearchOperator {
    #[default]
    Contains,
    StartsWith,
    EndsWith,
}

/// `asc` or `desc`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SortDirection {
    #[default]
    Asc,
    Desc,
}

/// How `filterValue` compares with `filterField`. `in` and `not_in` take a
/// comma-separated list; the text operators are case-sensitive.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WhereOperator {
    #[default]
    Eq,
    Ne,
    Lt,
    Lte,
    Gt,
    Gte,
    In,
    NotIn,
    Contains,
    StartsWith,
    EndsWith,
}

/// Query of `/admin/list-users`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListUsersQuery {
    pub search_value: Option<String>,
    pub search_field: Option<SearchField>,
    pub search_operator: Option<SearchOperator>,
    pub limit: Option<u64>,
    pub offset: Option<u64>,
    /// A user column in camelCase.
    pub sort_by: Option<String>,
    pub sort_direction: Option<SortDirection>,
    /// A user column in camelCase; `email` by default.
    pub filter_field: Option<String>,
    /// Parsed as the column's type: `true`, `42`, an RFC 3339 timestamp, or text.
    pub filter_value: Option<String>,
    pub filter_operator: Option<WhereOperator>,
}

/// `{ "users": [..], "total": 42, "limit": 10, "offset": 0 }`. `total` counts every match.
#[derive(Debug, Serialize)]
pub struct UserList {
    pub users: Vec<UserView>,
    pub total: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<u64>,
}

/// `GET /admin/list-users`: needs `user:list`. Search, filters, sorting and paging all
/// run in the database; `total` counts every match.
pub struct ListUsers(pub(crate) Caller);

/// A case-sensitive text match on a text column.
fn text_match(
    column: &ColumnDef,
    operator: WhereOperator,
    value: &str,
) -> Result<Where<User>, AuthError> {
    use fastauth_core::schema::ColumnType;
    if !matches!(column.ty, ColumnType::Text | ColumnType::Id) {
        return Err(invalid_field(format!(
            "`{}` isn't text",
            camel_case(column.name)
        )));
    }
    let col = Col::<User, String>::new(column.name);
    Ok(match operator {
        WhereOperator::StartsWith => col.starts_with(value),
        WhereOperator::EndsWith => col.ends_with(value),
        _ => col.contains(value),
    })
}

fn parse_filter<T: ColumnValue>(
    column: &'static str,
    operator: WhereOperator,
    raw: &str,
    parse: impl Fn(&str) -> Option<T>,
) -> Result<Where<User>, AuthError> {
    let col = Col::<User, T>::new(column);
    let one = || {
        parse(raw.trim())
            .ok_or_else(|| invalid_field(format!("can't compare `{column}` with `{raw}`")))
    };
    let many = || {
        raw.split(',')
            .map(|v| {
                parse(v.trim())
                    .ok_or_else(|| invalid_field(format!("can't compare `{column}` with `{v}`")))
            })
            .collect::<Result<Vec<T>, AuthError>>()
    };
    Ok(match operator {
        WhereOperator::Ne => col.ne(one()?),
        WhereOperator::Lt => col.lt(one()?),
        WhereOperator::Lte => col.lte(one()?),
        WhereOperator::Gt => col.gt(one()?),
        WhereOperator::Gte => col.gte(one()?),
        WhereOperator::In => col.in_(many()?),
        WhereOperator::NotIn => {
            let mut values = many()?.into_iter();
            let first = values
                .next()
                .ok_or_else(|| invalid_field("`not_in` needs a value".to_owned()))?;
            values.fold(col.ne(first), |filter, v| filter.and(col.ne(v)))
        }
        _ => col.eq(one()?),
    })
}

fn comparison(
    column: &ColumnDef,
    operator: WhereOperator,
    raw: &str,
) -> Result<Where<User>, AuthError> {
    use fastauth_core::schema::ColumnType;
    let name = column.name;
    match column.ty {
        ColumnType::Text | ColumnType::Id => {
            parse_filter::<String>(name, operator, raw, |v| Some(v.to_owned()))
        }
        ColumnType::Bool => parse_filter::<bool>(name, operator, raw, |v| v.parse().ok()),
        ColumnType::Int => parse_filter::<i64>(name, operator, raw, |v| v.parse().ok()),
        ColumnType::Timestamp => parse_filter::<OffsetDateTime>(name, operator, raw, |v| {
            OffsetDateTime::parse(v, &Rfc3339).ok()
        }),
        _ => Err(invalid_field(format!(
            "`{}` can't be filtered",
            camel_case(name)
        ))),
    }
}

impl<D: Adapter> Endpoint<D> for ListUsers {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/admin/list-users";
    type Access = MaybeAuthenticated;
    type Input = ListUsersQuery;
    type Output = UserList;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        query: ListUsersQuery,
    ) -> Result<UserList, AuthError> {
        self.0.check(
            &ctx,
            "user",
            "list",
            AdminError::YouAreNotAllowedToListUsers,
        )?;
        let mut conditions = Vec::new();
        if let Some(value) = query.search_value.as_deref().filter(|v| !v.is_empty()) {
            let field = match query.search_field.unwrap_or_default() {
                SearchField::Email => "email",
                SearchField::Name => "name",
            };
            let operator = match query.search_operator.unwrap_or_default() {
                SearchOperator::Contains => WhereOperator::Contains,
                SearchOperator::StartsWith => WhereOperator::StartsWith,
                SearchOperator::EndsWith => WhereOperator::EndsWith,
            };
            conditions.push(text_match(&user_column(&ctx, field)?, operator, value)?);
        }
        if let Some(value) = &query.filter_value {
            let column = user_column(&ctx, query.filter_field.as_deref().unwrap_or("email"))?;
            let operator = query.filter_operator.unwrap_or_default();
            conditions.push(match operator {
                WhereOperator::Contains | WhereOperator::StartsWith | WhereOperator::EndsWith => {
                    text_match(&column, operator, value)?
                }
                _ => comparison(&column, operator, value)?,
            });
        }
        let filter = conditions
            .into_iter()
            .reduce(Where::and)
            .unwrap_or_else(Where::all);
        let mut find = ctx.db().find::<User>(filter.clone());
        if let Some(sort_by) = &query.sort_by {
            let column = user_column(&ctx, sort_by)?;
            let col = Col::<User, String>::new(column.name);
            find = find.order_by(match query.sort_direction.unwrap_or_default() {
                SortDirection::Asc => col.asc(),
                SortDirection::Desc => col.desc(),
            });
        }
        let total = ctx.db().count::<User>(filter).await?;
        if let Some(offset) = query.offset {
            find = find.offset(offset);
        }
        if let Some(limit) = query.limit {
            find = find.limit(limit);
        }
        let users = find
            .all_rows()
            .await?
            .into_iter()
            .map(UserView::from_row)
            .collect::<Result<_, _>>()
            .map_err(AuthError::internal)?;
        Ok(UserList {
            users,
            total,
            limit: query.limit,
            offset: query.offset,
        })
    }
}

// ---------------------------------------------------------------------------- get-user

/// Query of `/admin/get-user`.
#[derive(Clone, Debug, Deserialize)]
pub struct GetUserQuery {
    pub id: Id,
}

/// `GET /admin/get-user?id=..`: needs `user:get`. Answers the user itself, unwrapped.
pub struct GetUser(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for GetUser {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/admin/get-user";
    type Access = MaybeAuthenticated;
    type Input = GetUserQuery;
    type Output = UserView;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        query: GetUserQuery,
    ) -> Result<UserView, AuthError> {
        self.0
            .check(&ctx, "user", "get", AdminError::YouAreNotAllowedToGetUser)?;
        find_user(&ctx, &query.id).await
    }
}

// ---------------------------------------------------------------------------- set-role

/// Body of `/admin/set-role`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetRoleInput {
    pub user_id: Id,
    pub role: Roles,
}

/// `POST /admin/set-role`: needs `user:set-role`. Every role must be defined.
pub struct SetRole(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for SetRole {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/set-role";
    type Access = MaybeAuthenticated;
    type Input = SetRoleInput;
    type Output = UserOutput;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: SetRoleInput,
    ) -> Result<UserOutput, AuthError> {
        self.0.check(
            &ctx,
            "user",
            "set-role",
            AdminError::YouAreNotAllowedToChangeUsersRole,
        )?;
        let role = input.role.stored(&self.0.plugin)?;
        find_user(&ctx, &input.user_id).await?;
        ctx.db()
            .update::<User>(User::ID.eq(&input.user_id))
            .set(AdminUser::ROLE, Some(role))
            .exec()
            .await?;
        updated(&ctx, &input.user_id).await
    }
}

/// Emits `UserUpdated` and answers the user as it is now.
async fn updated<D: Adapter, A>(ctx: &Ctx<D, A>, user_id: &Id) -> Result<UserOutput, AuthError> {
    ctx.emit(UserUpdated {
        user_id: user_id.clone(),
    });
    Ok(UserOutput {
        user: find_user(ctx, user_id).await?,
    })
}

// ---------------------------------------------------------------------------- ban-user

/// Body of `/admin/ban-user`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BanUserInput {
    pub user_id: Id,
    /// Defaults to the configured reason, or `"No reason"`.
    #[serde(default)]
    pub ban_reason: Option<String>,
    /// Seconds until the ban lifts. Defaults to the configured expiry, or never.
    #[serde(default)]
    pub ban_expires_in: Option<i64>,
}

impl BanUserInput {
    /// A ban with the configured reason and expiry.
    pub fn new(user_id: impl Into<Id>) -> Self {
        Self {
            user_id: user_id.into(),
            ban_reason: None,
            ban_expires_in: None,
        }
    }

    /// Why the user is banned.
    pub fn reason(mut self, reason: impl Into<String>) -> Self {
        self.ban_reason = Some(reason.into());
        self
    }

    /// When the ban lifts, from now.
    pub fn expires_in(mut self, duration: Duration) -> Self {
        self.ban_expires_in = Some(duration.whole_seconds());
        self
    }
}

/// `POST /admin/ban-user`: needs `user:ban`. Revokes every session of the user.
pub struct BanUser(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for BanUser {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/ban-user";
    type Access = MaybeAuthenticated;
    type Input = BanUserInput;
    type Output = UserOutput;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: BanUserInput,
    ) -> Result<UserOutput, AuthError> {
        let plugin = &self.0.plugin;
        let session = self
            .0
            .check(&ctx, "user", "ban", AdminError::YouAreNotAllowedToBanUsers)?;
        find_user(&ctx, &input.user_id).await?;
        if session.is_some_and(|s| s.user.id == input.user_id) {
            return Err(AdminError::YouCannotBanYourself.into());
        }
        let reason = input
            .ban_reason
            .filter(|r| !r.is_empty())
            .or_else(|| plugin.default_ban_reason.clone())
            .unwrap_or_else(|| "No reason".to_owned());
        let expires = input
            .ban_expires_in
            .filter(|s| *s != 0)
            .map(Duration::seconds)
            .or(plugin.default_ban_expires_in)
            .map(|d| ctx.now() + d);
        ctx.db()
            .update::<User>(User::ID.eq(&input.user_id))
            .set(AdminUser::BANNED, true)
            .set(AdminUser::BAN_REASON, Some(reason))
            .set(AdminUser::BAN_EXPIRES, expires)
            .exec()
            .await?;
        ctx.internal()
            .revoke_user_sessions(&input.user_id, None)
            .await?;
        updated(&ctx, &input.user_id).await
    }
}

// ---------------------------------------------------------------------------- unban-user

/// `POST /admin/unban-user`: needs `user:ban`.
pub struct UnbanUser(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for UnbanUser {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/unban-user";
    type Access = MaybeAuthenticated;
    type Input = UserIdInput;
    type Output = UserOutput;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: UserIdInput,
    ) -> Result<UserOutput, AuthError> {
        self.0
            .check(&ctx, "user", "ban", AdminError::YouAreNotAllowedToBanUsers)?;
        find_user(&ctx, &input.user_id).await?;
        lift_ban(&ctx, &input.user_id).await?;
        updated(&ctx, &input.user_id).await
    }
}

// ---------------------------------------------------------------------------- update-user

/// Body of `/admin/update-user`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateUserInput {
    pub user_id: Id,
    /// User columns by camelCase name.
    pub data: Map<String, Json>,
}

/// `POST /admin/update-user`: needs `user:update`, plus `user:set-role` for `role`,
/// `user:ban` for the ban columns and `user:set-email` for `email` and `emailVerified`.
/// Answers the user itself, unwrapped. Setting `banned: true` revokes the user's sessions.
pub struct UpdateUser(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for UpdateUser {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/update-user";
    type Access = MaybeAuthenticated;
    type Input = UpdateUserInput;
    type Output = UserView;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: UpdateUserInput,
    ) -> Result<UserView, AuthError> {
        let caller = &self.0;
        let session = caller.check(
            &ctx,
            "user",
            "update",
            AdminError::YouAreNotAllowedToUpdateUsers,
        )?;
        let data = input.data;
        if data.is_empty() {
            return Err(AdminError::NoDataToUpdate.into());
        }
        if data.contains_key("password") {
            return Err(AdminError::PasswordCannotBeUpdatedViaUpdateUser.into());
        }
        if data.contains_key("id") {
            return Err(AdminError::UserIdCannotBeUpdated.into());
        }
        let mut set = Vec::new();
        if let Some(role) = data.get("role") {
            caller.also(
                session,
                "user",
                "set-role",
                AdminError::YouAreNotAllowedToChangeUsersRole,
            )?;
            let role = Roles::from_json(role)?.stored(&caller.plugin)?;
            set.push(("role", Value::Text(role)));
        }
        let banning = data.get("banned") == Some(&Json::Bool(true));
        if BAN_COLUMNS.iter().any(|k| data.contains_key(*k)) {
            caller.also(
                session,
                "user",
                "ban",
                AdminError::YouAreNotAllowedToBanUsers,
            )?;
            if banning && session.is_some_and(|s| s.user.id == input.user_id) {
                return Err(AdminError::YouCannotBanYourself.into());
            }
        }
        if data.contains_key("email") || data.contains_key("emailVerified") {
            caller.also(
                session,
                "user",
                "set-email",
                AdminError::YouAreNotAllowedToSetUsersEmail,
            )?;
        }
        if let Some(email) = data.get("email") {
            let email = normalize_email(email.as_str().unwrap_or_default())?;
            let existing = ctx.internal().find_user_by_email(&email).await?;
            if existing.is_some_and(|u| u.id != input.user_id) {
                return Err(email_taken());
            }
            set.push(("email", Value::Text(email)));
        }
        for (key, json) in &data {
            if matches!(key.as_str(), "role" | "email") {
                continue;
            }
            let column = user_column(&ctx, key)?;
            if matches!(column.name, "created_at" | "updated_at") {
                return Err(invalid_field(format!("field `{key}` can't be set")));
            }
            set.push((column.name, column_value(&column, key, json)?));
        }
        find_user(&ctx, &input.user_id).await?;
        let mut update = ctx.db().update::<User>(User::ID.eq(&input.user_id));
        for (column, value) in set {
            update = update.set_raw(column, value);
        }
        update.exec().await?;
        if banning {
            ctx.internal()
                .revoke_user_sessions(&input.user_id, None)
                .await?;
        }
        Ok(updated(&ctx, &input.user_id).await?.user)
    }
}

// ---------------------------------------------------------------------------- set-user-password

/// Body of `/admin/set-user-password`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetUserPasswordInput {
    pub new_password: String,
    pub user_id: Id,
}

/// `POST /admin/set-user-password`: needs `user:set-password`. Checks the password policy,
/// creates the credential account when the user has none, and emits `PasswordChanged`.
pub struct SetUserPassword(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for SetUserPassword {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/set-user-password";
    type Access = MaybeAuthenticated;
    type Input = SetUserPasswordInput;
    type Output = Status;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: SetUserPasswordInput,
    ) -> Result<Status, AuthError> {
        self.0.check(
            &ctx,
            "user",
            "set-password",
            AdminError::YouAreNotAllowedToSetUsersPassword,
        )?;
        let internal = ctx.internal();
        internal.validate_new_password(&input.new_password)?;
        find_user(&ctx, &input.user_id).await?;
        internal
            .set_password(&input.user_id, &input.new_password)
            .await?;
        ctx.emit(PasswordChanged {
            user_id: input.user_id,
        });
        Ok(Status { status: true })
    }
}

// ---------------------------------------------------------------------------- remove-user

/// `POST /admin/remove-user`: needs `user:delete`. Deletes the user's sessions and
/// accounts, then the user, and emits `SessionRevoked` (when they had sessions) and
/// `UserDeleted`.
pub struct RemoveUser(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for RemoveUser {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/remove-user";
    type Access = MaybeAuthenticated;
    type Input = UserIdInput;
    type Output = Success;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: UserIdInput,
    ) -> Result<Success, AuthError> {
        let session = self.0.check(
            &ctx,
            "user",
            "delete",
            AdminError::YouAreNotAllowedToDeleteUsers,
        )?;
        if session.is_some_and(|s| s.user.id == input.user_id) {
            return Err(AdminError::YouCannotRemoveYourself.into());
        }
        let user = find_user(&ctx, &input.user_id).await?;
        ctx.internal()
            .revoke_user_sessions(&input.user_id, None)
            .await?;
        ctx.db()
            .delete::<Account>(Account::USER_ID.eq(&input.user_id))
            .await?;
        ctx.db().delete::<User>(User::ID.eq(&input.user_id)).await?;
        ctx.emit(UserDeleted {
            user_id: input.user_id,
            email: user.email.clone(),
        });
        Ok(Success { success: true })
    }
}

// ---------------------------------------------------------------------------- sessions

/// `{ "sessions": [..] }`.
#[derive(Debug, Serialize)]
pub struct SessionList {
    pub sessions: Vec<Map<String, Json>>,
}

/// `POST /admin/list-user-sessions`: needs `session:list`. The user's unexpired sessions.
pub struct ListUserSessions(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for ListUserSessions {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/list-user-sessions";
    type Access = MaybeAuthenticated;
    type Input = UserIdInput;
    type Output = SessionList;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: UserIdInput,
    ) -> Result<SessionList, AuthError> {
        self.0.check(
            &ctx,
            "session",
            "list",
            AdminError::YouAreNotAllowedToListUsersSessions,
        )?;
        let rows = ctx
            .db()
            .find::<Session>(
                Session::USER_ID
                    .eq(&input.user_id)
                    .and(Session::EXPIRES_AT.gt(ctx.now())),
            )
            .order_by(Session::CREATED_AT.desc())
            .all_rows()
            .await?;
        Ok(SessionList {
            sessions: rows.iter().map(Row::to_client_json).collect(),
        })
    }
}

/// Body of `/admin/revoke-user-session`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RevokeUserSessionInput {
    pub session_token: String,
}

/// `POST /admin/revoke-user-session`: needs `session:revoke`. Succeeds when no session
/// with that token is left, even if there never was one.
pub struct RevokeUserSession(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for RevokeUserSession {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/revoke-user-session";
    type Access = MaybeAuthenticated;
    type Input = RevokeUserSessionInput;
    type Output = Success;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: RevokeUserSessionInput,
    ) -> Result<Success, AuthError> {
        self.0.check(
            &ctx,
            "session",
            "revoke",
            AdminError::YouAreNotAllowedToRevokeUsersSessions,
        )?;
        let found = ctx
            .db()
            .find::<Session>(Session::TOKEN_HASH.eq(Hash::sha256(&input.session_token)))
            .one()
            .await?;
        if let Some(session) = found {
            ctx.internal()
                .revoke_session(&session.user_id, &session.id)
                .await?;
        }
        Ok(Success { success: true })
    }
}

/// `POST /admin/revoke-user-sessions`: needs `session:revoke`.
pub struct RevokeUserSessions(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for RevokeUserSessions {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/revoke-user-sessions";
    type Access = MaybeAuthenticated;
    type Input = UserIdInput;
    type Output = Success;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: UserIdInput,
    ) -> Result<Success, AuthError> {
        self.0.check(
            &ctx,
            "session",
            "revoke",
            AdminError::YouAreNotAllowedToRevokeUsersSessions,
        )?;
        ctx.internal()
            .revoke_user_sessions(&input.user_id, None)
            .await?;
        Ok(Success { success: true })
    }
}

// ---------------------------------------------------------------------------- impersonation

/// `{ "session": {..}, "user": {..} }`.
#[derive(Debug, Serialize)]
pub struct SessionOutput {
    pub session: Map<String, Json>,
    pub user: UserView,
}

/// `POST /admin/impersonate-user`: needs `user:impersonate`, plus
/// `user:impersonate-admins` when the target is an admin. Signs the browser in as the
/// user with a short session marked `impersonatedBy`, and keeps the admin's session token
/// in a signed `{prefix}.admin_session` cookie for `stop-impersonating`. Impersonation
/// sessions can't impersonate, and banned users can't be impersonated.
pub struct ImpersonateUser(pub(crate) Arc<Admin>);

impl<D: Adapter> Endpoint<D> for ImpersonateUser {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/impersonate-user";
    type Access = Authenticated;
    type Input = UserIdInput;
    type Output = SessionOutput;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: UserIdInput,
    ) -> Result<SessionOutput, AuthError> {
        let plugin = &self.0;
        let admin = ctx.session();
        let denied = || AuthError::from(AdminError::YouAreNotAllowedToImpersonateUsers);
        if !may(plugin, Some(admin), "user", "impersonate")? {
            return Err(denied());
        }
        let impersonating = admin
            .fields::<AdminSession>()
            .map_err(AuthError::internal)?
            .impersonated_by
            .is_some();
        let Some(admin_token) = admin.token.as_deref().filter(|_| !impersonating) else {
            return Err(denied());
        };
        let target = find_user(&ctx, &input.user_id).await?;
        let target_role = admin_fields(&target)?.role;
        if plugin.is_admin(&target.id, target_role.as_deref())
            && !may(plugin, Some(admin), "user", "impersonate-admins")?
        {
            return Err(AdminError::YouCannotImpersonateAdmins.into());
        }
        if let Some(err) = ban_error(plugin, &ctx, &target).await? {
            return Err(err);
        }

        let now = ctx.now();
        let dont_remember = ctx.signed_cookie(DONT_REMEMBER_COOKIE).is_some();
        let remaining = admin.session.expires_at - now;
        ctx.set_signed_cookie(
            ADMIN_SESSION_COOKIE,
            &format!("{admin_token}:{}", if dont_remember { "1" } else { "" }),
            (!dont_remember).then_some(remaining),
        );
        let session = ctx.internal().create_session(&target.id, false).await?;
        let expires_at = now + plugin.impersonation_session_duration;
        ctx.db()
            .update::<Session>(Session::ID.eq(&session.session.id))
            .set(AdminSession::IMPERSONATED_BY, Some(admin.user.id.clone()))
            .set(Session::EXPIRES_AT, expires_at)
            .exec()
            .await?;
        let mut json = session.to_json();
        json.insert(
            "impersonatedBy".into(),
            Json::String(admin.user.id.to_string()),
        );
        json.insert("expiresAt".into(), Value::Timestamp(expires_at).to_json());
        Ok(SessionOutput {
            session: json,
            user: target,
        })
    }
}

/// `POST /admin/stop-impersonating`: ends the impersonation session and signs the browser
/// back in as the admin from the `admin_session` cookie.
pub struct StopImpersonating;

impl<D: Adapter> Endpoint<D> for StopImpersonating {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/stop-impersonating";
    type Access = Authenticated;
    type Input = Empty;
    type Output = SessionOutput;

    async fn call(&self, ctx: Ctx<D, Authenticated>, _: Empty) -> Result<SessionOutput, AuthError> {
        let current = ctx.session();
        let admin_id = current
            .fields::<AdminSession>()
            .map_err(AuthError::internal)?
            .impersonated_by
            .ok_or(AdminError::NotImpersonating)?;
        let missing = || AuthError::internal("failed to find the admin session");
        let cookie = ctx
            .signed_cookie(ADMIN_SESSION_COOKIE)
            .ok_or_else(missing)?;
        let (token, dont_remember) = cookie.split_once(':').ok_or_else(missing)?;
        let row = ctx
            .db()
            .find::<Session>(Session::TOKEN_HASH.eq(Hash::sha256(token)))
            .one_row()
            .await?
            .ok_or_else(missing)?;
        let admin_session = Session::decode(&row).map_err(AuthError::internal)?;
        let now = ctx.now();
        if admin_session.user_id != admin_id || admin_session.expires_at <= now {
            return Err(missing());
        }
        let admin = find_user(&ctx, &admin_id).await?;
        ctx.internal()
            .revoke_session(&current.user.id, &current.session.id)
            .await?;

        let dont_remember = !dont_remember.is_empty();
        ctx.set_signed_cookie(
            SESSION_TOKEN_COOKIE,
            token,
            (!dont_remember).then_some(admin_session.expires_at - now),
        );
        if dont_remember {
            ctx.set_signed_cookie(DONT_REMEMBER_COOKIE, "true", None);
        } else {
            ctx.delete_cookie(DONT_REMEMBER_COOKIE);
        }
        ctx.delete_cookie(SESSION_CACHE_COOKIE);
        ctx.delete_cookie(ADMIN_SESSION_COOKIE);
        let mut session = row.to_client_json();
        session.insert("token".into(), Json::String(token.to_owned()));
        Ok(SessionOutput {
            session,
            user: admin,
        })
    }
}

// ---------------------------------------------------------------------------- has-permission

/// Body of `/admin/has-permission`. Over HTTP the caller's session decides; server calls
/// name a user or a role.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HasPermissionInput {
    #[serde(default)]
    pub user_id: Option<Id>,
    #[serde(default)]
    pub role: Option<String>,
    /// Better Auth's older spelling of `permissions`.
    #[serde(default)]
    pub permission: Option<Permissions>,
    #[serde(default)]
    pub permissions: Option<Permissions>,
}

/// `{ "error": null, "success": true }`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct PermissionOutput {
    pub error: Option<String>,
    pub success: bool,
}

/// `POST /admin/has-permission`: whether the signed-in user (any user, not only admins)
/// may do everything in `permissions`.
pub struct HasPermission(pub(crate) Caller);

impl<D: Adapter> Endpoint<D> for HasPermission {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/admin/has-permission";
    type Access = MaybeAuthenticated;
    type Input = HasPermissionInput;
    type Output = PermissionOutput;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        input: HasPermissionInput,
    ) -> Result<PermissionOutput, AuthError> {
        let Some(permissions) = input.permissions.or(input.permission) else {
            return Err(AuthError::bad_request(
                codes::INVALID_BODY,
                "invalid permission check. no permission(s) were passed.",
            ));
        };
        let session = match ctx.session() {
            Some(session) => Some(session),
            None if self.0.server => None,
            None => return Err(AuthError::unauthorized()),
        };
        let (user_id, role) = match (session, input.role, input.user_id) {
            (Some(session), _, _) => (session.user.id.clone(), admin_fields(&session.user)?.role),
            (None, Some(role), user_id) => (user_id.unwrap_or_else(|| Id::from("")), Some(role)),
            (None, None, Some(user_id)) => {
                let user = ctx
                    .internal()
                    .find_user_by_id(&user_id)
                    .await?
                    .ok_or_else(|| {
                        AuthError::bad_request(codes::USER_NOT_FOUND, "user not found")
                    })?;
                (user_id, admin_fields(&user)?.role)
            }
            (None, None, None) => {
                return Err(AuthError::bad_request(
                    codes::INVALID_BODY,
                    "user id or role is required",
                ));
            }
        };
        Ok(PermissionOutput {
            error: None,
            success: self
                .0
                .plugin
                .has_permission(&user_id, role.as_deref(), &permissions),
        })
    }
}