source: src-qt4/pc-usermanager/adddlgcode.cpp @ 87d35b7

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 87d35b7 was 87d35b7, checked in by Kris Moore <kris@…>, 14 months ago

Add ability to PEFS encrypt users home-directories when we
create a new user via the user-manager GUI

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/***************************************************************************
2 *   Copyright (C) 2007, 2012 by Tim McCormick   *
3 *   tim@pcbsd.org   *
4 *                                                                         *
5 *   Permission is hereby granted, free of charge, to any person obtaining *
6 *   a copy of this software and associated documentation files (the       *
7 *   "Software"), to deal in the Software without restriction, including   *
8 *   without limitation the rights to use, copy, modify, merge, publish,   *
9 *   distribute, sublicense, and/or sell copies of the Software, and to    *
10 *   permit persons to whom the Software is furnished to do so, subject to *
11 *   the following conditions:                                             *
12 *                                                                         *
13 *   The above copyright notice and this permission notice shall be        *
14 *   included in all copies or substantial portions of the Software.       *
15 *                                                                         *
16 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
17 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
18 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
20 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
21 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
22 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
23 ***************************************************************************/
24#include "adddlgcode.h"
25       
26#include <qbuttongroup.h>
27#include <qradiobutton.h>
28#include <qcombobox.h>
29#include <qlineedit.h>
30#include <qtooltip.h>
31#include <QMessageBox>
32       
33void AddDlgCode::programInit(UserManagerBackend *back)
34{
35    this->back = back;
36    white = QColor(255, 255, 255);
37    red = QColor(255, 78, 78);
38    orange = QColor(255, 211, 65);
39   
40    connect(groupRadioOld, SIGNAL(clicked()), this, SLOT(changeGroupBox()));
41    connect(groupRadioNew, SIGNAL(clicked()), this, SLOT(changeGroupBox()));
42    connect(usernameBox, SIGNAL(textChanged(const QString&)), this, SLOT(usernameChanged()));
43    connect(passBox, SIGNAL(textChanged(const QString&)), this, SLOT(passwordChanged()));
44    connect(confirmPassBox, SIGNAL(textChanged(const QString&)), this, SLOT(passwordChanged()));
45    connect(fullnameBox, SIGNAL(textChanged(const QString&)), this, SLOT(fullnameChanged()));
46    connect(homeBox, SIGNAL(textChanged(const QString&)), this, SLOT(homeChanged()));
47    connect(okButton, SIGNAL(clicked()), this, SLOT(submit()));
48   
49    //Populate shell box
50    QStringList shells = back->getShells();
51    shells.sort();
52    shellBox->clear();
53    shellBox->addItems(shells);
54   
55    //Populate group box
56    QStringList groups = back->getAllGroups(-1, -1);
57    groups.sort();
58    groupBox->clear();
59    groupBox->addItems(groups);
60   
61}
62
63void AddDlgCode::changeGroupBox()
64{
65    if ( groupRadioOld->isChecked() ) { groupBox->setEnabled(TRUE); }
66    else { groupBox->setEnabled(FALSE); }
67}
68
69void AddDlgCode::usernameChanged()
70{
71    QString username = usernameBox->text().toLower();
72    usernameBox->setText(username);
73   
74    QString groupText = tr("New Group");
75    if (username != "")
76    {
77        groupText+=" (";
78        groupText+=username;
79        groupText+=")";
80    }
81    groupRadioNew->setText(groupText);
82   
83    QString homeDir = "/usr/home/";
84    homeDir+=username;
85    if (username != "") {
86        homeDir+="/";
87    }
88    homeBox->setText(homeDir);
89   
90    //Validate
91    int usernameError = back->validateUsername(username);
92   
93    QColor usernameBoxColour = white;
94    usernameBox->setToolTip(tr("Good Input"));
95   
96    switch (usernameError) 
97    {
98    case 1:
99        usernameBox->setToolTip( tr("The field is empty"));
100        break;
101    case 2:
102        usernameBoxColour = red;
103        usernameBox->setToolTip(tr("This field can only accept letters and numbers"));
104        break;
105    case 3:
106        usernameBoxColour = red;
107        usernameBox->setToolTip(tr("This username is already in use, please choose another."));
108    }
109   
110    QPalette bgPal(usernameBoxColour);
111    bgPal.setColor(QPalette::Window,usernameBoxColour);
112    usernameBox->setPalette(bgPal);
113
114}
115
116void AddDlgCode::passwordChanged()
117{
118    int passError = back->validatePassword(passBox->text(), confirmPassBox->text());
119   
120    QColor passBoxColour = white;
121    passBox->setToolTip( tr("Passwords match"));
122   
123    switch (passError)
124    {
125    case 1:
126        passBox->setToolTip( "");
127        break;
128    case 2:
129        passBoxColour = red;
130        passBox->setToolTip(tr("Passwords do not match"));
131        break;
132    case 3:
133        passBoxColour = red;
134        passBox->setToolTip(tr("Invalid input"));
135    }
136   
137    QPalette bgPal(passBoxColour);
138    bgPal.setColor(QPalette::Window,passBoxColour);
139    passBox->setPalette(bgPal);
140
141}
142
143void AddDlgCode::fullnameChanged()
144{
145    int fullnameError = back->validateFullname(fullnameBox->text());
146   
147    QColor fullnameBoxColour = white;
148    fullnameBox->setToolTip(tr("Good Input"));
149   
150    switch(fullnameError)
151    {
152    case 1:
153        fullnameBox->setToolTip(tr("The field is empty"));
154        break;
155    case 2:
156        fullnameBoxColour = red;
157        fullnameBox->setToolTip(tr("This field may not contain ! @ or :"));
158        break;
159    }
160   
161    QPalette bgPal(fullnameBoxColour);
162    bgPal.setColor(QPalette::Window,fullnameBoxColour);
163    fullnameBox->setPalette(bgPal);
164
165}
166
167void AddDlgCode::homeChanged()
168{
169    int homeError = back->validateHome(homeBox->text());
170   
171    QColor homeBoxColour = white;
172    homeBox->setToolTip(tr("Directory will be used"));
173   
174    switch (homeError)
175    {
176    case 1:
177        homeBox->setToolTip( tr("The field is empty"));
178        break;
179    case 2:
180        homeBoxColour = red;
181        homeBox->setToolTip(tr("Invalid Path!"));
182        break;
183    case 3:
184        homeBox->setToolTip( tr("Directory does not exist, but will be made"));
185        break;
186    case 4:
187        homeBoxColour = orange;
188        homeBox->setToolTip(tr("Placing home directory outside '/home/' can cause compatibility problems with some programs. NOT RECOMMENDED"));
189        break;
190    case 5:
191        homeBoxColour = red;
192        homeBox->setToolTip(tr("Requested path is a file (not a directory!)"));
193        break;
194    }
195   
196    QPalette bgPal(homeBoxColour);
197    bgPal.setColor(QPalette::Window,homeBoxColour);
198    homeBox->setPalette(bgPal);
199
200}
201
202void AddDlgCode::submit()
203{
204    //Validate all data fields, prompt with errors or create the new user as requested
205    bool inputError = false;
206    bool warnError = false;
207    QString inputText = tr("The following errors occurred while trying to add a new user:\n");
208    QString warnText = tr("The following non-critical errors occurred while trying to add a new user, do you wish to continue?\n");
209   
210    //Username validation
211    QString username = usernameBox->text();
212    int usernameError = back->validateUsername(username);
213   
214    switch (usernameError)
215    {
216    case 1:
217        inputText += tr("\n- The 'Username' field has not been filled in.");
218        break;
219    case 2:
220        inputError = true;
221        inputText += tr("\n- The 'Username' field may only contain letters and numbers (not spaces).");
222        break;
223    case 3:
224        inputError = true;
225        inputText += tr("\n- The username you have chosen is already in use, please choose another.");
226        break;
227    }
228   
229    //Full name validation
230    QString fullname = fullnameBox->text();
231    int fullnameError = back->validateFullname(fullname);
232    switch (fullnameError)
233    {
234    case 1:
235        inputError = true;
236        inputText += tr("\n- The 'Full Name' field has not been filled in.");
237        break;
238    case 2:
239        inputError = true;
240        inputText += tr("\n- The 'Full Name' field may not contain ! @ or :");
241        break;
242    }
243   
244    //Home directory validation
245    QString home = homeBox->text();
246    int homeError = back->validateHome(home);
247           
248    switch (homeError)
249    {
250    case 1:
251        inputError = true;
252        inputText += "\n" + tr("- The 'Home Directory' field has not been filled in.");
253        break;
254    case 2:
255        inputError = true;
256        inputText += "\n" + tr("- The value entered as 'Home Directory' is not a valid path.");
257        break;
258    case 4:
259        warnError = true;
260        warnText += "\n" + tr("- Placing home directory outside '/home/' can cause compatibility problems with some programs and is NOT RECOMMENDED.");
261        break;
262    case 5:
263        inputError = true;
264        inputText += "\n" + tr("- A file was selected as 'Home Directory', this value must be a directory.");
265        break;
266    }
267   
268    //Password validation
269    QString password = passBox->text();
270    QString confirmPassword = confirmPassBox->text();
271   
272    int passError = back->validatePassword(password, confirmPassword);
273   
274    switch (passError)
275    {
276    case 0:
277        //back->changePassword(username, password);
278        close();
279        break;
280    case 1:
281        inputError = true;
282        inputText += tr("\n- Both password fields must be filled in.");
283        break;
284    case 2:
285        inputError = true;
286        inputText += tr("\n- Passwords don't match, please try again.");
287        break;
288    case 3:
289        inputError = true;
290        inputText += tr("\n- Invalid password");
291        break;
292    }
293   
294   //Throw up error or continue
295
296   if (inputError)
297   {
298       //Throw up the error box!
299       QMessageBox::critical(this, tr("Error!"), inputText);
300   }
301   else
302   {
303       //If we need to issue a warning, do it, only continue if the user wishes it to be so.
304       int abortAdd = QMessageBox::Yes;
305       if (warnError)
306         abortAdd = QMessageBox::warning(this, tr("Warning!"), warnText, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
307
308       //Check we didn't abort the operation
309       if (abortAdd == QMessageBox::Yes)
310       {
311           QString group = groupBox->currentText();
312           if (groupRadioNew->isChecked() == true) { group = QString::null; }
313           back->addUser(username, fullname, home, shellBox->currentText(), group, password);
314           back->setEnc(username, checkEnc->isChecked());
315           back->changePassword(username, password);
316       }
317   }
318}
319
Note: See TracBrowser for help on using the repository browser.