summaryrefslogtreecommitdiffstats
path: root/kuser/mainView.cpp
blob: 9c9b5d8200171f3425d996495070a8b658718c1a (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 *  Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
 *  Copyright (c) 2004 Szombathelyi Gy├Ârgy <gyurco@freemail.hu>
 *  Former maintainer: Adriaan de Groot <groot@kde.org>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "mainView.h"

#include <stdio.h>

#include <tqtooltip.h>
#include <tqfile.h>

#include <kinputdialog.h>
#include <ktoolbar.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <kdebug.h>

#include "misc.h"
#include "kglobal_.h"
#include "propdlg.h"
#include "addUser.h"
#include "delUser.h"
#include "pwddlg.h"
#include "editGroup.h"

mainView::mainView(TQWidget *parent) : TQTabWidget(parent)
{
  init();
}

void mainView::init() {

  lbusers = new KUserView( this, "lbusers" );
  addTab( lbusers, i18n("Users") );

  lbgroups = new KGroupView( this, "lbgroups" );
  addTab( lbgroups, i18n("Groups"));

  connect(lbusers, TQT_SIGNAL(doubleClicked(TQListViewItem *)), this, TQT_SLOT(userSelected()));
  connect(lbusers, TQT_SIGNAL(returnPressed(TQListViewItem *)), this, TQT_SLOT(userSelected()));

  connect(lbgroups, TQT_SIGNAL(doubleClicked(TQListViewItem *)), this, TQT_SLOT(groupSelected()));
  connect(lbgroups, TQT_SIGNAL(returnPressed(TQListViewItem *)), this, TQT_SLOT(groupSelected()));

  connect(this, TQT_SIGNAL(currentChanged(TQWidget *)), this, TQT_SLOT(slotTabChanged()));
}

mainView::~mainView()
{
}

void mainView::slotTabChanged()
{
  if (currentPage() == lbusers)
  {
     emit userSelected(true);
     emit groupSelected(false);
  }
  else
  {
     emit userSelected(false);
     emit groupSelected(true);
  }
}

void mainView::clearUsers()
{
  lbusers->clear();
}

void mainView::clearGroups()
{
  lbgroups->clear();
}

void mainView::reloadUsers()
{
  KU::KUser *ku;

  lbusers->clear();
  lbusers->init();
  uid_t uid = kug->kcfg()->firstUID();

  ku = kug->getUsers().first();
  while ( ku ) {
    if ( ku->getUID() >= uid || mShowSys ) lbusers->insertItem( ku );
    ku = kug->getUsers().next();
  }
  if (lbusers->firstChild())
    lbusers->setSelected(lbusers->firstChild(), true);
}

void mainView::reloadGroups()
{
  KU::KGroup *kg;

  lbgroups->clear();
  lbgroups->init();
  gid_t gid = kug->kcfg()->firstGID();

  kg = kug->getGroups().first();
  while ( kg ) {
    if ( kg->getGID() >= gid || mShowSys ) lbgroups->insertItem(kg);
    kg = kug->getGroups().next();
  }
}

void mainView::useredit()
{
  userSelected();
}

void mainView::userdel()
{
  KU::KUser *user = lbusers->getCurrentUser();
  if (!user)
    return;

  TQString username = user->getName();
  gid_t gid = user->getGID();
  delUser dlg(user, this);

  if ( dlg.exec() == TQDialog::Rejected )
     return;

  user->setDeleteHome( dlg.getDeleteHomeDir() );
  user->setDeleteMailBox( dlg.getDeleteMailBox() );

  kug->getUsers().del( user );
  if ( !updateUsers() ) return;

  KU::KGroup *group = 0;
  group = kug->getGroups().first();
  while ( group ) {
    kdDebug() << "group: " << group->getName() << endl;
    if ( group->lookup_user( username ) ) {
      kdDebug() << "group: " << group->getName() << " found user: " << username << endl;
      KU::KGroup newgroup( group );
      newgroup.removeUser( username );
      kug->getGroups().mod( group, newgroup );
    }
    group = kug->getGroups().next();
  }

  if ( kug->kcfg()->userPrivateGroup() ) {

    group = kug->getGroups().lookup( gid );

    if ( group &&
      KMessageBox::questionYesNo( 0, i18n("You are using private groups.\n"
        "Do you want to delete the user's private group '%1'?")
        .arg(group->getName()), TQString::null,
        KStdGuiItem::del(), i18n("Do Not Delete")) == KMessageBox::Yes) {
      kdDebug() << "del private group" << endl;
      kug->getGroups().del( group );
    }
  }
  kdDebug() << "update groups" << endl;
  updateGroups();
}

void mainView::useradd()
{
  KU::KUser *tk;

  showPage(lbusers);

  uid_t uid, rid = 0;
  bool samba = kug->getUsers().getCaps() & KU::KUsers::Cap_Samba;

  if ((uid = kug->getUsers().first_free()) == KU::KUsers::NO_FREE) {
    KMessageBox::sorry( 0, i18n("You have run out of uid space.") );
    return;
  }
/*
  if ( samba && (rid = kug->getUsers().first_free_sam()) == 0) {
    KMessageBox::sorry( 0, i18n("You have run out of user RID space.") );
    return;
  }
*/
  if ( samba ) rid = SID::uid2rid( uid );
  bool ok;
  TQString name = KInputDialog::getText( TQString::null,
    i18n("Please type the name of the new user:"),
    TQString::null, &ok );

  if ( !ok ) return;

  if ( kug->getUsers().lookup( name ) ) {
    KMessageBox::sorry( 0, i18n("User with name %1 already exists.").arg( name ) );
    return;
  }

  tk = new KU::KUser();
  tk->setCaps( samba ? KU::KUser::Cap_POSIX | KU::KUser::Cap_Samba : KU::KUser::Cap_POSIX );
  tk->setUID( uid );
  tk->setName( name );

  if ( samba ) {
    SID sid;
    sid.setDOM( kug->getUsers().getDOMSID() );
    sid.setRID( rid );
    tk->setSID( sid );
    tk->setProfilePath( kug->kcfg()->samprofilepath().replace( "%U",name ) );
    tk->setHomePath( kug->kcfg()->samhomepath().replace( "%U", name ) );
    tk->setHomeDrive( kug->kcfg()->samhomedrive() );
    tk->setLoginScript( kug->kcfg()->samloginscript() );
    tk->setDomain( kug->kcfg()->samdomain() );
  }

  tk->setShell( kug->kcfg()->shell() );
  tk->setHomeDir( kug->kcfg()->homepath().replace( "%U", name ) );
  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow || samba ) {
    tk->setLastChange( now() );
  }

  tk->setMin( kug->kcfg()->smin() );
  tk->setMax( kug->kcfg()->smax() );
  tk->setWarn( kug->kcfg()->swarn() );
  tk->setInactive( kug->kcfg()->sinact() );
  tk->setExpire( kug->kcfg()->sneverexpire() ? (uint) -1 :
    (kug->kcfg()->sexpire()).toTime_t() );

  bool privgroup = kug->kcfg()->userPrivateGroup();

  if ( !privgroup ) tk->setGID( kug->kcfg()->defaultgroup() );

  addUser au( tk, privgroup, this );

  au.setCreateHomeDir( kug->kcfg()->createHomeDir() );
  au.setCopySkel( kug->kcfg()->copySkel() );

  if ( au.exec() == TQDialog::Rejected ) {
    delete tk;
    return;
  }
  if ( privgroup ) {
    KU::KGroup *tg;

    if ((tg = kug->getGroups().lookup(tk->getName())) == 0) {
      gid_t gid;
      if ( kug->getGroups().lookup( tk->getUID() ) == 0 ) {
        gid = tk->getUID();
      } else {
        gid = kug->getGroups().first_free();
      }
      kdDebug() << "private group GID: " << gid << endl;
      uid_t rid = 0;
//      if ( samba ) rid = kug->getGroups().first_free_sam();
      if ( samba ) rid = SID::gid2rid( gid );
      if ( gid == KU::KGroups::NO_FREE || ( samba && rid == 0 ) ) {
        kug->getGroups().cancelMods();
        delete tk;
        return;
      }
      tg = new KU::KGroup();
      tg->setGID( gid );
      if ( samba && ( tk->getCaps() & KU::KUser::Cap_Samba ) ) {
        SID sid;
        sid.setDOM( kug->getGroups().getDOMSID() );
        sid.setRID( rid );
        tg->setSID( sid );
        tg->setDisplayName( tk->getName() );
        tg->setCaps( KU::KGroup::Cap_Samba );
      }
      tg->setName( tk->getName() );
      kug->getGroups().add( tg );
    }
    tk->setGID( tg->getGID() );
    tk->setPGSID( tg->getSID() );
  }
  kug->getUsers().add( tk );
  if ( !updateUsers() ) {
    kug->getGroups().cancelMods();
    return;
  }
  updateGroups();
}

