[users] Refactor user-creation and user-group-setting into methods
- This is prep-work for handling other tools for user- and group- creation as well.
This commit is contained in:
parent
1fddf723fe
commit
26b8c82630
@ -90,6 +90,40 @@ ensureGroupsExistInTarget( const QStringList& wantedGroups, const QStringList& a
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Calamares::JobResult
|
||||||
|
createUser( const QString& loginName, const QString& fullName, const QString& shell )
|
||||||
|
{
|
||||||
|
QStringList useradd { "useradd", "-m", "-U" };
|
||||||
|
if ( !shell.isEmpty() )
|
||||||
|
{
|
||||||
|
useradd << "-s" << shell;
|
||||||
|
}
|
||||||
|
useradd << "-c" << fullName;
|
||||||
|
useradd << loginName;
|
||||||
|
|
||||||
|
auto commandResult = CalamaresUtils::System::instance()->targetEnvCommand( useradd );
|
||||||
|
if ( commandResult.getExitCode() )
|
||||||
|
{
|
||||||
|
cError() << "useradd failed" << commandResult.getExitCode();
|
||||||
|
return commandResult.explainProcess( useradd, std::chrono::seconds( 10 ) /* bogus timeout */ );
|
||||||
|
}
|
||||||
|
return Calamares::JobResult::ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
static Calamares::JobResult
|
||||||
|
setUserGroups( const QString& loginName, const QStringList& groups )
|
||||||
|
{
|
||||||
|
auto commandResult
|
||||||
|
= CalamaresUtils::System::instance()->targetEnvCommand( { "usermod", "-aG", groups.join( ',' ), loginName } );
|
||||||
|
if ( commandResult.getExitCode() )
|
||||||
|
{
|
||||||
|
cError() << "usermod failed" << commandResult.getExitCode();
|
||||||
|
return commandResult.explainProcess( "usermod", std::chrono::seconds( 10 ) /* bogus timeout */ );
|
||||||
|
}
|
||||||
|
return Calamares::JobResult::ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Calamares::JobResult
|
Calamares::JobResult
|
||||||
CreateUserJob::exec()
|
CreateUserJob::exec()
|
||||||
{
|
{
|
||||||
@ -122,19 +156,13 @@ CreateUserJob::exec()
|
|||||||
cDebug() << "[CREATEUSER]: preparing groups";
|
cDebug() << "[CREATEUSER]: preparing groups";
|
||||||
|
|
||||||
QStringList availableGroups = groupsInTargetSystem( destDir );
|
QStringList availableGroups = groupsInTargetSystem( destDir );
|
||||||
|
QStringList groupsForThisUser = m_defaultGroups;
|
||||||
|
if ( m_autologin && gs->contains( "autologinGroup" ) && !gs->value( "autologinGroup" ).toString().isEmpty() )
|
||||||
|
{
|
||||||
|
groupsForThisUser << gs->value( "autologinGroup" ).toString();
|
||||||
|
}
|
||||||
ensureGroupsExistInTarget( m_defaultGroups, availableGroups );
|
ensureGroupsExistInTarget( m_defaultGroups, availableGroups );
|
||||||
|
|
||||||
QString defaultGroups = m_defaultGroups.join( ',' );
|
|
||||||
if ( m_autologin )
|
|
||||||
{
|
|
||||||
if ( gs->contains( "autologinGroup" ) && !gs->value( "autologinGroup" ).toString().isEmpty() )
|
|
||||||
{
|
|
||||||
QString autologinGroup = gs->value( "autologinGroup" ).toString();
|
|
||||||
ensureGroupsExistInTarget( QStringList { autologinGroup }, availableGroups );
|
|
||||||
defaultGroups.append( QString( ",%1" ).arg( autologinGroup ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're looking to reuse the contents of an existing /home.
|
// If we're looking to reuse the contents of an existing /home.
|
||||||
// This GS setting comes from the **partitioning** module.
|
// This GS setting comes from the **partitioning** module.
|
||||||
if ( gs->value( "reuseHome" ).toBool() )
|
if ( gs->value( "reuseHome" ).toBool() )
|
||||||
@ -154,33 +182,21 @@ CreateUserJob::exec()
|
|||||||
|
|
||||||
cDebug() << "[CREATEUSER]: creating user";
|
cDebug() << "[CREATEUSER]: creating user";
|
||||||
|
|
||||||
QStringList useradd { "useradd", "-m", "-U" };
|
auto useraddResult = createUser( m_userName, m_fullName, gs->value( "userShell" ).toString() );
|
||||||
QString shell = gs->value( "userShell" ).toString();
|
if ( !useraddResult )
|
||||||
if ( !shell.isEmpty() )
|
|
||||||
{
|
{
|
||||||
useradd << "-s" << shell;
|
return useraddResult;
|
||||||
}
|
|
||||||
useradd << "-c" << m_fullName;
|
|
||||||
useradd << m_userName;
|
|
||||||
|
|
||||||
auto commandResult = CalamaresUtils::System::instance()->targetEnvCommand( useradd );
|
|
||||||
if ( commandResult.getExitCode() )
|
|
||||||
{
|
|
||||||
cError() << "useradd failed" << commandResult.getExitCode();
|
|
||||||
return commandResult.explainProcess( useradd, std::chrono::seconds( 10 ) /* bogus timeout */ );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commandResult
|
auto usergroupsResult = setUserGroups( m_userName, groupsForThisUser );
|
||||||
= CalamaresUtils::System::instance()->targetEnvCommand( { "usermod", "-aG", defaultGroups, m_userName } );
|
if ( !usergroupsResult )
|
||||||
if ( commandResult.getExitCode() )
|
|
||||||
{
|
{
|
||||||
cError() << "usermod failed" << commandResult.getExitCode();
|
return usergroupsResult;
|
||||||
return commandResult.explainProcess( "usermod", std::chrono::seconds( 10 ) /* bogus timeout */ );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString userGroup = QString( "%1:%2" ).arg( m_userName ).arg( m_userName );
|
QString userGroup = QString( "%1:%2" ).arg( m_userName ).arg( m_userName );
|
||||||
QString homeDir = QString( "/home/%1" ).arg( m_userName );
|
QString homeDir = QString( "/home/%1" ).arg( m_userName );
|
||||||
commandResult = CalamaresUtils::System::instance()->targetEnvCommand( { "chown", "-R", userGroup, homeDir } );
|
auto commandResult = CalamaresUtils::System::instance()->targetEnvCommand( { "chown", "-R", userGroup, homeDir } );
|
||||||
if ( commandResult.getExitCode() )
|
if ( commandResult.getExitCode() )
|
||||||
{
|
{
|
||||||
cError() << "chown failed" << commandResult.getExitCode();
|
cError() << "chown failed" << commandResult.getExitCode();
|
||||||
|
Loading…
Reference in New Issue
Block a user