1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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();
}
}
|