on xenial help sign in
Switch to using uid/pwdent for getting username
commit854fc6b673a701a9c5b8d53340cffd1a119e40ce authorTed Gould <ted@gould.cx> dateWed 6 Apr 2016, 14:03 (11 years ago) parent8e46b45
6 files changed+49−8
plugins/AccountsService/AccountsService.cpp+11−1 plugins/SessionBroadcast/SessionBroadcast.cpp+8−1 plugins/SessionBroadcast/SessionBroadcast.h+3 plugins/Unity/Launcher/asadapter.cpp+9−2 tests/plugins/IntegratedLightDM/pam.cpp+6−1 tests/plugins/Unity/Launcher/launchermodeltest.cpp+12−3
plugins/AccountsService/AccountsService.cpp+11−1
@@ -22,6 +22,10 @@
2222#include <QStringList>
2323#include <QDebug>
2424
25+#include <unistd.h>
26+#include <sys/types.h>
27+#include <pwd.h>
28+
2529#define IFACE_ACCOUNTS_USER QStringLiteral("org.freedesktop.Accounts.User")
2630#define IFACE_LOCATION_HERE QStringLiteral("com.ubuntu.location.providers.here.AccountsService")
2731#define IFACE_UBUNTU_INPUT QStringLiteral("com.ubuntu.AccountsService.Input")
@@ -73,6 +77,12 @@ QVariant primaryButtonConverter(const QVariant &value)
7377 }
7478}
7579
80+QString systemUsername ()
81+{
82+ auto pw = getpwuid(getuid());
83+ return pw->pw_name;
84+}
85+
7686AccountsService::AccountsService(QObject* parent, const QString &user)
7787 : QObject(parent)
7888 , m_service(new AccountsServiceDBusAdaptor(this))
@@ -126,7 +136,7 @@ AccountsService::AccountsService(QObject* parent, const QString &user)
126136 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_TWO_FINGER_SCROLL,
127137 m_unityInput, QStringLiteral("setTouchpadTwoFingerScroll"));
128138
129− setUser(!user.isEmpty() ? user : QString::fromUtf8(qgetenv("USER")));
139+ setUser(!user.isEmpty() ? user : systemUsername());
130140}
131141
132142QString AccountsService::user() const
plugins/SessionBroadcast/SessionBroadcast.cpp+8−1
@@ -19,9 +19,16 @@
1919#include "SessionBroadcast.h"
2020#include <QDBusConnection>
2121
22+#include <unistd.h>
23+#include <sys/types.h>
24+#include <pwd.h>
25+
2226SessionBroadcast::SessionBroadcast(QObject* parent)
2327 : QObject(parent)
2428{
29+ auto pw = getpwuid(getuid());
30+ m_username = pw->pw_name;
31+
2532 auto connection = QDBusConnection::SM_BUSNAME();
2633
2734 connection.connect(QStringLiteral("com.canonical.Unity.Greeter.Broadcast"),
@@ -35,7 +42,7 @@ SessionBroadcast::SessionBroadcast(QObject* parent)
3542void SessionBroadcast::onShowHome(const QString &username)
3643{
3744 // Only listen to requests meant for us
38− if (username == qgetenv("USER")) {
45+ if (username == m_username) {
3946 Q_EMIT showHome();
4047 }
4148}
plugins/SessionBroadcast/SessionBroadcast.h+3
@@ -36,6 +36,9 @@ Q_SIGNALS:
3636
3737private Q_SLOTS:
3838 void onShowHome(const QString &username);
39+
40+private:
41+ QString m_username;
3942};
4043
4144#endif
plugins/Unity/Launcher/asadapter.cpp+9−2
@@ -18,14 +18,21 @@
1818#include "launcheritem.h"
1919#include "AccountsServiceDBusAdaptor.h"
2020
21+#include <unistd.h>
22+#include <sys/types.h>
23+#include <pwd.h>
24+
2125#include <QDebug>
2226
2327ASAdapter::ASAdapter()
2428{
2529 m_accounts = new AccountsServiceDBusAdaptor();
26− m_user = qgetenv("USER");
30+
31+ auto pw = getpwuid(getuid());
32+ m_user = pw->pw_name;
33+
2734 if (m_user.isEmpty()) {
28− qWarning() << "$USER not valid. Account Service integration will not work.";
35+ qWarning() << "username not valid. Account Service integration will not work.";
2936 }
3037}
3138
tests/plugins/IntegratedLightDM/pam.cpp+6−1
@@ -18,6 +18,10 @@
1818
1919#include <QtTest>
2020
21+#include <unistd.h>
22+#include <sys/types.h>
23+#include <pwd.h>
24+
2125class GreeterPamTest : public QObject
2226{
2327 Q_OBJECT
@@ -37,7 +41,8 @@ private Q_SLOTS:
3741
3842 void testRapidFireAuthentication()
3943 {
40− m_greeterpriv->authenticationUser = qgetenv("USER");
44+ auto pw = getpwuid(getuid());
45+ m_greeterpriv->authenticationUser = pw->pw_name;
4146 for (int i = 0; i < 100; i++) {
4247 m_greeterpriv->handleAuthenticate();
4348 }
tests/plugins/Unity/Launcher/launchermodeltest.cpp+12−3
@@ -34,6 +34,10 @@
3434#include <QDBusMetaType>
3535#include <QDomDocument>
3636
37+#include <unistd.h>
38+#include <sys/types.h>
39+#include <pwd.h>
40+
3741// This is a mock, specifically to test the LauncherModel
3842class MockApp: public unity::shell::application::ApplicationInfoInterface
3943{
@@ -72,6 +76,11 @@ private:
7276 bool m_focused;
7377};
7478
79+QString getUsername () {
80+ auto pw = getpwuid(getuid());
81+ return pw->pw_name;
82+}
83+
7584// This is a mock, specifically to test the LauncherModel
7685class MockAppManager: public unity::shell::application::ApplicationManagerInterface
7786{
@@ -142,7 +151,7 @@ private:
142151
143152 QList<QVariantMap> getASConfig() {
144153 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
145− QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString(qgetenv("USER")),
154+ QDBusReply<QVariant> reply = as->getUserPropertyAsync(getUsername(),
146155 "com.canonical.unity.AccountsService",
147156 "LauncherItems");
148157 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
@@ -167,7 +176,7 @@ private Q_SLOTS:
167176 QStringLiteral("/org/freedesktop/Accounts"),
168177 QStringLiteral("org.freedesktop.Accounts"));
169178 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
170− QString(qgetenv("USER")));
179+ getUsername());
171180 QVERIFY(addReply.isValid());
172181 QCOMPARE(addReply.value(), true);
173182
@@ -193,7 +202,7 @@ private Q_SLOTS:
193202 QStringLiteral("/org/freedesktop/Accounts"),
194203 QStringLiteral("org.freedesktop.Accounts"));
195204 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
196− QString(qgetenv("USER")));
205+ getUsername());
197206 QVERIFY(removeReply.isValid());
198207 QCOMPARE(removeReply.value(), true);
199208 }