origen_metal 1.4.0

Bare metal APIs for the Origen SDK
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
1367
1368
1369
1370
1371
1372
1373
1374
use super::data::{Data, DatasetConfig};
use super::password_cache_options::PasswordCacheOptions;
use crate::_utility::{unsorted_dedup, validate_input_list};
use crate::frontend::FeatureReturn;
use crate::prelude::session_store::*;
use crate::utils::file::FilePermissions;
use crate::{Outcome, OutcomeState, Result};
use indexmap::IndexMap;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::env;

pub const DEFAULT_DATASET_KEY: &str = "__origen__default__";
pub const DEFAULT_USER_SESSION_PATH_OFFSET: &str = "./.o2/.session";
pub const DEFAULT_USER_SESSION_GROUP_NAME: &str = "__user__";
pub const DEFAULT_USER_SESSION_STORE_NAME: &str = "__user__";

lazy_static! {
    pub static ref DEFAULT_DATASET_CONFIG: HashMap<String, HashMap<String, String>> = {
        let mut h: HashMap<String, HashMap<String, String>> = HashMap::new();
        h.insert(DEFAULT_DATASET_KEY.to_string(), {
            let mut c: HashMap<String, String> = HashMap::new();
            c.insert("data_source".to_string(), "git".to_string());
            c.insert("auto_populate".to_string(), "false".to_string());
            c
        });
        h
    };
    static ref SORRY_PW: &'static str = "Sorry, that password is not correct";
}

pub fn with_user_dataset<T, F>(user: Option<&str>, dataset: &str, func: F) -> Result<T>
where
    F: FnMut(&Data) -> Result<T>,
{
    let urs = crate::users();
    let u;
    if let Some(uname) = user {
        u = urs.user(uname)?;
    } else {
        u = urs.current_user()?;
    }
    u.with_dataset(dataset, func)
}

pub fn with_user_dataset_mut<T, F>(user: Option<&str>, dataset: &str, func: F) -> Result<T>
where
    F: FnMut(&mut Data) -> Result<T>,
{
    let urs = crate::users();
    let u;
    if let Some(uname) = user {
        u = urs.user(uname)?;
    } else {
        u = urs.current_user()?;
    }
    u.with_dataset_mut(dataset, func)
}

pub fn add_dataset_to_user(
    id: &str,
    dataset: &str,
    config: DatasetConfig,
    replace_existing: bool,
    as_topmost: bool,
) -> Result<Option<Outcome>> {
    User::add_dataset(id, dataset, config, replace_existing, as_topmost)
}

pub fn register_dataset_with_user(
    id: &str,
    dataset: &str,
    config: DatasetConfig,
    replace_existing: bool,
) -> Result<Option<Outcome>> {
    User::register_dataset(id, dataset, config, replace_existing)
}

/// Temporarily run some function with 'new_top' being the highest priority datasets
pub fn with_user_hierarchy<T, F, S>(user: Option<S>, new_top: &Vec<String>, func: F) -> Result<T>
where
    F: Fn(&User) -> Result<T>,
    S: AsRef<str>,
{
    let mut urs = crate::users_mut();
    let u;
    if let Some(uname) = user {
        u = urs.user_mut(uname.as_ref())?;
    } else {
        u = urs.current_user_mut()?;
    }

    let old_hierarchy = u.data_lookup_hierarchy.clone();
    let mut new_hierarchy = new_top.to_vec();
    new_hierarchy.extend(old_hierarchy.clone());
    unsorted_dedup(&mut new_hierarchy);
    u.set_data_lookup_hierarchy(new_hierarchy)?;
    let res = func(u);
    u.data_lookup_hierarchy = old_hierarchy;
    res
}

pub fn with_user_motive_or_default<T, F, S1, S2>(user: Option<S1>, motive: S2, func: F) -> Result<T>
where
    F: Fn(&User) -> Result<T>,
    S1: AsRef<str>,
    S2: AsRef<str>,
{
    let ds: String;
    {
        let mut urs = crate::users_mut();
        let u;
        if let Some(uname) = user.as_ref() {
            u = urs.user_mut(uname.as_ref())?;
        } else {
            u = urs.current_user_mut()?;
        }
        if let Some(d) = u.motive_for(motive.as_ref()) {
            ds = d.to_string()
        } else {
            return func(u);
        }
    }

    with_user_hierarchy(user, &vec![ds], func)
}

#[derive(Debug, Clone, Default)]
pub struct PopulateUserReturn {
    outcomes: IndexMap<String, Option<Outcome>>,

    // Cache the failed and errored dataset names
    failed_datasets: Vec<String>,
    errored_datasets: Vec<String>,
}

impl PopulateUserReturn {
    pub fn outcomes(&self) -> &IndexMap<String, Option<Outcome>> {
        &self.outcomes
    }

    pub fn all_succeeded(&self) -> bool {
        self.failed_datasets.is_empty() && self.errored_datasets.is_empty()
    }

    pub fn succeeded(&self) -> bool {
        self.all_succeeded()
    }

    pub fn failed(&self) -> bool {
        !self.failed_datasets.is_empty()
    }

    pub fn errored(&self) -> bool {
        !self.errored_datasets.is_empty()
    }

    pub fn failed_datasets(&self) -> &Vec<String> {
        &self.failed_datasets
    }