bool mainView::queryClose()
{
  return true;
}

void mainView::setpwd()
{
  int count = lbusers->selectedItems().count();
  if ( count == 0 ) return;
  if ( count > 1 ) {
    if ( KMessageBox::questionYesNo( 0,
      i18n("You have selected %1 users. Do you really want to change the password for all the selected users?")
		.arg( count ), TQString::null, i18n("Change"), i18n("Do Not Change") ) == KMessageBox::No ) return;
  }
  pwddlg d( this );
  if ( d.exec() != TQDialog::Accepted ) return;

  KU::KUser newuser, *user;
  TQListViewItem *item;

  item = lbusers->firstChild();
  while ( item ) {
    if ( item->isSelected() ) {
      user = ((KUserViewItem*) item)->user();
      newuser.copy( user );
      kug->getUsers().createPassword( &newuser, d.getPassword() );
      newuser.setLastChange( now() );
      newuser.setDisabled( false );
      kug->getUsers().mod( user, newuser );
    }
    item = item->nextSibling();
  }
  updateUsers();
}

void mainView::groupSelected()
{
  bool samba = kug->getGroups().getCaps() & KU::KGroups::Cap_Samba;
  KU::KGroup *tmpKG = lbgroups->getCurrentGroup();
  if ( !tmpKG ) return;
  KU::KGroup newGroup( tmpKG );

  kdDebug() << "The SID for group " << newGroup.getName() << " is: '" << newGroup.getSID().getSID() << "'" << endl;
  if ( samba && ( newGroup.getCaps() & KU::KGroup::Cap_Samba ) && 
      newGroup.getSID().isEmpty() ) {
    SID sid;
    sid.setDOM( kug->getGroups().getDOMSID() );
//    sid.setRID( kug->getGroups().first_free_sam() );
    sid.setRID( SID::gid2rid( newGroup.getGID() ) );
    newGroup.setSID( sid );
    kdDebug() << "The new SID for group " << newGroup.getName() << " is: " << sid.getSID() << endl;
  }
  editGroup egdlg( &newGroup, samba, false );

  if ( egdlg.exec() == TQDialog::Accepted ) {
    kug->getGroups().mod( tmpKG, newGroup );
    updateGroups();
  }
}

