1. Moises Henriquez
  2. vasm

Source

vasm / src / vasm / ui / gtk2 / modules / usermanage.py

  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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with VASM.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

import os
from vasm.ui.gtk2.support import widgets, dialogs
from vasm.backend import useradmin, passwords
from vasm.backend.utils import _, datapath
import gtk
import logging

logger = logging.getLogger('vasm')

# vasm flags
VASM_CATEGORY=_("System")
VASM_ICON=os.path.join(datapath, 'users.png')
VASM_LABEL=_("Users and Passwords")

class ActionButton(gtk.Button):
    """ Button that allows us to use a stock icon with a custom label for this
    module """
    def __init__(self, label, icon):
        gtk.Button.__init__(self)
        self.set_label(label)
        self.image = gtk.Image()
        self.image.set_from_stock(icon, gtk.ICON_SIZE_BUTTON)
        self.set_image(self.image)

class TabStrip(gtk.Notebook):
    """ tabstrip to be used in this module"""
    def __init__(self):
        gtk.Notebook.__init__(self)
        self.set_tab_pos(gtk.POS_TOP)
    
    def add_page(self, title):
        page = gtk.VBox()
        header = gtk.Label(title)
        return self.append_page(page, header)

class UserPropLabel(gtk.Label):
    """ Preset user property label """
    def __init__(self, text, size_chars=15):
        gtk.Label.__init__(self)
        self.set_use_markup(True)
        self.set_markup(text)
        self.set_property('width-chars', size_chars)
        self.set_property('xalign', 0.0)

