summaryrefslogtreecommitdiffstats
path: root/src/AuthDialog.cpp
blob: 03872af98324c08c3b4aab5a1c6afd71fc075222 (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
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
/*  This file is part of the TDE project
    Copyright (C) 2007-2008 Gökçen Eraslan <[email protected]>
    Copyright (C) 2008 Dirk Mueller <[email protected]>
    Copyright (C) 2008 Daniel Nicoletti <[email protected]>
    Copyright (C) 2008-2010 Dario Freddi <[email protected]>

    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 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "AuthDialog.h"
#include "AuthDialogWidget.h"

#include <tqlabel.h>
#include <tqprocess.h>
#include <tqpainter.h>

#include <kcombobox.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <klineedit.h>
#include "kuniqueapplication.h"
#include <kurllabel.h>
#include <kuser.h>
#include <tdelocale.h>

#include <PolkitTQt/Authority>
#include <PolkitTQt/Details>

using namespace PolkitTQt;

AuthDialog::AuthDialog(const TQString &actionId, const TQString &message,
        const TQString &iconName, const PolkitTQt::Details &details,
        const Identity::List &identities) :
        KDialogBase(0, 0, true, TQString::null, Ok|Cancel|Details, Ok),
        m_authWidget(new AuthDialogWidget(this))
{
  setMainWidget(m_authWidget);

  if (message.isEmpty())
  {
    kdWarning() << "Could not get action message for action." << endl;
    m_authWidget->lblHeader->hide();
  }
  else
  {
    kdDebug() << "Message of action: " << message << endl;
    m_authWidget->lblHeader->setText("<h3>" + message + "</h3>");
    setCaption(message);
    m_message = message;
  }

  // loads the standard key icon
  TQPixmap icon = TDEGlobal::iconLoader()->loadIcon("password", TDEIcon::NoGroup,
          TDEIcon::SizeHuge, TDEIcon::DefaultState);
  // create a painter to paint the action icon over the key icon
  TQPainter painter(&icon);
  const int iconSize = icon.size().width();
  int overlaySize = 32;
  const TQPixmap pixmap = TDEGlobal::iconLoader()->loadIcon(iconName, TDEIcon::NoGroup,
          overlaySize, TDEIcon::DefaultState, 0, true);
  if (!pixmap.isNull())
  {
    // bottom right corner
    TQPoint startPoint = TQPoint(iconSize - overlaySize - 2, iconSize - overlaySize - 2);
    painter.drawPixmap(startPoint, pixmap);
  }

  setIcon(icon);
  m_authWidget->lblPixmap->setPixmap(icon);

  // find action description for actionId
  for (const ActionDescription &desc : Authority::instance()->enumerateActionsSync())
  {
    if (actionId == desc.actionId())
    {
      m_actionDescription = desc;
      kdDebug() << "Action description has been found" << endl;
      break;
    }
  }

  AuthDetails *detailsDialog = new AuthDetails(details, m_actionDescription, m_appname, this);
  setDetailsWidget(detailsDialog);

  m_authWidget->userCB->hide();
  m_authWidget->lePassword->setFocus();
  m_authWidget->errorMessageKTW->hide();

  // If there is more than 1 identity we will show the combobox for user selection
  if (identities.size() > 1)
  {
    connect(m_authWidget->userCB, SIGNAL(activated(int)),
            this, SLOT(on_userCB_currentIndexChanged(int)));
    createUserCB(identities);
  }
  else
  {
    m_authWidget->userCB->insertItem("");
    m_userData.append(identities[0].toString());
    m_authWidget->userCB->setCurrentItem(0);
  }
	on_userCB_currentIndexChanged(-1);
}

AuthDialog::~AuthDialog()
{
}

void AuthDialog::accept()
{
  // Do nothing, do not close the dialog. This is needed so that the dialog stays
  m_authWidget->lePassword->setEnabled(false);
  return;
}

void AuthDialog::setRequest(const TQString &request, bool requiresAdmin)
{
  kdDebug() << request << endl;
  Identity identity = adminUserSelected();
  if (request.startsWith("password:", false))
  {
		if (!identity.isValid())
		{
			m_authWidget->lblPassword->setText(i18n("Password for root:"));
		}
		else
		{
			if (!m_authWidget->userCB->isVisible())
			{
				TQString username = identity.toString().remove("unix-user:");
				m_authWidget->lblPassword->setText(i18n("Password for %1:").arg(username));
			}
			else
			{
				m_authWidget->lblPassword->setText(i18n("Password:"));
			}
		}
  }
  else if (request.startsWith("password or swipe finger:"), false)
  {
		if (!identity.isValid())
		{
			m_authWidget->lblPassword->setText(i18n("Password or swipe finger for root:"));
		}
		else
		{
			if (!m_authWidget->userCB->isVisible())
			{
				TQString username = identity.toString().remove("unix-user:");
				m_authWidget->lblPassword->setText(i18n("Password or swipe finger for %1:").arg(username));
			}
			else
			{
				m_authWidget->lblPassword->setText(i18n("Password or swipe finger:"));
			}
		}
  }
  else
  {
    m_authWidget->lblPassword->setText(request);
  }
}

void AuthDialog::setOptions()
{
  m_authWidget->lblContent->setText(i18n("An application is attempting to perform an action "
        "that requires privileges. Authentication is required to perform this action."));
}

void AuthDialog::createUserCB(const Identity::List &identities)
{
  /* if we've already built the list of admin users once, then avoid
   * doing it again.. (this is mainly used when the user entered the
   * wrong password and the dialog is recycled)
   */
  if (identities.count() && (m_authWidget->userCB->count() - 1) != identities.count())
  {
    // Clears the combobox in the case some user be added
    m_authWidget->userCB->clear();
    m_userData.clear();

    // Adds a Dummy user
    m_authWidget->userCB->insertItem(i18n("Select User"));
    m_userData.append(TQString::null);

    // For each user
    int index = 1; // Start at 1 because of the "Select User" entry
    int currentUserIndex = -1;
    const KUser currentUser;
    for (const Identity &identity : identities)
    {
      // First check to see if the user is valid
      kdDebug() << "User: " << identity.toString() << endl;
      const KUser user(identity.toString().remove("unix-user:"));
      if (!user.isValid())
      {
        kdWarning() << "User invalid: " << user.loginName() << endl;
        continue;
      }

      // Display user full Name if available
      TQString display;
      if (!user.fullName().isEmpty())
      {
        display = user.fullName() + " (" + user.loginName() + ")";
      }
      else
      {
        display = user.loginName();
      }
      m_authWidget->userCB->insertItem(display);
      m_userData.append(identity.toString());

      if (user == currentUser)
      {
        currentUserIndex = index;
      }
      ++index;
    }

    // Show the widget and set focus
    if (currentUserIndex != -1)
    {
      m_authWidget->userCB->setCurrentItem(currentUserIndex);
    }
    m_authWidget->userCB->show();
  }
}

