fractal/session/view/account_settings/general_page/
change_password_subpage.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
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
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::{
    glib::{self, clone},
    CompositeTemplate,
};
use matrix_sdk::ruma::{
    api::client::{account::change_password, error::ErrorKind},
    assign,
};
use tracing::error;

use crate::{
    components::{AuthDialog, AuthError, LoadingButtonRow},
    session::model::Session,
    toast,
    utils::matrix::validate_password,
};

mod imp {
    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/account_settings/general_page/change_password_subpage.ui"
    )]
    #[properties(wrapper_type = super::ChangePasswordSubpage)]
    pub struct ChangePasswordSubpage {
        /// The current session.
        #[property(get, set, nullable)]
        pub session: glib::WeakRef<Session>,
        #[template_child]
        pub password: TemplateChild<adw::PasswordEntryRow>,
        #[template_child]
        pub password_progress: TemplateChild<gtk::LevelBar>,
        #[template_child]
        pub password_error_revealer: TemplateChild<gtk::Revealer>,
        #[template_child]
        pub password_error: TemplateChild<gtk::Label>,
        #[template_child]
        pub confirm_password: TemplateChild<adw::PasswordEntryRow>,
        #[template_child]
        pub confirm_password_error_revealer: TemplateChild<gtk::Revealer>,
        #[template_child]
        pub confirm_password_error: TemplateChild<gtk::Label>,
        #[template_child]
        pub button: TemplateChild<LoadingButtonRow>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for ChangePasswordSubpage {
        const NAME: &'static str = "ChangePasswordSubpage";
        type Type = super::ChangePasswordSubpage;
        type ParentType = adw::NavigationPage;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            Self::Type::bind_template_callbacks(klass);
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

    #[glib::derived_properties]
    impl ObjectImpl for ChangePasswordSubpage {
        fn constructed(&self) {
            self.parent_constructed();
            let obj = self.obj();

            self.password_progress.set_min_value(0.0);
            self.password_progress.set_max_value(5.0);
            self.password_progress
                .add_offset_value(gtk::LEVEL_BAR_OFFSET_LOW, 1.0);
            self.password_progress.add_offset_value("step2", 2.0);
            self.password_progress.add_offset_value("step3", 3.0);
            self.password_progress
                .add_offset_value(gtk::LEVEL_BAR_OFFSET_HIGH, 4.0);
            self.password_progress
                .add_offset_value(gtk::LEVEL_BAR_OFFSET_FULL, 5.0);

            self.password.connect_changed(clone!(
                #[weak]
                obj,
                move |_| {
                    obj.validate_password();
                    obj.validate_password_confirmation();
                }
            ));

            self.confirm_password.connect_changed(clone!(
                #[weak]
                obj,
                move |_| {
                    obj.validate_password_confirmation();
                }
            ));
        }
    }

    impl WidgetImpl for ChangePasswordSubpage {}
    impl NavigationPageImpl for ChangePasswordSubpage {}
}

glib::wrapper! {
    /// Account settings page about the user and the session.
    pub struct ChangePasswordSubpage(ObjectSubclass<imp::ChangePasswordSubpage>)
        @extends gtk::Widget, adw::NavigationPage, @implements gtk::Accessible;
}

#[gtk::template_callbacks]
impl ChangePasswordSubpage {
    pub fn new(session: &Session) -> Self {
        glib::Object::builder().property("session", session).build()
    }

    fn validate_password(&self) {
        let imp = self.imp();
        let entry = &imp.password;
        let progress = &imp.password_progress;
        let revealer = &imp.password_error_revealer;
        let label = &imp.password_error;
        let password = entry.text();

        if password.is_empty() {
            revealer.set_reveal_child(false);
            entry.remove_css_class("success");
            entry.remove_css_class("warning");
            progress.set_value(0.0);
            progress.remove_css_class("success");
            progress.remove_css_class("warning");
            self.update_button();
            return;
        }

        let validity = validate_password(&password);

        progress.set_value(f64::from(validity.progress) / 20.0);
        if validity.progress == 100 {
            revealer.set_reveal_child(false);
            entry.add_css_class("success");
            entry.remove_css_class("warning");
            progress.add_css_class("success");
            progress.remove_css_class("warning");
        } else {
            entry.remove_css_class("success");
            entry.add_css_class("warning");
            progress.remove_css_class("success");
            progress.add_css_class("warning");
            if !validity.has_length {
                label.set_label(&gettext("Password must be at least 8 characters long"));
            } else if !validity.has_lowercase {
                label.set_label(&gettext(
                    "Password must have at least one lower-case letter",
                ));
            } else if !validity.has_uppercase {
                label.set_label(&gettext(
                    "Password must have at least one upper-case letter",
                ));
            } else if !validity.has_number {
                label.set_label(&gettext("Password must have at least one digit"));
            } else if !validity.has_symbol {
                label.set_label(&gettext("Password must have at least one symbol"));
            }
            revealer.set_reveal_child(true);
        }

        self.update_button();
    }

    fn validate_password_confirmation(&self) {
        let imp = self.imp();
        let entry = &imp.confirm_password;
        let revealer = &imp.confirm_password_error_revealer;
        let label = &imp.confirm_password_error;
        let password = imp.password.text();
        let confirmation = entry.text();

        if confirmation.is_empty() {
            revealer.set_reveal_child(false);
            entry.remove_css_class("success");
            entry.remove_css_class("warning");
            return;
        }

        if password == confirmation {
            revealer.set_reveal_child(false);
            entry.add_css_class("success");
            entry.remove_css_class("warning");
        } else {
            entry.remove_css_class("success");
            entry.add_css_class("warning");
            label.set_label(&gettext("Passwords do not match"));
            revealer.set_reveal_child(true);
        }
        self.update_button();
    }

    fn update_button(&self) {
        self.imp().button.set_sensitive(self.can_change_password());
    }

    fn can_change_password(&self) -> bool {
        let imp = self.imp();
        let password = imp.password.text();
        let confirmation = imp.confirm_password.text();

        validate_password(&password).progress == 100 && password == confirmation
    }

    #[template_callback]
    async fn change_password(&self) {
        let Some(session) = self.session() else {
            return;
        };

        if !self.can_change_password() {
            return;
        }

        let imp = self.imp();
        let password = imp.password.text();

        imp.button.set_is_loading(true);
        imp.password.set_sensitive(false);
        imp.confirm_password.set_sensitive(false);

        let dialog = AuthDialog::new(&session);

        let result = dialog
            .authenticate(self, move |client, auth| {
                let password = password.clone();
                async move {
                    let request =
                        assign!(change_password::v3::Request::new(password.into()), { auth });
                    client.send(request, None).await.map_err(Into::into)
                }
            })
            .await;

        match result {
            Ok(_) => {
                toast!(self, gettext("Password changed successfully"));
                imp.password.set_text("");
                imp.confirm_password.set_text("");
                self.activate_action("account-settings.close-subpage", None)
                    .unwrap();
            }
            Err(error) => match error {
                AuthError::UserCancelled => {}
                AuthError::ServerResponse(error)
                    if matches!(error.client_api_error_kind(), Some(ErrorKind::WeakPassword)) =>
                {
                    error!("Weak password: {error}");
                    toast!(self, gettext("Password rejected for being too weak"));
                }
                _ => {
                    error!("Could not change the password: {error:?}");
                    toast!(self, gettext("Could not change password"));
                }
            },
        }
        imp.button.set_is_loading(false);
        imp.password.set_sensitive(true);
        imp.confirm_password.set_sensitive(true);
    }
}