class AboutUser(widgets.VasmModule):
    def __init__(self, parent, user):
        self.user = useradmin.User(user)
        self._parent = parent
        self.title = user
        widgets.VasmModule.__init__(self, parent, self.title, "")
        self.initial_group_membership = self.user.groups
        self.newgrouplist = []
        
        # create the tabstrip
        contentbox = gtk.HBox()
        self.tabstrip = TabStrip()
        self.tabstrip.add_page(_("General Information"))
        self.tabstrip.add_page(_("Account Privileges"))
        self.tabstrip.add_page(_("Password"))
        page0 = self.tabstrip.get_nth_page(0)
        page1 = self.tabstrip.get_nth_page(1)
        page2 = self.tabstrip.get_nth_page(2)
        
        # fill in the information
        self._display_general_info(page0)
        self._display_privilege_info(page1)
        self._display_password_info(page2)
        contentbox.pack_start(self.tabstrip, True, True, 4)
        self.pack_start(contentbox, True, True, 0)
        
        # the buttons
        self.btapply = gtk.Button(stock=gtk.STOCK_APPLY)
        # connect the apply button and save the handler id
        self._apply_handler = self.btapply.connect('clicked', self.apply_groups_event)
        btclose = gtk.Button(stock=gtk.STOCK_CANCEL)
        btclose.connect('clicked', self.close_module)
        
        self.add_button(btclose)
        self.add_button(self.btapply)
        
        # connect the tabstrip event
        self.tabstrip.connect('switch-page', self.page_change_event)
    
    def close_module(self, widget=None):
        #print self._parent
        #print self._parent._parent
        super(AboutUser, self).close_module()
        return self._parent.return_to_module()
        #return super(AboutUser, self).close_module()
        
    
    def _test_entered_passwords(self, entry):
        """ Return True if the entered passwords are fine.  False otherwise."""
        pass1 = self.passbox1.entry.get_text()
        pass2 = self.passbox2.entry.get_text()
        ret = False
        if not pass1:
            ret = False
        if len(pass2) > 2:
            ret = passwords.test_passwords_match(pass1, pass2)
        self.btapply.set_property('sensitive', ret)

    def page_change_event(self, tabstrip, page, pageNumber):
        if pageNumber == 1:
            self._refresh_privilege_info()
            # connect the apply button to save the groups info
            self.btapply.disconnect(self._apply_handler)
            self._apply_handler = self.btapply.connect('clicked', self.apply_groups_event)
        elif pageNumber == 2:
            # connect the apply button to change the password.
            self.btapply.disconnect(self._apply_handler)
            self._apply_handler = self.btapply.connect('clicked', self.apply_password_event)
        else:
            self.btapply.disconnect(self._apply_handler)
            self._apply_handler = self.btapply.connect('clicked', self.apply_groups_event)
        self.btapply.set_property('sensitive', False)
    
    def _display_password_info(self, box):
        """ pack password widgets into box """
        tx = _("Enter new password for this user in the fields below.") + \
        "  " + \
        _("Click 'Apply' to save changes.")
        lb = widgets.DLabel(tx)
        wbox = gtk.HBox()
        wbox.pack_start(lb, True, True, 4)
        box.pack_start(wbox, False, True, 24)
        self.passbox1 = widgets.TextEntry(label=_("New Password"), password=True, label_width_chars=15)
        self.passbox2 = widgets.TextEntry(label=_("Verify Password"), password=True, label_width_chars=15)
        self.passbox2.entry.connect('changed', self._test_entered_passwords)
        self.passbox1.entry.connect('changed', self._test_entered_passwords)
        box.pack_start(self.passbox1, False, True, 4)
        box.pack_start(self.passbox2, False, True, 4)

    def _display_general_info(self, box):
        """ Pupulate the tab with the general information in about the user account"""
        box.pack_start(gtk.Label(), False, False, 2)
        unamebox = widgets.TextEntry(_("User Login"), label_width_chars=15,
                                     entry_fill_box = False)
        unamebox.entry.set_text(self.user.login)
        unamebox.entry.set_property('sensitive', False)
        box.pack_start(unamebox, False, True, 2)
        
        fullnamebox = widgets.TextEntry(_("Full Name"),entry_fill_box=False,
                                        label_width_chars=15)
        fullnamebox.entry.set_text(self.user.fullname)
        fullnamebox.entry.set_property('sensitive', False)
        box.pack_start(fullnamebox, False, True, 2)
        homebox = widgets.TextEntry(_("Home Directory"), entry_fill_box=False,
                                    label_width_chars=15)
        homebox.entry.set_text(self.user.homedir)
        homebox.entry.set_property('sensitive', False)
        box.pack_start(homebox, False, True, 2)
        unamebox = widgets.TextEntry(_("User ID"), entry_fill_box = False,
                                     label_width_chars=15)
        unamebox.entry.set_text(str(self.user.uid))
        unamebox.entry.set_property('sensitive', False)
        box.pack_start(unamebox, False, True, 2)
        shellbox = widgets.TextEntry(_("Shell"), entry_fill_box=False,
                                     label_width_chars=15)
        shellbox.entry.set_text(str(self.user.shell))
        shellbox.entry.set_property('sensitive', False)
        box.pack_start(shellbox, False, True, 2)

    def _display_privilege_info(self, box):
        """ Display a list of privileges this user belongs to """
        model = gtk.ListStore(bool,bool, str, str)
        tree = gtk.TreeView(model=model)
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.add(tree)
        crtgl = gtk.CellRendererToggle()
        crtgl.connect('toggled', self.toggle_group, model)
        crgrpnam = gtk.CellRendererText()
        crgrpdes = gtk.CellRendererText()
        
        coltgl = gtk.TreeViewColumn("",crtgl, active=0, activatable=1)
        colnam=gtk.TreeViewColumn(_("Group"), crgrpnam, text=2)
        coldes=gtk.TreeViewColumn(_("Description"), crgrpdes, text=3)
        # Add the group listings
        exgrp = self.initial_group_membership
        for grp in useradmin.__distro_groups__:
            desc = useradmin.__distro_groups__[grp][0]
            model.append([grp in exgrp, os.geteuid() == 0, grp, desc])

        for col in (coltgl, colnam, coldes):
            tree.append_column(col)
        
        box.pack_start(sw, True, True, 2)

    def toggle_group(self, cell, path, model):
        """ Events that take place when a group is ticked on or off
         * Compare the initial list to the current selection and determine if
            there are changes.
         * Update the display to show which groups the user is now a member of.
         This sets the self.newgrouplist property to the correct value so that
         self.btapply can perform the usermod command.
         """
        grouplist = []
        initlist = self.initial_group_membership
        change=False
        item = model.get_iter(path)
        model.set_value(item,0,not cell.get_active())
        
        for line in model:
            isenabled = line[0]
            if isenabled:
                grouplist.append(line[2])

        if cell.get_active():
            # group was removed
            grp = model[model.get_iter(path)][2]
            if grp in initlist:
                change=True
            else:
                for grp in grouplist:
                    if grp not in initlist:
                        change=True
        else:
            for grp in grouplist:
                if grp not in initlist:
                    change=True
        
        self.btapply.set_property('sensitive', change)
        self.newgrouplist = grouplist

    def _refresh_privilege_info(self):
        """ Refresh privilege info """
        treeview = None
        # get the treeview
        box = self.tabstrip.get_nth_page(1)
        for item in box.get_children():
            if type(item) == gtk.ScrolledWindow:
                for widget in item:
                    if type(widget) == gtk.TreeView:
                        treeview = widget
        if treeview is None: return
        model = treeview.get_model()
        model.clear()
        usergroups = self.initial_group_membership
        for grp in useradmin.__distro_groups__:
            desc = useradmin.__distro_groups__[grp][0]
            model.append([grp in usergroups, os.geteuid() == 0, grp, desc])
    
    def apply_password_event(self, widget):
        """ Apply the password change """
        pass1 = self.passbox1.entry.get_text()
        pass2 = self.passbox1.entry.get_text()
        user = self.user.login
        # passwords  have already been tested as they are entered.
        # clear the text boxes
        self.passbox1.entry.set_text("")
        self.passbox2.entry.set_text("")
        try:
            passwords.change_password(user, "foo", pass2)
            logger.debug("Changed password for %s"%user)
        except passwords.UserModError as e:
            logger.error("Failed to change password for %s.  %s"% (user, e))
            return dialogs.error(
            _("Unable to change the user password.") + "  \n\n" + e
                )
        return dialogs.info(
            _("Password has been successfully changed for this user.")
            )

    def apply_groups_event(self, widget):
        # Defines what happens when the apply button is pressed.
        # What happens depends on which page on the tabstrip we are currently on.
        page = self.tabstrip.get_current_page()
        # page index
        # 0 = User Info
        # 1 = User group membership
        widget.set_property('sensitive', False)
        if page is 1:
            self.user.groups = self.newgrouplist
            try:
                self.user.save_groups_changes()
                logger.debug("Saved group memberships for %s"% self.user.login)
            except AssertionError as e:
                logger.error("Failed to save group memberships for %s.  %s"% (self.user.login, e))
                return dialogs.error(
                    _("Unable to save changes.") + "  \n\n" + e
                    )
            except AccountError as e:
                logger.error("Failed to save group memberships for %s.  %s"% (self.user.login, e))
                msg = (_("You may be trying to save changes to a new account.")," ",
                ("Make sure the account is created before trying to modify it."))
                return dialogs.error(" ".join(msg))
        return dialogs.info(
            _("Group membership changes were applied successfully.")
            )

