//! What Jarvis may do on this PC, and for whom (`app/src/admin.spec.md`, "PC control permissions"; //! `docs/v2/proactive.md` 제안 3). The PC control module asks `decide` before every action. use crate::settings::{Level, PcControlSettings}; /// A kind of action on the PC. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Action { /// See the screen, a monitor or a window, and list the windows. View, /// Press a button, a menu item or a point on the screen. Click, /// Put text or keys into a program. Type, /// Start a program. Run, /// Pay or buy anything. Pay, /// Read, type or change a password or another secret. Password, /// Delete files. DeleteFiles, /// Anything that needs administrator rights. Admin, } /// Why an action is refused (the PC control module tells the person). #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Refusal { /// The person is not in `pcControl.allowedUsers`. NotAllowed, /// The action's level is "안 함". TurnedOff, /// Never done, whatever the settings say. Never, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Decision { Allow, /// Allowed once the person agrees in their next message. Ask, Refuse(Refusal), } /// Whether `user` may have Jarvis do `action`. Only registered people may ask for anything (screen /// viewing too: the screen may show anything); payments, passwords, deleting files and /// administrator rights are never done. pub fn decide(s: &PcControlSettings, user: u64, action: Action) -> Decision { if !s.allowed_users.contains(&user) { return Decision::Refuse(Refusal::NotAllowed); } let level = match action { Action::View => s.view, Action::Click => s.click, Action::Type => s.type_text, Action::Run => s.run, Action::Pay | Action::Password | Action::DeleteFiles | Action::Admin => return Decision::Refuse(Refusal::Never), }; match level { Level::Auto => Decision::Allow, Level::Ask => Decision::Ask, Level::Never => Decision::Refuse(Refusal::TurnedOff), } } #[cfg(test)] mod tests { use super::*; const OWNER: u64 = 123456789012345678; fn settings() -> PcControlSettings { PcControlSettings { allowed_users: vec![OWNER], ..crate::settings::Settings::default().pc_control } } #[test] fn by_default_viewing_is_automatic_and_operating_asks_first() { let s = settings(); assert_eq!(decide(&s, OWNER, Action::View), Decision::Allow); for a in [Action::Click, Action::Type, Action::Run] { assert_eq!(decide(&s, OWNER, a), Decision::Ask, "{a:?}"); } } #[test] fn only_registered_people_may_ask_for_anything() { let s = settings(); for a in [Action::View, Action::Click, Action::Type, Action::Run, Action::Pay] { assert_eq!(decide(&s, 42, a), Decision::Refuse(Refusal::NotAllowed), "{a:?}"); } // Nobody is registered by default. let none = crate::settings::Settings::default().pc_control; assert_eq!(decide(&none, OWNER, Action::View), Decision::Refuse(Refusal::NotAllowed)); } #[test] fn the_levels_follow_the_settings_but_some_actions_are_never_done() { let s = PcControlSettings { view: Level::Never, click: Level::Auto, type_text: Level::Never, run: Level::Auto, ..settings() }; assert_eq!(decide(&s, OWNER, Action::View), Decision::Refuse(Refusal::TurnedOff)); assert_eq!(decide(&s, OWNER, Action::Click), Decision::Allow); assert_eq!(decide(&s, OWNER, Action::Type), Decision::Refuse(Refusal::TurnedOff)); let all_auto = PcControlSettings { view: Level::Auto, click: Level::Auto, type_text: Level::Auto, run: Level::Auto, ..settings() }; for a in [Action::Pay, Action::Password, Action::DeleteFiles, Action::Admin] { assert_eq!(decide(&all_auto, OWNER, a), Decision::Refuse(Refusal::Never), "{a:?}"); } } }