summaryrefslogtreecommitdiff
path: root/raveos-calamares-module/raveusers/RaveUsersBackend.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'raveos-calamares-module/raveusers/RaveUsersBackend.cpp')
-rw-r--r--raveos-calamares-module/raveusers/RaveUsersBackend.cpp242
1 files changed, 242 insertions, 0 deletions
diff --git a/raveos-calamares-module/raveusers/RaveUsersBackend.cpp b/raveos-calamares-module/raveusers/RaveUsersBackend.cpp
new file mode 100644
index 0000000..6f8eea6
--- /dev/null
+++ b/raveos-calamares-module/raveusers/RaveUsersBackend.cpp
@@ -0,0 +1,242 @@
+#include "RaveUsersBackend.h"
+
+#include <QRegularExpression>
+
+namespace
+{
+const QRegularExpression USERNAME_RX( "^[a-z_][a-z0-9_-]*[$]?$" );
+constexpr int USERNAME_MAX_LENGTH = 31;
+
+const QRegularExpression HOSTNAME_RX( "^[a-zA-Z0-9][-a-zA-Z0-9_]*$" );
+constexpr int HOSTNAME_MIN_LENGTH = 2;
+constexpr int HOSTNAME_MAX_LENGTH = 63;
+}
+
+RaveUsersBackend::RaveUsersBackend( QObject* parent )
+ : QObject( parent )
+ , m_forbiddenLoginNames { QStringLiteral( "root" ), QStringLiteral( "nobody" ) }
+ , m_forbiddenHostNames { QStringLiteral( "localhost" ) }
+{
+ m_defaultGroups = { "lp", "video", "network", "storage", "wheel", "audio" };
+}
+
+QString
+RaveUsersBackend::usernameStatus() const
+{
+ if ( m_username.isEmpty() )
+ {
+ return QString();
+ }
+ if ( m_username.length() > USERNAME_MAX_LENGTH )
+ {
+ return tr( "Your username is too long." );
+ }
+ if ( m_username.indexOf( QRegularExpression( "^[a-z_]" ) ) != 0 )
+ {
+ return tr( "Your username must start with a lowercase letter or underscore." );
+ }
+ if ( m_username.indexOf( USERNAME_RX ) != 0 )
+ {
+ return tr( "Only lowercase letters, numbers, underscore and hyphen are allowed." );
+ }
+ if ( m_forbiddenLoginNames.contains( m_username, Qt::CaseInsensitive ) )
+ {
+ return tr( "'%1' is not allowed as username." ).arg( m_username );
+ }
+ return QString();
+}
+
+QString
+RaveUsersBackend::hostnameStatus() const
+{
+ if ( m_hostname.isEmpty() )
+ {
+ return QString();
+ }
+ if ( m_hostname.length() < HOSTNAME_MIN_LENGTH )
+ {
+ return tr( "Your hostname is too short." );
+ }
+ if ( m_hostname.length() > HOSTNAME_MAX_LENGTH )
+ {
+ return tr( "Your hostname is too long." );
+ }
+ if ( m_forbiddenHostNames.contains( m_hostname, Qt::CaseInsensitive ) )
+ {
+ return tr( "'%1' is not allowed as hostname." ).arg( m_hostname );
+ }
+ if ( m_hostname.indexOf( HOSTNAME_RX ) != 0 )
+ {
+ return tr( "Only letters, numbers, underscore and hyphen are allowed." );
+ }
+ return QString();
+}
+
+QString
+RaveUsersBackend::passwordStatus() const
+{
+ if ( m_password.isEmpty() )
+ {
+ return QString();
+ }
+ if ( m_password != m_passwordSecondary )
+ {
+ return tr( "Your passwords do not match!" );
+ }
+ return QString();
+}
+
+QString
+RaveUsersBackend::rootPasswordStatus() const
+{
+ if ( m_reuseRootPassword )
+ {
+ return QString();
+ }
+ if ( m_rootPassword.isEmpty() )
+ {
+ return QString();
+ }
+ if ( m_rootPassword != m_rootPasswordSecondary )
+ {
+ return tr( "Your root passwords do not match!" );
+ }
+ return QString();
+}
+
+bool
+RaveUsersBackend::isReady() const
+{
+ if ( m_username.isEmpty() || !usernameStatus().isEmpty() )
+ {
+ return false;
+ }
+ if ( m_hostname.isEmpty() || !hostnameStatus().isEmpty() )
+ {
+ return false;
+ }
+ if ( m_password.isEmpty() || !passwordStatus().isEmpty() )
+ {
+ return false;
+ }
+ if ( m_setRootPassword && !m_reuseRootPassword )
+ {
+ if ( m_rootPassword.isEmpty() || !rootPasswordStatus().isEmpty() )
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+QStringList
+RaveUsersBackend::groupsForThisUser() const
+{
+ QStringList groups = m_defaultGroups;
+ groups.removeDuplicates();
+ return groups;
+}
+
+void
+RaveUsersBackend::setUsername( const QString& s )
+{
+ if ( s != m_username )
+ {
+ m_username = s;
+ emit usernameChanged();
+ emit statusChanged();
+
+ if ( !m_customHostname )
+ {
+ const QString generated = s.isEmpty() ? QString() : s + QStringLiteral( "-RaveOS-PC" );
+ if ( generated != m_hostname )
+ {
+ m_hostname = generated;
+ emit hostnameChanged();
+ emit statusChanged();
+ }
+ }
+
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::setHostname( const QString& s )
+{
+ m_customHostname = true;
+ if ( s != m_hostname )
+ {
+ m_hostname = s;
+ emit hostnameChanged();
+ emit statusChanged();
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::setPassword( const QString& s )
+{
+ if ( s != m_password )
+ {
+ m_password = s;
+ emit statusChanged();
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::setPasswordSecondary( const QString& s )
+{
+ if ( s != m_passwordSecondary )
+ {
+ m_passwordSecondary = s;
+ emit statusChanged();
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::setRootPasswordText( const QString& s )
+{
+ if ( s != m_rootPassword )
+ {
+ m_rootPassword = s;
+ emit statusChanged();
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::setRootPasswordSecondaryText( const QString& s )
+{
+ if ( s != m_rootPasswordSecondary )
+ {
+ m_rootPasswordSecondary = s;
+ emit statusChanged();
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::setReuseRootPassword( bool b )
+{
+ if ( b != m_reuseRootPassword )
+ {
+ m_reuseRootPassword = b;
+ emit reuseRootPasswordChanged();
+ emit statusChanged();
+ checkReady();
+ }
+}
+
+void
+RaveUsersBackend::checkReady()
+{
+ const bool ready = isReady();
+ if ( ready != m_lastReady )
+ {
+ m_lastReady = ready;
+ emit readyChanged();
+ }
+}