[removeuser] Port to C++

No changes in functionality; add a little description in the .conf file.
This commit is contained in:
Adriaan de Groot 2020-03-03 14:22:59 +01:00
parent 0f8751497e
commit 623a8c2d43
6 changed files with 136 additions and 56 deletions

View File

@ -0,0 +1,9 @@
calamares_add_plugin( removeuser
TYPE job
EXPORT_MACRO PLUGINDLLEXPORT_PRO
SOURCES
RemoveUserJob.cpp
LINK_PRIVATE_LIBRARIES
calamares
SHARED_LIB
)

View File

@ -0,0 +1,74 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
* Copyright 2017. Alf Gaida <agaida@siduction.org>
* Copyright 2019-2020, Adriaan de Groot <groot@kde.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RemoveUserJob.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include "utils/Variant.h"
#include <QFile>
RemoveUserJob::RemoveUserJob( QObject* parent )
: Calamares::CppJob( parent )
{
}
RemoveUserJob::~RemoveUserJob() {}
QString
RemoveUserJob::prettyName() const
{
return tr( "Remove live user from target system" );
}
Calamares::JobResult
RemoveUserJob::exec()
{
if ( m_username.isEmpty() )
{
cWarning() << "Ignoring an empty username.";
return Calamares::JobResult::ok();
}
auto* s = CalamaresUtils::System::instance();
auto r = s->targetEnvCommand( { QStringLiteral( "userdel" ),
QStringLiteral( "-f" ), // force
QStringLiteral( "-r" ), // remove home-dir and mail
m_username } );
if ( r.getExitCode() != 0 )
{
cWarning() << "Cannot remove user `" << m_username << "`. userdel terminated with exit code" << r.getExitCode();
}
return Calamares::JobResult::ok();
}
void
RemoveUserJob::setConfigurationMap( const QVariantMap& map )
{
m_username = CalamaresUtils::getString( map, "username" );
}
CALAMARES_PLUGIN_FACTORY_DEFINITION( RemoveUserJobFactory, registerPlugin< RemoveUserJob >(); )

View File

@ -0,0 +1,49 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2020, Adriaan de Groot <groot@kde.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REMOVEUSERJOB_H
#define REMOVEUSERJOB_H
#include "CppJob.h"
#include "DllMacro.h"
#include "utils/PluginFactory.h"
#include <QObject>
#include <QVariantMap>
class PLUGINDLLEXPORT RemoveUserJob : public Calamares::CppJob
{
Q_OBJECT
public:
explicit RemoveUserJob( QObject* parent = nullptr );
virtual ~RemoveUserJob() override;
QString prettyName() const override;
Calamares::JobResult exec() override;
void setConfigurationMap( const QVariantMap& configurationMap ) override;
private:
QString m_username;
};
CALAMARES_PLUGIN_FACTORY_DECLARATION( RemoveUserJobFactory )
#endif // REMOVEUSERJOB_H

View File

@ -1,50 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <https://github.com/calamares> ===
#
# Copyright 2015, Teo Mrnjavac <teo@kde.org>
# Copyright 2017. Alf Gaida <agaida@siduction.org>
# Copyright 2019, Adriaan de Groot <groot@kde.org>
#
# Calamares 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.
#
# Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
import subprocess
import libcalamares
import gettext
_ = gettext.translation("calamares-python",
localedir=libcalamares.utils.gettext_path(),
languages=libcalamares.utils.gettext_languages(),
fallback=True).gettext
def pretty_name():
return _("Remove live user from target system")
def run():
"""
Remove live user from target system
"""
username = libcalamares.job.configuration["username"]
try:
libcalamares.utils.check_target_env_call(["userdel", "-f",
"-r", username])
except subprocess.CalledProcessError as e:
libcalamares.utils.debug("Cannot remove user. "
"'userdel' terminated with exit code "
"{}.".format(e.returncode))
return None

View File

@ -1,6 +0,0 @@
---
type: "job"
name: "removeuser"
interface: "python"
requires: []
script: "main.py"

View File

@ -1,6 +1,10 @@
# Removes a single user (with userdel) from the system. # Removes a single user (with userdel) from the system.
# This is typically used in OEM setups or if the live user # This is typically used in OEM setups or if the live user
# spills into the target system. # spills into the target system.
#
# The module never fails; if userdel fails, this is logged
# but the module still reports success and installation / setup
# continues as normal.
--- ---
# Username in the target system to be removed. # Username in the target system to be removed.
username: live username: live