[users] Factor out Sudo creation into separate job

This commit is contained in:
Adriaan de Groot 2020-10-21 14:43:45 +02:00
parent 328a5bbbfb
commit 29e6934672
5 changed files with 100 additions and 26 deletions

View File

@ -25,6 +25,7 @@ include_directories( ${PROJECT_BINARY_DIR}/src/libcalamaresui )
set( JOB_SRC
CreateUserJob.cpp
MiscJobs.cpp
SetPasswordJob.cpp
SetHostNameJob.cpp
)

View File

@ -10,6 +10,7 @@
#include "Config.h"
#include "CreateUserJob.h"
#include "MiscJobs.h"
#include "SetHostNameJob.h"
#include "SetPasswordJob.h"
@ -103,7 +104,7 @@ Config::setUserShell( const QString& shell )
if ( shell != m_userShell )
{
m_userShell = shell;
emit userShellChanged(shell);
emit userShellChanged( shell );
// The shell is put into GS as well.
auto* gs = Calamares::JobQueue::instance()->globalStorage();
if ( gs )
@ -796,6 +797,12 @@ Config::createJobs() const
Calamares::Job* j;
if ( m_sudoersGroup.isEmpty() )
{
j = new SetupSudoJob( m_sudoersGroup );
jobs.append( Calamares::job_ptr( j ) );
}
j = new CreateUserJob( this );
jobs.append( Calamares::job_ptr( j ) );

View File

@ -166,31 +166,6 @@ CreateUserJob::exec()
reuseHome = gs->value( "reuseHome" ).toBool();
}
if ( !m_config->sudoersGroup().isEmpty() )
{
m_status = tr( "Preparing sudo for user %1" ).arg( m_config->loginName() );
emit progress( 0.05 );
cDebug() << "[CREATEUSER]: preparing sudoers";
QString sudoersLine = QString( "%%1 ALL=(ALL) ALL\n" ).arg( m_config->sudoersGroup() );
auto fileResult
= CalamaresUtils::System::instance()->createTargetFile( QStringLiteral( "/etc/sudoers.d/10-installer" ),
sudoersLine.toUtf8().constData(),
CalamaresUtils::System::WriteMode::Overwrite );
if ( fileResult )
{
if ( !CalamaresUtils::Permissions::apply( fileResult.path(), 0440 ) )
{
return Calamares::JobResult::error( tr( "Cannot chmod sudoers file." ) );
}
}
else
{
return Calamares::JobResult::error( tr( "Cannot create sudoers file for writing." ) );
}
}
cDebug() << "[CREATEUSER]: preparing groups";
m_status = tr( "Preparing groups for user %1" ).arg( m_config->loginName() );

View File

@ -0,0 +1,57 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
#include "MiscJobs.h"
#include "Config.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include "utils/Permissions.h"
SetupSudoJob::SetupSudoJob( const QString& group )
: m_sudoGroup( group )
{
}
QString
SetupSudoJob::prettyName() const
{
return tr( "Configure <pre>sudo</pre> users." );
}
Calamares::JobResult
SetupSudoJob::exec()
{
if ( m_sudoGroup.isEmpty() )
{
return Calamares::JobResult::ok();
}
QString sudoersLine = QString( "%%1 ALL=(ALL) ALL\n" ).arg( m_sudoGroup );
auto fileResult
= CalamaresUtils::System::instance()->createTargetFile( QStringLiteral( "/etc/sudoers.d/10-installer" ),
sudoersLine.toUtf8().constData(),
CalamaresUtils::System::WriteMode::Overwrite );
if ( fileResult )
{
if ( !CalamaresUtils::Permissions::apply( fileResult.path(), 0440 ) )
{
return Calamares::JobResult::error( tr( "Cannot chmod sudoers file." ) );
}
}
else
{
return Calamares::JobResult::error( tr( "Cannot create sudoers file for writing." ) );
}
return Calamares::JobResult::ok();
}

View File

@ -0,0 +1,34 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
/**@file Various small jobs
*
* This file collects miscellaneous jobs that need to be run to prepare
* the system for the user-creation job.
*/
#ifndef USERS_MISCJOBS_H
#define USERS_MISCJOBS_H
#include "Job.h"
class SetupSudoJob : public Calamares::Job
{
Q_OBJECT
public:
SetupSudoJob( const QString& group );
QString prettyName() const override;
Calamares::JobResult exec() override;
public:
QString m_sudoGroup;
};
#endif