on xenial help sign in
Switch to using uid/pwdent for getting username
854fc6bTed Gould
plugins/AccountsService/AccountsService.cpp+11−1
@@ -22,6 +22,10 @@
22#include <QStringList>
22#include <QStringList>
23#include <QDebug>
23#include <QDebug>
24
24
25+#include <unistd.h>
26+#include <sys/types.h>
27+#include <pwd.h>
28+
25#define IFACE_ACCOUNTS_USER QStringLiteral("org.freedesktop.Accounts.User")
29#define IFACE_ACCOUNTS_USER QStringLiteral("org.freedesktop.Accounts.User")
26#define IFACE_LOCATION_HERE QStringLiteral("com.ubuntu.location.providers.here.AccountsService")
30#define IFACE_LOCATION_HERE QStringLiteral("com.ubuntu.location.providers.here.AccountsService")
27#define IFACE_UBUNTU_INPUT QStringLiteral("com.ubuntu.AccountsService.Input")
31#define IFACE_UBUNTU_INPUT QStringLiteral("com.ubuntu.AccountsService.Input")
@@ -73,6 +77,12 @@ QVariant primaryButtonConverter(const QVariant &value)
73 }
77 }
74}
78}
75
79
80+QString systemUsername ()
81+{
82+ auto pw = getpwuid(getuid());
83+ return pw->pw_name;
84+}
85+
76AccountsService::AccountsService(QObject* parent, const QString &user)
86AccountsService::AccountsService(QObject* parent, const QString &user)
77 : QObject(parent)
87 : QObject(parent)
78 , m_service(new AccountsServiceDBusAdaptor(this))
88 , m_service(new AccountsServiceDBusAdaptor(this))
@@ -126,7 +136,7 @@ AccountsService::AccountsService(QObject* parent, const QString &user)
126 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_TWO_FINGER_SCROLL,
136 registerProxy(IFACE_UBUNTU_INPUT, PROP_TOUCHPAD_TWO_FINGER_SCROLL,
127 m_unityInput, QStringLiteral("setTouchpadTwoFingerScroll"));
137 m_unityInput, QStringLiteral("setTouchpadTwoFingerScroll"));
128
138
129− setUser(!user.isEmpty() ? user : QString::fromUtf8(qgetenv("USER")));
139+ setUser(!user.isEmpty() ? user : systemUsername());
130}
140}
131
141
132QString AccountsService::user() const
142QString AccountsService::user() const
plugins/SessionBroadcast/SessionBroadcast.cpp+8−1
@@ -19,9 +19,16 @@
19#include "SessionBroadcast.h"
19#include "SessionBroadcast.h"
20#include <QDBusConnection>
20#include <QDBusConnection>
21
21
22+#include <unistd.h>
23+#include <sys/types.h>
24+#include <pwd.h>
25+
22SessionBroadcast::SessionBroadcast(QObject* parent)
26SessionBroadcast::SessionBroadcast(QObject* parent)
23 : QObject(parent)
27 : QObject(parent)
24{
28{
29+ auto pw = getpwuid(getuid());
30+ m_username = pw->pw_name;
31+
25 auto connection = QDBusConnection::SM_BUSNAME();
32 auto connection = QDBusConnection::SM_BUSNAME();
26
33
27 connection.connect(QStringLiteral("com.canonical.Unity.Greeter.Broadcast"),
34 connection.connect(QStringLiteral("com.canonical.Unity.Greeter.Broadcast"),
@@ -35,7 +42,7 @@ SessionBroadcast::SessionBroadcast(QObject* parent)
35void SessionBroadcast::onShowHome(const QString &username)
42void SessionBroadcast::onShowHome(const QString &username)
36{
43{
37 // Only listen to requests meant for us
44 // Only listen to requests meant for us
38− if (username == qgetenv("USER")) {
45+ if (username == m_username) {
39 Q_EMIT showHome();
46 Q_EMIT showHome();
40 }
47 }
41}
48}
plugins/SessionBroadcast/SessionBroadcast.h+3
@@ -36,6 +36,9 @@ Q_SIGNALS:
36
36
37private Q_SLOTS:
37private Q_SLOTS:
38 void onShowHome(const QString &username);
38 void onShowHome(const QString &username);
39+
40+private:
41+ QString m_username;
39};
42};
40
43
41#endif
44#endif
plugins/Unity/Launcher/asadapter.cpp+9−2
@@ -18,14 +18,21 @@
18#include "launcheritem.h"
18#include "launcheritem.h"
19#include "AccountsServiceDBusAdaptor.h"
19#include "AccountsServiceDBusAdaptor.h"
20
20
21+#include <unistd.h>
22+#include <sys/types.h>
23+#include <pwd.h>
24+
21#include <QDebug>
25#include <QDebug>
22
26
23ASAdapter::ASAdapter()
27ASAdapter::ASAdapter()
24{
28{
25 m_accounts = new AccountsServiceDBusAdaptor();
29 m_accounts = new AccountsServiceDBusAdaptor();
26− m_user = qgetenv("USER");
30+
31+ auto pw = getpwuid(getuid());
32+ m_user = pw->pw_name;
33+
27 if (m_user.isEmpty()) {
34 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.";
29 }
36 }
30}
37}
31
38
tests/plugins/IntegratedLightDM/pam.cpp+6−1
@@ -18,6 +18,10 @@
18
18
19#include <QtTest>
19#include <QtTest>
20
20
21+#include <unistd.h>
22+#include <sys/types.h>
23+#include <pwd.h>
24+
21class GreeterPamTest : public QObject
25class GreeterPamTest : public QObject
22{
26{
23 Q_OBJECT
27 Q_OBJECT
@@ -37,7 +41,8 @@ private Q_SLOTS:
37
41
38 void testRapidFireAuthentication()
42 void testRapidFireAuthentication()
39 {
43 {
44+ auto pw = getpwuid(getuid());
40− m_greeterpriv->authenticationUser = qgetenv("USER");
45+ m_greeterpriv->authenticationUser = pw->pw_name;
41 for (int i = 0; i < 100; i++) {
46 for (int i = 0; i < 100; i++) {
42 m_greeterpriv->handleAuthenticate();
47 m_greeterpriv->handleAuthenticate();
43 }
48 }
tests/plugins/Unity/Launcher/launchermodeltest.cpp+12−3
@@ -34,6 +34,10 @@
34#include <QDBusMetaType>
34#include <QDBusMetaType>
35#include <QDomDocument>
35#include <QDomDocument>
36
36
37+#include <unistd.h>
38+#include <sys/types.h>
39+#include <pwd.h>
40+
37// This is a mock, specifically to test the LauncherModel
41// This is a mock, specifically to test the LauncherModel
38class MockApp: public unity::shell::application::ApplicationInfoInterface
42class MockApp: public unity::shell::application::ApplicationInfoInterface
39{
43{
@@ -72,6 +76,11 @@ private:
72 bool m_focused;
76 bool m_focused;
73};
77};
74
78
79+QString getUsername () {
80+ auto pw = getpwuid(getuid());
81+ return pw->pw_name;
82+}
83+
75// This is a mock, specifically to test the LauncherModel
84// This is a mock, specifically to test the LauncherModel
76class MockAppManager: public unity::shell::application::ApplicationManagerInterface
85class MockAppManager: public unity::shell::application::ApplicationManagerInterface
77{
86{
@@ -142,7 +151,7 @@ private:
142
151
143 QList<QVariantMap> getASConfig() {
152 QList<QVariantMap> getASConfig() {
144 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
153 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
145− QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString(qgetenv("USER")),
154+ QDBusReply<QVariant> reply = as->getUserPropertyAsync(getUsername(),
146 "com.canonical.unity.AccountsService",
155 "com.canonical.unity.AccountsService",
147 "LauncherItems");
156 "LauncherItems");
148 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
157 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
@@ -167,7 +176,7 @@ private Q_SLOTS:
167 QStringLiteral("/org/freedesktop/Accounts"),
176 QStringLiteral("/org/freedesktop/Accounts"),
168 QStringLiteral("org.freedesktop.Accounts"));
177 QStringLiteral("org.freedesktop.Accounts"));
169 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
178 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
170− QString(qgetenv("USER")));
179+ getUsername());
171 QVERIFY(addReply.isValid());
180 QVERIFY(addReply.isValid());
172 QCOMPARE(addReply.value(), true);
181 QCOMPARE(addReply.value(), true);
173
182
@@ -193,7 +202,7 @@ private Q_SLOTS:
193 QStringLiteral("/org/freedesktop/Accounts"),
202 QStringLiteral("/org/freedesktop/Accounts"),
194 QStringLiteral("org.freedesktop.Accounts"));
203 QStringLiteral("org.freedesktop.Accounts"));
195 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
204 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
196− QString(qgetenv("USER")));
205+ getUsername());
197 QVERIFY(removeReply.isValid());
206 QVERIFY(removeReply.isValid());
198 QCOMPARE(removeReply.value(), true);
207 QCOMPARE(removeReply.value(), true);
199 }
208 }