Identity AuthDialog::adminUserSelected() const
{
  if (m_authWidget->userCB->currentItem() == -1)
  {
    return Identity();
  }

  TQString id = m_userData[m_authWidget->userCB->currentItem()];
  if (id.isEmpty())
  {
    return Identity();
  }
  return Identity::fromString(id);
}

void AuthDialog::on_userCB_currentIndexChanged(int /*index*/)
{
  Identity identity = adminUserSelected();
  // identity is now valid when "Select user" is selected
  if (!identity.isValid())
  {
    m_authWidget->lePassword->setEnabled(false);
		m_authWidget->lblPassword->setText(i18n("Password:"));
    m_authWidget->lblPassword->setEnabled(false);
    enableButtonOK(false);
  }
  else
  {
    m_authWidget->lePassword->setEnabled(true);
    m_authWidget->lblPassword->setEnabled(true);
    enableButtonOK(true);
    // We need this to restart the auth with the new user
    emit adminUserSelected(identity);
    // give password label focus
    m_authWidget->lePassword->setFocus();
  }
}

TQString AuthDialog::password() const
{
  return m_authWidget->lePassword->text();
}

void AuthDialog::authenticationFailure()
{
  TQFont bold = font();
  bold.setBold(true);
  m_authWidget->errorMessageKTW->setText(i18n("Authentication failure, please try again."));
  m_authWidget->errorMessageKTW->setFont(bold);
  m_authWidget->errorMessageKTW->show();
  m_authWidget->lePassword->setEnabled(true);
  m_authWidget->lePassword->clear();
  m_authWidget->lePassword->setFocus();
}

AuthDetails::AuthDetails(const Details &details, const ActionDescription &actionDescription,
        const TQString &appname, TQWidget *parent) : AuthDetailsWidget(parent)
{
  app_label->setText(appname);

  for (const TQString &key : details.keys())
  {
    int row = AuthDetailsWidgetLayout->numRows() + 1;

    TQLabel *keyLabel = new TQLabel(this);
    keyLabel->setText(key);
    AuthDetailsWidgetLayout->addWidget(keyLabel, row, 0);

    TQLabel *valueLabel = new TQLabel(this);
    valueLabel->setText(details.lookup(key));
    AuthDetailsWidgetLayout->addWidget(valueLabel, row, 1);
  }

  action_label->setText(actionDescription.description());

  TQString vendor    = actionDescription.vendorName();
  TQString vendorUrl = actionDescription.vendorUrl();

  if (!vendor.isEmpty())
  {
    vendorUL->setText(vendor);
    vendorUL->setTipText(vendorUrl);
    vendorUL->setURL(vendorUrl);
  }
  else if (!vendorUrl.isEmpty())
  {
    vendorUL->setText(vendorUrl);
    vendorUL->setTipText(vendorUrl);
    vendorUL->setURL(vendorUrl);
  }
  else
  {
    vendorL->hide();
    vendorUL->hide();
  }

  connect(vendorUL, TQT_SIGNAL(leftClickedURL(const TQString&)),
          TQT_SLOT(openUrl(const TQString&)));
}

void AuthDetails::openUrl(const TQString &url)
{
  kapp->invokeBrowser(url);
}

#include "AuthDialog.moc"