    pub fn failed_outcomes(&self) -> IndexMap<&String, &Outcome> {
        self.failed_datasets
            .iter()
            .map(|d| {
                (
                    d,
                    self.outcomes
                        .get(d)
                        .expect(&format!(
                            "Expected PopulateUserReturn to have outcome for {}",
                            d
                        ))
                        .as_ref()
                        .expect(&format!(
                            "Expected outcome for dataset {} to not be None",
                            d
                        )),
                )
            })
            .collect::<IndexMap<&String, &Outcome>>()
    }

    pub fn errored_datasets(&self) -> &Vec<String> {
        &self.errored_datasets
    }

    pub fn errored_outcomes(&self) -> IndexMap<&String, &Outcome> {
        self.errored_datasets
            .iter()
            .map(|d| {
                (
                    d,
                    self.outcomes
                        .get(d)
                        .expect(&format!(
                            "Expected PopulateUserReturn to have outcome for {}",
                            d
                        ))
                        .as_ref()
                        .expect(&format!(
                            "Expected outcome for dataset {} to not be None",
                            d
                        )),
                )
            })
            .collect::<IndexMap<&String, &Outcome>>()
    }

    pub fn insert(&mut self, dataset: &str, outcome: Option<Outcome>) -> Option<Option<Outcome>> {
        match outcome {
            Some(oc) => {
                match oc.state {
                    OutcomeState::Success(_, _) => {}
                    OutcomeState::Fail(_, _) => self.failed_datasets.push(dataset.to_owned()),
                    OutcomeState::Error(_, _) => self.errored_datasets.push(dataset.to_owned()),
                }
                self.outcomes
                    .insert(dataset.to_owned(), Some(oc.to_owned()))
            }
            None => self.outcomes.insert(dataset.to_owned(), None),
        }
    }

    pub fn log(&self, uid: &str) -> Result<Option<String>> {
        if self.succeeded() {
            log_info!(
                "Successfully populated datasets {} for user '{}'",
                self.outcomes
                    .keys()
                    .map(|k| k.as_str())
                    .collect::<Vec<&str>>()
                    .join(","),
                uid
            );
            Ok(None)
        } else {
            let mut retn = format!("Could not fully populate user '{}'.", uid);
            log_error!("Could not fully populate user '{}'", uid);
            if !&self.failed_datasets.is_empty() {
                retn += &format!(
                    " Failures occurred populating these datasets: {}",
                    &self.failed_datasets.join(",")
                );
                log_error!("");
                log_error!("Failures occurred populating these datasets:");
                for (n, outcome) in &self.failed_outcomes() {
                    log_error!("{}: {}", n, outcome.msg_or_default());
                }
            }
            log_error!("");
            log_error!("Errors occurred populating these datasets:");
            Ok(Some(retn))
        }
    }
}

pub fn try_default_home_dir(user: Option<&str>, dataset: Option<&str>) -> Result<Option<PathBuf>> {
    let u_id: String;
    let is_current;
    match user {
        Some(id) => {
            if let Some(cid) = super::users::get_current_user_id()? {
                is_current = cid == id;
            } else {
                is_current = false;
            }
            u_id = id.to_owned();
        },
        None => {
            if let Some(id) = super::users::get_current_user_id()? {
                u_id = id;
                is_current = true;
            } else {
                bail!("Cannot attempt to lookup home directory when no current user has been set!")
            }
        }
    }

    // TODO see about wrapping function calls like this (optional frontend functions)
    let fe_res = crate::with_optional_frontend(|f| {
        if let Some(fe) = f {
            if let Some(result) = fe.lookup_home_dir(&u_id, dataset, is_current) {
                return Ok(Some(result?))
            }
        }
        Ok(None)
    })?;

    if let Some(r) = fe_res {
        Ok(r)
    } else {
        let hd: PathBuf;
        if cfg!(windows) {
            match env::var("USERPROFILE") {
                Ok(path) => hd = PathBuf::from(path),
                Err(e) => {
                    match e {
                        env::VarError::NotPresent => bail!("Please set environment variable USERPROFILE to point to your home directory, then try again"),
                        _ => bail!(&e.to_string())
                    }
                }
            }
        } else {
            match env::var("HOME") {
                Ok(path) => hd = PathBuf::from(path),
                Err(e) => {
                    match e {
                        env::VarError::NotPresent => bail!("Please set environment variable HOME to point to your home directory, then try again"),
                        _ => bail!(&e.to_string())
                    }
                }
            }
        }

        if !hd.ends_with(&u_id) {
            bail!("Home directory '{}' is not appropriate for current user with id '{}'", hd.display(), &u_id)
        } else {
            Ok(Some(hd))
        }
        // try_default_home_dir_lookup() // super::whoami()
    }
}

// pub fn try_default_home_dir_lookup() -> Result<PathBuf> {
//     let hd;
//     if cfg!(windows) {
//         match env::var("USERPROFILE") {
//             Some(path) => hd = PathBuf::from(path),
//             None => bail!("Please set environment variable USERPROFILE to point to your home directory, then try again")
//         }
//     } else {
//         match env::var("HOME") {
//             Some(path) => hd = PathBuf::from(path),
//             None => bail!("Please set environment variable HOME to point to your home directory, then try again")
//         }
//     }

