-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_controller.py
More file actions
692 lines (533 loc) · 26.4 KB
/
auth_controller.py
File metadata and controls
692 lines (533 loc) · 26.4 KB
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
import logging
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import pyqtSignal, QObject
from .auth_view import AuthView
from .auth_model import AuthModel
from core.worker import APIWorker
from utils.fields_validators import FieldValidator
from utils import NotificationService
from utils.error_messages import get_friendly_error_message
from utils.email_confirm_modal import EmailConfirmDialog
class AuthController(QObject):
"""Orchestrates the authentication UI and logic.
This controller acts as an intermediary between the authentication view
(`AuthView`) and the authentication model (`AuthModel`), following the MVC
pattern. It handles user interactions from the view, such as button clicks
and text changes, by invoking methods on the model. It initiates
asynchronous API calls using `APIWorker` for non-blocking operations like
login, signup, and password reset.
Signals:
login_successful (pyqtSignal): Emitted with the login type ('auth' or
'guest') upon a successful login, signaling the main application to
proceed.
Attributes:
model (AuthModel): The data and business logic component.
view (AuthView): The UI component for authentication pages.
auth_window (QMainWindow): The main window containing the auth UI.
active_workers (set): A set to hold references to running APIWorker
threads, preventing them from being garbage-collected.
field_validator (FieldValidator): An instance of the validator for
input fields like email and password.
"""
# Successful login signal
login_successful = pyqtSignal(str, int)
def __init__(
self,
model: AuthModel,
view: AuthView,
window: QMainWindow
) -> None:
"""Initializes the AuthController.
Args:
model (AuthModel): An instance of the authentication model.
view (AuthView): An instance of the authentication view.
window (QMainWindow): The main application window that will host
the authentication UI.
"""
super().__init__()
self.model = model
self.view = view
self.auth_window = window
# Windows
self.main_module_window = None
self.email_confirm_window = None
# Set of active workers
self.active_workers = set()
# Initialize field validator
self.field_validator = FieldValidator()
# Setup connections
self._setup_login_connections()
self._setup_signup_connections()
self._setup_confirm_email_connections()
self._setup_reset_password_connections()
self.view.theme_switcher_clicked(self.on_theme_switcher_clicked)
# ====================
# Contoller Basic Methods
# ====================
"""=== Login ==="""
def login_user(self, data: dict) -> None:
"""Handles successful user login.
This method is called as a success callback after the login API call
is successfully completed. It saves the user data using the model,
clears the login form fields, and emits a signal to notify the
main application to switch to the main module.
Args:
data (dict): The user data received from the API upon login.
"""
try:
auto_login = self.view.login_page.get_auto_login_state()
user_id = self.model.save_user(user_data=data, auto_login=auto_login)
if not isinstance(user_id, int):
NotificationService().show_toast(
"error",
"Ошибка входа",
"Не удалось сохранить сессию. Попробуйте войти снова."
)
return
self.view.login_page.clear_lineedits()
self.login_successful.emit("auth", user_id)
except Exception as e:
msg = get_friendly_error_message(e)
NotificationService().show_toast("error", "Ошибка входа", msg)
"""=== Sign Up ==="""
def signup_user(self, user_data: dict) -> None:
"""Handles successful user registration.
This method is called as a success callback after the user has
successfully signed up and confirmed their email. It saves the new
user's data, clears the signup form fields, and emits a signal
to switch to the main application window.
Args:
user_data (dict): The user data received from the API upon signup.
"""
try:
auto_login = self.view.signup_page.get_auto_login_state()
user_id = self.model.save_user(user_data=user_data, auto_login=auto_login)
if not isinstance(user_id, int):
NotificationService().show_toast(
"error",
"Ошибка регистрации",
"Не удалось сохранить сессию. Попробуйте войти снова."
)
return
self.view.signup_page.clear_lineedits()
NotificationService().show_toast(
notification_type="success",
title="Регистрация успешна",
message="Аккаунт создан. Добро пожаловать!"
)
self.login_successful.emit("auth", user_id)
except Exception as e:
msg = get_friendly_error_message(e)
NotificationService().show_toast("error", "Ошибка регистрации", msg)
def signup(self, data: dict, email: str, password: str) -> None:
"""Processes the signup after email verification.
This method is called after the initial signup request (sending the
confirmation code) succeeds. It prompts the user to enter the
verification code via a modal dialog. It then creates and starts a
worker to finalize the signup process with the provided code.
Args:
data (dict): Data from the initial signup request (unused).
email (str): The user's email address.
password (str): The user's chosen password.
"""
logging.info(f"Signup data received: {data}")
NotificationService().show_toast(
notification_type="info",
title="Код отправлен",
message=f"Код подтверждения отправлен на {email}"
)
code = EmailConfirmDialog.get_code(parent=self.auth_window)
if not code:
NotificationService().show_toast(
notification_type="info",
title="Подтверждение отменено",
message="Регистрация не завершена: код подтверждения не введён."
)
return
# Create success callback
success_cb = lambda data: self.signup_user(user_data=data)
# Create worker
self._create_worker(
method=self.model.signup,
success_signal=success_cb,
error_signal=self._worker_error,
code=code,
email=email,
password=password
)
"""=== Reset Password ==="""
def password_reseted(self, data: dict) -> None:
"""Handles post-password-reset actions.
This method is called after the user's password has been successfully
reset. It switches the view back to the login page and clears the
fields of the password reset form.
Args:
data (dict): Data from the password reset API call (unused).
"""
# Save token
# Switch to login page
page = self.view.pages.get("login_page", None)
if page:
self.view.switch_page(page=page)
# Clear lineedits
self.view.forgot_page_reset_password.clear_lineedits()
NotificationService().show_toast(notification_type="success",
title="Пароль успешно изменён",
message="Ваш пароль успешно изменён!")
def switch_to_reset_password_page(self, data: dict) -> None:
"""Switches to the password reset page after email confirmation.
This method is called after the user successfully verifies their email
for a password reset. It saves the necessary reset token and switches
the UI to the page where the new password can be entered.
Args:
data (dict): Data containing the reset token from the API.
"""
try:
self.model.save_token(token_name="reset_token", token="reset_token", data=data)
page = self.view.pages.get("change_password_change_page", None)
if page:
self.view.switch_page(page=page)
except Exception as e:
msg = get_friendly_error_message(e)
NotificationService().show_toast("error", "Ошибка восстановления", msg)
def open_email_confirm_modal_window(self, data, email: str) -> None:
"""Opens email confirmation dialog for password reset.
This method is called after a password reset is requested. It opens
a modal dialog for the user to enter the confirmation code sent to
their email. It then creates a worker to verify this code.
Args:
data (dict): Data from the initial reset request (unused).
email (str): The email address to which the code was sent.
"""
logging.info(f"Reset password data received: {data}")
code = EmailConfirmDialog.get_code(parent=self.auth_window)
if not code:
NotificationService().show_toast(
notification_type="info",
title="Подтверждение отменено",
message="Сброс пароля не завершён: код подтверждения не введён."
)
return
# Create success callback
success_cb = lambda data: self.switch_to_reset_password_page(data=data)
# Create worker
self._create_worker(
method=self.model.reset_password_confirm_email,
success_signal=success_cb,
error_signal=self._worker_error,
email=email,
code=code
)
# ====================
# Contoller Handlers
# ====================
"""=== Theme ==="""
def on_theme_switcher_clicked(self) -> None:
"""Handles the theme switcher button click.
Calls the view's `set_theme` method to toggle between light and dark
themes for the application.
"""
self.view.set_theme()
"""=== Login ==="""
def on_login_page_login_button_clicked(self) -> None:
"""Handles the login button click on the login page.
Retrieves the email and password from the view, then creates an
asynchronous worker to perform the login operation via the model.
"""
# Get data from lineedits
email = self.view.login_page.get_email()
password = self.view.login_page.get_password()
# Create success callback
success_cb = lambda data: self.login_user(data=data)
# Create worker
self._create_worker(
method=self.model.login,
success_signal=success_cb,
error_signal=self._worker_error,
email=email,
password=password
)
def on_login_page_guest_button_clicked(self) -> None:
"""Handles the 'Continue as Guest' button click.
Emits the `login_successful` signal with 'guest' as the login type,
allowing guest access to the application.
"""
self.login_successful.emit("guest", -1)
def on_login_page_create_button_clicked(self) -> None:
"""Handles the 'Create Account' button click on the login page.
Switches the view from the login page to the signup page.
"""
page = self.view.pages.get("signup_page", None)
if page:
self.view.switch_page(page=page)
def on_login_page_forgot_button_clicked(self, event) -> None:
"""Handles the 'Forgot Password' button click.
Switches the view to the first page of the password reset flow,
which prompts the user for their email address.
Args:
event: The event object from the button click signal (unused).
"""
page = self.view.pages.get("change_password_confirm_page", None)
if page:
self.view.switch_page(page=page)
def on_login_page_lineedits_changed(self) -> None:
"""Handles text changes in the login page's input fields.
Validates the format of the email and password fields. The submit
button's state is updated to be enabled only if both fields contain
text and are valid.
"""
# Get texts from lineedits
email = self.view.login_page.get_email()
password = self.view.login_page.get_password()
# Validate email
if not self.field_validator.validate_email(email=email):
self.view.login_page.update_submit_button_state(state=False)
return
# Validate password
if not self.field_validator.validate_password(password=password):
self.view.login_page.update_submit_button_state(state=False)
return
# Defining login button state (True if both lineedits has text, else False)
state = bool(email.strip() and password.strip())
# Update login button state
self.view.login_page.update_submit_button_state(state=state)
def on_login_page_view_password_checkbox_state_changed(self):
"""Handles the state change of the 'View Password' checkbox.
Toggles the visibility of the password text in the login form based on
the checkbox's state.
"""
state = self.view.login_page.get_view_password_state()
self.view.login_page.set_password_visibility(state)
"""=== Signup ==="""
def on_signup_page_create_button_clicked(self) -> None:
"""Handles the 'Create Account' button click on the signup page.
Retrieves the email and password from the view, then creates a worker
to request an email confirmation code from the server.
"""
# Get data from lineedits
email = self.view.signup_page.get_email()
password = self.view.signup_page.get_password()
# Create success callback
success_cb = lambda data: self.signup(data=data, email=email, password=password)
# Create email confirm worker
self._create_worker(
method=self.model.signup_send_code,
success_signal=success_cb,
error_signal=self._worker_error,
email=email
)
def on_signup_page_have_account_button_clicked(self) -> None:
"""Handles the 'I have an account' button click on the signup page.
Switches the view from the signup page back to the login page.
"""
page = self.view.pages.get("login_page", None)
if page:
self.view.switch_page(page=page)
def on_signup_page_lineedits_changed(self) -> None:
"""Handles text changes in the signup page's input fields.
Validates the email and password fields. The submit button is enabled
only if all fields are filled, valid, and the password matches the
confirmation password.
"""
# Get texts from lineedits
email = self.view.signup_page.get_email()
password = self.view.signup_page.get_password()
confirm_password = self.view.signup_page.get_confirm_password()
# Validate email
if not self.field_validator.validate_email(email=email):
self.view.signup_page.update_submit_button_state(state=False)
return
# Validate password
if not self.field_validator.validate_password(password=password):
self.view.signup_page.update_submit_button_state(state=False)
return
# Check password matching
passwords_match = password == confirm_password
# Defining signup button state
state = bool(email.strip() and password.strip() and confirm_password.strip() and passwords_match)
# Update signup button state
self.view.signup_page.update_submit_button_state(state=state)
def on_view_password_signup_page_checkbox_state_changed(self):
"""Handles the 'View Password' checkbox state change on the signup page.
Toggles the visibility of both the password and confirm password
fields based on the checkbox's state.
"""
state = self.view.signup_page.get_view_password_state()
self.view.signup_page.set_password_visibility(state)
"""=== Reset Password ==="""
def on_confirm_email_page_confirm_button_clicked(self) -> None:
"""Handles the confirm button click for password reset.
Retrieves the email from the view and creates a worker to request a
password reset code from the server.
"""
email = self.view.forgot_page_email_confirm.get_email()
# Create success callback
success_cb = lambda data: self.open_email_confirm_modal_window(data=data, email=email)
# Create worker
self._create_worker(
method=self.model.request_reset_password,
success_signal=success_cb,
error_signal=self._worker_error,
email=email
)
def on_email_confirm_page_email_lineedit_text_changed(self) -> None:
"""Handles text changes in the email field on the password reset page.
Validates the email format and enables the submit button only if the
email is valid.
"""
# Get text from lineedit
email = self.view.forgot_page_email_confirm.get_email()
# Validate email lineedit
validate_email_satate = self.field_validator.validate_email(email=email)
# Change confirm email button state
self.view.forgot_page_email_confirm.update_submit_button_state(state=validate_email_satate)
def on_reset_password_page_back_button_clicked(self) -> None:
"""Handles the cancel button click during the password reset flow.
Switches the view back to the login page and clears any input from
the email confirmation form.
"""
# Switch to login page
page = self.view.pages.get("login_page", None)
if page:
self.view.switch_page(page=page)
# Clear lineedits
self.view.forgot_page_email_confirm.clear_lineedits()
def on_reset_password_page_lineedits_changed(self) -> None:
"""Handles text changes in the new password and confirm password fields.
Validates the password fields. The submit button is enabled only if
both fields are filled, valid, and the passwords match.
"""
# Get texts from lineedits
password = self.view.forgot_page_reset_password.get_password()
confirm_password = self.view.forgot_page_reset_password.get_confirm_password()
# Validate password
if not self.field_validator.validate_password(password=password):
self.view.forgot_page_reset_password.update_submit_button_state(state=False)
return
# Check password matching
passwords_match = password == confirm_password
# Defining signup button state
state = bool(password.strip() and confirm_password.strip() and passwords_match)
# Update signup button state
self.view.forgot_page_reset_password.update_submit_button_state(state=state)
def on_reset_password_page_reset_button_clicked(self) -> None:
"""Handles the final 'Change Password' button click.
Retrieves the new password from the view and creates a worker to
submit the new password to the server.
"""
# Get new password
password = self.view.forgot_page_reset_password.get_password()
# Create success callback
success_cb = lambda data: self.password_reseted(data=data)
# Create worker
self._create_worker(
method=self.model.reset_password,
success_signal=success_cb,
error_signal=self._worker_error,
password=password
)
def on_reset_password_view_password_checkbox_state_changed(self):
"""Handles the 'View Password' checkbox on the password change page.
Toggles the visibility of the new password and confirm password fields
based on the checkbox's state.
"""
state = self.view.forgot_page_reset_password.get_view_password_state()
self.view.forgot_page_reset_password.set_password_visibility(state)
# ====================
# Controller methods
# ====================
"""=== Setup connections ==="""
def _setup_login_connections(self) -> None:
"""Connects signals from the login page widgets to controller handlers."""
# Get login page
login_page = self.view.login_page
# Connect signals to handlers
login_page.submit_button_clicked(self.on_login_page_login_button_clicked)
login_page.back_button_clicked(self.on_login_page_guest_button_clicked)
login_page.tertiary_button_clicked(self.on_login_page_create_button_clicked)
login_page.forgot_password_button_clicked(self.on_login_page_forgot_button_clicked)
login_page.view_password_checkbox_state_changed(self.on_login_page_view_password_checkbox_state_changed)
login_page.email_lineedit_text_changed(self.on_login_page_lineedits_changed)
login_page.password_lineedit_text_changed(self.on_login_page_lineedits_changed)
def _setup_signup_connections(self) -> None:
"""Connects signals from the signup page widgets to controller handlers."""
# Get signup page
signup_page = self.view.signup_page
# Connect signals to handlers
signup_page.submit_button_clicked(self.on_signup_page_create_button_clicked)
signup_page.back_button_clicked(self.on_signup_page_have_account_button_clicked)
signup_page.view_password_checkbox_state_changed(self.on_view_password_signup_page_checkbox_state_changed)
signup_page.email_lineedit_text_changed(self.on_signup_page_lineedits_changed)
signup_page.password_lineedit_text_changed(self.on_signup_page_lineedits_changed)
signup_page.confirm_password_lineedit_text_changed(self.on_signup_page_lineedits_changed)
def _setup_confirm_email_connections(self) -> None:
"""Connects signals from the email confirmation page to controller handlers."""
# Get confirm email page
email_confirm_page = self.view.forgot_page_email_confirm
# Connect signals to handlers
email_confirm_page.submit_button_clicked(self.on_confirm_email_page_confirm_button_clicked)
email_confirm_page.back_button_clicked(self.on_reset_password_page_back_button_clicked)
email_confirm_page.email_lineedit_text_changed(self.on_email_confirm_page_email_lineedit_text_changed)
def _setup_reset_password_connections(self) -> None:
"""Connects signals from the password reset page to controller handlers."""
# Get reset password page
reset_password_page = self.view.forgot_page_reset_password
# Connect signals to handlers
reset_password_page.submit_button_clicked(self.on_reset_password_page_reset_button_clicked)
reset_password_page.back_button_clicked(self.on_reset_password_page_back_button_clicked)
reset_password_page.view_password_checkbox_state_changed(self.on_reset_password_view_password_checkbox_state_changed)
reset_password_page.password_lineedit_text_changed(self.on_reset_password_page_lineedits_changed)
reset_password_page.confirm_password_lineedit_text_changed(self.on_reset_password_page_lineedits_changed)
"""=== API Worker ==="""
def _create_worker(
self,
method: callable,
success_signal: callable,
error_signal: callable,
**kwargs
) -> None:
"""Creates, configures, and starts a new APIWorker thread.
This factory method encapsulates the creation and management of an
APIWorker for performing asynchronous API calls. It connects the
worker's `finished` and `error` signals to the provided callbacks.
The worker object is added to the `active_workers` set to prevent
premature garbage collection and is automatically removed upon
completion.
Args:
method (callable): The model's method to be executed in the worker
thread.
success_signal (callable): The callback function to execute upon
successful completion. It receives the data returned by the
worker.
error_signal (callable): The callback function to execute when an
error occurs. It receives the exception object.
**kwargs: Arbitrary keyword arguments to be passed to the `method`
when it's called.
"""
# Create a new APIWorker instance
worker = APIWorker(method, **kwargs)
# Connect the worker's signals to the provided success and error signals
worker.finished.connect(success_signal)
worker.error.connect(error_signal)
# Delete the worker from active workers when it finishes
worker.finished.connect(lambda: self.active_workers.discard(worker))
worker.error.connect(lambda: self.active_workers.discard(worker))
# Add the worker to active workers
self.active_workers.add(worker)
# Start the worker
worker.start()
def _worker_error(self, exception: Exception) -> None:
"""Handles errors reported by APIWorker threads.
This method is connected to the `error` signal of all workers.
Currently, it prints the exception to the console. For production,
it should display a user-friendly error message.
Args:
exception (Exception): The exception object caught by the worker.
"""
logging.error(f"Worker error: {exception}", exc_info=True)
message = get_friendly_error_message(exception)
NotificationService().show_toast(
notification_type="error",
title="Ошибка",
message=message
)