edge_frame/
role.rs

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
use std::rc::Rc;

use yew::prelude::*;
use yew_router::prelude::*;
use yewdux::use_store_value;
use yewdux_middleware::*;

use crate::auth::*;
use crate::frame::*;
use crate::loading::*;

pub use crate::dto::Role as RoleDto;

#[derive(Default, Clone, Debug, PartialEq, Eq, Store)]
pub struct RoleStore(pub Option<RoleState>);

#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct Credentials {
    pub username: String,
    pub password: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RoleState {
    Authenticating(Credentials),
    AuthenticationFailed(Credentials),
    Role(RoleDto),
    LoggingOut(RoleDto),
    LoggedOut,
}

impl Reducer<RoleStore> for RoleState {
    fn apply(self, mut store: Rc<RoleStore>) -> Rc<RoleStore> {
        let state = Rc::make_mut(&mut store);

        state.0 = Some(self);

        store
    }
}

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct RoleProps {
    pub role: RoleDto,

    #[prop_or_default]
    pub auth: bool,

    #[prop_or_default]
    pub children: Children,
}

#[function_component(Role)]
pub fn role(props: &RoleProps) -> Html {
    let mcx = use_mcx();
    let role = use_store_value::<RoleStore>();
    let role = role.0.as_ref();

    match (&role, &Default::default()) {
        (Some(RoleState::Role(role)), _) if *role >= props.role => {
            // Have permissions to render the content
            html! {
                { for props.children.iter() }
            }
        }
        (None, _) if props.auth => {
            // Unknown permissions => render modal loader if auth=true
            html! {
                <Loading/>
            }
        }
        (Some(RoleState::AuthenticationFailed(credentials)), _)
        | (Some(RoleState::Authenticating(credentials)), _)
        | (Some(RoleState::Role(RoleDto::None)), credentials)
            if props.auth =>
        {
            // Not authenticated yet or previous authentication attempt failed => render login dialog if auth=true
            let submit = {
                Callback::from(move |(username, password)| {
                    mcx.invoke(RoleState::Authenticating(Credentials {
                        username,
                        password,
                    }));
                })
            };

            html! {
                <Auth
                    username={credentials.username.clone()}
                    password={credentials.password.clone()}
                    authenticating={matches!(role, Some(RoleState::Authenticating(_)))}
                    auth_failed={matches!(role, Some(RoleState::AuthenticationFailed(_)))}
                    {submit}
                />
            }
        }
        _ if props.auth => {
            // No permissions => render permissions denied modal if auth=true
            html! {
                <NoPerm/>
            }
        }
        _ => {
            // In all other cases just hide the content
            // This is when auth=false and we don't have a role (possibly not yet) that allows us to display the content
            html! {}
        }
    }
}

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct RoleLogoutStatusItemProps<R: Routable + PartialEq + Clone + 'static> {
    pub auth_status_route: R,
}

#[function_component(RoleLogoutStatusItem)]
pub fn role_logout_status_item<R: Routable + PartialEq + Clone + 'static>(
    props: &RoleLogoutStatusItemProps<R>,
) -> Html {
    let mcx = use_mcx();
    let role = use_store_value::<RoleStore>();
    let role = role.0.as_ref();

    let history = use_navigator();

    match &role {
        Some(RoleState::Role(role_value)) if *role_value >= crate::dto::Role::None => {
            let selected = {
                let auth_status_route = props.auth_status_route.clone();
                let role_value = *role_value;

                Callback::from(move |_| {
                    mcx.invoke(RoleState::LoggingOut(role_value));

                    if let Some(history) = history.as_ref() {
                        history.push(&auth_status_route);
                    }
                })
            };

            html! {
                <StatusItem
                    icon="fa-lg fa-solid fa-right-from-bracket"
                    {selected}/>
            }
        }
        _ => {
            html! {}
        }
    }
}

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct RoleAuthStateProps<R: Routable + PartialEq + Clone + 'static> {
    #[prop_or_default]
    pub home: Option<R>,
}

#[function_component(RoleAuthState)]
pub fn role_auth_state<R: Routable + PartialEq + Clone + 'static>(
    props: &RoleAuthStateProps<R>,
) -> Html {
    let history = use_navigator();

    let role = use_store_value::<RoleStore>();
    let role = role.0.as_ref();

    let role = match &role {
        Some(RoleState::Role(role_value)) => Some(*role_value),
        _ => None,
    };

    let login = if let Some(home) = props.home.as_ref() {
        let home = home.clone();

        Some(Callback::from(move |_| {
            if let Some(history) = history.as_ref() {
                history.push(&home);
            }
        }))
    } else {
        None
    };

    html! {
        <AuthState {login} {role}/>
    }
}