root/lib/octofuss/users/ui/__init__.py @ 438db0a339b00595809818139b828d3a8a9b6428

Revision 438db0a339b00595809818139b828d3a8a9b6428, 123.2 KB (checked in by iacopo <iacopo@…>, 6 years ago)

Edit user desktop

git-svn-id:  https://labs.truelite.it/svn/octofuss/trunk@375 5a101938-5c21-0410-9b5a-a83d6f3706a9

  • Property mode set to 100644
Line 
1#!/usr/bin/env python
2# -*- coding: latin-1 -*-
3#
4#  Copyright (C) 2006 Iacopo Pecchi <iacopo@truelite.it>
5#
6#  This program is free software; you can redistribute it and/or modify
7#  it under the terms of the GNU General Public License as published by
8#  the Free Software Foundation; either version 2 of the License, or
9#  (at your option) any later version.
10#
11import re
12import pygtk
13import gtk
14import gobject
15import csv
16import sys
17from gettext import gettext as _
18from octofuss import config
19from octofuss import art
20from octofuss import users
21from octofuss import log
22from octofuss.users import backendSingleton
23from octofuss.users import Backend
24from octofuss.users import LdapUser
25from octofuss.users import LdapGroup
26from octofuss.ldap.objectclass import ListAttribute
27
28
29#backend = Backend()
30backend = backendSingleton
31
32
33
34def getPermissions():
35        __groupListConf = {}
36        options = config.options("GROUPS")               
37        for op in options:
38                __groupListConf[op] = config.get("GROUPS",op)
39       
40        """
41        __groupList = {"scanner":"scanner",
42                "cdrom":"cdrom",
43                "floppy":"floppy",
44                "audio":"audio",
45                "usb key": "plugdev",
46                "internet":"internet",
47        }
48        """
49        #return __groupList
50        return __groupListConf
51
52
53
54
55
56class Management(gtk.VBox):
57        global backend
58        def __init__(self):
59                self.octofuss_name = _("User Management")
60
61                gtk.VBox.__init__(self,homogeneous=False)
62                # ------> Users <------
63                topHbox = gtk.HBox()
64                # Add frame that contains user list
65
66                self.userFrame = gtk.Frame("All users")
67                topHbox.pack_start(self.userFrame,padding=5)
68                userButtonBox = gtk.VButtonBox()
69                userButtonBox.set_layout(gtk.BUTTONBOX_START)
70
71                # Buttons
72                userButtonAdd = gtk.Button(stock="gtk-add")
73                userButtonRemove = gtk.Button(stock="gtk-remove")
74                userButtonModify =gtk.Button(stock="gtk-edit")
75                userButtonMass = gtk.Button(label=_("Mass Creation"))
76                userButtonProfiles = gtk.Button(_("Profili"))
77               
78
79                # Adding buttons
80                userButtonBox.pack_start(userButtonAdd,padding=0)
81                userButtonBox.pack_start(userButtonRemove,padding=0)
82                userButtonBox.pack_start(userButtonModify,padding=0)
83
84                userButtonBox.pack_start(userButtonMass,padding=0)
85                #userButtonBox.pack_start(userButtonProfiles,padding=10)
86
87
88                #Search users filed
89                labelEmpty = gtk.Label("")
90                labelSearch = gtk.Label(_("User search"))
91                entrySearch = gtk.Entry()
92                userButtonBox.pack_end(labelEmpty,padding=0) 
93
94
95                #Combo field, main group selection
96               
97                labelMainGroup = gtk.Label(_("Main group filter:"))
98
99                groupModel =  self.__create_group_model_icon()
100                comboMainGroup = gtk.ComboBox(groupModel)
101                cell = gtk.CellRendererText()
102                comboMainGroup.pack_start(cell)
103                comboMainGroup.add_attribute(cell, 'text', 0)
104                comboMainGroup.prepend_text("All groups")
105                comboMainGroup.set_active(0)
106
107               
108               
109                #userButtonBox.pack_end(labelSearch,padding=10)
110                #userButtonBox.pack_end(entrySearch)
111                vBoxTmp = gtk.VBox()
112                vBoxTmp.pack_start(userButtonBox,False,False,padding=5)
113                vBoxTmp.pack_end(comboMainGroup,False,False,padding=5)
114                vBoxTmp.pack_end(labelMainGroup,False,False,padding=5)
115
116                vBoxTmp.pack_end(entrySearch,False,False,padding=5)
117                vBoxTmp.pack_end(labelSearch,False,False,padding=5)
118
119
120               
121
122               
123                # Hbox inside the frame
124                topContentHbox = gtk.HBox()
125                topContentHbox.pack_end(vBoxTmp,False,False,padding=5)
126                # Add hbox to top frame
127                self.userFrame.add(topContentHbox)
128
129                #Scrolled window + icon list
130                hboxUserIcon = gtk.HBox()
131                swinUserIcon = gtk.ScrolledWindow()
132                swinUserIcon.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
133                swinUserIcon.set_shadow_type(gtk.SHADOW_ETCHED_IN)
134                hboxUserIcon.pack_start(swinUserIcon)
135                self.viewUserIcon = gtk.IconView()
136                self.viewUserIcon.set_selection_mode(gtk.SELECTION_MULTIPLE)
137                self.viewUserIcon.set_text_column(0)
138                self.viewUserIcon.set_pixbuf_column(1)
139                self.viewUserIcon.set_size_request(200, 150)
140                self.viewUserIcon.set_item_width(-1)
141                #self.viewUserIcon.set_columns(1)
142                #self.viewUserIcon.set_orientation(gtk.ORIENTATION_HORIZONTAL)
143
144                self.button_release_signal_id = self.viewUserIcon.connect("button-release-event", self.host_item_event, self.viewUserIcon)
145                modelUserIcon = self.__create_user_model_icon()
146               
147               
148                self.viewUserIcon.set_model(modelUserIcon)
149                #viewUserIcon.connect('selection-changed', self.on_host_select)
150                self.viewUserIcon.connect('item-activated',self.on_item_activated)
151                swinUserIcon.add(self.viewUserIcon)
152
153               
154
155                # Add scrolled window to hbox
156                #topContentHbox.pack_start(userSw)
157                topContentHbox.pack_start(hboxUserIcon)
158
159
160                # Connect events to buttons
161                #userButtonRemove.connect("clicked", self.user_remove_clicked, userTreeview)
162                userButtonRemove.connect("clicked", self.user_remove_clicked, self.viewUserIcon)
163
164                #userButtonAdd.connect("clicked", self.user_add_clicked, userTreeview)
165                userButtonAdd.connect("clicked", self.user_add_clicked, self.viewUserIcon)
166
167                userButtonModify.connect("clicked", self.on_button_modify_clicked, self.viewUserIcon)
168
169
170                #userButtonMass.connect("clicked", self.user_mass_clicked, userTreeview)
171                userButtonMass.connect("clicked", self.user_mass_clicked,self.viewUserIcon)
172
173                # Connect to insert event entry search
174                entrySearch.connect("changed",self.insert_text, entrySearch)
175
176                #Combo box main group event
177                comboMainGroup.connect("changed", self.main_group_selection, self.viewUserIcon)
178
179
180
181
182
183                # ------> Groups <------
184                bottomHbox = gtk.HBox()
185
186                # Add frame that contains group list
187                groupFrame = gtk.Frame("Group list")
188                bottomHbox.pack_start(groupFrame)
189                groupButtonBox = gtk.VButtonBox()
190                groupButtonBox.set_layout(gtk.BUTTONBOX_START)
191
192                # Buttons
193                groupButtonAdd = gtk.Button(stock="gtk-add")
194                groupButtonRemove = gtk.Button(stock="gtk-remove")
195                groupButtonManage = gtk.Button(_("Gestione gruppi"))
196
197                # Adding buttons
198                groupButtonBox.pack_start(groupButtonAdd,padding=10)
199                groupButtonBox.pack_start(groupButtonRemove,padding=10)
200                #groupButtonBox.pack_start(groupButtonManage,padding=10)
201
202                # Hbox inside the frame
203                bottomContentHbox = gtk.HBox()
204                bottomContentHbox.pack_end(groupButtonBox,False,False,padding=25)
205
206
207
208
209                # Add hbox to top frame
210                groupFrame.add(bottomContentHbox)
211
212                # Scrolled window + icon view group list
213
214                hboxGroupIcon = gtk.HBox()
215                swinGroupIcon = gtk.ScrolledWindow()
216                swinGroupIcon.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
217                swinGroupIcon.set_shadow_type(gtk.SHADOW_ETCHED_IN)
218                hboxGroupIcon.pack_start(swinGroupIcon)
219                self.viewGroupIcon = gtk.IconView()
220                self.viewGroupIcon.set_text_column(0)
221                self.viewGroupIcon.set_pixbuf_column(1)
222                self.viewGroupIcon.set_selection_mode(gtk.SELECTION_SINGLE)
223                self.viewGroupIcon.set_size_request(200, 150)
224                #model = gtk.ListStore(str, gtk.gdk.Pixbuf)
225                modelGroupIcon = self.__create_group_model_icon()
226               
227                #pix = art.get_pixbuf_from_name("workstation-up")
228                #pix = art.get_pixbuf_from_name("workstation")
229                #model.append([host.name,pix])
230               
231                self.viewGroupIcon.set_model(modelGroupIcon)
232                #viewGroupIcon.connect('selection-changed', self.on_host_select)
233                swinGroupIcon.add(self.viewGroupIcon)
234                self.viewGroupIcon.connect('selection-changed',self.on_group_item_activated, self.viewUserIcon)
235
236
237
238
239
240
241
242                # Add scrolled window to hbox
243                bottomContentHbox.pack_start(hboxGroupIcon)
244
245                # Connect events to buttons
246                groupButtonRemove.connect("clicked", self.group_remove_clicked, self.viewGroupIcon,self.viewUserIcon)
247                groupButtonAdd.connect("clicked", self.group_add_clicked,self.viewGroupIcon)
248
249
250
251
252                self.set_spacing(20)
253                self.pack_start(topHbox,padding=5)
254                self.pack_start(bottomHbox)
255
256
257                self.show_all()
258
259        def main_group_selection(self, comboBox, userView):
260                """ Show users which have the selected group as main group """
261                mainGroup = comboBox.get_active_text()
262                if comboBox.get_active() > 0:
263                       
264                        # Get all users with main group
265                        users = backend.ldapUsersByMainGroup(mainGroup)
266                        if users:
267                                model = self.__create_user_model_by_list(users)
268                                userView.set_model(model)
269                                userView.show_all()
270                        else:
271                                self.showDialog("No users in selected group!",gtk.MESSAGE_INFO)
272                elif comboBox.get_active() == 0:
273                        userView.set_model(self.__create_user_model_icon())               
274                        userView.show_all()
275               
276               
277        def host_item_event(self,obj,event, view):
278                # view is an iconView
279                try:
280                        selected = view.get_selected_items()
281                        # Subpath is the user under the right click, None if click is done on blank space.
282                        # Subpath is a list. Ex: (11,)
283                        subpath = view.get_path_at_pos(int(event.x), int(event.y))
284
285                        try:
286                                #subpath = self.view.get_path_at_pos(int(event.x), int(event.y))
287                                #path = (clusterPath, subpath[0])
288                                if event.button == 3:
289                                        userList = []
290                                             
291                                        if selected:
292                                                for i in selected:
293                                                        userList.append(i[0])
294                                        # User list is a list of path. Ex: [1,5,3,7]
295                                        # User list count users selected by mouse
296                                        if subpath != None:
297                                                # Multiple selection and right click
298                                                if userList:
299                                                        if subpath in selected:
300                                                                # Show a context menu with:
301                                                                # 1) Delete users
302                                                                # 2) Add users to group
303                                                                self.show_context_menu_user(event,selected,view)
304                                                        else:   
305                                                                view.unselect_all()
306                                                                view.select_path(subpath)
307                                                                selected = view.get_selected_items()
308                                                                self.show_context_menu_user(event,selected,view)
309                                                               
310                                                # No selection and right click
311                                                else:       
312                                                        view.select_path(subpath)
313                                                        self.show_context_menu_user(event,subpath,view)
314
315                        except Exception, e:
316                              log.debug(str(e)) 
317                except TypeError,e:
318                        log.debug(str(e))
319                except Exception, e:
320                        log.debug(str(e))
321
322
323        def host_item_event_group(self,obj,event, view, groupname):
324                # view is an iconView
325                try:
326                        selected = view.get_selected_items()
327                        # Subpath is the user under the right click, None if click is done on blank space.
328                        # Subpath is a list. Ex: (11,)
329                        subpath = view.get_path_at_pos(int(event.x), int(event.y))
330
331                        try:
332                                #subpath = self.view.get_path_at_pos(int(event.x), int(event.y))
333                                #path = (clusterPath, subpath[0])
334                                if event.button == 3:
335                                        userList = []
336                                        if selected:
337                                                for i in selected:
338                                                        userList.append(i[0])
339                                        # User list is a list of path. Ex: [1,5,3,7]
340                                        # User list count users selected by mouse
341                                        if subpath != None:
342                                                # Multiple selection and right click
343                                                if userList:
344                                                        if subpath in selected:
345                                                                # Show a context menu with:
346                                                                # 1) Delete users
347                                                                # 2) Add users to group
348                                                                self.show_context_menu_user_group(event,selected,view, groupname)
349
350                                                               
351                                                # No selection and right click
352                                                else:       
353                                                        self.show_context_menu_user_group(event,subpath,view, groupname)
354
355
356
357                                               
358                                           
359 
360                        except Exception, e:
361                                log.debug(str(e)) 
362                               
363                except TypeError,e:
364                        pass
365                except Exception, e:
366                         e
367
368
369
370        def show_context_menu_user(self,event,userList,view):
371                menu = gtk.Menu()
372                menufor = "User menu" 
373
374
375                subMenu = gtk.Menu()
376
377                # Get ldapGroup
378                #list = backend.getGroupsList()
379                list = backend.getLdapGroups()
380
381                showGroup = []
382                for gr in list:
383                        #num = int(backend.getGroup(gr).getGid().getValue())
384                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
385                        showGroup.append(gr)
386
387                for group in showGroup:
388                        it = gtk.MenuItem(_(group))
389                        it.connect("event", self.addToGroup, userList, group, view, menu)
390                        subMenu.append(it)
391               
392                if len(userList) == 1:
393                        item = gtk.MenuItem(menufor)
394                        item.set_sensitive(False)
395                        menu.append(item)
396                        # remote shell
397                        item = gtk.MenuItem(_("Delete user"))
398                        item.connect("activate", self.deleteUser, userList, view)
399                        menu.append(item)
400                        # install software
401                        item = gtk.MenuItem(_("Add user to group"))
402                        #item.connect("activate", self.addToGroup, userList, view )
403                        item.set_submenu(subMenu)
404                        menu.append(item)
405
406                        #Show user groups
407                        item = gtk.MenuItem(_("Show user groups"))
408                        item.connect("activate", self.show_user_groups, userList, view, self.viewGroupIcon)
409                        menu.append(item)
410
411                        #event_button = self.get_possible_button_event(event)
412                        menu.attach_to_widget(view, None)
413
414                        #menu.popup(None,None,None,3,0)
415                        menu.popup(None, None, None, 3,0)
416                        menu.show_all()
417                else:
418                        item = gtk.MenuItem(menufor)
419                        item.set_sensitive(False)
420                        menu.append(item)
421                        # remote shell
422                        item = gtk.MenuItem(_("Delete users"))
423                        item.connect("activate", self.deleteUser, userList, view)
424                        menu.append(item)
425                        # install software
426                        item = gtk.MenuItem(_("Add users to group"))
427                        #item.connect("activate", self.addToGroup, userList,view)
428                        item.set_submenu(subMenu)
429
430                        menu.append(item)
431
432                        #event_button = self.get_possible_button_event(event)
433                        menu.attach_to_widget(view, None)
434
435                        #menu.popup(None,None,None,3,0)
436                        menu.popup(None, None, None, 3,0)
437                        menu.show_all()
438
439        def show_user_groups(self, obj, userList, user_view, group_view):
440                """ Show groups of selected user """
441                model = user_view.get_model() 
442
443                if len(userList) == 1:
444                        if type(userList) ==  list:
445                                iter = model.get_iter_from_string(str(userList[0][0])) 
446                        else:
447                                iter = model.get_iter_from_string(str(userList[0])) 
448
449                        username = model.get_value(iter, 0)
450                        userGroups = backend.groupsByUser(username)
451                        string = username+"'s groups:\n"
452                        for group in userGroups:
453                                string = string +"- "+  group + "\n"
454                        self.showDialog(string,gtk.MESSAGE_INFO)
455                               
456
457                 
458
459        def show_context_menu_user_group(self,event,userList,view, groupname):
460                menu = gtk.Menu()
461                menufor = "User menu" 
462
463
464                subMenu = gtk.Menu()
465
466                # Get ldapGroup
467                #list = backend.getGroupsList()
468                list = backend.getLdapGroups()
469
470                showGroup = []
471                for gr in list:
472                        #num = int(backend.getGroup(gr).getGid().getValue())
473                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
474                        showGroup.append(gr)
475
476                for group in showGroup:
477                        it = gtk.MenuItem(_(group))
478                        it.connect("event", self.addToGroup, userList, group, view, menu)
479                        subMenu.append(it)
480
481
482                if len(userList) == 1:
483                        item = gtk.MenuItem(menufor)
484                        item.set_sensitive(False)
485                        menu.append(item)
486                        # remote shell
487                        item = gtk.MenuItem(_("Delete user"))
488                        item.connect("activate", self.deleteUser, userList, view)
489                        menu.append(item)
490                        # install software
491                        item = gtk.MenuItem(_("Add user to group"))
492                        #item.connect("activate", self.addToGroup, userList, view )
493                        item.set_submenu(subMenu)
494                        menu.append(item)
495
496                        # Remove from group
497                        item = gtk.MenuItem(_("Remove from "+groupname))
498                        item.connect("activate", self.removeFromGroup, userList,groupname, view )
499                        menu.append(item)
500
501                        #Show user groups
502                        item = gtk.MenuItem(_("Show user groups"))
503                        item.connect("activate", self.show_user_groups, userList, view, self.viewGroupIcon)
504                        menu.append(item)
505
506
507                        #event_button = self.get_possible_button_event(event)
508                        menu.attach_to_widget(view, None)
509
510                        #menu.popup(None,None,None,3,0)
511                        menu.popup(None, None, None, 3,0)
512                        menu.show_all()
513                else:
514                        item = gtk.MenuItem(menufor)
515                        item.set_sensitive(False)
516                        menu.append(item)
517                        # remote shell
518                        item = gtk.MenuItem(_("Delete users"))
519                        item.connect("activate", self.deleteUser, userList, view)
520                        menu.append(item)
521                        # install software
522                        item = gtk.MenuItem(_("Add users to group"))
523                        #item.connect("activate", self.addToGroup, userList,view)
524                        item.set_submenu(subMenu)
525
526                        menu.append(item)
527
528                        # Remove from group
529                        item = gtk.MenuItem(_("Remove from "+groupname))
530                        item.connect("activate", self.removeFromGroup, userList,groupname, view )
531                        menu.append(item)
532
533
534                        #event_button = self.get_possible_button_event(event)
535                        menu.attach_to_widget(view, None)
536
537                        #menu.popup(None,None,None,3,0)
538                        menu.popup(None, None, None, 3,0)
539                        menu.show_all()
540
541
542        def usersFromIconView(self, iconView):
543                """ Return a list of users shown on the iconView """
544                model = iconView.get_model()
545                users = []
546                iter = model.get_iter_root()
547                while iter != None:
548                        username = model.get_value(iter, 0)
549                        users.append(username)
550                        iter = model.iter_next(iter)
551                return users
552
553               
554
555                       
556       
557        def deleteUser(self,obj, userList, view):
558                """ Delete selected users """
559                # Get users from the model using the path passed as argument
560                model = view.get_model() 
561
562                if len(userList) == 1:
563                        if type(userList) ==  list:
564                                iter = model.get_iter_from_string(str(userList[0][0])) 
565                        else:
566                                iter = model.get_iter_from_string(str(userList[0])) 
567
568                        username = model.get_value(iter, 0)
569                        if username:
570                                if self.dialogRun(username):
571                                        backend.deleteLdapUser2(username)
572                                        view.set_model(self.__create_user_model_icon()) 
573                                        view.show()
574
575                elif len(userList) > 1:
576                        usernameList = []
577                        for user in userList:
578                                path = str(user[0])
579                                iter = model.get_iter_from_string(path)
580                                username = model.get_value(iter, 0)
581                                usernameList.append(username)
582                        if self.dialogRun("selected users"):
583
584                                for us in usernameList:
585                                        backend.deleteLdapUser2(us)
586                                view.set_model(self.__create_user_model_icon()) 
587                                view.set_item_width(-1)
588                                view.show()
589
590        def addToGroup(self, obj,event, userList, group, view, menu):
591                # Add user to ldap group
592                # Get group
593                # add user to group
594                # modifyLdapGroup
595                if not event.type == gtk.gdk.BUTTON_PRESS:
596                        return
597                menu.destroy()
598                from octofuss.error import GroupError
599                model = view.get_model() 
600                try:
601                        groupToModify = backend.getGroup(group)
602                        usernameList = []
603                        if type(userList) == list:
604                                for us in userList:
605                                        path = str(us[0])
606                                        iter = model.get_iter_from_string(path)
607                                        username = model.get_value(iter, 0)
608                                        if username:
609                                                usernameList.append(username)
610                                                groupToModify.addUser(username)
611                                backend.modifyLdapGroup(groupToModify)
612                                string = ""
613                                for us in usernameList:
614                                        string = string + " "+ us + ","
615                                string = string.strip(",")
616                                string = string + " "
617
618                                if len(usernameList) == 1:
619                                        self.showDialog("User "+string+"added to group "+group, gtk.MESSAGE_INFO)
620                                elif len(usernameList) > 1:
621                                        self.showDialog("Users "+string+"added to group "+group,gtk.MESSAGE_INFO)
622                        if type(userList) == tuple:
623                                for us in userList:
624                                        path = str(us)
625                                        iter = model.get_iter_from_string(path)
626                                        username = model.get_value(iter, 0)
627                                        if username:
628                                                usernameList.append(username)
629                                                groupToModify.addUser(username)
630                                backend.modifyLdapGroup(groupToModify)
631                                string = ""
632                                for us in usernameList:
633                                        string = string + " "+ us + ","
634                                string = string.strip(",")
635                                string = string + " "
636                                if len(usernameList) == 1:
637                                        self.showDialog("User "+string+"added to group "+group, gtk.MESSAGE_INFO)
638                                elif len(usernameList) > 1:
639                                        self.showDialog("Users "+string+"added to group "+group, gtk.MESSAGE_INFO)
640
641
642                except GroupError,e:
643                        log.debug(str(e))
644                except Exception,e:
645                        log.debug(str(e))
646
647
648        def removeFromGroup(self,obj, userList, group, view):
649                # Add user to ldap group
650                # Get group
651                # add user to group
652                # modifyLdapGroup
653                from octofuss.error import GroupError
654                model = view.get_model() 
655
656                try:
657                        groupToModify = backend.getGroup(group)
658                        usernameList = []
659                        if type(userList) == list:
660                                for us in userList:
661                                        path = str(us[0])
662                                        iter = model.get_iter_from_string(path)
663                                        username = model.get_value(iter, 0)
664                                        if username:
665                                                usernameList.append(username)
666                                                groupToModify.removeUser(username)
667                                backend.modifyLdapGroup(groupToModify)
668                                string = ""
669                                for us in usernameList:
670                                        string = string + " "+ us + ","
671                                string = string.strip(",")
672                                string = string + " "
673
674                                if len(usernameList) == 1:
675                                        self.showDialog("User "+string+"removed from group "+group, gtk.MESSAGE_INFO)
676                                elif len(usernameList) > 1:
677                                        self.showDialog("Users "+string+"removed from group "+group,gtk.MESSAGE_INFO)
678                        if type(userList) == tuple:
679                                for us in userList:
680                                        path = str(us)
681                                        iter = model.get_iter_from_string(path)
682                                        username = model.get_value(iter, 0)
683                                        if username:
684                                                usernameList.append(username)
685                                                groupToModify.removeUser(username)
686                                backend.modifyLdapGroup(groupToModify)
687                                string = ""
688                                for us in usernameList:
689                                        string = string + " "+ us + ","
690                                string = string.strip(",")
691                                string = string + " "
692                                if len(usernameList) == 1:
693                                        self.showDialog("User "+string+"removed from group "+group, gtk.MESSAGE_INFO)
694                                elif len(usernameList) > 1:
695                                        self.showDialog("Users "+string+"removed from group "+group, gtk.MESSAGE_INFO)
696                        users = backend.usersByGroup(group)
697                        newModel = self.__create_user_model_by_list(users)           
698                        view.set_model(newModel)
699                        view.set_item_width(-1)
700
701                        view.show()
702
703
704
705                except GroupError,e:
706                        pass
707                except Exception,e:
708                        log.debug(str(e))
709                       
710
711                           
712                   
713                   
714
715
716
717
718
719        def insert_text(self, editable, entry):
720                """ Insert text action event """
721                # Get users like inserted text
722                insertedString = editable.get_chars(0,-1)
723                if not insertedString:
724                        #list = backend.getUsersList()
725                        list = backend.getLdapUsers()
726                else:
727                        list = self.usersFromIconView(self.viewUserIcon)
728                showUser = []
729                subList = [] 
730                for us in list:
731                        #num = int(backend.getUser(us).getUidNumber().getValue())
732                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
733                        showUser.append(us)
734               
735                for u in showUser:
736                        if insertedString in u:
737                                subList.append(u)
738
739                # create list store
740                model = gtk.ListStore(
741                gobject.TYPE_STRING,
742                gtk.gdk.Pixbuf,
743                )
744                # add items
745                for item in subList:
746                        iter = model.append()
747                        type = ""
748                        if backend.isUnixUser(item):
749                                type=_("locale")
750                        else:
751                                if backend.isLdapUser(item):
752                                        type = _("network")       
753                        #User main group
754                        if backend.isEnabled(item):
755                                pix = art.get_pixbuf_from_name("stock_person")
756                        else:
757                                pix = art.get_pixbuf_from_name("user_disabled")
758
759                        mainGroupLabel = _("main group")
760                        mainGroupGid = backend.getUser(item).getGid().getValue()
761                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
762                        #tooltip = gtk.Tooltips()
763                        #tooltip.set_tip(item,mainGroupName)     
764
765                        model.set(iter,0,item,1, pix)
766
767                self.viewUserIcon.set_model(model) 
768                self.viewUserIcon.set_item_width(-1) 
769                self.viewUserIcon.show()
770
771
772
773
774        def get_users_icon_list(self):
775                hbox = gtk.HBox()
776                swin = gtk.ScrolledWindow()
777                swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
778                swin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
779                hbox.pack_start(swin)
780                view = gtk.IconView()
781                view.set_text_column(0)
782                view.set_pixbuf_column(1)
783                view.set_selection_mode(gtk.SELECTION_SINGLE)
784                view.set_size_request(200, 150)
785                #model = gtk.ListStore(str, gtk.gdk.Pixbuf)
786                model = self.__create_user_model_icon()
787               
788                #pix = art.get_pixbuf_from_name("workstation-up")
789                #pix = art.get_pixbuf_from_name("workstation")
790                #model.append([host.name,pix])
791               
792                view.set_model(model)
793                view.set_item_width(-1) 
794
795                #view.connect('selection-changed', self.on_host_select)
796                swin.add(view)
797                hbox.show_all()
798                return hbox
799
800
801        def get_groups_icon_list(self):
802                hbox = gtk.HBox()
803                swin = gtk.ScrolledWindow()
804                swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
805                swin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
806                hbox.pack_start(swin)
807                view = gtk.IconView()
808                view.set_text_column(0)
809                view.set_pixbuf_column(1)
810                view.set_selection_mode(gtk.SELECTION_SINGLE)
811                view.set_size_request(200, 150)
812                #model = gtk.ListStore(str, gtk.gdk.Pixbuf)
813                model = self.__create_group_model_icon()
814               
815                #pix = art.get_pixbuf_from_name("workstation-up")
816                #pix = art.get_pixbuf_from_name("workstation")
817                #model.append([host.name,pix])
818               
819                view.set_model(model)
820                view.set_item_width(-1) 
821
822                #view.connect('selection-changed', self.on_host_select)
823                swin.add(view)
824                hbox.show_all()
825                return hbox
826
827        def __create_user_model_icon(self):
828                """ Create model for list of users """
829                #list = backend.getUsersList()
830                list = backend.getLdapUsers()
831               
832                showUser = []
833                for us in list:
834                        #num = int(backend.getUser(us).getUidNumber().getValue())
835                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
836                        showUser.append(us)
837                               
838
839                # create list store
840                model = gtk.ListStore(
841                gobject.TYPE_STRING,
842                gtk.gdk.Pixbuf,
843                )
844                # add items
845                for item in showUser:
846                        iter = model.append()
847                        type = ""
848                        if backend.isUnixUser(item):
849                                type=_("locale")
850                        else:
851                                if backend.isLdapUser(item):
852                                        type = _("network")       
853                        #User main group
854                        if backend.isEnabled(item):
855                                pix = art.get_pixbuf_from_name("stock_person")
856                        else:
857                                pix = art.get_pixbuf_from_name("user_disabled")
858
859                        mainGroupLabel = _("main group")
860                        mainGroupGid = backend.getUser(item).getGid().getValue()
861                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
862                        #tooltip = gtk.Tooltips()
863                        #tooltip.set_tip(item,mainGroupName)     
864
865                        model.set(iter,0,item,1, pix)
866
867                return model
868
869        def __create_user_model_by_list(self, list):
870                """ Create model for list of users """
871                showUser = []
872                for us in list:
873                        #num = int(backend.getUser(us).getUidNumber().getValue())
874                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
875                        #Check ldap user
876                        if backend.isLdapUser(us):
877                                showUser.append(us)
878
879
880                # create list store
881                model = gtk.ListStore(
882                gobject.TYPE_STRING,
883                gtk.gdk.Pixbuf,
884                )
885                # add items
886                for item in showUser:
887                        iter = model.append()
888                        type = ""
889                        if backend.isUnixUser(item):
890                                type=_("locale")
891                        else:
892                                if backend.isLdapUser(item):
893                                        type = _("network")       
894                        #User main group
895                        if backend.isEnabled(item):
896                                pix = art.get_pixbuf_from_name("stock_person")
897                        else:
898                                pix = art.get_pixbuf_from_name("user_disabled")
899
900                        mainGroupLabel = _("main group")
901                        mainGroupGid = backend.getUser(item).getGid().getValue()
902                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
903                        #tooltip = gtk.Tooltips()
904                        #tooltip.set_tip(item,mainGroupName)     
905
906                        model.set(iter,0,item,1, pix)
907
908                return model
909
910
911
912        def __create_user_model(self):
913                """ Create model for list of users """
914                list = backend.getUsersList()
915
916                # create list store
917                model = gtk.ListStore(
918                gobject.TYPE_STRING,
919                gobject.TYPE_STRING,
920                gobject.TYPE_STRING,
921                )
922                # add items
923                for item in list:
924                        iter = model.append()
925                        type = ""
926                        if backend.isUnixUser(item):
927                                type=_("locale")
928                        else:
929                                if backend.isLdapUser(item):
930                                        type = _("network")       
931                        #User main group
932                        mainGroupLabel = _("main group")
933                        mainGroupGid = backend.getUser(item).getGid().getValue()
934                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
935                        model.set(iter, 0, item, 1, type,  2, mainGroupName )
936
937                return model
938
939        def __create_group_model_icon(self):
940                """ Create model for list of groups """
941                #list = backend.getGroupsList()
942                list = backend.getLdapGroups()
943                showGroup = []
944                for gr in list:
945                        #num = int(backend.getGroup(gr).getGid().getValue())
946                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
947                        showGroup.append(gr)
948
949                # create list store
950                model = gtk.ListStore(
951                gobject.TYPE_STRING,
952                gtk.gdk.Pixbuf,
953                )
954                # add items
955                for item in showGroup:
956                        iter = model.append()
957                        type = ""
958                        if backend.isUnixGroup(item):
959                                type=_("locale")
960                        else:
961                                if backend.isLdapGroup(item):
962                                        type = _("network")       
963                                else:
964                                        log.debug(_("Warning: problem identifying group type!"))
965                        pix = art.get_pixbuf_from_name("stock_people")
966
967                        model.set (iter, 0, item, 1, pix )
968                return model
969
970
971        def __create_group_model(self):
972                """ Create model for list of groups """
973                list = backend.getGroupsList()
974                # create list store
975                model = gtk.ListStore(
976                gobject.TYPE_STRING,
977                gobject.TYPE_STRING,
978                )
979                # add items
980                for item in list:
981                        iter = model.append()
982                        type = ""
983                        if backend.isUnixGroup(item):
984                                type=_("locale")
985                        else:
986                                if backend.isLdapGroup(item):
987                                        type = _("network")       
988                                else:
989                                        log.debug(_("Warning: problem identifying group type!"))
990                                       
991                        model.set (iter, 0, item, 1, type )
992                return model
993
994        def __add_columns_users(self, treeview):
995                model = treeview.get_model()
996                # Name column
997                renderer = gtk.CellRendererText()
998                renderer.set_data("column", 0)
999
1000                column = gtk.TreeViewColumn("Name", renderer, text=0)
1001                column.set_min_width(100)
1002                column.set_spacing(30)
1003                #column.set_sort_order(gtk.SORT_ASCENDING) 
1004                #column.set_sort_column_id(0)
1005
1006                treeview.append_column(column)
1007
1008                renderer = gtk.CellRendererText()
1009                renderer.set_data("column", 1)
1010
1011                column = gtk.TreeViewColumn(_("Type"), renderer, text=1)
1012                column.set_min_width(100)
1013
1014                #column.set_sort_order(gtk.SORT_ASCENDING) 
1015                #column.set_sort_column_id(0)
1016
1017                treeview.append_column(column)
1018                column.set_spacing(30)
1019               
1020                renderer = gtk.CellRendererText()
1021                renderer.set_data("column", 2)
1022
1023                column = gtk.TreeViewColumn(_("Main group"), renderer, text=2)
1024                column.set_min_width(100)
1025
1026                #column.set_sort_order(gtk.SORT_ASCENDING) 
1027                #column.set_sort_column_id(0)
1028
1029                treeview.append_column(column)
1030                column.set_spacing(30)
1031
1032
1033        def __add_columns_groups(self, treeview):
1034                model = treeview.get_model()
1035                # Name column
1036                renderer = gtk.CellRendererText()
1037                renderer.set_data("column", 0)
1038
1039                column = gtk.TreeViewColumn("Name", renderer, text=0)
1040                column.set_min_width(100)
1041
1042                column.set_spacing(30)
1043                #column.set_sort_order(gtk.SORT_ASCENDING) 
1044                #column.set_sort_column_id(0)
1045
1046                treeview.append_column(column)
1047
1048                renderer = gtk.CellRendererText()
1049                renderer.set_data("column", 1)
1050
1051                column = gtk.TreeViewColumn(_("Type"), renderer, text=1)
1052                column.set_min_width(100)
1053
1054                #column.set_sort_order(gtk.SORT_ASCENDING) 
1055                #column.set_sort_column_id(0)
1056
1057                treeview.append_column(column)
1058                column.set_spacing(30)
1059               
1060                """
1061                renderer = gtk.CellRendererText()
1062                renderer.set_data("column", 2)
1063
1064                column = gtk.TreeViewColumn(_("Main group"), renderer, text=1)
1065                #column.set_sort_order(gtk.SORT_ASCENDING) 
1066                #column.set_sort_column_id(0)
1067
1068                treeview.append_column(column)
1069                column.set_spacing(30)
1070                """
1071
1072
1073
1074        def user_remove_clicked(self, button, iconView):
1075                try:
1076                        model = iconView.get_model()
1077                        selectedItem = iconView.get_selected_items()
1078                       
1079                        values = []
1080                        for it in selectedItem:
1081                                iter = model.get_iter(it[0])
1082                                value = model.get_value(iter,0)
1083                                values.append(value)
1084                        if not values:
1085                                dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select an user!", buttons=gtk.BUTTONS_OK)   
1086                                dialog.show_all()
1087                                response = dialog.run()
1088                                dialog.destroy()   
1089                                return 
1090
1091                        #treeSelection = treeView.get_selection()
1092                        #selection = treeSelection.get_selected()
1093                        #iter = selection[1]
1094
1095                        #value = model.get_value(iter,0)
1096                except:
1097                        dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select an user!", buttons=gtk.BUTTONS_OK)   
1098                        dialog.show_all()
1099                        response = dialog.run()
1100                        dialog.destroy()   
1101                        return 
1102                try:
1103                        for value in values:
1104                                if self.dialogRun(value):
1105                                        if backend.isUnixUser(value):
1106                                                backend.deleteUnixUser2(value)       
1107                                        else:
1108                                                if backend.isLdapUser(value):
1109                                                         backend.deleteLdapUser2(value)       
1110                                                else:
1111                                                        log.debug(_("Invalid user"))
1112                                               
1113                        iconView.set_model(self.__create_user_model_icon()) 
1114                        iconView.set_item_width(-1) 
1115
1116                        iconView.show()
1117                except Exception, e:
1118                        log.debug(str(e))
1119                        self.showDialog(str(e),gtk.MESSAGE_ERROR)
1120
1121
1122
1123
1124        def user_add_clicked(self, button, iconView):
1125                """ Show new user GUI """
1126                newUserGUI = Newuser(iconView)
1127                newUserGUI.show_all()
1128
1129
1130        def user_mass_clicked(self, button, iconView):
1131                """ Show user mass creation Gui """
1132                newMassGUI = MassCreation(iconView)
1133                newMassGUI.show_all()
1134       
1135
1136        def group_remove_clicked(self, button, iconView, iconViewUser):
1137                try:
1138                        model = iconView.get_model()
1139                        selectedItem = iconView.get_selected_items()
1140                        iter = model.get_iter(selectedItem[0])
1141                        value = model.get_value(iter,0)
1142
1143                except:
1144                        dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select a group !", buttons=gtk.BUTTONS_OK) 
1145                        dialog.show_all()
1146                        response = dialog.run()
1147                        dialog.destroy()   
1148                        return 
1149                try:
1150                        if self.dialogRun(value):
1151                                if backend.isUnixGroup(value):
1152                                        backend.deleteUnixGroup2(value)       
1153                                else:
1154                                        if backend.isLdapGroup(value):
1155                                                backend.deleteLdapGroup2(value)       
1156
1157                                        else:
1158                                                log.debug(_("Invalid group"))
1159
1160                                iconView.set_model(self.__create_group_model_icon()) 
1161                                iconView.set_item_width(-1) 
1162
1163                                #iconViewUser.set_model(self.__create_user_model_icon())
1164                                iconView.show()
1165
1166                                iconViewUser.show()
1167                except Exception, e:
1168                        self.showDialog("Problem removing group\n"+str(e.args[0]),gtk.MESSAGE_ERROR)
1169                        return 
1170
1171        def group_add_clicked(self, button, iconView):
1172                """ Show new user GUI """
1173                newGroupGUI = Newgroup(iconView)
1174                newGroupGUI.show_all()
1175
1176
1177
1178
1179        def showDialog(self, message, type):
1180                dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
1181                dialog.run()
1182                dialog.destroy()     
1183
1184
1185        def dialogRun(self, entry): 
1186                self.dialog = gtk.Dialog('Sure?', None, 
1187                        gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, 
1188                        (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
1189                self.dialog.vbox.pack_start(gtk.Label(_("Do you want to remove "+entry+"?")))
1190                self.dialog.show_all()
1191                response = self.dialog.run()
1192                if response == gtk.RESPONSE_ACCEPT: 
1193                      self.dialog.destroy()
1194                      self.dialog.destroy()       
1195                      return True
1196                elif response == gtk.RESPONSE_REJECT: 
1197                      self.dialog.destroy()
1198                      self.dialog.destroy()                       
1199                      return False         
1200
1201
1202        def on_button_modify_clicked(self,button, iconView):
1203                try:
1204                        model = iconView.get_model()
1205                        selectedItem = iconView.get_selected_items()
1206                        if len(selectedItem) == 1:
1207                                iter = model.get_iter(selectedItem[0])
1208                                value = model.get_value(iter,0)
1209                                info = Info(value, iconView)
1210                                info.show()
1211                        elif len(selectedItem) > 1:
1212                                self.showDialog("You must select a single user!",gtk.MESSAGE_WARNING)
1213                        else:
1214                                self.showDialog("You must select a user!",gtk.MESSAGE_WARNING)
1215
1216                       
1217                except Exception, e:
1218                        self.showDialog("You must select a user!",gtk.MESSAGE_WARNING)
1219
1220        def on_item_activated(self,iconView, path):
1221                try:
1222                        model = iconView.get_model()
1223                        selectedItem = iconView.get_selected_items()
1224                        iter = model.get_iter(selectedItem[0])
1225                        value = model.get_value(iter,0)
1226                        info = Info(value, iconView)
1227                        info.show()
1228                except Exception, e:
1229                        log.debug(str(e)) 
1230                        self.showDialog("Invalid user!\n"+str(e),gtk.MESSAGE_ERROR)
1231                       
1232
1233
1234        def on_group_item_activated(self,groupView,userView):
1235                """ Selecting a group in groupView it shows users in that group """
1236                try:
1237                        model = groupView.get_model()
1238
1239                        selectedItem = groupView.get_selected_items()
1240                        iter = model.get_iter(selectedItem[0])
1241                        groupname = model.get_value(iter,0)
1242                        # Select all user in groupname
1243                        users = backend.usersByGroup(groupname)
1244                        if not users:
1245                                userView.hide()
1246                                #self.userFrame.remove(child)
1247                                self.userFrame.set_label("No users in group \""+groupname+"\"")
1248                                #label = gtk.Label("No users in selected group")
1249                                #self.userFrame.add(label)
1250                                #label.show()
1251                               
1252                        else:
1253                                newModel = self.__create_user_model_by_list(users)           
1254                                userView.disconnect(self.button_release_signal_id)
1255                                self.button_release_signal_id = userView.connect("button-release-event", self.host_item_event_group, userView, groupname)
1256
1257
1258                                self.userFrame.set_label("Users in group \""+groupname+"\"")
1259                                userView.set_model(newModel)
1260                                userView.set_item_width(-1) 
1261
1262                                userView.show()
1263
1264                except IndexError, e:
1265                        newModel = self.__create_user_model_icon()           
1266                        userView.disconnect(self.button_release_signal_id)
1267
1268                        self.button_release_signal_id = userView.connect("button-release-event", self.host_item_event, userView)
1269                        self.userFrame.set_label("All users")
1270
1271                        userView.set_model(newModel)
1272                        userView.set_item_width(-1) 
1273
1274                        userView.show()
1275
1276                       
1277                except Exception, e:
1278                        log.debug(str(e))
1279                       
1280
1281               
1282
1283
1284
1285
1286        def show_context_menu(self,user):
1287                menu = gtk.Menu()
1288                item = gtk.MenuItem("")
1289                item.set_sensitive(False)
1290                #menu.append(item)
1291                # remote shell
1292                item = gtk.MenuItem(_("Details"))
1293                item.connect("activate", self.user_details,user )
1294                menu.append(item)
1295
1296                #event_button = self.get_possible_button_event(event)
1297                #menu.attach_to_widget(self.cluster_tree, None)
1298
1299                #menu.popup(None,None,None,3,0)
1300                menu.popup(None, None, None, 3,0)
1301                menu.show_all()
1302
1303
1304        def user_details(self,us,user ):
1305                """ Show a window that show user infos and lets the admin to change the password """
1306                #Never userd
1307                try:
1308                        info = Info()
1309                        info.show()
1310                except Exception,e:
1311                        log.debug(str(e))
1312
1313class Info(gtk.Window):
1314        global backend
1315        __groupList = getPermissions()
1316
1317        def __init__(self, username, view=None):
1318                gtk.Window.__init__(self)
1319                if backend.isUser(username):
1320                        user = backend.getUser(username)
1321                        mainFrame = gtk.Frame(_("User details"))
1322                        vboxMain = gtk.VBox()
1323                        mainTable = gtk.Table(8,2,False)
1324
1325                        # Details title
1326                        fieldLabel = gtk.Label(_("<b><big>Details</big></b>"))
1327                        fieldLabel.set_alignment(0.1,0.3)
1328                        fieldLabel.set_use_markup(True)
1329                       
1330                        mainTable.attach(fieldLabel,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1331
1332           
1333                        label1 = gtk.Label(_("Real name:"))
1334                        labelRealname = gtk.Label(user.getAttribute("posixAccount","gecos").getValue())
1335                        mainTable.attach(label1,0,1,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
1336                        mainTable.attach(labelRealname,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1337
1338
1339
1340
1341                        label2 = gtk.Label(_("Username:"))
1342                        labelUsername = gtk.Label(username)               
1343                        mainTable.attach(label2,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
1344                        mainTable.attach(labelUsername,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1345
1346
1347                        label3 = gtk.Label(_("Home:"))
1348                        try:
1349                                home = user.getHome().getValue()
1350                        except:
1351                                home = "None"
1352                        labelHome = gtk.Label(home)               
1353                        mainTable.attach(label3,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
1354                        mainTable.attach(labelHome,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1355
1356
1357
1358
1359
1360                       
1361
1362                        labelMainGroup= gtk.Label(_("Main group:"))
1363                        mainGroupName = backend.getGroupByGid(user.getGid().getValue()).getName().getValue()
1364                        labelValue = gtk.Label(mainGroupName)               
1365
1366                        # Main group
1367                        mainTable.attach(labelMainGroup,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1368                        mainTable.attach(labelValue,1,2,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1369
1370
1371                        labelPassword = gtk.Label(_("Change Password:"))
1372                        buttonPassword = gtk.Button("Change Password")               
1373                        buttonPassword.connect("clicked", self.change_password, username)
1374
1375                        buttonEnable = gtk.Button("Enable User")               
1376                        buttonEnable.connect("clicked", self.enable_user, username,view)
1377
1378                        buttonDisable = gtk.Button("DisableUser")               
1379                        buttonDisable.connect("clicked", self.disable_user, username, view)
1380                        enabled = backend.isEnabled(username)
1381                       
1382                        if enabled != None:
1383                                if enabled:
1384                                        mainTable.attach(buttonDisable,0,1,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1385                                else:
1386                                        mainTable.attach(buttonEnable,0,1,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1387                        else:
1388                                log.debug("Is enabled returned None, problems")
1389                                mainTable.attach(buttonDisable,0,1,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1390
1391                                       
1392
1393                                       
1394                                             
1395
1396                        mainTable.attach(buttonPassword,1,2,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1397
1398
1399
1400                        # Privileges title
1401                        groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
1402                        groupLabel.set_alignment(0.1,0.3)
1403                        groupLabel.set_use_markup(True)
1404               
1405
1406                        mainTable.attach(groupLabel,0,1,6,7,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1407
1408
1409
1410                        #User unix groups
1411                       
1412                        userGroups = backend.groupsByUser(username)
1413                        #print "user groups: "+str(userGroups)
1414
1415                        # We need to load user group (privileges) and set active checkbox
1416
1417                        groups = backend.getUnixGroups()
1418                        groupsToShow = {}
1419                       
1420                        for group in self.__groupList:
1421                                if self.__groupList[group] in groups:
1422                                        # A dictionary of checkButtons
1423                                        groupsToShow[group] = gtk.CheckButton(group)
1424                                        if self.__groupList[group] in userGroups:
1425                                                groupsToShow[group].set_active(True)
1426                                                #groupsToShow[group].set_alignment(0,0)
1427                                       
1428                               
1429                        l = len(groupsToShow)
1430                       
1431                        if l%2 == 0: 
1432                                groupTable = gtk.Table(l/2,2,False)
1433                                groupTable.set_homogeneous(False)
1434                                list = groupsToShow.keys()
1435                                for i in range(l):
1436                                        if i%2 == 0: # SX
1437                                                groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,yoptions=gtk.FILL,xpadding=50,ypadding=5)
1438
1439         
1440                                        else: # DX
1441                                                groupTable.attach(groupsToShow[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
1442                                 
1443                        else:
1444                                groupTable = gtk.Table(l/2+1,2,False)
1445                                groupTable.set_homogeneous(False)
1446
1447                                list = groupsToShow.keys()
1448                                for i in range(l):
1449                                        if i%2 == 0: # SX
1450                                                groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=50,yoptions=gtk.FILL,ypadding=5)
1451
1452         
1453                                        else: # DX
1454                                                groupTable.attach(groupsToShow[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
1455
1456
1457               
1458
1459
1460
1461
1462                        vboxMain.pack_start(mainTable)
1463
1464                        vboxMain.pack_start(groupTable)
1465
1466                        mainFrame.add(vboxMain)
1467
1468                        # Save-cancel buttons
1469                        v = gtk.VBox()
1470                        v.pack_start(mainFrame)
1471                        save = gtk.Button(stock="gtk-save")
1472                        save.connect("clicked", self.__save_clicked, groupsToShow, username )
1473
1474                        cancel = gtk.Button(stock="gtk-cancel")
1475                        cancel.connect("clicked",self.__cancel_clicked,self )
1476                        hboxButton = gtk.HBox()
1477                        hboxButton.pack_end(save,False,False)
1478                        hboxButton.pack_end(cancel,False,False)
1479                        v.pack_start(hboxButton)
1480
1481                        del(userGroups)
1482
1483                        self.add(v)
1484                        self.show_all()
1485
1486
1487
1488                               
1489                else:
1490                        self.showDialog("Not a valid user!",gtk.MESSAGE_ERROR)
1491
1492        def __create_user_model_icon(self):
1493                """ Create model for list of users """
1494                #list = backend.getUsersList()
1495                list = backend.getLdapUsers()
1496                showUser = []
1497                for us in list:
1498                        #num = int(backend.getUser(us).getUidNumber().getValue())
1499                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
1500                        showUser.append(us)
1501
1502
1503                # create list store
1504                model = gtk.ListStore(
1505                gobject.TYPE_STRING,
1506                gtk.gdk.Pixbuf,
1507                )
1508                # add items
1509                for item in showUser:
1510                        iter = model.append()
1511                        type = ""
1512                        if backend.isUnixUser(item):
1513                                type=_("locale")
1514                        else:
1515                                if backend.isLdapUser(item):
1516                                        type = _("network")       
1517                        #User main group
1518                        if backend.isEnabled(item):
1519                                pix = art.get_pixbuf_from_name("stock_person")
1520                        else:
1521                                pix = art.get_pixbuf_from_name("user_disabled")
1522                        mainGroupLabel = _("main group")
1523                        mainGroupGid = backend.getUser(item).getGid().getValue()
1524                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
1525                        #tooltip = gtk.Tooltips()
1526                        #tooltip.set_tip(item,mainGroupName)     
1527
1528                        model.set(iter,0,item,1, pix)
1529
1530                return model
1531
1532
1533
1534        def enable_user(self, button, username, view):
1535                """ Set username's account enabled """
1536                backend.enableLdapUser(username)
1537                self.destroy()
1538                view.set_model(self.__create_user_model_icon()) 
1539                view.show()
1540
1541
1542
1543
1544
1545        def disable_user(self, button, username, view):
1546                """ Set username's account disabled """
1547                backend.disableLdapUser(username)
1548                self.destroy()
1549                view.set_model(self.__create_user_model_icon()) 
1550                view.show()
1551
1552
1553
1554        def __save_clicked(self, button, groupsToShow, username):
1555                """ Update user unix groups (privileges) """
1556                __groupList = getPermissions()
1557                groupsEmpty = []
1558                for g in groupsToShow:
1559                        value = False
1560                        value = groupsToShow[g].get_active()
1561                        if value:
1562                                groupsEmpty.append(__groupList[g]) 
1563                #print groups
1564                backend.updateUserLocalGroups(username,groupsEmpty)
1565                del(groupsEmpty)
1566                self.destroy()
1567
1568        def change_password(self, button, username):
1569                """ Show a dialog to change password """
1570                winPass = gtk.Window()
1571                table = gtk.Table(2,2)
1572               
1573                lab0 = gtk.Label(_("<b><big>Change Password</big></b>"))
1574                lab0.set_alignment(0.1,0.3)
1575                lab0.set_use_markup(True)
1576                lab1 = gtk.Label("Password")
1577                lab2 = gtk.Label("Confirm")
1578                en1 = gtk.Entry()
1579                en1.set_visibility(False)
1580                en2 = gtk.Entry()
1581                en2.set_visibility(False)
1582
1583                table.attach(lab0,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1584
1585                table.attach(lab1,0,1,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1586                table.attach(en1,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1587                table.attach(lab2,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1588                table.attach(en2,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1589
1590                v = gtk.VBox()
1591                v.pack_start(table)
1592                save = gtk.Button(stock="gtk-save")
1593                save.connect("clicked", self.__change_ldap_password, en1,en2,username, winPass)
1594
1595                cancel = gtk.Button(stock="gtk-cancel")
1596                cancel.connect("clicked",self.__cancel_clicked, winPass)
1597                hboxButton = gtk.HBox()
1598                hboxButton.pack_end(save,False,False)
1599                hboxButton.pack_end(cancel,False,False)
1600                v.pack_start(hboxButton)
1601                winPass.add(v)
1602                winPass.show_all()
1603
1604
1605        def __cancel_clicked(self, button, window):
1606                window.destroy()
1607
1608        def __change_ldap_password(self, button, entry1,entry2, username, winPass):
1609                """ Change password of username user """
1610                try:
1611                        pass1 = entry1.get_text()
1612                        pass2 = entry2.get_text()
1613                        if pass1 and pass2:
1614                                if pass1 == pass2:
1615                                        #Change password
1616                                        backend.changeLdapPassword(username,pass1)
1617                                        winPass.destroy()
1618                                else:
1619                                        self.showDialog("Problem: passwords do not match!",gtk.MESSAGE_ERROR)
1620                               
1621                        else:
1622                                self.showDialog("Problem: password must be not null!", gtk.MESSAGE_ERROR)
1623                except Exception, e:
1624                        self.showDialog("Problem: "+str(e),gtk.MESSAGE_ERROR)
1625       
1626
1627
1628               
1629       
1630               
1631
1632
1633
1634               
1635
1636        def showDialog(self, message, type):
1637                dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
1638                dialog.run()
1639                dialog.destroy()     
1640
1641
1642             
1643               
1644               
1645
1646
1647
1648
1649class Newuser(gtk.Window):
1650        global backend
1651        __parent = None
1652        #backend = backend
1653        # Groups of user, the value is the name of group, the key is the description showed in
1654        # user creation gui. It searchs for group in system group list.
1655
1656        __groupList = getPermissions()
1657
1658        def __init__(self, iconView):
1659                gtk.Window.__init__(self)
1660                self.__parent = iconView
1661                mainFrame = gtk.Frame(_("New User"))
1662                vboxMain = gtk.VBox()
1663                mainTable = gtk.Table(8,2,False)
1664
1665                fieldLabel = gtk.Label(_("<b><big>Base preferences</big></b>"))
1666                fieldLabel.set_alignment(0.1,0.3)
1667                fieldLabel.set_use_markup(True)
1668
1669                mainTable.attach(fieldLabel,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=20,xpadding=10)
1670
1671
1672         
1673                labelRealname = gtk.Label(_("Real name"))
1674                al1 = gtk.Alignment(xalign=0.5)
1675                al1.add(labelRealname)
1676                entryRealname = gtk.Entry()               
1677                mainTable.attach(al1,0,1,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=20,xpadding=10)
1678                mainTable.attach(entryRealname,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1679               
1680
1681
1682
1683                labelUsername = gtk.Label(_("Username"))
1684                al2 = gtk.Alignment(xalign=0.5)
1685                al2.add(labelUsername)
1686
1687                labelUsername.set_justify(gtk.JUSTIFY_LEFT)
1688
1689                entryUsername = gtk.Entry()               
1690                mainTable.attach(al2,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1691                mainTable.attach(entryUsername,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1692
1693
1694
1695
1696                labelPassword = gtk.Label(_("Password"))
1697                al3 = gtk.Alignment(xalign=0.5)
1698                al3.add(labelPassword)
1699               
1700
1701                labelPassword.set_justify(gtk.JUSTIFY_LEFT)
1702
1703                entryPassword = gtk.Entry()               
1704                entryPassword.set_visibility(False)
1705                mainTable.attach(al3,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1706                mainTable.attach(entryPassword,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1707
1708
1709 
1710
1711
1712                labelPasswordConferma = gtk.Label(_("Confirm"))
1713                al4 = gtk.Alignment(xalign=0.5)
1714                al4.add(labelPasswordConferma)
1715
1716                entryPasswordConferma = gtk.Entry()
1717                entryPasswordConferma.set_visibility(False)
1718
1719                mainTable.attach(al4,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1720                mainTable.attach(entryPasswordConferma,1,2,4,5,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1721
1722
1723                #Home directory selection
1724                labelHome = gtk.Label(_("Home directory"))
1725                al5 = gtk.Alignment(xalign=0.5)
1726                al5.add(labelHome)
1727
1728                entryHome= gtk.Entry()
1729
1730                mainTable.attach(al5,0,1,5,6,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1731                mainTable.attach(entryHome,1,2,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1732
1733
1734                # Maing group selection comboBox
1735                groupComboLabel = gtk.Label(_("Main group"))
1736                al6 = gtk.Alignment(xalign=0.5)
1737                al6.add(groupComboLabel)
1738
1739                groupComboBox = gtk.ComboBox()
1740                groupComboBox.set_model(self.__create_group_model_icon())
1741                cell = gtk.CellRendererText()
1742                groupComboBox.pack_start(cell)
1743                groupComboBox.add_attribute(cell, 'text', 0)
1744                groupComboBox.prepend_text("Default Group")
1745                groupComboBox.set_active(0)
1746
1747                mainTable.attach(al6,0,1,6,7,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1748                mainTable.attach(groupComboBox,1,2,6,7,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1749
1750                groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
1751                groupLabel.set_alignment(0.1,0.3)
1752                groupLabel.set_use_markup(True)
1753
1754                mainTable.attach(groupLabel,0,1,7,8,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1755
1756
1757
1758
1759
1760
1761                groups = backend.getUnixGroups()
1762                groupsToShow = {}
1763                 
1764                for group in self.__groupList:
1765                        if self.__groupList[group] in groups:
1766                                # A dictionary of checkButtons
1767                                groupsToShow[group] = gtk.CheckButton(group)
1768                                #groupsToShow[group].set_alignment(0,0)
1769                               
1770                       
1771                l = len(groupsToShow)
1772               
1773                if l%2 == 0: 
1774                        groupTable = gtk.Table(l/2,2,False)
1775                        groupTable.set_homogeneous(False)
1776                        list = groupsToShow.keys()
1777                        for i in range(l):
1778                                if i%2 == 0: # SX
1779                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=50,ypadding=5)
1780
1781 
1782                                else: # DX
1783                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i)
1784                         
1785                else:
1786                        groupTable = gtk.Table(l/2+1,2,False)
1787                        groupTable.set_homogeneous(False)
1788
1789                        list = groupsToShow.keys()
1790                        for i in range(l):
1791                                if i%2 == 0: # SX
1792                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=50,ypadding=5)
1793
1794 
1795                                else: # DX
1796                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i)
1797                         
1798
1799               
1800
1801                       
1802               
1803                """
1804                fieldLabel = gtk.Label(_("<b><big>Base preferences</big></b>"))
1805                fieldLabel.set_alignment(0.1,0.3)
1806                fieldLabel.set_use_markup(True)
1807
1808
1809               
1810                groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
1811                groupLabel.set_alignment(0.1,0.3)
1812                groupLabel.set_use_markup(True)
1813                """
1814                #vboxMain.pack_start(fieldLabel)
1815
1816                vboxMain.pack_start(mainTable)
1817                #vboxMain.pack_start(groupLabel,padding=10)
1818
1819                vboxMain.pack_start(groupTable)
1820
1821
1822                mainFrame.add(vboxMain)
1823                v = gtk.VBox()
1824                v.pack_start(mainFrame)
1825                save = gtk.Button(stock="gtk-add")
1826                save.connect("clicked", self.__save_clicked,entryRealname,entryUsername,entryPassword,entryPasswordConferma,entryHome,groupsToShow,self.__groupList, groupComboBox)
1827
1828                cancel = gtk.Button(stock="gtk-cancel")
1829                cancel.connect("clicked",self.__cancel_clicked)
1830                hboxButton = gtk.HBox()
1831                hboxButton.pack_end(save,False,False)
1832                hboxButton.pack_end(cancel,False,False)
1833                v.pack_start(hboxButton)
1834
1835
1836                self.add(v)
1837               
1838
1839        def __save_clicked(self,button, entryRealname,entryUsername,entryPassword,entryPasswordConferma,entryHome,groupsToShow, groupList,groupComboBox):
1840                """ Check fields and add the user to Ldap Server """ 
1841                global backend
1842               
1843                try:
1844                        if not entryRealname.get_text():
1845                                self.showDialog(_("Real name must be not empty"),gtk.MESSAGE_ERROR)
1846                                return
1847                               
1848                        if not entryUsername.get_text():
1849                                self.showDialog(_("Username must be not empty"),gtk.MESSAGE_ERROR)
1850                                return
1851                               
1852                        if not entryPassword.get_text():
1853                                self.showDialog(_("Password must be not empty"),gtk.MESSAGE_ERROR)
1854                                return
1855
1856                        if not entryPasswordConferma.get_text():
1857                                self.showDialog(_("Confirm password must be not empty"),gtk.MESSAGE_ERROR)
1858                                return
1859
1860                        if not entryPassword.get_text() == entryPasswordConferma.get_text():
1861                                self.showDialog(_("Password fields does not match."),gtk.MESSAGE_ERROR)
1862                                return
1863
1864                        active = []
1865                        for group in groupsToShow:
1866                                if groupsToShow[group].get_active():
1867                                        active.append(groupList[group])
1868
1869                        # Selection of main group
1870                        model = groupComboBox.get_model()
1871                        activeG = groupComboBox.get_active()
1872                        if activeG < 0:
1873                                mainGroup = None
1874                        else:
1875                                mainGroup = model[activeG][0]
1876                        n = LdapUser(['inetOrgPerson','shadowAccount','sambaSamAccount'])
1877                        n.setAttribute("cn", entryUsername.get_text())
1878                        n.setAttribute("sn", entryUsername.get_text())
1879                        uid = backend.getNewLdapID()
1880                        if not mainGroup:
1881                                gid = backend.getGroup("Domain Users").getGid().getValue()       
1882                        else:
1883                                if mainGroup == "Default Group":
1884                                        gid = backend.getGroup("Domain Users").getGid().getValue()
1885                                       
1886                                else:
1887                                        gid = backend.getGroup(mainGroup).getGid().getValue()
1888
1889                        attrObjectClass = ListAttribute("objectClass")
1890                        attrObjectClass.setValue(['posixAccount','top','inetOrgPerson','shadowAccount','sambaSamAccount'])
1891                        n.setAttribute("objectClass",attrObjectClass)
1892                        n.setAttribute("gidNumber", str(gid))
1893                        #n.setAttribute("homeDirectory","/home/"+entryUsername.get_text())
1894                        homePrefix = "/home/"
1895                        lenHome = len(homePrefix)
1896                        directoryHome = entryHome.get_text().replace(" ","")                               
1897
1898
1899           
1900                        #Check home directory
1901                        dir = entryHome.get_text().strip()
1902
1903                        if not dir:
1904                                n.setAttribute("homeDirectory","/home/"+entryUsername.get_text())
1905                        else:
1906                                if not self.__checkHomeDirectory(dir):
1907                                        self.showDialog(_("Invalid characters in home directory field!"),gtk.MESSAGE_ERROR)
1908                                        return
1909                                log.debug("Dirrecotry: "+dir)
1910                                n.setAttribute("homeDirectory",dir) 
1911                               
1912                           
1913                        n.setAttribute("loginShell","/bin/sh")
1914
1915                        n.setAttribute("uid", entryUsername.get_text())
1916                        n.setAttribute("uidNumber", str(uid))
1917                        n.setAttribute("gecos", entryRealname.get_text())
1918                        n.setAttribute("userPassword", entryPassword.get_text())
1919                        n.setAttribute("sambaSID", backend.getUserSID(uid))
1920                       
1921                        #New attributes
1922                        n.setAttribute("sambaLogonTime","0")
1923                        n.setAttribute("sambaLogoffTime", "0")
1924                        n.setAttribute("sambaKickoffTime","0")
1925                        n.setAttribute("sambaPwdCanChange","0")
1926                        n.setAttribute("sambaHomeDrive","H:")
1927                        n.setAttribute("sambaPwdCanChange","0")
1928                        n.setAttribute("sambaHomeDrive","H:")
1929                        n.setAttribute("sambaPrimaryGroupSID",backend.getGroupSID(str(gid)))
1930                        n.setAttribute("sambaAcctFlags", "[UX         ]")
1931
1932
1933
1934
1935
1936
1937
1938                        backend.addLdapUser2(n,active)
1939                     
1940                        self.__parent.set_model(self.__create_user_model_icon()) 
1941                        self.__parent.show()
1942                        backend = Backend()
1943
1944                        self.destroy() 
1945
1946
1947                                       
1948                               
1949                except Exception, inst:
1950                        username = entryUsername.get_text()
1951                        if backend.isUser(username):
1952                                if backend.getUser(username).getGid().getValue() == str(uid): 
1953                                        backend.deleteLdapUser2(username)
1954
1955                        self.showDialog(_("Problems adding new user")+"\n"+str(inst),gtk.MESSAGE_ERROR)
1956
1957        def __checkHomeDirectory(self,dir):
1958                """ Do checks on dir string """
1959                import re
1960                import os
1961           
1962                pat1 = re.compile(r"/home(/[a-zA-z0-9]+)+(/)?$")
1963                res1 = pat1.match(dir)       
1964                if res1 == None:
1965                        return False
1966
1967                sp = os.path.split(dir)[0]
1968                if dir.endswith("/"):
1969                        if not os.path.isdir(os.path.dirname(sp)):
1970                                return False
1971                else:
1972                        if not os.path.isdir(sp):
1973                                return False
1974           
1975                return True
1976
1977
1978
1979        def __create_user_model(self):
1980                """ Create model for list of users """
1981                list = backend.getUsersList()
1982
1983                # create list store
1984                model = gtk.ListStore(
1985                gobject.TYPE_STRING,
1986                gobject.TYPE_STRING,
1987                gobject.TYPE_STRING,
1988                )
1989                # add items
1990                for item in list:
1991                        iter = model.append()
1992                        type = ""
1993                        if backend.isUnixUser(item):
1994                                type=_("locale")
1995                        else:
1996                                if backend.isLdapUser(item):
1997                                        type = _("network")       
1998                        #User main group
1999                        mainGroupLabel = _("main group")
2000                        mainGroupGid = backend.getUser(item).getGid().getValue()
2001                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2002                        model.set(iter, 0, item, 1, type,  2, mainGroupName )
2003
2004                return model
2005
2006        def __create_user_model_icon(self):
2007                """ Create model for list of users """
2008                #list = backend.getUsersList()
2009                list = backend.getLdapUsers()
2010                showUser = []
2011                for us in list:
2012                        #num = int(backend.getUser(us).getUidNumber().getValue())
2013                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2014                        showUser.append(us)
2015
2016
2017                # create list store
2018                model = gtk.ListStore(
2019                gobject.TYPE_STRING,
2020                gtk.gdk.Pixbuf,
2021                )
2022                # add items
2023                for item in showUser:
2024                        iter = model.append()
2025                        type = ""
2026                        if backend.isUnixUser(item):
2027                                type=_("locale")
2028                        else:
2029                                if backend.isLdapUser(item):
2030                                        type = _("network")       
2031                        #User main group
2032                        if backend.isEnabled(item):
2033                                pix = art.get_pixbuf_from_name("stock_person")
2034                        else:
2035                                pix = art.get_pixbuf_from_name("user_disabled")
2036                        mainGroupLabel = _("main group")
2037                        mainGroupGid = backend.getUser(item).getGid().getValue()
2038                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2039                        #tooltip = gtk.Tooltips()
2040                        #tooltip.set_tip(item,mainGroupName)     
2041
2042                        model.set(iter,0,item,1, pix)
2043
2044                return model
2045
2046
2047
2048
2049
2050        def __create_group_model(self):
2051                """ Create model for list of groups """
2052                list = backend.getGroupsList()
2053                # create list store
2054                model = gtk.ListStore(
2055                gobject.TYPE_STRING,
2056                gobject.TYPE_STRING,
2057                )
2058                # add items
2059                for item in list:
2060                        iter = model.append()
2061                        type = ""
2062                        if backend.isUnixGroup(item):
2063                                type=_("locale")
2064                        else:
2065                                if backend.isLdapGroup(item):
2066                                        type = _("network")       
2067                                else:
2068                                        log.debug(_("Warning: problem identifying group type!"))
2069                        model.set (iter, 0, item, 1, type )
2070                return model
2071
2072        def __create_group_model_icon(self):
2073                """ Create model for list of groups """
2074                #list = backend.getGroupsList()
2075                list = backend.getLdapGroups()
2076
2077                showGroup = []
2078                for gr in list:
2079                        #num = int(backend.getGroup(gr).getGid().getValue())
2080                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2081                        showGroup.append(gr)
2082
2083
2084                # create list store
2085                model = gtk.ListStore(
2086                gobject.TYPE_STRING,
2087                gtk.gdk.Pixbuf,
2088                )
2089                # add items
2090                for item in showGroup:
2091                        iter = model.append()
2092                        type = ""
2093                        if backend.isUnixGroup(item):
2094                                type=_("locale")
2095                        else:
2096                                if backend.isLdapGroup(item):
2097                                        type = _("network")       
2098                                else:
2099                                        log.debug(_("Warning: problem identifying group type!"))
2100                        pix = art.get_pixbuf_from_name("stock_people")
2101
2102                        model.set (iter, 0, item, 1, pix )
2103                return model
2104
2105
2106
2107       
2108
2109        def __cancel_clicked(self, button):
2110                self.destroy()
2111
2112        def showDialog(self, message, type):
2113               dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
2114               dialog.run()
2115               dialog.destroy()     
2116
2117
2118class MassCreation(gtk.Window):
2119        global backend
2120        __parent = None 
2121        #backend = backend
2122        # Groups of user, the value is the name of group, the key is the description showed in
2123        # user creation gui. It searchs for group in system group list.
2124
2125        __groupList = getPermissions()
2126
2127        def __init__(self, iconView):
2128                gtk.Window.__init__(self)
2129                mainFrame = gtk.Frame(_("Massive User Creation"))
2130                self.__parent = iconView
2131 
2132                notebook = gtk.Notebook()
2133                # SIMPLE MASSIVE CREATION
2134                vbox = gtk.VBox()
2135                tableSimple = gtk.Table(6,2,False)
2136                labelSimple = gtk.Label(_("<b><big>Simple Massive User Creation</big></b>"))
2137                labelSimple.set_use_markup(True)
2138                tableSimple.attach(labelSimple, 0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2139
2140
2141               
2142                labelSimple.set_alignment(0.1,0.2)
2143                labelSimpleDescription = gtk.Label(_("<small>This panel lets you to create a certain number of user.\nUsername and password will be the same.</small>"))
2144                 
2145                #labelSimpleDescription.set_alignment(0.2,0.2)
2146                labelSimpleDescription.set_use_markup(True)
2147                tableSimple.attach(labelSimpleDescription, 1,2,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2148
2149
2150               
2151                numberLabel = gtk.Label(_("User number"))
2152                numberLabel.set_alignment(0.4,0.2)
2153                spin = gtk.SpinButton()
2154                spin.set_range(1,100)
2155                spin.set_increments(1,1)
2156                tableSimple.attach(numberLabel,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2157
2158                tableSimple.attach(spin,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2159
2160
2161                prefixLabel = gtk.Label(_("Username prefix"))
2162                prefixLabel.set_alignment(0.45,0.2)
2163
2164
2165                prefixEntry = gtk.Entry()
2166                tableSimple.attach(prefixLabel,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2167
2168                tableSimple.attach(prefixEntry,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2169
2170
2171                # Maing group selection comboBox
2172                groupComboLabel = gtk.Label(_("Main group"))
2173                groupComboLabel.set_alignment(0.395,0.2)
2174
2175
2176                groupComboBox = gtk.ComboBox()
2177                groupComboBox.set_model(self.__create_group_model_icon())
2178                cell = gtk.CellRendererText()
2179                groupComboBox.pack_start(cell)
2180                groupComboBox.add_attribute(cell, 'text', 0)
2181                groupComboBox.prepend_text("Default Group")
2182                groupComboBox.set_active(0)
2183
2184                tableSimple.attach(groupComboLabel,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
2185                tableSimple.attach(groupComboBox,1,2,4,5,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2186
2187                groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
2188                groupLabel.set_alignment(0.1,0.2)
2189                groupLabel.set_use_markup(True)
2190
2191                tableSimple.attach(groupLabel,0,1,5,6,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2192               
2193
2194
2195
2196                groups = backend.getUnixGroups()
2197                groupsToShow = {}
2198               
2199                for group in self.__groupList:
2200                        if self.__groupList[group] in groups:
2201                                # A dictionary of checkButtons
2202                                groupsToShow[group] = gtk.CheckButton(group)
2203                       
2204                l = len(groupsToShow)
2205               
2206                if l%2 == 0: 
2207                        groupTable = gtk.Table(l/2,2,False)
2208                        groupTable.set_homogeneous(False)
2209                        list = groupsToShow.keys()
2210                        for i in range(l):
2211                                if i%2 == 0: # SX
2212                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=94,ypadding=5)
2213
2214 
2215                                else: # DX
2216                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i)
2217                         
2218                else:
2219                        groupTable = gtk.Table(l/2+1,2,False)
2220                        groupTable.set_homogeneous(False)
2221
2222                        list = groupsToShow.keys()
2223                        for i in range(l):
2224                                if i%2 == 0: # SX
2225                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=94,yoptions=gtk.FILL,ypadding=5)
2226
2227 
2228                                else: # DX
2229                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
2230
2231
2232
2233                v = gtk.VBox()
2234                save = gtk.Button(stock="gtk-apply")
2235                save.connect("clicked", self.apply_simple_clicked,spin,prefixEntry,groupsToShow,self.__groupList, groupComboBox)
2236
2237                cancel = gtk.Button(stock="gtk-cancel")
2238                cancel.connect("clicked",self.cancel_clicked)
2239                h = gtk.HBox()
2240                h.pack_end(save,False,False)
2241                h.pack_end(cancel,False,False)
2242               
2243
2244
2245                #vbox.pack_start(labelSimple,True,True,padding=10)
2246                #vbox.pack_start(labelSimpleDescription)
2247                vbox.pack_start(tableSimple)
2248
2249                #vbox.pack_start(groupLabel, False,False, padding=10)
2250                #vbox.pack_start(labelGroupDescription)
2251                vbox.pack_start(groupTable)
2252                sep = gtk.HSeparator()
2253                vbox.pack_start(sep)
2254                vbox.pack_start(h)
2255                simple = gtk.Label(_("Simple"))
2256                notebook.insert_page(vbox,tab_label=simple)
2257
2258                # ADVANCED MASSIVE USER CREATION
2259               
2260
2261
2262                vboxAdvanced = gtk.VBox()
2263                tableAdvanced = gtk.Table(4,3,False)
2264                labelAdvanced = gtk.Label(_("<b><big>Advanced Massive User Creation</big></b>"))
2265                labelAdvanced.set_use_markup(True)
2266
2267                tableAdvanced.attach(labelAdvanced,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2268
2269
2270                groupLabelA = gtk.Label(_("<b><big>User privileges</big></b>"))
2271                groupLabelA.set_alignment(0.1,0.2)
2272                groupLabelA.set_use_markup(True)
2273
2274
2275               
2276                labelAdvanced.set_alignment(0.1,0.2)
2277                labelAdvancedDescription = gtk.Label(_("<small>This panel lets you to load an user file that\ncontains a list of users.\nThe format must be a comma separated values (CSV):\nname,surname,username</small>"))
2278                 
2279                #labelAdvancedDescription.set_alignment(0.2,0.2)
2280                labelAdvancedDescription.set_use_markup(True)
2281                tableAdvanced.attach(labelAdvancedDescription,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2282
2283
2284               
2285
2286
2287                fileLabel = gtk.Label(_("Users file"))
2288                fileLabel.set_alignment(0.43,0.2)
2289
2290
2291                fileEntry = gtk.Entry()
2292                tableAdvanced.attach(fileLabel,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2293                tableAdvanced.attach(fileEntry,1,2,2,3,xpadding=5)
2294                fileButton = gtk.Button(_("..."))
2295                fileButton.connect("clicked", self.fileChooser, fileEntry)
2296                tableAdvanced.attach(fileButton,2,3,2,3,xpadding=5, xoptions=gtk.SHRINK, yoptions=gtk.SHRINK)
2297
2298
2299                # Maing group selection comboBox
2300                groupComboLabelA = gtk.Label(_("Main group"))
2301                groupComboLabelA.set_alignment(0.45,0.2)
2302                groupComboBoxA = gtk.ComboBox()
2303                groupComboBoxA.set_model(self.__create_group_model_icon())
2304                cellA = gtk.CellRendererText()
2305                groupComboBoxA.pack_start(cellA)
2306                groupComboBoxA.add_attribute(cellA, 'text', 0)
2307                groupComboBoxA.prepend_text("Default Group")
2308                groupComboBoxA.set_active(0)
2309
2310                tableAdvanced.attach(groupComboLabelA,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2311                tableAdvanced.attach(groupComboBoxA,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2312
2313
2314                tableAdvanced.attach(groupLabelA,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2315
2316
2317
2318
2319
2320
2321                groupsA = backend.getUnixGroups()
2322                groupsToShowA = {}
2323               
2324                for group in self.__groupList:
2325                        if self.__groupList[group] in groups:
2326                                # A dictionary of checkButtons
2327                                groupsToShowA[group] = gtk.CheckButton(group)
2328                       
2329                l = len(groupsToShowA)
2330               
2331                if l%2 == 0: 
2332                        groupTableA = gtk.Table(l/2,2,False)
2333                        groupTableA.set_homogeneous(False)
2334                        list = groupsToShowA.keys()
2335                        for i in range(l):
2336                                if i%2 == 0: # SX
2337                                        groupTableA.attach(groupsToShowA[list[i]],0,1,i,i+1,xpadding=120,yoptions=gtk.FILL,ypadding=5)
2338
2339 
2340                                else: # DX
2341                                        groupTableA.attach(groupsToShowA[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
2342                         
2343                else:
2344                        groupTableA = gtk.Table(l/2+1,2,False)
2345                        groupTableA.set_homogeneous(False)
2346
2347                        list = groupsToShowA.keys()
2348                        for i in range(l):
2349                                if i%2 == 0: # SX
2350                                        groupTableA.attach(groupsToShowA[list[i]],0,1,i,i+1,xpadding=120,yoptions=gtk.FILL,ypadding=5)
2351
2352                                else: # DX
2353                                        groupTableA.attach(groupsToShowA[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
2354
2355               
2356               
2357
2358
2359
2360                vAdvanced = gtk.VBox()
2361                saveAdvanced = gtk.Button(stock="gtk-apply")
2362                saveAdvanced.connect("clicked", self.apply_advanced_clicked,fileEntry,groupsToShowA,self.__groupList,groupComboBoxA)
2363
2364                cancelAdvanced = gtk.Button(stock="gtk-cancel")
2365                cancelAdvanced.connect("clicked",self.cancel_clicked)
2366                hAdvanced = gtk.HBox()
2367                hAdvanced.pack_end(saveAdvanced,False,False)
2368                hAdvanced.pack_end(cancelAdvanced,False,False)
2369               
2370
2371
2372                #vboxAdvanced.pack_start(labelAdvanced,False,False,padding=10)
2373                #vboxAdvanced.pack_start(labelAdvancedDescription)
2374                vboxAdvanced.pack_start(tableAdvanced)
2375
2376                #vboxAdvanced.pack_start(groupLabelA, False,False, padding=10)
2377                #vbox.pack_start(labelGroupDescription)
2378                vboxAdvanced.pack_start(groupTableA)
2379                sepAdvanced = gtk.HSeparator()
2380
2381                vboxAdvanced.pack_start(sepAdvanced)
2382                vboxAdvanced.pack_start(hAdvanced)
2383                advanced = gtk.Label(_("Advanced"))
2384                notebook.insert_page(vboxAdvanced,tab_label=advanced)
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400                # Add tabs to main window
2401                self.add(notebook)
2402                self.show_all()
2403               
2404               
2405        def cancel_clicked(self, button):
2406                self.destroy()
2407
2408
2409        def apply_simple_clicked(self, button,spin,prefixEntry,groupsToShow,groupList,groupComboBox):
2410                """ Called when apply button is clicked on simple user mass creation """
2411                global backend
2412                if not prefixEntry.get_text():
2413                        self.showDialog(_("Username prefix must be not empty"),gtk.MESSAGE_ERROR)
2414                        return
2415               
2416                createdUsers = []
2417                failedUsers = []
2418                passwordUsers = {}
2419
2420                active = [] # User's groups
2421                for group in groupsToShow:
2422                        if groupsToShow[group].get_active():
2423                                active.append(groupList[group])
2424                #print active
2425                model = groupComboBox.get_model()
2426                activeG = groupComboBox.get_active()
2427                if activeG < 0:
2428                        mainGroup = None
2429                else:
2430                        mainGroup = model[activeG][0]
2431
2432                # Main group selection
2433
2434                if not mainGroup:
2435                        gid = backend.getGroup("Domain Users").getGid().getValue()       
2436                else:
2437                        if mainGroup == "Default Group":
2438                                gid = backend.getGroup("Domain Users").getGid().getValue()
2439                               
2440                        else:
2441                                gid = backend.getGroup(mainGroup).getGid().getValue()
2442
2443
2444
2445                for i in range(spin.get_value_as_int()):
2446                        value = i+1
2447                        username = prefixEntry.get_text()+str(value)
2448
2449
2450
2451
2452                        n = LdapUser(['inetOrgPerson','shadowAccount','sambaSamAccount'])
2453
2454                        attrObjectClass = ListAttribute("objectClass")
2455                        attrObjectClass.setValue(['posixAccount','top','inetOrgPerson','shadowAccount','sambaSamAccount'])
2456                        n.setAttribute("objectClass",attrObjectClass)
2457
2458                        n.setAttribute("cn", username)
2459                        n.setAttribute("sn", username)
2460                        uid = backend.getNewLdapID()
2461                        n.setAttribute("gidNumber", str(gid))
2462                        n.setAttribute("homeDirectory","/home/"+username)
2463                        n.setAttribute("loginShell","/bin/sh")
2464                        n.setAttribute("uid", username)
2465                        n.setAttribute("uidNumber", str(uid))
2466                        n.setAttribute("gecos", username)
2467                        password = backend.genPassword()
2468                        n.setAttribute("userPassword", password)
2469                        n.setAttribute("sambaSID", backend.getUserSID(uid))
2470
2471                        #New attributes
2472                        n.setAttribute("sambaLogonTime","0")
2473                        n.setAttribute("sambaLogoffTime", "0")
2474                        n.setAttribute("sambaKickoffTime","0")
2475                        n.setAttribute("sambaPwdCanChange","0")
2476                        n.setAttribute("sambaHomeDrive","H:")
2477                        n.setAttribute("sambaPwdCanChange","0")
2478                        n.setAttribute("sambaHomeDrive","H:")
2479                        n.setAttribute("sambaPrimaryGroupSID",backend.getGroupSID(str(gid)))
2480                        n.setAttribute("sambaAcctFlags", "[UX         ]")
2481
2482
2483                        try:
2484                                backend.addLdapUser2(n,active)
2485                                createdUsers.append(username)
2486                                passwordUsers[username] = password
2487                        except Exception, e:
2488                                #TODO: problem, if user already exists how to decide if user is old or new (if new => remove user)
2489                                if backend.isUser(username):
2490                                        if backend.getUser(username).getGid().getValue() == str(uid): 
2491                                                backend.deleteLdapUser2(username)
2492                                failedUsers.append(username)
2493                c = ""
2494                for us in createdUsers:
2495                      c = c + us + ", " 
2496                c=c.strip(" ")
2497                c=c.strip(",")
2498                f = ""
2499                for us in failedUsers:
2500                      f = f + us + ", " 
2501                f=f.strip(" ")
2502                f=f.strip(",")
2503                if c and f:
2504                        self.showDialog("Created: "+c+"\nFailed: "+f,gtk.MESSAGE_INFO)
2505                if c and not f:
2506                        self.showDialog("Created: "+c,gtk.MESSAGE_INFO)
2507                if not c and f:
2508                        self.showDialog("Failed : "+f,gtk.MESSAGE_INFO)
2509     
2510                # Update user list
2511                # Manca da ricaricare tutta la lista di utenti perchÚ altrimenti in memoria non ci sono e non possono essere cancellati
2512
2513                self.__parent.set_model(self.__create_user_model_icon()) 
2514                self.__parent.show()
2515                backend = Backend()
2516
2517                file = open("/tmp/filePassword","w")
2518                for user in passwordUsers:
2519                        file.write(user+","+passwordUsers[user]+"\n")
2520                file.close()
2521
2522                self.destroy()
2523                               
2524
2525                       
2526               
2527
2528                               
2529
2530
2531        def apply_advanced_clicked(self, button, fileEntry, groupsToShow, groupList, groupComboBox):
2532                """ Called when apply button is clicked on simple user mass creation """
2533                global backend
2534                if not fileEntry.get_text():
2535                        self.showDialog(_("File name must be not empty."),gtk.MESSAGE_ERROR)
2536                        return
2537               
2538                createdUsers = []
2539                failedUsers = []
2540                passwordUsers = {}
2541
2542                active = [] # User's groups
2543                for group in groupsToShow:
2544                        if groupsToShow[group].get_active():
2545                                active.append(groupList[group])
2546
2547                #print active
2548               
2549                model = groupComboBox.get_model()
2550                activeG = groupComboBox.get_active()
2551                if activeG < 0:
2552                        mainGroup = None
2553                else:
2554                        mainGroup = model[activeG][0]
2555                # Main group selection
2556
2557                if not mainGroup:
2558                        gid = backend.getGroup("Domain Users").getGid().getValue()       
2559                else:
2560                        if mainGroup == "Default Group":
2561                                gid = backend.getGroup("Domain Users").getGid().getValue()
2562                               
2563                        else:
2564                                gid = backend.getGroup(mainGroup).getGid().getValue()
2565
2566
2567
2568                try:
2569                        filename = fileEntry.get_text() 
2570                        reader = csv.reader(open(filename, "rb"))
2571                except Exception, e:
2572                        self.showDialog(e.strerror,gtk.MESSAGE_ERROR)
2573                        return
2574
2575                try:
2576                        for row in reader:
2577                                if row:
2578                                        # Create user
2579                                        nome = row[0]
2580                                        cognome = row[1]
2581                                        username = row[2]
2582                                        if row[0] and row[1] and row[2]:
2583
2584
2585
2586                                                n = LdapUser(['inetOrgPerson','shadowAccount','sambaSamAccount'])
2587
2588                                                attrObjectClass = ListAttribute("objectClass")
2589                                                attrObjectClass.setValue(['posixAccount','top','inetOrgPerson','shadowAccount','sambaSamAccount'])
2590                                                n.setAttribute("objectClass",attrObjectClass)
2591
2592
2593                                                n.setAttribute("cn", username)
2594                                                n.setAttribute("sn", username)
2595                                                uid = backend.getNewLdapID()
2596                                                n.setAttribute("gidNumber", str(gid))
2597                                                n.setAttribute("homeDirectory","/home/"+username)
2598                                                n.setAttribute("loginShell","/bin/sh")
2599                                                n.setAttribute("uid", username)
2600                                                n.setAttribute("uidNumber", str(uid))
2601                                                n.setAttribute("gecos", nome+cognome)
2602                                                password = backend.genPassword()
2603                                                n.setAttribute("userPassword", password)
2604                                                n.setAttribute("sambaSID", backend.getUserSID(uid))
2605                                                #New attributes
2606                                                n.setAttribute("sambaLogonTime","0")
2607                                                n.setAttribute("sambaLogoffTime", "0")
2608                                                n.setAttribute("sambaKickoffTime","0")
2609                                                n.setAttribute("sambaPwdCanChange","0")
2610                                                n.setAttribute("sambaHomeDrive","H:")
2611                                                n.setAttribute("sambaPwdCanChange","0")
2612                                                n.setAttribute("sambaHomeDrive","H:")
2613                                                n.setAttribute("sambaPrimaryGroupSID",backend.getGroupSID(str(gid)))
2614                                                n.setAttribute("sambaAcctFlags", "[UX         ]")
2615
2616
2617                                                try:
2618                                                        backend.addLdapUser2(n,active)
2619                                                        createdUsers.append(username)
2620                                                        passwordUsers[username] = password
2621                                                except:
2622                                                        if backend.isUser(username):
2623                                                                if backend.getUser(username).getGid().getValue() == str(uid): 
2624
2625                                                                        backend.deleteLdapUser2(username)
2626                                                        failedUsers.append(username)
2627                                        else:
2628                                                self.showDialog("Problem parsing user file!", gtk.MESSAGE_ERROR)
2629
2630                                       
2631                except csv.Error, e:
2632                        log.debug(str(e))
2633                        self.showDialog('file %s, line %d: %s' % (filename, reader.line_num, e),gtk.MESSAGE_ERROR)
2634                except IndexError,e:
2635                        self.showDialog("Error reading file!",gtk.MESSAGE_ERROR)
2636
2637                c = ""
2638                for us in createdUsers:
2639                      c = c + us + ", " 
2640                c=c.strip(" ")
2641                c=c.strip(",")
2642                f = ""
2643                for us in failedUsers:
2644                      f = f + us + ", " 
2645                f=f.strip(" ")
2646                f=f.strip(",")
2647                if c and f:
2648                        self.showDialog("Created: "+c+"\nFailed: "+f,gtk.MESSAGE_INFO)
2649                if c and not f:
2650                        self.showDialog("Created: "+c,gtk.MESSAGE_INFO)
2651                if not c and f:
2652                        self.showDialog("Failed : "+f,gtk.MESSAGE_INFO)
2653
2654
2655       
2656                # Update user list
2657                # Manca da ricaricare tutta la lista di utenti perchÚ altrimenti in memoria non ci sono e non possono essere cancellati
2658                self.__parent.set_model(self.__create_user_model_icon()) 
2659                self.__parent.show()
2660                backend = Backend()
2661                file = open("/tmp/filePassword","w")
2662                for user in passwordUsers:
2663                        file.write(user+","+passwordUsers[user]+"\n")
2664                file.close()
2665
2666                self.destroy()
2667
2668
2669 
2670
2671
2672        def fileChooser(self, button, entry):
2673                dialog = gtk.FileChooserDialog(_("Open.."),
2674                                       None,
2675                                       gtk.FILE_CHOOSER_ACTION_OPEN,
2676                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
2677                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
2678                dialog.set_default_response(gtk.RESPONSE_OK)
2679                filter = gtk.FileFilter()
2680                filter.set_name(_("All files"))
2681                filter.add_pattern("*")
2682                dialog.add_filter(filter)
2683                response = dialog.run()
2684                if response == gtk.RESPONSE_OK:
2685                        entry.set_text(dialog.get_filename())
2686                        dialog.destroy()
2687                elif response == gtk.RESPONSE_CANCEL:
2688                        dialog.destroy()
2689                       
2690        def showDialog(self, message, type):
2691               dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
2692               dialog.run()
2693               dialog.destroy()     
2694        def __create_user_model(self):
2695                """ Create model for list of users """
2696                list = backend.getUsersList()
2697
2698                # create list store
2699                model = gtk.ListStore(
2700                gobject.TYPE_STRING,
2701                gobject.TYPE_STRING,
2702                gobject.TYPE_STRING,
2703                )
2704                # add items
2705                for item in list:
2706                        iter = model.append()
2707                        type = ""
2708                        if backend.isUnixUser(item):
2709                                type=_("locale")
2710                        else:
2711                                if backend.isLdapUser(item):
2712                                        type = _("network")       
2713                        #User main group
2714                        mainGroupLabel = _("main group")
2715                        mainGroupGid = backend.getUser(item).getGid().getValue()
2716                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2717                        model.set(iter, 0, item, 1, type,  2, mainGroupName )
2718
2719                return model
2720
2721
2722        def __create_user_model_icon(self):
2723                """ Create model for list of users """
2724                #list = backend.getUsersList()
2725                list = backend.getLdapUsers()
2726                showUser = []
2727                for us in list:
2728                        #num = int(backend.getUser(us).getUidNumber().getValue())
2729                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2730                        showUser.append(us)
2731
2732
2733                # create list store
2734                model = gtk.ListStore(
2735                gobject.TYPE_STRING,
2736                gtk.gdk.Pixbuf,
2737                )
2738                # add items
2739                for item in showUser:
2740                        iter = model.append()
2741                        type = ""
2742                        if backend.isUnixUser(item):
2743                                type=_("locale")
2744                        else:
2745                                if backend.isLdapUser(item):
2746                                        type = _("network")       
2747                        #User main group
2748                        if backend.isEnabled(item):
2749                                pix = art.get_pixbuf_from_name("stock_person")
2750                        else:
2751                                pix = art.get_pixbuf_from_name("user_disabled")
2752
2753                        mainGroupLabel = _("main group")
2754                        mainGroupGid = backend.getUser(item).getGid().getValue()
2755                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2756                        #tooltip = gtk.Tooltips()
2757                        #tooltip.set_tip(item,mainGroupName)     
2758
2759                        model.set(iter,0,item,1, pix)
2760
2761                return model
2762
2763
2764
2765        def __create_group_model(self):
2766                """ Create model for list of groups """
2767                list = backend.getGroupsList()
2768                # create list store
2769                model = gtk.ListStore(
2770                gobject.TYPE_STRING,
2771                gobject.TYPE_STRING,
2772                )
2773                # add items
2774                for item in list:
2775                        iter = model.append()
2776                        type = ""
2777                        if backend.isUnixGroup(item):
2778                                type=_("locale")
2779                        else:
2780                                if backend.isLdapGroup(item):
2781                                        type = _("network")       
2782                                else:
2783                                        log.debug(_("Warning: problem identifying group type!"))
2784                        model.set (iter, 0, item, 1, type )
2785                return model
2786
2787        def __create_group_model_icon(self):
2788                """ Create model for list of groups """
2789                #list = backend.getGroupsList()
2790                list = backend.getLdapGroups()
2791                showGroup = []
2792                for gr in list:
2793                        #num = int(backend.getGroup(gr).getGid().getValue())
2794                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2795                        showGroup.append(gr)
2796
2797                # create list store
2798                model = gtk.ListStore(
2799                gobject.TYPE_STRING,
2800                gtk.gdk.Pixbuf,
2801                )
2802                # add items
2803                for item in showGroup:
2804                        iter = model.append()
2805                        type = ""
2806                        if backend.isUnixGroup(item):
2807                                type=_("locale")
2808                        else:
2809                                if backend.isLdapGroup(item):
2810                                        type = _("network")       
2811                                else:
2812                                        log.debug(_("Warning: problem identifying group type!"))
2813                        pix = art.get_pixbuf_from_name("stock_people")
2814
2815                        model.set (iter, 0, item, 1, pix )
2816                return model
2817
2818
2819
2820
2821
2822
2823               
2824
2825               
2826class Newgroup(gtk.Window):
2827        global backend
2828        __parent = None
2829        #backend = backend
2830        def __init__(self, iconView):
2831                gtk.Window.__init__(self)
2832                self.__parent = iconView
2833                mainFrame = gtk.Frame(_("New Group"))
2834                vboxMain = gtk.VBox()
2835                mainTable = gtk.Table(4,2,False)
2836
2837   
2838                labelRealname = gtk.Label(_("Group name"))
2839                entryRealname = gtk.Entry()               
2840                mainTable.attach(labelRealname,0,1,0,1,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2841                mainTable.attach(entryRealname,1,2,0,1,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2842
2843
2844
2845
2846
2847                vboxMain.pack_start(mainTable)
2848               
2849
2850
2851
2852                mainFrame.add(vboxMain)
2853                v = gtk.VBox()
2854                v.pack_start(mainFrame)
2855                save = gtk.Button(stock="gtk-add")
2856                save.connect("clicked", self.__save_clicked,entryRealname)
2857
2858                cancel = gtk.Button(stock="gtk-cancel")
2859                cancel.connect("clicked",self.__cancel_clicked)
2860                hboxButton = gtk.HBox()
2861                hboxButton.pack_end(save,False,False)
2862                hboxButton.pack_end(cancel,False,False)
2863                v.pack_start(hboxButton)
2864
2865
2866                self.add(v)
2867               
2868
2869        def __save_clicked(self,button, entryGroupname):
2870                """ Check fields and add the user to Ldap Server """ 
2871                global backend
2872               
2873                try:
2874                        if not entryGroupname.get_text():
2875                                self.showDialog(_("Group name must be not empty"),gtk.MESSAGE_ERROR)
2876                                return
2877                               
2878                        # Set group attributes
2879                        g = LdapGroup(['sambaGroupMapping'])
2880                        attrObjectClass = ListAttribute("objectClass")
2881                        attrObjectClass.setValue(['posixGroup','top','sambaGroupMapping'])
2882                        g.setAttribute("objectClass",attrObjectClass)
2883
2884                        g.setAttribute("cn", entryGroupname.get_text())
2885                        uid = backend.getNewLdapID()
2886                        g.setAttribute("gidNumber", str(uid))
2887                        sid = backend.getGroupSID(uid)
2888                        g.setAttribute("sambaSID", str(sid))
2889                        g.setAttribute("sambaGroupType","2")
2890
2891
2892                        backend.addLdapGroup2(g)
2893                     
2894                        self.__parent.set_model(self.__create_group_model_icon()) 
2895                        self.__parent.show()
2896                        backend = Backend()
2897                        self.destroy() 
2898                                         
2899                except Exception, inst:
2900                        self.showDialog(_("Problems adding new group\n"+str(inst)),gtk.MESSAGE_ERROR)
2901
2902
2903
2904
2905
2906 
2907        def __create_group_model(self):
2908                """ Create model for list of groups """
2909                list = backend.getGroupsList()
2910                # create list store
2911                model = gtk.ListStore(
2912                gobject.TYPE_STRING,
2913                gobject.TYPE_STRING,
2914                )
2915                # add items
2916                for item in list:
2917                        iter = model.append()
2918                        type = ""
2919                        if backend.isUnixGroup(item):
2920                                type=_("locale")
2921                        else:
2922                                if backend.isLdapGroup(item):
2923                                        type = _("network")       
2924                                else:
2925                                        log.debug(_("Warning: problem identifying group type!"))
2926                        model.set (iter, 0, item, 1, type )
2927                return model
2928
2929        def __create_group_model_icon(self):
2930                """ Create model for list of groups """
2931                #list = backend.getGroupsList()
2932                list = backend.getLdapGroups()
2933                showGroup = []
2934                for gr in list:
2935                        #num = int(backend.getGroup(gr).getGid().getValue())
2936                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2937                        showGroup.append(gr)
2938
2939                # create list store
2940                model = gtk.ListStore(
2941                gobject.TYPE_STRING,
2942                gtk.gdk.Pixbuf,
2943                )
2944                # add items
2945                for item in showGroup:
2946                        iter = model.append()
2947                        type = ""
2948                        if backend.isUnixGroup(item):
2949                                type=_("locale")
2950                        else:
2951                                if backend.isLdapGroup(item):
2952                                        type = _("network")       
2953                                else:
2954                                        log.debug(_("Warning: problem identifying group type!"))
2955                        pix = art.get_pixbuf_from_name("stock_people")
2956
2957                        model.set (iter, 0, item, 1, pix )
2958                return model
2959
2960                               
2961                               
2962         
2963
2964        def __cancel_clicked(self, button):
2965                self.destroy()
2966
2967        def showDialog(self, message, type):
2968               dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
2969               dialog.run()
2970               dialog.destroy()     
2971
2972               
2973               
2974
2975if __name__ == '__main__':
2976        m = Management()
2977        #m = MassCreation()
2978       
2979        w = gtk.Window()
2980        w.add(m)
2981        #m = Newuser()
2982        w.show_all()
2983        gtk.main()
2984
2985
2986
2987               
2988
2989
2990
2991
Note: See TracBrowser for help on using the browser.