//     // Confirm that this directory is suitable for the current user
//     super::users::with_current_user( |user| { // get_current_user_id
//         if let Some(u) = user {
//             if !hd.ends_with(u.id) {
//                 bail!("Home directory '{}' is not appropriate for current user with id '{}'", hd.display(), u.id)
//             }
//         } else {
//             bail!("Cannot attempt to lookup home directory when no current user has been set!")
//         }
//         Ok(hd)
//     })
// }


#[derive(Debug, Default)]
struct PopulateStatus {
    populating: RwLock<bool>,
    lock: Mutex<bool>,
}

impl PopulateStatus {
    pub fn while_populating<T, F>(&self, mut func: F) -> T
    where
        F: FnMut() -> T,
    {
        let _lock = self.lock.lock().unwrap();
        {
            let mut popping = self.populating.write().unwrap();
            *popping = true;
        }
        let result = func();

        {
            let mut popping = self.populating.write().unwrap();
            *popping = false;
        }
        result
    }
}

#[derive(Debug, Clone)]
pub struct SessionConfig {
    pub root: Option<PathBuf>,
    pub offset: Option<PathBuf>,
    pub file_permissions: FilePermissions,
}

impl SessionConfig {
    pub fn new() -> Self {
        Self {
            root: None,
            offset: Some(PathBuf::from(DEFAULT_USER_SESSION_PATH_OFFSET)),
            file_permissions: FilePermissions::Private,
        }
    }

    pub fn to_sg_name(id: &str) -> String {
        format!("__user__{}__", id)
    }

    pub fn set_offset(&mut self, new_offset: Option<PathBuf>) -> Result<()> {
        match new_offset {
            Some(o) => {
                if o.is_absolute() {
                    bail!(
                        "Absolute offsets are not allowed in a user's session config (given: {})",
                        o.display()
                    );
                }
                self.offset = Some(o)
            }
            None => self.offset = None,
        }
        Ok(())
    }

    pub fn resolved_root(&self, user: &User) -> Result<PathBuf> {
        let mut rr: PathBuf = PathBuf::new();
        match self.root.as_ref() {
            Some(r) => rr.push(r),
            None => {
                if let Some(d) = user.home_dir()? {
                    rr.push(d);
                }
            }
        }
        if let Some(o) = self.offset.as_ref() {
            rr.push(o);
        }
        Ok(rr)
    }

    pub fn resolved_path(&self, user: &User) -> Result<PathBuf> {
        let mut rr = self.resolved_root(user)?;
        rr.push(format!(
            "{}{}__",
            DEFAULT_USER_SESSION_GROUP_NAME,
            user.id()
        ));
        Ok(rr)
    }
}

#[derive(Debug)]
pub struct User {
    // All user data is stored behind a RW lock so that it can be lazily loaded
    // from the environment and cached behind the scenes
    data: IndexMap<String, RwLock<Data>>,
    data_lookup_hierarchy: Vec<String>,
    password_semaphore: Mutex<u8>,
    id: String,
    password_cache_option: PasswordCacheOptions,
    motive_mapping: IndexMap<String, String>,
    populate_status: PopulateStatus,
    session_config: RwLock<SessionConfig>,
    uid_num: usize,
    roles: RwLock<HashSet<String>>,

    // User-level overrides for dataset configuration
    auto_populate: Option<bool>,
    should_validate_passwords: Option<bool>,
    prompt_for_passwords: Option<bool>,
}

impl User {
    pub fn top_datakey(&self) -> Result<&str> {
        if let Some(key) = self.data_lookup_hierarchy.first() {
            Ok(key)
        } else {
            bail!("Data lookup hierarchy for user '{}' is empty", self.id)
        }
    }

    pub fn data_lookup_hierarchy(&self) -> &Vec<String> {
        &self.data_lookup_hierarchy
    }

    /// Returns the data lookup hierarchy or an error, if the hierarchy is empty
    pub fn data_lookup_hierarchy_or_err(&self) -> Result<&Vec<String>> {
        if self.data_lookup_hierarchy.is_empty() {
            bail!("Dataset hierarchy is empty! Data lookups must explicitly name the dataset to query")
        } else {
            Ok(&self.data_lookup_hierarchy)
        }
    }

    pub fn set_data_lookup_hierarchy(&mut self, hierarchy: Vec<String>) -> Result<()> {
        // Check that each item in hierarchy is valid and that there are no duplicates
        validate_input_list(
            hierarchy.iter().collect::<Vec<&String>>(),
            Some(self.data.keys()),
            false,
            Some(&super::duplicate_dataset_hierarchy_closure),
            Some(&super::invalid_dataset_hierarchy_closure),
        )?;
        self.data_lookup_hierarchy = hierarchy;
        Ok(())
    }

