Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AccessControlDialog.cpp « widgets « fdosecrets « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c8fd696a7014a799cf7397a7d94aafc938b8c70 (plain)
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
/*
 *  Copyright (C) 2013 Francois Ferrand
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
 *  Copyright (C) 2020 Aetf <aetf@unlimitedcodeworks.xyz>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "AccessControlDialog.h"
#include "ui_AccessControlDialog.h"

#include "fdosecrets/dbus/DBusClient.h"
#include "fdosecrets/widgets/RowButtonHelper.h"

#include "core/Entry.h"
#include "gui/Icons.h"

#include <QWindow>

#include <functional>

AccessControlDialog::AccessControlDialog(QWindow* parent,
                                         const QList<Entry*>& entries,
                                         const QString& app,
                                         const FdoSecrets::PeerInfo& info,
                                         AuthOptions authOptions)
    : m_ui(new Ui::AccessControlDialog())
    , m_rememberCheck()
    , m_model(new EntryModel(entries))
    , m_decisions()
{
    if (parent) {
        // Force the creation of the QWindow, without this windowHandle() will return nullptr
        winId();
        auto window = windowHandle();
        Q_ASSERT(window);
        window->setTransientParent(parent);
    }
    setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);

    connect(this, &QDialog::finished, this, &AccessControlDialog::dialogFinished);

    m_ui->setupUi(this);
    m_ui->appLabel->setText(m_ui->appLabel->text().arg(app));

    // items table
    connect(m_ui->itemsTable, &QTableView::clicked, m_model.data(), &EntryModel::toggleCheckState);
    m_ui->itemsTable->setModel(m_model.data());
    installWidgetItemDelegate<DenyButton>(m_ui->itemsTable, 2, [this](QWidget* p, const QModelIndex& idx) {
        auto btn = new DenyButton(p, idx);
        connect(btn, &DenyButton::clicked, this, &AccessControlDialog::denyEntryClicked);
        return btn;
    });
    m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
    m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
    m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
    m_ui->itemsTable->resizeColumnsToContents();

    // the info widget
    m_ui->rememberMsg->setMessageType(MessageWidget::Information);
    m_ui->rememberMsg->show(); // sync with m_rememberCheck->setChecked(true)
    m_ui->exePathWarn->setMessageType(MessageWidget::Warning);
    m_ui->exePathWarn->hide();

    setupDetails(info);

    // the button box
    QString detailsButtonText = tr("Details");
    auto detailsButton =
        m_ui->buttonBox->addButton(detailsButtonText + QStringLiteral(" >>"), QDialogButtonBox::HelpRole);
    detailsButton->setCheckable(true);

    m_rememberCheck = new QCheckBox(tr("Remember"), this);
    m_rememberCheck->setObjectName("rememberCheck"); // for testing
    m_rememberCheck->setChecked(true);
    m_ui->buttonBox->addButton(m_rememberCheck, QDialogButtonBox::ActionRole);

    auto allowButton = m_ui->buttonBox->addButton(tr("Allow Selected"), QDialogButtonBox::AcceptRole);
    allowButton->setDefault(true);

    auto cancelButton = m_ui->buttonBox->addButton(tr("Deny All"), QDialogButtonBox::RejectRole);

    connect(cancelButton, &QPushButton::clicked, this, [this]() { done(DenyAll); });
    connect(allowButton, &QPushButton::clicked, this, [this]() { done(AllowSelected); });
    connect(m_rememberCheck, &QCheckBox::clicked, this, &AccessControlDialog::rememberChecked);
    connect(detailsButton, &QPushButton::clicked, this, [=](bool checked) {
        m_ui->detailsContainer->setVisible(checked);
        if (checked) {
            detailsButton->setText(detailsButtonText + QStringLiteral(" <<"));
        } else {
            detailsButton->setText(detailsButtonText + QStringLiteral(" >>"));
        }
        adjustSize();
    });

    // tune the UI according to options
    if (!authOptions.testFlag(AuthOption::Remember)) {
        m_rememberCheck->hide();
        m_rememberCheck->setChecked(false);
    }
    if (!authOptions.testFlag(AuthOption::PerEntryDeny)) {
        m_ui->itemsTable->horizontalHeader()->setSectionHidden(2, true);
    }

    // show warning and details if not valid
    if (!info.valid) {
        m_ui->exePathWarn->show();
        detailsButton->click();
    }

    allowButton->setFocus();
}

AccessControlDialog::~AccessControlDialog() = default;

void AccessControlDialog::setupDetails(const FdoSecrets::PeerInfo& info)
{
    QTreeWidgetItem* item = nullptr;
    for (auto it = info.hierarchy.crbegin(); it != info.hierarchy.crend(); ++it) {
        QStringList columns = {
            it->name,
            QString::number(it->pid),
            it->exePath,
            it->command,
        };
        if (item) {
            item = new QTreeWidgetItem(item, columns);
        } else {
            item = new QTreeWidgetItem(m_ui->procTree, columns);
        }
    }
    m_ui->procTree->expandAll();
    m_ui->procTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    m_ui->procTree->scrollToBottom();
    m_ui->detailsContainer->hide();
}

void AccessControlDialog::rememberChecked(bool checked)
{
    if (checked) {
        m_ui->rememberMsg->animatedShow();
    } else {
        m_ui->rememberMsg->animatedHide();
    }
}

void AccessControlDialog::denyEntryClicked(Entry* entry, const QModelIndex& index)
{
    m_decisions.insert(entry, AuthDecision::Denied);
    m_model->removeRow(index.row());
    if (m_model->rowCount({}) == 0) {
        reject();
    }
}

void AccessControlDialog::dialogFinished(int result)
{
    auto allow = m_rememberCheck->isChecked() ? AuthDecision::Allowed : AuthDecision::AllowedOnce;
    auto deny = m_rememberCheck->isChecked() ? AuthDecision::Denied : AuthDecision::DeniedOnce;

    for (int row = 0; row != m_model->rowCount({}); ++row) {
        auto entry = m_model->data(m_model->index(row, 2), Qt::EditRole).value<Entry*>();
        auto selected = m_model->data(m_model->index(row, 0), Qt::CheckStateRole).value<Qt::CheckState>();
        Q_ASSERT(entry);
        switch (result) {
        case AllowSelected:
            if (selected) {
                m_decisions.insert(entry, allow);
            } else {
                m_decisions.insert(entry, AuthDecision::Undecided);
            }
            break;
        case DenyAll:
            m_decisions.insert(entry, deny);
            break;
        case Rejected:
        default:
            m_decisions.insert(entry, AuthDecision::Undecided);
            break;
        }
    }

    emit finished(m_decisions);
}

QHash<Entry*, AuthDecision> AccessControlDialog::decisions() const
{
    return m_decisions;
}

AccessControlDialog::EntryModel::EntryModel(QList<Entry*> entries, QObject* parent)
    : QAbstractTableModel(parent)
    , m_entries(std::move(entries))
    , m_selected(QSet<Entry*>::fromList(m_entries))
{
}

int AccessControlDialog::EntryModel::rowCount(const QModelIndex& parent) const
{
    return isValid(parent) ? 0 : m_entries.count();
}

int AccessControlDialog::EntryModel::columnCount(const QModelIndex& parent) const
{
    return isValid(parent) ? 0 : 3;
}

bool AccessControlDialog::EntryModel::isValid(const QModelIndex& index) const
{
    return index.isValid() && index.row() < rowCount({}) && index.column() < columnCount({});
}

void AccessControlDialog::EntryModel::toggleCheckState(const QModelIndex& index)
{
    if (!isValid(index)) {
        return;
    }
    auto entry = m_entries.at(index.row());
    // click anywhere in the row to check/uncheck the item
    auto it = m_selected.find(entry);
    if (it == m_selected.end()) {
        m_selected.insert(entry);
    } else {
        m_selected.erase(it);
    }
    auto rowIdx = index.sibling(index.row(), 0);
    emit dataChanged(rowIdx, rowIdx, {Qt::CheckStateRole});
}

QVariant AccessControlDialog::EntryModel::data(const QModelIndex& index, int role) const
{
    if (!isValid(index)) {
        return {};
    }
    auto entry = m_entries.at(index.row());

    switch (index.column()) {
    case 0:
        switch (role) {
        case Qt::DisplayRole:
            return entry->title();
        case Qt::DecorationRole:
            return Icons::entryIconPixmap(entry);
        case Qt::CheckStateRole:
            return QVariant::fromValue(m_selected.contains(entry) ? Qt::Checked : Qt::Unchecked);
        default:
            return {};
        }
    case 1:
        switch (role) {
        case Qt::DisplayRole:
            return entry->username();
        default:
            return {};
        }
    case 2:
        switch (role) {
        case Qt::EditRole:
            return QVariant::fromValue(entry);
        default:
            return {};
        }
    default:
        return {};
    }
}

bool AccessControlDialog::EntryModel::removeRows(int row, int count, const QModelIndex& parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    while (count--) {
        m_entries.removeAt(row);
    }
    endRemoveRows();
    return true;
}

AccessControlDialog::DenyButton::DenyButton(QWidget* p, const QModelIndex& idx)
    : QPushButton(p)
    , m_index(idx)
    , m_entry()
{
    setText(tr("Deny for this program"));
    connect(this, &QPushButton::clicked, [this]() { emit clicked(entry(), m_index); });
}

void AccessControlDialog::DenyButton::setEntry(Entry* e)
{
    m_entry = e;
}

Entry* AccessControlDialog::DenyButton::entry() const
{
    return m_entry;
}