source: src-qt4/pc-usermanager/maindlgcode.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@…>, 12 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: 12.2 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 "maindlgcode.h"
25#include "user.h"
26#include "group.h"
27#include <QMessageBox>
28#include <QInputDialog>
29#include <QDebug>
30
31#include <qcombobox.h>
32#include <qlabel.h>
33#include <qlineedit.h>
34#include <qpushbutton.h>
35#include <qtooltip.h>
36       
37void mainDlgCode::programInit(UserManagerBackend *back, QString dir)
38{
39    setEnabled(false);
40   
41    chroot = dir;
42
43    this->back = back;
44    availablePointer = 0;
45    memberPointer = 0;
46    white = QColor(255, 255, 255);
47    red = QColor(255, 78, 78);
48    orange = QColor(255, 211, 65);
49    systemChange = false;
50   
51    updateShellList();
52    updateGroupList();
53    updateUserList();
54   
55    setEnabled(true);
56   
57    //Make connections
58    connect(groupList, SIGNAL(currentTextChanged( const QString & )), this, SLOT(getGroupDetails( const QString& )));
59    connect(newButton, SIGNAL(clicked()), this, SLOT(showAddDlg()));
60    connect(back, SIGNAL(usersChanged()), this, SLOT(usersChanged()));
61    connect(deleteButton, SIGNAL(clicked()), this, SLOT(removeUser()));
62    connect(makeMemberBut, SIGNAL(clicked()), this, SLOT(makeMember()));
63    connect(makeAvailableBut, SIGNAL(clicked()), this, SLOT(makeAvailable()));
64    connect(back, SIGNAL(groupsChanged()), this, SLOT(groupsChanged()));
65    connect(removeGroupBut, SIGNAL(clicked()), this, SLOT(removeGroup()));
66    connect(addGroupBut, SIGNAL(clicked()), this, SLOT(addGroup()));
67    connect(passwordButton, SIGNAL(clicked()), this, SLOT(changePassword()));
68    connect(fullnameBox, SIGNAL(textChanged(const QString&)), this, SLOT(fullnameChanged()));
69    connect(homeBox, SIGNAL(textChanged(const QString&)), this, SLOT(homeChanged()));
70    connect(simpleBut, SIGNAL(clicked()), this, SLOT(simplePressed()));
71    connect(shellBox, SIGNAL(activated(const QString&)), this, SLOT(shellChanged()));
72    connect(groupBox, SIGNAL(activated(const QString&)), this, SLOT(groupChanged()));
73}
74
75void mainDlgCode::updateUserList()
76{
77    qDebug() << "MainDlg: Updating user list";
78    disconnect(userList, SIGNAL(currentTextChanged( const QString & )), 0, 0);
79   
80    QStringList users = back->getAllUsers(-1, -1);
81    users.sort();
82    userList->clear();
83    userList->addItems(users);
84    userList->setCurrentRow(0);
85   
86    getUserDetails(userList->currentItem()->text());
87   
88    connect(userList, SIGNAL(currentTextChanged( const QString & )), this, SLOT(getUserDetails( const QString& )));
89   
90    //These won't always invoke first time without these lines
91    fullnameChanged();
92    homeChanged();
93}
94
95void mainDlgCode::updateShellList()
96{
97    qDebug() << "MainDlg: Updating shell list";
98    QStringList shells = back->getShells();
99    shells.sort();
100    shellBox->clear();
101    shellBox->addItems(shells);
102}
103
104void mainDlgCode::updateGroupList()
105{
106    qDebug() << "MainDlg: Updating group list";
107    disconnect(groupList, SIGNAL(currentTextChanged( const QString & )), 0, 0);
108
109    QStringList groups = back->getAllGroups(-1, -1);
110    groups.sort();
111    groupBox->clear();
112    groupBox->addItems(groups);
113    groupList->clear();
114    groupList->addItems(groups);
115    groupList->setCurrentRow(0);
116
117    getGroupDetails(groupList->currentItem()->text());
118
119    qDebug() << "MainDlg: Done with updateGroupList()";
120    connect(groupList, SIGNAL(currentTextChanged( const QString & )), this, SLOT(getGroupDetails( const QString& )));
121}
122
123void mainDlgCode::enableEdits(bool enable)
124{
125    deleteButton->setEnabled(enable);
126    fullnameBox->setEnabled(enable);
127    homeBox->setEnabled(enable);
128    groupBox->setEnabled(enable);
129}
130
131void mainDlgCode::getUserDetails(const QString &username)
132{
133    systemChange = true;
134   
135    qDebug() << "Fetching details for user: " << username;
136    User *user = back->getUser(username);
137   
138    //Disable certain form fields if the user is root, to stop changing of important fields
139    enableEdits(user->getUid() != 0);
140    //Do not allow the removal of the currently logged in user
141    if (getenv("USER") == username) deleteButton->setEnabled(false);
142
143    if ( user->getEnc() )
144      passwordButton->setEnabled(false);
145    else
146      passwordButton->setEnabled(true);
147   
148    QString uid = QString::number(user->getUid());
149    if (uid == "-1") { uid = tr("New User"); }
150    uidBox->setText(uid);
151    fullnameBox->setText(user->getFullname());
152    homeBox->setText(user->getHome());
153   
154    shellBox->setItemText(shellBox->currentIndex(), user->getShell());
155   
156    QString groupname;
157    if ( user->getGid() != -1 ) {
158        Group *group = back->getGroup(user->getGid());
159        groupname = group->getGroupname();
160        if (groupname == "Not Set") { groupname = username; }
161    } else {
162        groupname = username;
163    }
164    groupBox->setItemText(groupBox->currentIndex(), groupname);
165   
166    systemChange = false;
167}
168
169void mainDlgCode::getGroupDetails(const QString &groupname)
170{
171    qDebug() << "Fetching details for group: " << groupname;
172    QStringList users = back->getAllUsers(-1, -1);
173    users.sort();
174    Group *group = back->getGroup(groupname);
175
176    //Disable remove button if we're wheel
177    removeGroupBut->setEnabled(group->getGid() != 0);
178    memberPointer = memberList->currentItem();
179    availablePointer = availableList->currentItem();
180    memberList->clear();
181    availableList->clear();
182   
183    //Traverse the user list, if the user is a member place in the 'members' box, otherwise place them in the 'available' box.
184    for (QStringList::Iterator it = users.begin(); it != users.end(); it++)
185    {
186        if (group->isMember(*it) == true) { memberList->addItem(*it); }
187        else { availableList->addItem(*it); }
188    }
189    //memberList->setCurrentItem(memberPointer);
190    //availableList->setCurrentItem(availablePointer);
191}
192
193void mainDlgCode::showAddDlg()
194{
195    addui = new AddDlgCode();
196    addui->programInit(back);
197    addui->show();
198}
199
200UserManagerBackend* mainDlgCode::getBackend()
201{
202    return back;
203}
204
205void mainDlgCode::usersChanged()
206{
207    updateGroupList();
208    updateUserList();
209    emit changed(true);
210}
211
212void mainDlgCode::removeUser()
213{
214    QString username = userList->currentItem()->text();
215    User *user = back->getUser(username);
216    QString home = user->getHome();
217    int answer = QMessageBox::question(this,
218                                       tr("Delete Home Directory?"),
219                                       tr("Delete the home directory for ") + username + " (" + home + ")?" ,
220                                       QMessageBox::Yes | QMessageBox::No,
221                                       QMessageBox::No);
222    if(answer == QMessageBox::Yes) 
223      user->setDeleteHome(true);
224    else
225      user->setDeleteHome(false);
226    back->deleteUser(username);
227}
228
229void mainDlgCode::makeAvailable()
230{
231    back->removeUserFromGroup(memberList->currentItem()->text(), groupList->currentItem()->text());
232}
233
234void mainDlgCode::makeMember()
235{
236    back->addUserToGroup(availableList->currentItem()->text(), groupList->currentItem()->text());
237}
238
239void mainDlgCode::groupsChanged()
240{
241    updateGroupList();
242    emit changed(true);
243}
244
245void mainDlgCode::removeGroup()
246{
247    back->deleteGroup(groupList->currentItem()->text());
248}
249
250void mainDlgCode::addGroup()
251{
252    int valid = -1;
253    QString group = "";
254    while (valid != 0)
255    {
256        switch(valid)
257        {
258            case 1:
259                QMessageBox::critical(this, tr("Error adding group..."), tr("You must specify a name for your group"));
260                break;
261            case 2:
262                QMessageBox::critical(this, tr("Error adding group..."), tr("Invalid group name specified. Group names may contain only letters and number. Spaces are NOT permitted"));
263                break;
264            case 3:
265                QMessageBox::critical(this, tr("Error adding group..."), tr("The group '") + group + tr("' already exists, please choose a different name"));
266                break;
267        }
268       
269        group = QInputDialog::getText(this, tr("New Group"), tr("Please enter a name for your new group"));
270        if (group == QString::null) { return; }
271        valid = back->validateGroupname(group);
272    }
273    back->addGroup(group);
274}
275
276void mainDlgCode::changePassword()
277{
278    changePassUi = new ChangePassCode();
279    changePassUi->programInit(userList->currentItem()->text(), back);
280    changePassUi->show();
281}
282
283void mainDlgCode::homeChanged()
284{
285    User *user = back->getUser(userList->currentItem()->text());
286   
287    int homeError = back->validateHome(homeBox->text());
288   
289    QColor homeBoxColour = white;
290    homeBox->setToolTip(tr("Directory will be used"));
291   
292    switch (homeError)
293    {
294    case 1:
295        homeBox->setToolTip(tr("The field is empty"));
296        break;
297    case 2:
298        homeBoxColour = red;
299        homeBox->setToolTip(tr("Invalid Path!"));
300        break;
301    case 3:
302        homeBox->setToolTip(tr("Directory does not exist, but will be made"));
303        break;
304    case 4:
305        homeBoxColour = orange;
306        homeBox->setToolTip(tr("Placing home directory outside '/home/' can cause compatibility problems with some programs. NOT RECOMMENDED"));
307        break;
308    case 5:
309        homeBoxColour = red;
310        homeBox->setToolTip(tr("Requested path is a file (not a directory!)"));
311        break;
312    }
313   
314    if ((! systemChange) && (homeError != 5) && (homeError != 2))
315    {
316        user->setHome(homeBox->text());
317        user->setChanges();
318        emit changed(true);
319    }
320   
321    QPalette bgPal(homeBoxColour);
322    bgPal.setColor(QPalette::Window,homeBoxColour);
323    homeBox->setPalette(bgPal);
324}
325
326void mainDlgCode::fullnameChanged()
327{
328    User *user = back->getUser(userList->currentItem()->text());
329   
330    int fullnameError = back->validateFullname(fullnameBox->text());
331   
332    QColor fullnameBoxColour = white;
333    fullnameBox->setToolTip(tr("Good Input"));
334   
335    switch(fullnameError)
336    {
337    case 1:
338        fullnameBox->setToolTip(tr("The field is empty"));
339        break;
340    case 2:
341        fullnameBoxColour = red;
342        fullnameBox->setToolTip(tr("This field may not contain ! @ or :"));
343        break;
344    }
345   
346    if ((! systemChange) && (fullnameError != 2))
347    {
348        user->setFullname(fullnameBox->text());
349        user->setChanges();
350        emit changed(true);
351    }
352   
353    QPalette bgPal(fullnameBoxColour);
354    bgPal.setColor(QPalette::Window,fullnameBoxColour);
355    fullnameBox->setPalette(bgPal);
356}
357
358void mainDlgCode::simplePressed()
359{
360    emit simpleView();
361}
362
363void mainDlgCode::shellChanged()
364{
365    User *user = back->getUser(userList->currentItem()->text());
366   
367    if (! systemChange)
368    {
369        user->setShell(shellBox->currentText());
370        user->setChanges();
371        emit changed(true);
372    }
373}
374
375void mainDlgCode::groupChanged()
376{
377    User *user = back->getUser(userList->currentItem()->text());
378   
379    if (! systemChange)
380    {
381        user->setGid(back->getGroup(groupBox->currentText())->getGid());
382        user->setChanges();
383        emit changed(true);
384    }
385}
386
Note: See TracBrowser for help on using the repository browser.