    pub fn write_data(&self, key: Option<&str>) -> Result<RwLockWriteGuard<'_, Data>> {
        let k;
        if let Some(tmp) = key {
            k = tmp;
        } else {
            k = self.top_datakey()?;
        }
        if let Some(d) = self.data.get(k) {
            Ok(d.write().unwrap())
        } else {
            bail!("Could not find user dataset {}", k)
        }
    }

    fn read_data(&self, key: Option<&str>) -> Result<RwLockReadGuard<'_, Data>> {
        let k;
        if let Some(tmp) = key {
            k = tmp;
        } else {
            k = self.top_datakey()?;
        }
        if let Some(d) = self.data.get(k) {
            Ok(d.read().unwrap())
        } else {
            bail!("Could not find user dataset {}", k)
        }
    }

    pub fn datasets(&self) -> &IndexMap<String, RwLock<Data>> {
        &self.data
    }

    pub fn datakeys(&self) -> Vec<&String> {
        self.data.keys().into_iter().collect::<Vec<&String>>()
    }

    fn add_dataset_placeholder(&mut self, dataset: &str, config: &DatasetConfig) -> Result<()> {
        self.data
            .insert(dataset.to_string(), RwLock::new(Data::new(dataset, config)));
        Ok(())
    }

    pub fn add_dataset(
        id: &str,
        dataset: &str,
        config: DatasetConfig,
        replace_existing: bool,
        as_topmost: bool,
    ) -> Result<Option<Outcome>> {
        let rtn = Self::register_dataset(id, dataset, config, replace_existing)?;

        // Mutably borrow the user again to update the hierarchy
        super::users::with_user_mut(id, |u| {
            if let Some(i) = u.data_lookup_hierarchy.iter().position(|i| i == dataset) {
                u.data_lookup_hierarchy.remove(i);
            }

            if as_topmost {
                u.data_lookup_hierarchy.insert(0, dataset.to_owned());
            } else {
                u.data_lookup_hierarchy.push(dataset.to_owned())
            }
            Ok(())
        })?;
        Ok(rtn)
    }

    pub fn register_dataset(
        id: &str,
        dataset: &str,
        config: DatasetConfig,
        replace_existing: bool,
    ) -> Result<Option<Outcome>> {
        // Grab a mutable reference and update the users
        super::users::with_user_mut(id, |u| {
            if u.data.contains_key(dataset) {
                if replace_existing {
                    u.data.shift_remove(dataset);
                } else {
                    bail!("User '{}' already has dataset '{}'", &u.id, dataset);
                }
            }
            u.data.insert(
                dataset.to_string(),
                RwLock::new(Data::new(dataset, &config)),
            );
            Ok(())
        })?;

        // Free up the mutable borrow, and, if needed, populate the config
        if config.should_auto_populate() {
            super::users::with_user(id, |u| {
                match Data::populate(u, dataset, false, false, true)? {
                    Some(res) => Ok(Some(res)),
                    // TODO backend bail
                    None => bail!("Something has gone wrong and a newly added dataset is already marked as populated")
                }
            })
        } else {
            Ok(None)
        }
    }

    pub fn new(
        id: &str,
        users: &super::users::Users,
        password_cache_option: Option<PasswordCacheOptions>,
        uid_cnt: usize,
        auto_populate: Option<bool>,
    ) -> Result<Self> {
        let mut u = Self {
            id: id.to_string(),
            data: IndexMap::new(),

            password_semaphore: Mutex::new(0),
            data_lookup_hierarchy: users.default_data_lookup_hierarchy().clone(),
            password_cache_option: match password_cache_option {
                Some(pco) => pco,
                None => PasswordCacheOptions::None,
            },
            motive_mapping: users.motive_mapping().to_owned(),
            populate_status: PopulateStatus::default(),
            session_config: RwLock::new(users.default_session_config().clone()),
            uid_num: uid_cnt,
            roles: RwLock::new(HashSet::new()),
            auto_populate: auto_populate,
            should_validate_passwords: users.default_should_validate_passwords().to_owned(),

            // TODO add a users option to inherit from?
            prompt_for_passwords: None,
        };

        for (ds, config) in users.default_datasets().iter() {
            u.add_dataset_placeholder(ds, config).unwrap();
        }
        Ok(u)
    }

    pub fn unload(&self) -> Result<()> {
        // TODO call some frontend method? Mark as stale somewhere? Idk. Placeholder for now
        Ok(())
    }

    pub fn is_current(&self) -> Result<bool> {
        Ok(match super::users::get_current_user_id()? {
            Some(id) => id == self.id,
            None => false,
        })
    }

    pub fn id(&self) -> &String {
        &self.id
    }

    pub fn uid_num(&self) -> usize {
        self.uid_num
    }

    pub fn password_cache_option(&self) -> &PasswordCacheOptions {
        &self.password_cache_option
    }

    pub fn add_motive(
        &mut self,
        motive: String,
        dataset: String,
        replace_existing: bool,
    ) -> Result<Option<String>> {
        if !self.datasets().contains_key(&dataset) {
            bail!(
                "Cannot add motive for user '{}' corresponding to nonexistent dataset '{}'",
                &self.id,
                &dataset
            );
        }
        if !replace_existing {
            if let Some(ds) = self.motive_mapping.get(&motive) {
                bail!(
                    "Motive '{}' for user '{}' already corresponds to dataset '{}'. Use the 'replace_existing' option to update the motive",
                    motive,
                    self.id,
                    ds
                );
            }
        }
        Ok(self.motive_mapping.insert(motive, dataset))
    }

    pub fn motive_mapping(&self) -> &IndexMap<String, String> {
        &self.motive_mapping
    }

    pub fn motive_for<S: AsRef<str>>(&self, motive: S) -> Option<&String> {
        self.motive_mapping.get(motive.as_ref())
    }

    pub fn username(&self) -> Result<String> {
        let uname;
        {
            let data = self.read_data(None).unwrap();
            uname = data.username.clone();
        }
        if let Some(u) = uname {
            Ok(u)
        } else {
            Ok(self.id.to_string())
        }
    }

    pub fn set_username(&self, username: Option<String>) -> Result<()> {
        self.with_dataset_mut(&self.top_datakey()?, |d| {
            d.username = username.clone();
            Ok(())
        })
    }

    pub fn get_email(&self) -> Result<Option<String>> {
        for dn in self.data_lookup_hierarchy_or_err()?.iter() {
            if let Some(e) = self.with_dataset(dn, |d| Ok(d.email.clone()))? {
                return Ok(Some(e));
            }
        }
        Ok(None)
    }

    pub fn require_email(&self) -> Result<String> {
        if let Some(e) = self.get_email()? {
            Ok(e)
        } else {
            bail!(
                "Tried to retrieve email for user '{}' but no email has been set!",
                self.id
            )
        }
    }

    pub fn set_email(&self, email: Option<String>) -> Result<()> {
        self.with_dataset_mut(&self.top_datakey()?, |d| {
            d.email = email.clone();
            Ok(())
        })
    }

    pub fn first_name(&self) -> Result<Option<String>> {
        for dn in self.data_lookup_hierarchy_or_err()?.iter() {
            if let Some(n) = self.with_dataset(&dn, |d| Ok(d.first_name.clone()))? {
                return Ok(Some(n));
            }
        }
        Ok(None)
    }

    pub fn set_first_name(&self, first_name: Option<String>) -> Result<()> {
        self.with_dataset_mut(&self.top_datakey()?, |d| {
            d.first_name = first_name.clone();
            Ok(())
        })
    }

    pub fn last_name(&self) -> Result<Option<String>> {
        for dn in self.data_lookup_hierarchy_or_err()?.iter() {
            if let Some(n) = self.with_dataset(&dn, |d| Ok(d.last_name.clone()))? {
                return Ok(Some(n));
            }
        }
        Ok(None)
    }

    pub fn set_last_name(&self, last_name: Option<String>) -> Result<()> {
        self.with_dataset_mut(&self.top_datakey()?, |d| {
            d.last_name = last_name.clone();
            Ok(())
        })
    }

    pub fn display_name(&self) -> Result<String> {
        self.display_name_for(None)
    }

    pub fn display_name_for(&self, dataset: Option<&str>) -> Result<String> {
        let key = dataset.unwrap_or(&self.top_datakey()?);
        self.with_dataset(key, |d| {
            if let Some(n) = d.get_display_name().clone() {
                Ok(n.to_string())
            } else {
                Ok(self.id.to_string())
            }
        })
    }

    pub fn set_display_name(&self, display_name: Option<String>) -> Result<()> {
        self.with_dataset_mut(&self.top_datakey()?, |d| {
            d.display_name = display_name.clone();
            Ok(())
        })
    }

    pub fn home_dir(&self) -> Result<Option<PathBuf>> {
        for dn in self.data_lookup_hierarchy_or_err()?.iter() {
            if let Some(n) = self.with_dataset(&dn, |d| Ok(d.home_dir.clone()))? {
                return Ok(Some(n));
            }
        }
        Ok(None)
    }

    pub fn require_home_dir(&self) -> Result<PathBuf> {
        if let Some(hd) = self.home_dir()? {
            Ok(hd.to_owned())
        } else {
            bail!(
                "Required a home directory for user '{}' but none has been set",
                &self.id
            )
        }
    }

    pub fn set_home_dir(&self, new_dir: Option<PathBuf>) -> Result<()> {
        if new_dir.is_some() {
            let mut data = self.write_data(None)?;
            data.home_dir = new_dir;
        } else {
            self.for_datasets_in_hierarchy_mut( |d| { d.home_dir = None; Ok(()) })?;
        }
        Ok(())
    }

    pub fn dot_origen_dir(&self) -> Result<PathBuf> {
        let mut dot = self.require_home_dir()?;
        dot.push(".origen");
        Ok(dot)
    }

    pub fn _cache_password(&self, password: &str, dataset: &str) -> Result<bool> {
        self.password_cache_option
            .cache_password(self, password, dataset)
    }

    pub fn _password_dialog(&self, dataset: &str, motive: Option<&str>) -> Result<String> {
        if !self.prompt_for_passwords() {
            bail!("Cannot prompt for passwords for user '{}'. Passwords must be loaded by the config or set directly.", &self.id);
        }
        // TODO add attempts back in
        // for _attempt in 0..ORIGEN_CONFIG.user__password_auth_attempts {
        let msg;
        if dataset == "" {
            msg = match motive {
                Some(x) => format!("\nPlease enter your password {}: ", x),
                None => "\nPlease enter your password: ".to_string(),
            };
        } else {
            msg = match motive {
                Some(x) => format!("\nPlease enter your password ({}) {}: ", dataset, x),
                None => format!("\nPlease enter your password ({}): ", dataset),
            };
        }
        let pass = match rpassword::read_password_from_tty(Some(&msg)) {
            Ok(pw) => pw,
            Err(e) => bail!("Error encountered prompting for password: {}", e)
        };
        let attempt = self._try_password(&pass, Some(dataset))?;

        if attempt.0 {
            self._cache_password(&pass, dataset)?;
            let mut data = self.write_data(Some(dataset))?;
            data.password = Some(pass.clone());
            return Ok(pass);
        } else {
            display_redln!("Sorry, that password is incorrect");
        }
        // }
        // display_redln!(
        //     "Maximum number of authentication attempts reached ({}), exiting...",
        //     ORIGEN_CONFIG.user__password_auth_attempts
        // );
        bail!(
            // TODO
            "Maximum number of authentication attempts reached ({})",
            1 // ORIGEN_CONFIG.user__password_auth_attempts
        )
    }

    // Utility function to try a password, returning a tuple of:
    // (bool, bool, Option<Outcome>)
    // where:
    //  return.0 -> If the password validation was successful or the password was not to be validated
    //  return.1 -> If the password was actually validated
    //  return.2 -> The outcome returned from the frontend, in the event of return.1 == true
    fn _try_password(
        &self,
        password: &str,
        dataset_name: Option<&str>,
    ) -> Result<(bool, bool, Option<Outcome>)> {
        // TODO support this?
        // Check if we are even supposed to try the password or if its already been tried
        // let dn_opt = Some(dataset_name.unwrap_or(&self.top_datakey()?));
        if self.should_validate_passwords() {
            let ds = self.read_data(dataset_name)?;
            if ds.password_needs_validation() {
                let r = self._validate_password(password, &ds)?;
                let o = r.outcome()?;
                return {
                    if o.errored() {
                        bail!(
                            "Errors encountered validating password: {}",
                            o.msg_or_default()
                        )
                    } else if o.failed() {
                        Ok((false, true, Some(o.to_owned())))
                    } else {
                        Ok((true, true, Some(o.to_owned())))
                    }
                };
            }
        }
        Ok((true, false, None))
    }

    pub fn validate_password(
        &self,
        password: &str,
        dataset_name: Option<&str>,
    ) -> Result<FeatureReturn> {
        let ds = self.read_data(dataset_name)?;
        self._validate_password(password, &ds)
    }

    fn _validate_password(&self, password: &str, ds: &Data) -> Result<FeatureReturn> {
        let lookup = ds.require_data_source_for("password validation", &self.id)?;
        let f = crate::frontend::require()?;
        f.with_data_store(lookup.0, lookup.1, |dstore| {
            dstore.validate_password(
                &ds.username
                    .as_ref()
                    .map_or_else(|| self.id.as_str(), |u| &u.as_str()),
                password,
                &self.id,
                &ds.dataset_name,
            )
        })
    }

    pub fn dataset_for(&self, motive: &str) -> Result<Option<&String>> {
        Ok(self.motive_mapping().get(motive))
    }

    pub fn set_password(
        &self,
        password: Option<String>,
        dataset: Option<&str>,
        validate: Option<bool>,
    ) -> Result<()> {
        let _lock = self.password_semaphore.lock().unwrap();

        if let Some(p) = password.as_ref() {
            let dn = dataset.unwrap_or(&self.top_datakey()?);
            self.with_dataset_mut(dn, |d| {
                d.authenticated = false;
                Ok(())
            })?;

            let res;
            if let Some(v) = validate {
                if v {
                    res = self._try_password(p, dataset)?;
                } else {
                    res = (true, false, None);
                }
            } else {
                // Consult the config whether or not to validate the password
                res = self._try_password(p, dataset)?;
            }
            if !res.0 {
                if let Some(o) = res.2 {
                    if let Some(m) = o.msg() {
                        bail!(m)
                    } else {
                        bail!(&SORRY_PW)
                    }
                } else {
                    bail!(&SORRY_PW)
                }
            }

            // either we aren't to validate, or the validation was successful
            self.with_dataset_mut(dn, |d| {
                d.password = Some(p.to_string());
                Ok(())
            })?;
            self._cache_password(p, dn)?;
            Ok(())
        } else {
            self.clear_cached_password(dataset)
        }
    }

    pub fn should_validate_passwords(&self) -> bool {
        self.should_validate_passwords.unwrap_or(true)
    }

    pub fn should_validate_passwords_value(&self) -> &Option<bool> {
        &self.should_validate_passwords
    }

    pub fn set_should_validate_passwords(&mut self, new: Option<bool>) -> () {
        self.should_validate_passwords = new;
    }

    pub fn prompt_for_passwords(&self) -> bool {
        self.prompt_for_passwords.unwrap_or(true)
    }

    pub fn prompt_for_passwords_value(&self) -> &Option<bool> {
        &self.prompt_for_passwords
    }

    pub fn set_prompt_for_passwords(&mut self, new: Option<bool>) -> () {
        self.prompt_for_passwords = new;
    }

    pub fn password(
        &self,
        motive_or_dataset: Option<&str>,
        motive_not_dataset: bool,
        default: Option<Option<&str>>,
    ) -> Result<String> {
        // In a multi-threaded scenario, this prevents concurrent threads from prompting the user for
        // the password at the same time.
        // Instead the first thread to arrive will do it, then by the time the lock is released awaiting
        // threads will be able to used the cached value instead of prompting the user.
        let _lock = self.password_semaphore.lock().unwrap();
        let dataset: &str;

        if let Some(rod) = motive_or_dataset.as_ref() {
            if motive_not_dataset {
                if let Some(mapped_dataset) = self.dataset_for(rod)? {
                    // Use this dataset
                    dataset = mapped_dataset
                } else {
                    // motive wasn't mapped to a dataset. See if a default should be used
                    if let Some(d1) = default {
                        // Default was given
                        if let Some(d2) = d1 {
                            // A explicit dataset was given. Check that it exists, then use that
                            if !self.data.contains_key(d2) {
                                bail!(
                                    "A default dataset '{}' was provided, but this dataset does not exists for user '{}'",
                                    d2,
                                    self.id
                                )
                            }
                            dataset = d2;
                        } else {
                            // A default of None was given, meaning use the current dataset
                            dataset = &self.top_datakey()?;
                        }
                    } else {
                        // Raise an error
                        bail!("No password available for motive: '{}'", rod,);
                    }
                }
            } else {
                dataset = rod;
            }
        } else {
            dataset = &self.top_datakey()?;
        }

        let motive: Option<&str>;
        if motive_or_dataset.is_some() && motive_not_dataset {
            motive = motive_or_dataset;
        } else {
            motive = None;
        }

        // If the password has already been set, can return it.
        // Non-current users which have had their password set can still be retrieved as these
        // are likely service/function users
        // Check if the password is already set
        // Important, this is in a block to release the read lock
        {
            let data = self.read_data(Some(dataset)).unwrap();
            if let Some(p) = &data.password {
                if self._try_password(p, Some(dataset))?.0 {
                    return Ok(p.to_string());
                }
            }
        }

        // Need to lookup the password, but will only do this for the current user
        if self.is_current()? {
            if let Some(pw) = self.password_cache_option.get_password(self, dataset)? {
                // Password was cached and retrieved - test password
                if self._try_password(&pw, Some(dataset))?.0 {
                    let mut data = self.write_data(Some(dataset)).unwrap();
                    data.password = Some(pw.clone());
                    return Ok(pw);
                } else {
                    // Note: the session will be updated if the correct password is
                    // provided from the dialog
                    display_redln!("Cached password is not valid!");
                }
            }
            return self._password_dialog(dataset, motive);
        } else {
            bail!("Can't get the password for a user which is not the current user")
        }
    }

    /*
        // TODO support this again?
        pub fn authenticated(&self) -> bool {
            self.read_data(None).unwrap().authenticated
        }
    */
    /// Clear the cached password for all datasets
    pub fn clear_cached_passwords(&self) -> Result<()> {
        // TODO is this needed?
        // Important: need to ensure sessions was instantiated prior to grabbing a write-lock to avoid deadlock
        {
            if self.password_cache_option.is_session_store() {
                let _unused = crate::sessions();
            }
        }
        self.for_all_datasets_mut(|d| d.clear_cached_password(self))
    }

    /// Clear the cached password for the current/default dataset
    pub fn clear_cached_password(&self, dataset: Option<&str>) -> Result<()> {
        // Important: need to ensure sessions was instantiated prior to grabbing a write-lock to avoid deadlock
        {
            if self.password_cache_option.is_session_store() {
                // TODO still needed?
                let _unused = crate::sessions();
            }
        }
        if dataset.is_some() {
            self.write_data(dataset)?.clear_cached_password(self)
        } else {
            self.for_datasets_in_hierarchy_mut(|d| d.clear_cached_password(self))
        }
    }

    pub fn should_auto_populate(&self) -> bool {
        self.auto_populate.unwrap_or(true)
    }

    pub fn auto_populate_value(&self) -> &Option<bool> {
        &self.auto_populate
    }

    pub fn set_auto_populate(&mut self, pop: Option<bool>) -> () {
        self.auto_populate = pop;
    }

    pub fn autopopulate(&self) -> Result<Option<PopulateUserReturn>> {
        if self.should_auto_populate() {
            self.populate_status.while_populating(|| {
                let mut rtn = PopulateUserReturn::default();
                for (n, d) in self.data.iter() {
                    if d.read().unwrap().should_auto_populate() {
                        log_trace!("Auto-populating dataset '{}' for user '{}'", n, &self.id);
                        match self.populate_dataset(n, false, false, true)? {
                            Some(r) => rtn.insert(n, Some(r)),
                            None => bail!("Something has gone wrong and a newly added dataset is already marked as populated")
                        };
                    }
                }
                Ok(Some(rtn))
            })
        } else {
            log_trace!("Auto-populating disabled for user '{}'", &self.id);
            Ok(None)
        }
    }

    pub fn populate(
        &self,
        repopulate: bool,
        continue_on_error: bool,
        stop_on_failure: bool,
    ) -> Result<PopulateUserReturn> {
        // This also functions as means to lock out multiple, simultaneous populate attempts
        self.populate_status.while_populating(|| {
            let mut rtn = PopulateUserReturn::default();
            for (n, d) in self.data.iter() {
                if !d.read().unwrap().has_empty_populate_config() {
                    rtn.insert(
                        n,
                        self.populate_dataset(n, repopulate, continue_on_error, stop_on_failure)?,
                    );
                }
            }
            Ok(rtn)
        })
    }

    /// Populate any data fields
    pub fn populate_dataset(
        &self,
        name: &str,
        repopulate: bool,
        continue_on_error: bool,
        stop_on_failure: bool,
    ) -> Result<Option<Outcome>> {
        log_trace!("Populating user dataset {}", name);
        Data::populate(&self, name, repopulate, continue_on_error, stop_on_failure)
    }

    pub fn session_config(&self) -> RwLockReadGuard<'_, SessionConfig> {
        self.session_config.read().unwrap()
    }

    pub fn session_config_mut(&self) -> Result<RwLockWriteGuard<'_, SessionConfig>> {
        let sessions = crate::sessions();
        if sessions
            .groups()
            .contains_key(&SessionConfig::to_sg_name(&self.id))
        {
            bail!("The session config cannot be updated for user '{}' after the session has been created", &self.id);
        }
        Ok(self.session_config.write().unwrap())
    }

    pub fn ensure_session(
        &self,
        sessions: &mut Sessions,
        namespace: Option<&str>,
    ) -> Result<(bool, bool, String, String)> {
        let sc = self.session_config();
        let sg_name = SessionConfig::to_sg_name(&self.id);
        let was_group_added;
        if let Some(grp) = sessions.groups().get(&sg_name) {
            if grp.path() != &sc.resolved_path(&self)? {
                bail!(
                    "Session group '{}' does not match the session config for user '{}'",
                    &sg_name,
                    &self.id
                );
            }
            was_group_added = false;
        } else {
            sessions.add_group(
                &sg_name,
                &sc.resolved_root(&self)?,
                Some(sc.file_permissions.to_owned()),
            )?;
            was_group_added = true;
        }
        let sname = namespace.unwrap_or(DEFAULT_USER_SESSION_STORE_NAME);
        let was_session_added = sessions.require_mut_group(&sg_name)?.ensure(sname)?;
        Ok((
            was_group_added,
            was_session_added,
            sg_name,
            sname.to_string(),
        ))
    }

    fn roles_mut(&self) -> Result<RwLockWriteGuard<'_, HashSet<String>>> {
        Ok(self.roles.write()?)
    }

    pub fn roles(&self) -> Result<RwLockReadGuard<'_, HashSet<String>>> {
        Ok(self.roles.read()?)
    }

    pub fn add_roles<S: AsRef<str>>(&self, roles: &Vec<S>) -> Result<Vec<bool>> {
        let mut rls = self.roles_mut()?;
        Ok(roles
            .iter()
            .map(|r| rls.insert(r.as_ref().to_string()))
            .collect::<Vec<bool>>())
    }

    pub fn remove_roles<S: AsRef<str>>(&self, roles: &Vec<S>) -> Result<Vec<bool>> {
        let mut rls = self.roles_mut()?;
        Ok(roles
            .iter()
            .map(|r| rls.remove(r.as_ref()))
            .collect::<Vec<bool>>())
    }

    pub fn with_session_group<T, F>(&self, mut func: F) -> Result<T>
    where
        F: FnMut(&Sessions, &SessionGroup) -> Result<T>,
    {
        let mut sessions = crate::sessions();
        let s = self.ensure_session(&mut sessions, None)?;
        let grp = sessions.require_group(&s.2)?;
        func(&sessions, &grp)
    }

    // TEST_NEEDED namespace tests
    pub fn with_session<T, F>(&self, namespace: Option<String>, mut func: F) -> Result<T>
    where
        F: FnMut(&Sessions, &SessionGroup, &SessionStore) -> Result<T>,
    {
        self.with_session_group(|sessions, sg| {
            let s = sg.require(
                namespace
                    .as_ref()
                    .map_or(DEFAULT_USER_SESSION_STORE_NAME, |v| v.as_str()),
            )?;
            func(&sessions, &sg, &s)
        })
    }

    pub fn with_dataset<T, F>(&self, dataset: &str, mut func: F) -> Result<T>
    where
        F: FnMut(&Data) -> Result<T>,
    {
        let d = self.read_data(Some(dataset))?;
        let retn = func(&d)?;
        Ok(retn)
    }

    pub fn with_dataset_mut<T, F>(&self, dataset: &str, mut func: F) -> Result<T>
    where
        F: FnMut(&mut Data) -> Result<T>,
    {
        let mut d = self.write_data(Some(dataset))?;
        let retn = func(&mut d)?;
        Ok(retn)
    }

    pub fn for_all_datasets<T, F>(&self, mut func: F) -> Result<()>
    where
        F: FnMut(&Data) -> Result<T>,
    {
        for (_n, d) in self.data.iter() {
            func(&d.read().unwrap())?;
        }
        Ok(())
    }

    pub fn for_all_datasets_mut<T, F>(&self, mut func: F) -> Result<()>
    where
        F: FnMut(&mut Data) -> Result<T>,
    {
        for (_n, d) in self.data.iter() {
            func(&mut d.write().unwrap())?;
        }
        Ok(())
    }

    pub fn for_datasets_in_hierarchy<T, F>(&self, mut func: F) -> Result<()>
    where
        F: FnMut(&Data) -> Result<T>,
    {
        for n in self.data_lookup_hierarchy.iter() {
            self.with_dataset(&n, |d| {
                func(d)
            })?;
        };
        Ok(())
    }

    pub fn for_datasets_in_hierarchy_mut<T, F>(&self, mut func: F) -> Result<()>
    where
        F: FnMut(&mut Data) -> Result<T>,
    {
        for n in self.data_lookup_hierarchy.iter() {
            self.with_dataset_mut(&n, |d| {
                func(d)
            })?;
        };
        Ok(())
    }
}

impl PartialEq for User {
    fn eq(&self, other: &Self) -> bool {
        self.uid_num == other.uid_num
    }
}