class UserAdd(gtk.Dialog):
    """Dialog to add a new user account"""
    
    def set_module_text(self, markup):
        """ set the text on top"""
        box = gtk.HBox()
        lb = widgets.DLabel(markup)
        box.pack_start(lb, False, True, 2)
        self.body.pack_start(box, False, True, 2)
        
    def __init__(self, parent=None, usermodel=None):
        """ Initialization procedures for the add user dialog """
        gtk.Dialog.__init__(self, parent=parent, title="Add user account")
        self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
        self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
        self.set_default_response(gtk.RESPONSE_OK)
        self.set_has_separator(True)
        self.body = self.get_content_area()
        # set the ok_button public so we can enabled/disable it when the
        # right conditions are not met.
        for bt in self.get_action_area().get_children():
            if bt.get_label() == "gtk-ok":
                self.ok_button = bt

        self.usermodel = usermodel
        self.account = useradmin.User(new=True)
        self.set_size_request(540,460)
        topbox = gtk.HBox()
        banner = widgets.DLabel()
        banner.set_use_markup(True)
        mkup = "<span size='larger'><b>%s</b></span>\n\n%s" % (
            _("Create New Account."),
            _("Fill in the fields below and click 'OK' to create a new user account.") + \
            "  " + \
            "%s <b>%s</b> %s"% (
                _("Fields maked in"), _("bold"), _("are required")
            ) + \
            _("Passwords must be at least 6 characters long.")
        )
        banner.set_markup(mkup)
        topbox.pack_start(banner, True, True, 2)
        self.body.pack_start(topbox, False, True, 12)

        toparea = gtk.HBox()
        picbox = gtk.VBox()
        toparea.pack_start(picbox, False, False, 8)
        picbt = gtk.Button()
        picbt.connect('clicked', self.select_user_face)
        picimg = gtk.Image()
        # set default icon
        iconpath = "/usr/share/apps/kdm/pics/users/default2.png"
        if not os.path.exists(iconpath):
            iconpath = os.path.join(datapath, 'category_personal.png')
        self.facepath = iconpath
        picimg.set_from_pixbuf(gtk.gdk.pixbuf_new_from_file_at_size(iconpath, 96, 96))
        picbt.set_image(picimg)
        picbox.pack_start(picbt, False, False, 4)

        topright = gtk.VBox()
        namebox = gtk.HBox()
        namebox.pack_start(UserPropLabel("<b>%s</b>"% _("Login Name"), 15),
                           False, False, 2)
        self.txtlogin = gtk.Entry()
        self.txtlogin.connect('changed', self.update_entered_login)
        namebox.pack_start(self.txtlogin, True, True, 2)
        rnamebox = gtk.HBox()
        rnamebox.pack_start(UserPropLabel(_("Real Name"), 15), False, False, 2)
        txtrnamebox = gtk.Entry()
        txtrnamebox.connect('changed', self.update_entered_comment)
        rnamebox.pack_start(txtrnamebox, True, True, 2)
        topright.pack_start(namebox, False, True, 2)
        topright.pack_start(rnamebox, False, True, 2)
        toparea.pack_start(topright, True, True, 2)
        self.body.pack_start(toparea, False, False, 2)
        passline1 = gtk.HBox()
        passline1.pack_start(UserPropLabel("<b>%s</b>"% _("Enter Password")),
                             False, False, 2)
        self.txtpass1 = gtk.Entry()
        passline1.pack_start(self.txtpass1, True, True, 2)
        topright.pack_start(passline1, False, True, 2)
        passline2 = gtk.HBox()
        passline2.pack_start(UserPropLabel("<b>%s</b>"% _("Verify Password")),
                             False, False, 2)
        self.txtpass2 = gtk.Entry()
        self.txtpass2.connect('changed', self.update_entered_password)
        passline2.pack_start(self.txtpass2, True, True, 2)
        topright.pack_start(passline2, False, True, 2)
        for box in (self.txtpass1, self.txtpass2):
            box.set_visibility(False)
        
        # present the user membership list
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        store = gtk.ListStore(bool, bool, str, str)
        self.treeview = gtk.TreeView(model=store)
        cr0 = gtk.CellRendererToggle()
        cr0.connect('toggled', self.toggle_user_group, store)
        crgrp = gtk.CellRendererText()
        crdes = gtk.CellRendererText()
        
        col0 = gtk.TreeViewColumn("",cr0, active=0, activatable=1)
        col1 = gtk.TreeViewColumn(_("Group"), crgrp, text=2)
        col2 = gtk.TreeViewColumn(_("Group Description"),crdes, text=3)
        
        for col in (col0, col1, col2):
            self.treeview.append_column(col)
        sw.add(self.treeview)
        
        # Add the default group setup
        self.account.groups = []
        for grp in useradmin.__distro_groups__:
            default=useradmin.__distro_groups__[grp][1]
            desc = useradmin.__distro_groups__[grp][0]
            store.append([default, True, grp, desc])
            # Add the default list
            if default:
                self.account.groups.append(grp)

        
        self.body.pack_start(widgets.DLabel(
            _("User group membership")), False, False, 2)
        self.body.pack_start(sw, True, True, 8)
        
        self.set_position(gtk.WIN_POS_CENTER)
        self.ok_button.set_property('sensitive', False)
        # set the focus to the first entry
        self.txtlogin.grab_focus()
        self.show_all()
        
    def select_user_face(self, widget):
        """ Select the user face icon """
        dia = gtk.FileChooserDialog(parent=self,
                                    buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
                                    gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        flt = gtk.FileFilter()
        flt.set_name(_("Icon files"))
        flt.add_pattern("*.png")
        flt.add_pattern("*.xpm")
        flt.add_pattern("*.jpg")
        dia.add_filter(flt)
        dia.set_current_folder("/usr/share/apps/kdm/pics/users")
        
        # set the previewer
        viewer = FacePreview(dia)
        dia.connect('selection-changed', viewer.update_preview)
        dia.set_preview_widget(viewer)
        
        # capture the response
        ret = dia.run()
        dia.hide()
        if ret == gtk.RESPONSE_OK:
            fpath = dia.get_filename()
            if fpath is None:
                dia.destroy()
                return
            # update the image using the filename selected
            self.account.faceicon = fpath
            img = widget.get_image()
            img.set_from_pixbuf(
                gtk.gdk.pixbuf_new_from_file_at_size(self.facepath, 96, 96)
            )
        dia.destroy()
    
    def update_entered_login(self, widget):
        self.account.login = widget.get_text()
        self.toggle_ok_button(self.check_passwords())
    
    def update_entered_comment(self, widget):
        self.account.fullname = widget.get_text()
    
    def check_passwords(self):
        """ Check if the entered passwords are suitable to continue.
        Return True if passwords are OK, False othewise """
        ret = False
        pass1 = self.txtpass1.get_text()
        pass2 = self.txtpass2.get_text()
        if len(pass2) < 5: return False
        if pass1 != pass2: return False
        return True
    
    def toggle_ok_button(self, value):
        """ Toggle the ok button to value """
        return self.ok_button.set_property('sensitive', value)
    
    def update_entered_password(self, widget):
        # check the passwords to see if they match before enabling the OK button
        if self.check_passwords():
            if self.account.login:
                self.account.password = widget.get_text()
                self.toggle_ok_button(True)
            else:
                self.toggle_ok_button(False)

    def toggle_user_group(self, cell, path, model):
        """ Update the self.user_groups propety so that we can access it later
         """
        self.account.groups = []
        item = model.get_iter(path)
        model.set_value(item,0,not cell.get_active())
        for grp in model:
            enabled = grp[0]
            grpnam = grp[2]
            if enabled:
                self.account.groups.append(grpnam)

class FacePreview(gtk.Image):
    """Preview widget for the face picker """
    def __init__(self, filechooser):
        gtk.Image.__init__(self)
        self.filechooser = filechooser
    
    def update_preview(self, widget=None):
        """ Refresh the preview """
        if self.filechooser.get_preview_filename() is None: return
        self.set_from_pixbuf(
            gtk.gdk.pixbuf_new_from_file_at_size(
                self.filechooser.get_preview_filename(), 64, 64
            )
        )

class UserManager(widgets.VasmModule):
    def __init__(self, parent):
        tx = _("Add, delete and modify user accounts.")
        widgets.VasmModule.__init__(self, parent, VASM_LABEL, tx)
        self.selected = None
        
        # create the users model we will be working with
        self.usermodel = useradmin.UserModel()
        # Add the observable to automatically refresh the user list upon any changes
        self.usermodel.add_observer(self.refresh_user_list)
        
        # content box contains the user list (treeview) on the left
        # and the action buttons on the right
        contentbox = gtk.HBox()
        self.buttonbox = gtk.VBox()
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        contentbox.pack_start(sw, True, True, 4)
        contentbox.pack_end(self.buttonbox, False, True, 2)
        self.pack_start(contentbox, True, True, 4)
        
        # Add the action buttons to the button box
        bt_user_add = ActionButton(_("Add User"), gtk.STOCK_ADD)
        bt_user_add.connect('clicked', self.launch_add_user)
        bt_user_del = ActionButton(_("Delete User"), gtk.STOCK_DELETE)
        bt_user_del.connect("clicked", self.confirm_delete_user)
        bt_user_del.set_property('sensitive', self.selected is not None)
        bt_user_props = ActionButton(_("Properties"), gtk.STOCK_PROPERTIES)
        bt_user_props.set_property('sensitive', self.selected is not None)
        bt_user_props.connect("clicked", self.show_user_props)

        
        # add the treeview on the left
        model = gtk.ListStore(gtk.gdk.Pixbuf, str, str, str)
        self.tree = gtk.TreeView(model = model)
        self.tree.connect("cursor-changed", self.update_tree_selection)
        self.tree.connect("row-activated", self.row_activate)
        self.tree.set_headers_visible(True)
        # add tree to scrolled window
        sw.add(self.tree)
        rendface = gtk.CellRendererPixbuf()
        rendlogin = gtk.CellRendererText()
        rendcomment = gtk.CellRendererText()
        rendhome = gtk.CellRendererText()
        colface = gtk.TreeViewColumn("", rendface, pixbuf=0)
        coluname = gtk.TreeViewColumn(_("Login Name"), rendlogin, text=1)
        coluid = gtk.TreeViewColumn(_("UID"), rendhome, text=2)
        colhome = gtk.TreeViewColumn(_("Home Directory"), rendcomment, text=3)
        
        for col in (colface, coluname, coluid, colhome):
            self.tree.append_column(col)

        # Force a notify to display the list for the first time.
        self.usermodel.notify()
        
        for bt in (bt_user_add, bt_user_del, bt_user_props):
            self.buttonbox.pack_start(bt, False, False, 2)
            
        
        # module buttons
        btclose = gtk.Button(stock=gtk.STOCK_CANCEL)
        btclose.connect('clicked', self.close_module)
        self.add_button(btclose)

    def show_module(self, widget=None):
        return self._parent.show_module(widget)
    
    def return_to_module(self):
        """ Return to this module after displaying user properties. """
        self._parent.return_home()
        return self.launch()

    def show_user_props(self, widget):
        """ Display account properties for the selected user """
        title = _("About") + " " + self.selected
        #body = widgets.VasmModule(self._parent, title, "")
        body = AboutUser(self, self.selected)
        body.launch()
        
    def _show_user_props(self, widget):
        """ Display the selected user's properties """
        if self.selected is None:
            return dialogs.error(
                _("No user has been selected.  Click on user list to select"),
                parent = self._parent)
        else:
            dia = AboutUser(self._parent,self.selected)
            if dia.run():
                dia.destroy()

    def confirm_delete_user(self, widget):
        """ Ask to be sure before we delete an account """
        ask = _("Are you sure you want to delete user account for") + \
        self.selected + "?" + "  " + \
        _("This will also destroy all of this user's personal data.") +\
        "  " + "<b>%s</b>"%(_("This cannot  be undone."))
        response = dialogs.question(ask)
        if response == gtk.RESPONSE_YES:
            try:
                self.usermodel.deleteUser(self.selected)
                logger.debug("Deleted user account %s"% self.selected)
            except AssertionError as e:
                logger.error("Failed to delete account for %s.  %s"% (self.selected, e))
                return dialogs.error(
                _("Unable to delete user.") + "\n\n" + e
                    )
        return dialogs.info(
            _("User account has been deleted."))

    def launch_add_user(self, widget=None):
        box = UserAdd(parent=self._parent, usermodel=self.usermodel)
        box.show_all()
        ret = box.run()
        box.hide()
        if ret == gtk.RESPONSE_OK:
            acct = box.account
            try:
                self.usermodel.addUser(acct)
                logger.info("Added user account %s"% acct.login)
            except AssertionError as e:
                logger.error("Failed to add user %s.  %s"% (acct.login, e))
                box.destroy()
                return dialogs.error(
                _("Unable to add new user account.") + " \n\n" + e)
            except useradmin.EmptyAttributeError as e:
                logger.error("Failed to add user %s.  %s"% (acct.login, e))
                box.destroy()
                return dialogs.error(
                _("Account information is incomplete.") + " \n\n" + e.message
                )
            msg = _("User account was successfully created and is ready for immediate use")
            box.destroy()
            return dialogs.info(msg)
        return

    def refresh_user_list(self, datamodel=None):
        model = self.tree.get_model()
        # clear the model
        model.clear()
        # get the user list
        users = datamodel.listUsers()
        for account in users:
            model.append([self.get_user_face(account.login),
                account.login,
                account.uid,
                account.homedir])
        return

    def update_tree_selection(self, treeview=None):
        """ Update the global selected value """
        if treeview is None: return
        path, column = treeview.get_cursor()
        model = treeview.get_model()
        self.selected = model[path[0]][1]
        # update the property button status
        for wid in self.buttonbox:
            if wid.get_label() == _("Properties"):
                wid.set_property('sensitive', self.selected is not None)
            elif wid.get_label() == _("Delete User"):
                if self.selected == "root":
                    wid.set_property('sensitive', False)
                else:
                    wid.set_property('sensitive', os.geteuid() == 0)

    def row_activate(self, treeview, cell, path):
        return self.show_user_props(treeview)

    def get_user_face(self, uname):
        """ Find the user face icon and return it in the form of a gtk.gdk.Pixbuf"""
        defaulticon = os.path.join(datapath, "category_personal.png")
        usericon = os.path.join("/home",uname,".face")
        if not os.path.exists(usericon):
            return gtk.gdk.pixbuf_new_from_file_at_size(defaulticon, 32, 32)
        return gtk.gdk.pixbuf_new_from_file_at_size(usericon, 32,32)

def __vasm_test__():
    """ Set of tests to determine who should see this module in vasm
    must return True or False after all tests are conducted.
    """
    return os.geteuid() == 0

def __run__(parent=None):
    ui = UserManager(parent=parent)
    return ui.launch()