Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-systemsettings] Add methods to modify groups to UserModel. Cont…
…ributes to JB#49566

Add new UserModel methods hasGroup, addGroups and removeGroups to modify
user's groups. hasGroup is basically the same as AccessControl.hasGroup
but uses model index instead of uid. addGroups and removeGroups use
user-managerd to change groups.

Signed-off-by: Tomi Leppänen <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed Apr 9, 2020
1 parent 053b423 commit 0d90514
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion rpm/nemo-qml-plugin-systemsettings.spec
Expand Up @@ -11,7 +11,7 @@ Requires: connman
Requires: mce >= 1.83.0
Requires: libsailfishkeyprovider >= 0.0.14
Requires: connman-qt5 >= 1.2.21
Requires: user-managerd >= 0.3.0
Requires: user-managerd >= 0.4.0
Requires(post): coreutils
BuildRequires: pkgconfig(Qt5Qml)
BuildRequires: pkgconfig(Qt5SystemInfo)
Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Expand Up @@ -7,7 +7,7 @@ QT -= gui

CONFIG += c++11 hide_symbols link_pkgconfig
PKGCONFIG += profile mlite5 mce timed-qt5 libshadowutils blkid libcrypto nemomodels-qt5 libsailfishkeyprovider connman-qt5 glib-2.0
PKGCONFIG += ssu-sysinfo nemodbus packagekitqt5 libsystemd sailfishusermanager
PKGCONFIG += ssu-sysinfo nemodbus packagekitqt5 libsystemd sailfishusermanager sailfishaccesscontrol

system(qdbusxml2cpp -p mceiface.h:mceiface.cpp mce.xml)

Expand Down
76 changes: 75 additions & 1 deletion src/usermodel.cpp
Expand Up @@ -41,9 +41,10 @@
#include <QDBusServiceWatcher>
#include <QString>
#include <functional>
#include <grp.h>
#include <sailfishaccesscontrol.h>
#include <sailfishusermanagerinterface.h>
#include <sys/types.h>
#include <grp.h>

namespace {
const auto UserManagerService = QStringLiteral(SAILFISH_USERMANAGER_DBUS_INTERFACE);
Expand All @@ -59,6 +60,9 @@ const QHash<const QString, int> errorTypeMap = {
{ QStringLiteral(SailfishUserManagerErrorUserModifyFailed), UserModel::UserModifyFailed },
{ QStringLiteral(SailfishUserManagerErrorUserRemoveFailed), UserModel::UserRemoveFailed },
{ QStringLiteral(SailfishUserManagerErrorGetUidFailed), UserModel::GetUidFailed },
{ QStringLiteral(SailfishUserManagerErrorUserNotFound), UserModel::UserNotFound },
{ QStringLiteral(SailfishUserManagerErrorAddToGroupFailed), UserModel::AddToGroupFailed },
{ QStringLiteral(SailfishUserManagerErrorRemoveFromGroupFailed), UserModel::RemoveFromGroupFailed },
};

int getErrorType(QDBusError &error)
Expand Down Expand Up @@ -282,6 +286,50 @@ UserInfo * UserModel::getCurrentUser() const
return new UserInfo();
}

bool UserModel::hasGroup(int row, const QString &group) const
{
if (row < 0 || row >= m_users.count())
return false;

auto user = m_users.at(row);
if (!user.isValid())
return false;

return sailfish_access_control_hasgroup(user.uid(), group.toUtf8().constData());
}

void UserModel::addGroups(int row, const QStringList &groups)
{
if (row < 0 || row >= m_users.count())
return;

auto user = m_users.at(row);
if (!user.isValid())
return;

createInterface();
auto call = m_dBusInterface->asyncCall(QStringLiteral("addToGroups"), (uint)user.uid(), groups);
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::addToGroupsFinished, this, std::placeholders::_1, row));
}

void UserModel::removeGroups(int row, const QStringList &groups)
{
if (row < 0 || row >= m_users.count())
return;

auto user = m_users.at(row);
if (!user.isValid())
return;

createInterface();
auto call = m_dBusInterface->asyncCall(QStringLiteral("removeFromGroups"), (uint)user.uid(), groups);
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::removeFromGroupsFinished, this, std::placeholders::_1, row));
}

void UserModel::onUserAdded(const SailfishUserManagerEntry &entry)
{
if (m_uidsToRows.contains(entry.uid))
Expand Down Expand Up @@ -411,6 +459,32 @@ void UserModel::setCurrentUserFinished(QDBusPendingCallWatcher *call, int row)
call->deleteLater();
}

void UserModel::addToGroupsFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
auto error = reply.error();
emit addGroupsFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Adding user to groups failed:" << error;
} else {
emit userGroupsChanged(row);
}
call->deleteLater();
}

void UserModel::removeFromGroupsFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
auto error = reply.error();
emit removeGroupsFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Adding user to groups failed:" << error;
} else {
emit userGroupsChanged(row);
}
call->deleteLater();
}

void UserModel::createInterface()
{
if (!m_dBusInterface) {
Expand Down
14 changes: 14 additions & 0 deletions src/usermodel.h
Expand Up @@ -79,6 +79,9 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
UserModifyFailed,
UserRemoveFailed,
GetUidFailed,
UserNotFound,
AddToGroupFailed,
RemoveFromGroupFailed,
};
Q_ENUM(ErrorType)

Expand All @@ -94,18 +97,27 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;

// Methods to modify users
Q_INVOKABLE void createUser();
Q_INVOKABLE void removeUser(int row);
Q_INVOKABLE void reset(int row);
Q_INVOKABLE void setCurrentUser(int row);
Q_INVOKABLE UserInfo * getCurrentUser() const;

// Methods to modify user's groups
Q_INVOKABLE bool hasGroup(int row, const QString &group) const;
Q_INVOKABLE void addGroups(int row, const QStringList &groups);
Q_INVOKABLE void removeGroups(int row, const QStringList &groups);

signals:
void placeholderChanged();
void userGroupsChanged(int row);
void userAddFailed(int error);
void userModifyFailed(int row, int error);
void userRemoveFailed(int row, int error);
void setCurrentUserFailed(int row, int error);
void addGroupsFailed(int row, int error);
void removeGroupsFailed(int row, int error);

private slots:
void onUserAdded(const SailfishUserManagerEntry &entry);
Expand All @@ -118,6 +130,8 @@ private slots:
void userModifyFinished(QDBusPendingCallWatcher *call, int row);
void userRemoveFinished(QDBusPendingCallWatcher *call, int row);
void setCurrentUserFinished(QDBusPendingCallWatcher *call, int row);
void addToGroupsFinished(QDBusPendingCallWatcher *call, int row);
void removeFromGroupsFinished(QDBusPendingCallWatcher *call, int row);

void createInterface();
void destroyInterface();
Expand Down

0 comments on commit 0d90514

Please sign in to comment.