void mainView::userSelected()
{
  TQListViewItem *item;
  TQPtrList<KU::KUser> ulist;

  item = lbusers->firstChild();
  while ( item ) {
    if ( item->isSelected() ) {
      ulist.append( ((KUserViewItem*) item)->user() );
    }
    item = item->nextSibling();
  }
  if ( ulist.isEmpty() ) return;

  propdlg editUser( ulist, this );
  if ( editUser.exec() == TQDialog::Rejected ) return;

  KU::KUser *user, newuser;
  user = ulist.first();
  while ( user ) {
    editUser.mergeUser( user, &newuser );
    kug->getUsers().mod( user, newuser );
    user = ulist.next();
  }
  updateUsers();
  updateGroups();
}

void mainView::grpadd()
{
  showPage(lbgroups);

  gid_t gid;
  uid_t rid = 0;
  bool samba;

  samba = kug->getGroups().getCaps() & KU::KGroups::Cap_Samba;

  if ( (gid = kug->getGroups().first_free()) == KU::KGroups::NO_FREE )
  {
    KMessageBox::sorry( 0, i18n("You have run out of gid space.") );
    return;
  }
/*
  if ( samba && (rid = kug->getGroups().first_free_sam()) == 0 )
  {
    KMessageBox::sorry( 0, i18n("You have run out of group RID space.") );
    return;
  }
*/
  if ( samba ) rid = SID::gid2rid( gid );

  KU::KGroup *tk = new KU::KGroup();
  tk->setGID(gid);
  if ( samba ) {
    SID sid;
    sid.setRID( rid );
    sid.setDOM( kug->getGroups().getDOMSID() );
    tk->setSID( sid );
  }
  editGroup egdlg( tk, samba, true );

  if ( egdlg.exec() == TQDialog::Rejected ) {
    delete tk;
    return;
  }
  kug->getGroups().add(tk);
  updateGroups();
}

