|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter or change the main password. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from eric7.Utilities.crypto.py3PBKDF2 import verifyPassword |
|
14 |
|
15 from .Ui_MainPasswordEntryDialog import Ui_MainPasswordEntryDialog |
|
16 |
|
17 |
|
18 class MainPasswordEntryDialog(QDialog, Ui_MainPasswordEntryDialog): |
|
19 """ |
|
20 Class implementing a dialog to enter or change the main password. |
|
21 """ |
|
22 |
|
23 def __init__(self, oldPasswordHash, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param oldPasswordHash hash of the current password (string) |
|
28 @param parent reference to the parent widget (QWidget) |
|
29 """ |
|
30 super().__init__(parent) |
|
31 self.setupUi(self) |
|
32 |
|
33 self.__oldPasswordHash = oldPasswordHash |
|
34 if self.__oldPasswordHash == "": |
|
35 self.currentPasswordEdit.setEnabled(False) |
|
36 if hasattr(self.currentPasswordEdit, "setPlaceholderText"): |
|
37 self.currentPasswordEdit.setPlaceholderText( |
|
38 self.tr("(not defined yet)") |
|
39 ) |
|
40 |
|
41 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
|
42 |
|
43 def __updateUI(self): |
|
44 """ |
|
45 Private slot to update the variable parts of the UI. |
|
46 """ |
|
47 enable = True |
|
48 error = "" |
|
49 if self.currentPasswordEdit.isEnabled(): |
|
50 enable = verifyPassword( |
|
51 self.currentPasswordEdit.text(), self.__oldPasswordHash |
|
52 ) |
|
53 if not enable: |
|
54 error = error or self.tr("Wrong password entered.") |
|
55 |
|
56 if self.newPasswordEdit.text() == "": |
|
57 enable = False |
|
58 error = error or self.tr("New password must not be empty.") |
|
59 |
|
60 if ( |
|
61 self.newPasswordEdit.text() != "" |
|
62 and self.newPasswordEdit.text() != self.newPasswordAgainEdit.text() |
|
63 ): |
|
64 enable = False |
|
65 error = error or self.tr("Repeated password is wrong.") |
|
66 |
|
67 if ( |
|
68 self.currentPasswordEdit.isEnabled() |
|
69 and self.newPasswordEdit.text() == self.currentPasswordEdit.text() |
|
70 ): |
|
71 enable = False |
|
72 error = error or self.tr("Old and new password must not be the same.") |
|
73 |
|
74 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
75 self.errorLabel.setText(error) |
|
76 |
|
77 @pyqtSlot(str) |
|
78 def on_currentPasswordEdit_textChanged(self, txt): |
|
79 """ |
|
80 Private slot to handle changes of the current password. |
|
81 |
|
82 @param txt content of the edit widget (string) |
|
83 """ |
|
84 self.__updateUI() |
|
85 |
|
86 @pyqtSlot(str) |
|
87 def on_newPasswordEdit_textChanged(self, txt): |
|
88 """ |
|
89 Private slot to handle changes of the new password. |
|
90 |
|
91 @param txt content of the edit widget (string) |
|
92 """ |
|
93 self.passwordMeter.checkPasswordStrength(txt) |
|
94 self.__updateUI() |
|
95 |
|
96 @pyqtSlot(str) |
|
97 def on_newPasswordAgainEdit_textChanged(self, txt): |
|
98 """ |
|
99 Private slot to handle changes of the new again password. |
|
100 |
|
101 @param txt content of the edit widget (string) |
|
102 """ |
|
103 self.__updateUI() |
|
104 |
|
105 def getMainPassword(self): |
|
106 """ |
|
107 Public method to get the new main password. |
|
108 |
|
109 @return new main password (string) |
|
110 """ |
|
111 return self.newPasswordEdit.text() |
|
112 |
|
113 def getCurrentPassword(self): |
|
114 """ |
|
115 Public method to get the current main password. |
|
116 |
|
117 @return current main password (string) |
|
118 """ |
|
119 return self.currentPasswordEdit.text() |