Debian Codegraph @

src/fdosecrets/widgets/AccessControlDialog.cpp

raw
1 /*
2 * Copyright (C) 2013 Francois Ferrand
3 * Copyright (C) 2017 KeePassXC Team <[email protected]>
4 * Copyright (C) 2020 Aetf <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "AccessControlDialog.h"
21 #include "ui_AccessControlDialog.h"
22
23 #include "fdosecrets/dbus/DBusClient.h"
24 #include "fdosecrets/widgets/RowButtonHelper.h"
25
26 #include "core/Entry.h"
27 #include "gui/Icons.h"
28
29 #include <QWindow>
30
31 #include <functional>
32
33 AccessControlDialog::AccessControlDialog(QWindow* parent,
34 const QList<Entry*>& entries,
35 const QString& app,
36 const FdoSecrets::PeerInfo& info,
37 AuthOptions authOptions)
38 : m_ui(new Ui::AccessControlDialog())
39 , m_rememberCheck()
40 , m_model(new EntryModel(entries))
41 , m_decisions()
42 {
43 if (parent) {
44 // Force the creation of the QWindow, without this windowHandle() will return nullptr
45 winId();
46 auto window = windowHandle();
47 Q_ASSERT(window);
48 window->setTransientParent(parent);
49 }
50 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
51
52 connect(this, &QDialog::finished, this, &AccessControlDialog::dialogFinished);
53
54 m_ui->setupUi(this);
55 m_ui->appLabel->setText(m_ui->appLabel->text().arg(app));
56
57 // items table
58 connect(m_ui->itemsTable, &QTableView::clicked, m_model.data(), &EntryModel::toggleCheckState);
59 m_ui->itemsTable->setModel(m_model.data());
60 installWidgetItemDelegate<DenyButton>(m_ui->itemsTable, 2, [this](QWidget* p, const QModelIndex& idx) {
61 auto btn = new DenyButton(p, idx);
62 connect(btn, &DenyButton::clicked, this, &AccessControlDialog::denyEntryClicked);
63 return btn;
64 });
65 m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
66 m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
67 m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
68 m_ui->itemsTable->resizeColumnsToContents();
69
70 // the info widget
71 m_ui->exePathWarn->setMessageType(MessageWidget::Warning);
72 m_ui->exePathWarn->hide();
73
74 setupDetails(info);
75
76 // the button box
77 QString detailsButtonText = tr("Details");
78 auto detailsButton =
79 m_ui->buttonBox->addButton(detailsButtonText + QStringLiteral(" >>"), QDialogButtonBox::HelpRole);
80 detailsButton->setCheckable(true);
81
82 QString tooltip = QStringLiteral("<p align='justify'>%1</p>")
83 .arg(tr("Your decision will be remembered for the duration while both the requesting client "
84 "AND KeePassXC are running."));
85
86 m_rememberCheck = new QCheckBox(tr("Remember"), this);
87 m_rememberCheck->setObjectName("rememberCheck"); // for testing
88 m_rememberCheck->setChecked(true);
89 m_ui->buttonBox->addButton(m_rememberCheck, QDialogButtonBox::ActionRole);
90 m_rememberCheck->setToolTip(tooltip);
91
92 auto allowButton = m_ui->buttonBox->addButton(tr("Allow Selected"), QDialogButtonBox::AcceptRole);
93 allowButton->setDefault(true);
94
95 auto cancelButton = m_ui->buttonBox->addButton(tr("Deny All && Future"), QDialogButtonBox::RejectRole);
96 cancelButton->setToolTip(tooltip);
97
98 auto allowAllButton = m_ui->buttonBox->addButton(tr("Allow All && &Future"), QDialogButtonBox::AcceptRole);
99 allowAllButton->setToolTip(tooltip);
100
101 connect(cancelButton, &QPushButton::clicked, this, [this]() { done(DenyAll); });
102 connect(allowButton, &QPushButton::clicked, this, [this]() { done(AllowSelected); });
103 connect(allowAllButton, &QPushButton::clicked, this, [this]() { done(AllowAll); });
104 connect(detailsButton, &QPushButton::clicked, this, [this, detailsButton, detailsButtonText](bool checked) {
105 m_ui->detailsContainer->setVisible(checked);
106 if (checked) {
107 detailsButton->setText(detailsButtonText + QStringLiteral(" <<"));
108 } else {
109 detailsButton->setText(detailsButtonText + QStringLiteral(" >>"));
110 }
111 adjustSize();
112 });
113
114 // tune the UI according to options
115 if (!authOptions.testFlag(AuthOption::Remember)) {
116 m_rememberCheck->hide();
117 m_rememberCheck->setChecked(false);
118 }
119 if (!authOptions.testFlag(AuthOption::PerEntryDeny)) {
120 m_ui->itemsTable->horizontalHeader()->setSectionHidden(2, true);
121 }
122
123 // show warning and details if not valid
124 if (!info.valid) {
125 m_ui->exePathWarn->show();
126 detailsButton->click();
127 }
128
129 // adjust size after we initialize the button box
130 adjustSize();
131
132 allowButton->setFocus();
133 }
134
135 AccessControlDialog::~AccessControlDialog() = default;
136
137 void AccessControlDialog::setupDetails(const FdoSecrets::PeerInfo& info)
138 {
139 QTreeWidgetItem* item = nullptr;
140 for (auto it = info.hierarchy.crbegin(); it != info.hierarchy.crend(); ++it) {
141 QStringList columns = {
142 it->name,
143 QString::number(it->pid),
144 it->exePath,
145 it->command,
146 };
147 if (item) {
148 item = new QTreeWidgetItem(item, columns);
149 } else {
150 item = new QTreeWidgetItem(m_ui->procTree, columns);
151 }
152 }
153 m_ui->procTree->expandAll();
154 m_ui->procTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
155 m_ui->procTree->scrollToBottom();
156 m_ui->detailsContainer->hide();
157 }
158
159 void AccessControlDialog::denyEntryClicked(Entry* entry, const QModelIndex& index)
160 {
161 m_decisions.insert(entry, AuthDecision::Denied);
162 m_model->removeRow(index.row());
163 if (m_model->rowCount({}) == 0) {
164 reject();
165 }
166 }
167
168 void AccessControlDialog::dialogFinished(int result)
169 {
170 auto decision = AuthDecision::Undecided;
171 auto futureDecision = AuthDecision::Undecided;
172 switch (result) {
173 case AllowSelected:
174 decision = m_rememberCheck->isChecked() ? AuthDecision::Allowed : AuthDecision::AllowedOnce;
175 break;
176 case AllowAll:
177 decision = AuthDecision::Allowed;
178 futureDecision = AuthDecision::Allowed;
179 break;
180 case DenyAll:
181 decision = AuthDecision::Denied;
182 futureDecision = AuthDecision::Denied;
183 break;
184 case Rejected:
185 default:
186 break;
187 }
188
189 for (int row = 0; row != m_model->rowCount({}); ++row) {
190 auto entry = m_model->data(m_model->index(row, 2), Qt::EditRole).value<Entry*>();
191 auto selected = m_model->data(m_model->index(row, 0), Qt::CheckStateRole).value<Qt::CheckState>();
192 Q_ASSERT(entry);
193
194 auto undecided = result == AllowSelected && !selected;
195 m_decisions.insert(entry, undecided ? AuthDecision::Undecided : decision);
196 }
197
198 emit finished(m_decisions, futureDecision);
199 }
200
201 QHash<Entry*, AuthDecision> AccessControlDialog::decisions() const
202 {
203 return m_decisions;
204 }
205
206 AccessControlDialog::EntryModel::EntryModel(QList<Entry*> entries, QObject* parent)
207 : QAbstractTableModel(parent)
208 , m_entries(std::move(entries))
209 , m_selected(QSet<Entry*>::fromList(m_entries))
210 {
211 }
212
213 int AccessControlDialog::EntryModel::rowCount(const QModelIndex& parent) const
214 {
215 return isValid(parent) ? 0 : m_entries.count();
216 }
217
218 int AccessControlDialog::EntryModel::columnCount(const QModelIndex& parent) const
219 {
220 return isValid(parent) ? 0 : 3;
221 }
222
223 bool AccessControlDialog::EntryModel::isValid(const QModelIndex& index) const
224 {
225 return index.isValid() && index.row() < rowCount({}) && index.column() < columnCount({});
226 }
227
228 void AccessControlDialog::EntryModel::toggleCheckState(const QModelIndex& index)
229 {
230 if (!isValid(index)) {
231 return;
232 }
233 auto entry = m_entries.at(index.row());
234 // click anywhere in the row to check/uncheck the item
235 auto it = m_selected.find(entry);
236 if (it == m_selected.end()) {
237 m_selected.insert(entry);
238 } else {
239 m_selected.erase(it);
240 }
241 auto rowIdx = index.sibling(index.row(), 0);
242 emit dataChanged(rowIdx, rowIdx, {Qt::CheckStateRole});
243 }
244
245 QVariant AccessControlDialog::EntryModel::data(const QModelIndex& index, int role) const
246 {
247 if (!isValid(index)) {
248 return {};
249 }
250 auto entry = m_entries.at(index.row());
251
252 switch (index.column()) {
253 case 0:
254 switch (role) {
255 case Qt::DisplayRole:
256 return entry->title();
257 case Qt::DecorationRole:
258 return Icons::entryIconPixmap(entry);
259 case Qt::CheckStateRole:
260 return QVariant::fromValue(m_selected.contains(entry) ? Qt::Checked : Qt::Unchecked);
261 default:
262 return {};
263 }
264 case 1:
265 switch (role) {
266 case Qt::DisplayRole:
267 return entry->username();
268 default:
269 return {};
270 }
271 case 2:
272 switch (role) {
273 case Qt::EditRole:
274 return QVariant::fromValue(entry);
275 default:
276 return {};
277 }
278 default:
279 return {};
280 }
281 }
282
283 bool AccessControlDialog::EntryModel::removeRows(int row, int count, const QModelIndex& parent)
284 {
285 beginRemoveRows(parent, row, row + count - 1);
286 while (count--) {
287 m_entries.removeAt(row);
288 }
289 endRemoveRows();
290 return true;
291 }
292
293 AccessControlDialog::DenyButton::DenyButton(QWidget* p, const QModelIndex& idx)
294 : QPushButton(p)
295 , m_index(idx)
296 , m_entry()
297 {
298 setText(tr("Deny for this program"));
299 connect(this, &QPushButton::clicked, [this]() { emit clicked(entry(), m_index); });
300 }
301
302 void AccessControlDialog::DenyButton::setEntry(Entry* e)
303 {
304 m_entry = e;
305 }
306
307 Entry* AccessControlDialog::DenyButton::entry() const
308 {
309 return m_entry;
310 }