void mainView::grpedit()
{
  groupSelected();
}

void mainView::grpdel()
{
  TQListViewItem *item;
  KU::KGroup *group = NULL;
  int selected = 0;

  item = lbgroups->firstChild();
  while ( item ) {
    if ( item->isSelected() ) {

      selected++;
      group = ((KGroupViewItem*) item)->group();

      KU::KUser *user = kug->getUsers().first();
      while ( user ) {
        if ( user->getGID() == group->getGID() ) {
          KMessageBox::error( 0, i18n( "The group '%1' is the primary group of one or more users (such as '%2'); it cannot be deleted." ).arg( group->getName() ).arg( user->getName() ) );
          return;
        }
        user = kug->getUsers().next();
      }
    }
    item = item->nextSibling();
  }

  switch ( selected ) {
    case 0: return;
    case 1:
      if (KMessageBox::warningContinueCancel( 0,
        i18n("Do you really want to delete the group '%1'?").arg(group->getName()),
        TQString::null, KStdGuiItem::del()) != KMessageBox::Continue) return;
      break;
    default:
      if (KMessageBox::warningContinueCancel( 0,
        i18n("Do you really want to delete the %1 selected groups?").arg(selected),
        TQString::null, KStdGuiItem::del()) != KMessageBox::Continue) return;
  }

  item = lbgroups->firstChild();
  while ( item ) {
    if ( item->isSelected() ) {
      group = ((KGroupViewItem*) item)->group();
      kug->getGroups().del( group );
    }
    item = item->nextSibling();
  }
  updateGroups();
}

bool mainView::updateGroups()
{
  bool ret;
  kdDebug() << "updateGroups() " << endl;
  ret = kug->getGroups().dbcommit();

  KU::KGroup *group;
  KU::KGroups::DelIt dit( kug->getGroups().mDelSucc );
  KU::KGroups::AddIt ait( kug->getGroups().mAddSucc );

  while ( (group = dit.current()) != 0 ) {
    ++dit;
    lbgroups->removeItem( group );
  }
  while ( (group = ait.current()) != 0 ) {
    ++ait;
    lbgroups->insertItem( group );
  }
  kdDebug() << "commit groups" << endl;
  kug->getGroups().commit();
  return ret;
}

bool mainView::updateUsers()
{
  bool ret;
  kdDebug() << "updateUsers() " << endl;
  ret = kug->getUsers().dbcommit();

  KU::KUser *user;
  KU::KUsers::DelIt dit( kug->getUsers().mDelSucc );
  KU::KUsers::AddIt ait( kug->getUsers().mAddSucc );

  while ( (user = dit.current()) != 0 ) {
    ++dit;
    lbusers->removeItem( user );
  }
  while ( (user = ait.current()) != 0 ) {
    ++ait;
    lbusers->insertItem( user );
  }
  kug->getUsers().commit();
  return ret;
}


#include "mainView.moc"