root/lib/octofuss/users/ui/__init__.py @ 419963371a32e1ff6c61b365ec83c88b4bfd3b4b

Revision 419963371a32e1ff6c61b365ec83c88b4bfd3b4b, 124.2 KB (checked in by iacopo <iacopo@…>, 6 years ago)

New octofuss icons

git-svn-id:  https://labs.truelite.it/svn/octofuss/trunk@380 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                                pix = art.get_pixbuf_from_name("user")
757
758                        else:
759                                #pix = art.get_pixbuf_from_name("user_disabled")
760                                pix = art.get_pixbuf_from_name("user-disabled")
761
762
763                        mainGroupLabel = _("main group")
764                        mainGroupGid = backend.getUser(item).getGid().getValue()
765                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
766                        #tooltip = gtk.Tooltips()
767                        #tooltip.set_tip(item,mainGroupName)     
768
769                        model.set(iter,0,item,1, pix)
770
771                self.viewUserIcon.set_model(model) 
772                self.viewUserIcon.set_item_width(-1) 
773                self.viewUserIcon.show()
774
775
776
777
778        def get_users_icon_list(self):
779                hbox = gtk.HBox()
780                swin = gtk.ScrolledWindow()
781                swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
782                swin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
783                hbox.pack_start(swin)
784                view = gtk.IconView()
785                view.set_text_column(0)
786                view.set_pixbuf_column(1)
787                view.set_selection_mode(gtk.SELECTION_SINGLE)
788                view.set_size_request(200, 150)
789                #model = gtk.ListStore(str, gtk.gdk.Pixbuf)
790                model = self.__create_user_model_icon()
791               
792                #pix = art.get_pixbuf_from_name("workstation-up")
793                #pix = art.get_pixbuf_from_name("workstation")
794                #model.append([host.name,pix])
795               
796                view.set_model(model)
797                view.set_item_width(-1) 
798
799                #view.connect('selection-changed', self.on_host_select)
800                swin.add(view)
801                hbox.show_all()
802                return hbox
803
804
805        def get_groups_icon_list(self):
806                hbox = gtk.HBox()
807                swin = gtk.ScrolledWindow()
808                swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
809                swin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
810                hbox.pack_start(swin)
811                view = gtk.IconView()
812                view.set_text_column(0)
813                view.set_pixbuf_column(1)
814                view.set_selection_mode(gtk.SELECTION_SINGLE)
815                view.set_size_request(200, 150)
816                #model = gtk.ListStore(str, gtk.gdk.Pixbuf)
817                model = self.__create_group_model_icon()
818               
819                #pix = art.get_pixbuf_from_name("workstation-up")
820                #pix = art.get_pixbuf_from_name("workstation")
821                #model.append([host.name,pix])
822               
823                view.set_model(model)
824                view.set_item_width(-1) 
825
826                #view.connect('selection-changed', self.on_host_select)
827                swin.add(view)
828                hbox.show_all()
829                return hbox
830
831        def __create_user_model_icon(self):
832                """ Create model for list of users """
833                #list = backend.getUsersList()
834                list = backend.getLdapUsers()
835               
836                showUser = []
837                for us in list:
838                        #num = int(backend.getUser(us).getUidNumber().getValue())
839                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
840                        showUser.append(us)
841                               
842
843                # create list store
844                model = gtk.ListStore(
845                gobject.TYPE_STRING,
846                gtk.gdk.Pixbuf,
847                )
848                # add items
849                for item in showUser:
850                        iter = model.append()
851                        type = ""
852                        if backend.isUnixUser(item):
853                                type=_("locale")
854                        else:
855                                if backend.isLdapUser(item):
856                                        type = _("network")       
857                        #User main group
858                        if backend.isEnabled(item):
859                                #pix = art.get_pixbuf_from_name("stock_person")
860                                pix = art.get_pixbuf_from_name("user")
861
862                        else:
863                                #pix = art.get_pixbuf_from_name("user_disabled")
864                                pix = art.get_pixbuf_from_name("user-disabled")
865
866
867                        mainGroupLabel = _("main group")
868                        mainGroupGid = backend.getUser(item).getGid().getValue()
869                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
870                        #tooltip = gtk.Tooltips()
871                        #tooltip.set_tip(item,mainGroupName)     
872
873                        model.set(iter,0,item,1, pix)
874
875                return model
876
877        def __create_user_model_by_list(self, list):
878                """ Create model for list of users """
879                showUser = []
880                for us in list:
881                        #num = int(backend.getUser(us).getUidNumber().getValue())
882                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
883                        #Check ldap user
884                        if backend.isLdapUser(us):
885                                showUser.append(us)
886
887
888                # create list store
889                model = gtk.ListStore(
890                gobject.TYPE_STRING,
891                gtk.gdk.Pixbuf,
892                )
893                # add items
894                for item in showUser:
895                        iter = model.append()
896                        type = ""
897                        if backend.isUnixUser(item):
898                                type=_("locale")
899                        else:
900                                if backend.isLdapUser(item):
901                                        type = _("network")       
902                        #User main group
903                        if backend.isEnabled(item):
904                                #pix = art.get_pixbuf_from_name("stock_person")
905                                pix = art.get_pixbuf_from_name("user")
906
907                        else:
908                                #pix = art.get_pixbuf_from_name("user_disabled")
909                                pix = art.get_pixbuf_from_name("user-disabled")
910
911
912                        mainGroupLabel = _("main group")
913                        mainGroupGid = backend.getUser(item).getGid().getValue()
914                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
915                        #tooltip = gtk.Tooltips()
916                        #tooltip.set_tip(item,mainGroupName)     
917
918                        model.set(iter,0,item,1, pix)
919
920                return model
921
922
923
924        def __create_user_model(self):
925                """ Create model for list of users """
926                list = backend.getUsersList()
927
928                # create list store
929                model = gtk.ListStore(
930                gobject.TYPE_STRING,
931                gobject.TYPE_STRING,
932                gobject.TYPE_STRING,
933                )
934                # add items
935                for item in list:
936                        iter = model.append()
937                        type = ""
938                        if backend.isUnixUser(item):
939                                type=_("locale")
940                        else:
941                                if backend.isLdapUser(item):
942                                        type = _("network")       
943                        #User main group
944                        mainGroupLabel = _("main group")
945                        mainGroupGid = backend.getUser(item).getGid().getValue()
946                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
947                        model.set(iter, 0, item, 1, type,  2, mainGroupName )
948
949                return model
950
951        def __create_group_model_icon(self):
952                """ Create model for list of groups """
953                #list = backend.getGroupsList()
954                list = backend.getLdapGroups()
955                showGroup = []
956                for gr in list:
957                        #num = int(backend.getGroup(gr).getGid().getValue())
958                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
959                        showGroup.append(gr)
960
961                # create list store
962                model = gtk.ListStore(
963                gobject.TYPE_STRING,
964                gtk.gdk.Pixbuf,
965                )
966                # add items
967                for item in showGroup:
968                        iter = model.append()
969                        type = ""
970                        if backend.isUnixGroup(item):
971                                type=_("locale")
972                        else:
973                                if backend.isLdapGroup(item):
974                                        type = _("network")       
975                                else:
976                                        log.debug(_("Warning: problem identifying group type!"))
977                        #pix = art.get_pixbuf_from_name("stock_people")
978                        pix = art.get_pixbuf_from_name("group")
979
980
981                        model.set (iter, 0, item, 1, pix )
982                return model
983
984
985        def __create_group_model(self):
986                """ Create model for list of groups """
987                list = backend.getGroupsList()
988                # create list store
989                model = gtk.ListStore(
990                gobject.TYPE_STRING,
991                gobject.TYPE_STRING,
992                )
993                # add items
994                for item in list:
995                        iter = model.append()
996                        type = ""
997                        if backend.isUnixGroup(item):
998                                type=_("locale")
999                        else:
1000                                if backend.isLdapGroup(item):
1001                                        type = _("network")       
1002                                else:
1003                                        log.debug(_("Warning: problem identifying group type!"))
1004                                       
1005                        model.set (iter, 0, item, 1, type )
1006                return model
1007
1008        def __add_columns_users(self, treeview):
1009                model = treeview.get_model()
1010                # Name column
1011                renderer = gtk.CellRendererText()
1012                renderer.set_data("column", 0)
1013
1014                column = gtk.TreeViewColumn("Name", renderer, text=0)
1015                column.set_min_width(100)
1016                column.set_spacing(30)
1017                #column.set_sort_order(gtk.SORT_ASCENDING) 
1018                #column.set_sort_column_id(0)
1019
1020                treeview.append_column(column)
1021
1022                renderer = gtk.CellRendererText()
1023                renderer.set_data("column", 1)
1024
1025                column = gtk.TreeViewColumn(_("Type"), renderer, text=1)
1026                column.set_min_width(100)
1027
1028                #column.set_sort_order(gtk.SORT_ASCENDING) 
1029                #column.set_sort_column_id(0)
1030
1031                treeview.append_column(column)
1032                column.set_spacing(30)
1033               
1034                renderer = gtk.CellRendererText()
1035                renderer.set_data("column", 2)
1036
1037                column = gtk.TreeViewColumn(_("Main group"), renderer, text=2)
1038                column.set_min_width(100)
1039
1040                #column.set_sort_order(gtk.SORT_ASCENDING) 
1041                #column.set_sort_column_id(0)
1042
1043                treeview.append_column(column)
1044                column.set_spacing(30)
1045
1046
1047        def __add_columns_groups(self, treeview):
1048                model = treeview.get_model()
1049                # Name column
1050                renderer = gtk.CellRendererText()
1051                renderer.set_data("column", 0)
1052
1053                column = gtk.TreeViewColumn("Name", renderer, text=0)
1054                column.set_min_width(100)
1055
1056                column.set_spacing(30)
1057                #column.set_sort_order(gtk.SORT_ASCENDING) 
1058                #column.set_sort_column_id(0)
1059
1060                treeview.append_column(column)
1061
1062                renderer = gtk.CellRendererText()
1063                renderer.set_data("column", 1)
1064
1065                column = gtk.TreeViewColumn(_("Type"), renderer, text=1)
1066                column.set_min_width(100)
1067
1068                #column.set_sort_order(gtk.SORT_ASCENDING) 
1069                #column.set_sort_column_id(0)
1070
1071                treeview.append_column(column)
1072                column.set_spacing(30)
1073               
1074                """
1075                renderer = gtk.CellRendererText()
1076                renderer.set_data("column", 2)
1077
1078                column = gtk.TreeViewColumn(_("Main group"), renderer, text=1)
1079                #column.set_sort_order(gtk.SORT_ASCENDING) 
1080                #column.set_sort_column_id(0)
1081
1082                treeview.append_column(column)
1083                column.set_spacing(30)
1084                """
1085
1086
1087
1088        def user_remove_clicked(self, button, iconView):
1089                try:
1090                        model = iconView.get_model()
1091                        selectedItem = iconView.get_selected_items()
1092                       
1093                        values = []
1094                        for it in selectedItem:
1095                                iter = model.get_iter(it[0])
1096                                value = model.get_value(iter,0)
1097                                values.append(value)
1098                        if not values:
1099                                dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select an user!", buttons=gtk.BUTTONS_OK)   
1100                                dialog.show_all()
1101                                response = dialog.run()
1102                                dialog.destroy()   
1103                                return 
1104
1105                        #treeSelection = treeView.get_selection()
1106                        #selection = treeSelection.get_selected()
1107                        #iter = selection[1]
1108
1109                        #value = model.get_value(iter,0)
1110                except:
1111                        dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select an user!", buttons=gtk.BUTTONS_OK)   
1112                        dialog.show_all()
1113                        response = dialog.run()
1114                        dialog.destroy()   
1115                        return 
1116                try:
1117                        for value in values:
1118                                if self.dialogRun(value):
1119                                        if backend.isUnixUser(value):
1120                                                backend.deleteUnixUser2(value)       
1121                                        else:
1122                                                if backend.isLdapUser(value):
1123                                                         backend.deleteLdapUser2(value)       
1124                                                else:
1125                                                        log.debug(_("Invalid user"))
1126                                               
1127                        iconView.set_model(self.__create_user_model_icon()) 
1128                        iconView.set_item_width(-1) 
1129
1130                        iconView.show()
1131                except Exception, e:
1132                        log.debug(str(e))
1133                        self.showDialog(str(e),gtk.MESSAGE_ERROR)
1134
1135
1136
1137
1138        def user_add_clicked(self, button, iconView):
1139                """ Show new user GUI """
1140                newUserGUI = Newuser(iconView)
1141                newUserGUI.show_all()
1142
1143
1144        def user_mass_clicked(self, button, iconView):
1145                """ Show user mass creation Gui """
1146                newMassGUI = MassCreation(iconView)
1147                newMassGUI.show_all()
1148       
1149
1150        def group_remove_clicked(self, button, iconView, iconViewUser):
1151                try:
1152                        model = iconView.get_model()
1153                        selectedItem = iconView.get_selected_items()
1154                        iter = model.get_iter(selectedItem[0])
1155                        value = model.get_value(iter,0)
1156
1157                except:
1158                        dialog = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, message_format="You must select a group !", buttons=gtk.BUTTONS_OK) 
1159                        dialog.show_all()
1160                        response = dialog.run()
1161                        dialog.destroy()   
1162                        return 
1163                try:
1164                        if self.dialogRun(value):
1165                                if backend.isUnixGroup(value):
1166                                        backend.deleteUnixGroup2(value)       
1167                                else:
1168                                        if backend.isLdapGroup(value):
1169                                                backend.deleteLdapGroup2(value)       
1170
1171                                        else:
1172                                                log.debug(_("Invalid group"))
1173
1174                                iconView.set_model(self.__create_group_model_icon()) 
1175                                iconView.set_item_width(-1) 
1176
1177                                #iconViewUser.set_model(self.__create_user_model_icon())
1178                                iconView.show()
1179
1180                                iconViewUser.show()
1181                except Exception, e:
1182                        self.showDialog("Problem removing group\n"+str(e.args[0]),gtk.MESSAGE_ERROR)
1183                        return 
1184
1185        def group_add_clicked(self, button, iconView):
1186                """ Show new user GUI """
1187                newGroupGUI = Newgroup(iconView)
1188                newGroupGUI.show_all()
1189
1190
1191
1192
1193        def showDialog(self, message, type):
1194                dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
1195                dialog.run()
1196                dialog.destroy()     
1197
1198
1199        def dialogRun(self, entry): 
1200                self.dialog = gtk.Dialog('Sure?', None, 
1201                        gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, 
1202                        (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
1203                self.dialog.vbox.pack_start(gtk.Label(_("Do you want to remove "+entry+"?")))
1204                self.dialog.show_all()
1205                response = self.dialog.run()
1206                if response == gtk.RESPONSE_ACCEPT: 
1207                      self.dialog.destroy()
1208                      self.dialog.destroy()       
1209                      return True
1210                elif response == gtk.RESPONSE_REJECT: 
1211                      self.dialog.destroy()
1212                      self.dialog.destroy()                       
1213                      return False         
1214
1215
1216        def on_button_modify_clicked(self,button, iconView):
1217                try:
1218                        model = iconView.get_model()
1219                        selectedItem = iconView.get_selected_items()
1220                        if len(selectedItem) == 1:
1221                                iter = model.get_iter(selectedItem[0])
1222                                value = model.get_value(iter,0)
1223                                info = Info(value, iconView)
1224                                info.show()
1225                        elif len(selectedItem) > 1:
1226                                self.showDialog("You must select a single user!",gtk.MESSAGE_WARNING)
1227                        else:
1228                                self.showDialog("You must select a user!",gtk.MESSAGE_WARNING)
1229
1230                       
1231                except Exception, e:
1232                        self.showDialog("You must select a user!",gtk.MESSAGE_WARNING)
1233
1234        def on_item_activated(self,iconView, path):
1235                try:
1236                        model = iconView.get_model()
1237                        selectedItem = iconView.get_selected_items()
1238                        iter = model.get_iter(selectedItem[0])
1239                        value = model.get_value(iter,0)
1240                        info = Info(value, iconView)
1241                        info.show()
1242                except Exception, e:
1243                        log.debug(str(e)) 
1244                        self.showDialog("Invalid user!\n"+str(e),gtk.MESSAGE_ERROR)
1245                       
1246
1247
1248        def on_group_item_activated(self,groupView,userView):
1249                """ Selecting a group in groupView it shows users in that group """
1250                try:
1251                        model = groupView.get_model()
1252
1253                        selectedItem = groupView.get_selected_items()
1254                        iter = model.get_iter(selectedItem[0])
1255                        groupname = model.get_value(iter,0)
1256                        # Select all user in groupname
1257                        users = backend.usersByGroup(groupname)
1258                        if not users:
1259                                userView.hide()
1260                                #self.userFrame.remove(child)
1261                                self.userFrame.set_label("No users in group \""+groupname+"\"")
1262                                #label = gtk.Label("No users in selected group")
1263                                #self.userFrame.add(label)
1264                                #label.show()
1265                               
1266                        else:
1267                                newModel = self.__create_user_model_by_list(users)           
1268                                userView.disconnect(self.button_release_signal_id)
1269                                self.button_release_signal_id = userView.connect("button-release-event", self.host_item_event_group, userView, groupname)
1270
1271
1272                                self.userFrame.set_label("Users in group \""+groupname+"\"")
1273                                userView.set_model(newModel)
1274                                userView.set_item_width(-1) 
1275
1276                                userView.show()
1277
1278                except IndexError, e:
1279                        newModel = self.__create_user_model_icon()           
1280                        userView.disconnect(self.button_release_signal_id)
1281
1282                        self.button_release_signal_id = userView.connect("button-release-event", self.host_item_event, userView)
1283                        self.userFrame.set_label("All users")
1284
1285                        userView.set_model(newModel)
1286                        userView.set_item_width(-1) 
1287
1288                        userView.show()
1289
1290                       
1291                except Exception, e:
1292                        log.debug(str(e))
1293                       
1294
1295               
1296
1297
1298
1299
1300        def show_context_menu(self,user):
1301                menu = gtk.Menu()
1302                item = gtk.MenuItem("")
1303                item.set_sensitive(False)
1304                #menu.append(item)
1305                # remote shell
1306                item = gtk.MenuItem(_("Details"))
1307                item.connect("activate", self.user_details,user )
1308                menu.append(item)
1309
1310                #event_button = self.get_possible_button_event(event)
1311                #menu.attach_to_widget(self.cluster_tree, None)
1312
1313                #menu.popup(None,None,None,3,0)
1314                menu.popup(None, None, None, 3,0)
1315                menu.show_all()
1316
1317
1318        def user_details(self,us,user ):
1319                """ Show a window that show user infos and lets the admin to change the password """
1320                #Never userd
1321                try:
1322                        info = Info()
1323                        info.show()
1324                except Exception,e:
1325                        log.debug(str(e))
1326
1327class Info(gtk.Window):
1328        global backend
1329        __groupList = getPermissions()
1330
1331        def __init__(self, username, view=None):
1332                gtk.Window.__init__(self)
1333                if backend.isUser(username):
1334                        user = backend.getUser(username)
1335                        mainFrame = gtk.Frame(_("User details"))
1336                        vboxMain = gtk.VBox()
1337                        mainTable = gtk.Table(8,2,False)
1338
1339                        # Details title
1340                        fieldLabel = gtk.Label(_("<b><big>Details</big></b>"))
1341                        fieldLabel.set_alignment(0.1,0.3)
1342                        fieldLabel.set_use_markup(True)
1343                       
1344                        mainTable.attach(fieldLabel,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1345
1346           
1347                        label1 = gtk.Label(_("Real name:"))
1348                        labelRealname = gtk.Label(user.getAttribute("posixAccount","gecos").getValue())
1349                        mainTable.attach(label1,0,1,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
1350                        mainTable.attach(labelRealname,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1351
1352
1353
1354
1355                        label2 = gtk.Label(_("Username:"))
1356                        labelUsername = gtk.Label(username)               
1357                        mainTable.attach(label2,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
1358                        mainTable.attach(labelUsername,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1359
1360
1361                        label3 = gtk.Label(_("Home:"))
1362                        try:
1363                                home = user.getHome().getValue()
1364                        except:
1365                                home = "None"
1366                        labelHome = gtk.Label(home)               
1367                        mainTable.attach(label3,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
1368                        mainTable.attach(labelHome,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1369
1370
1371
1372
1373
1374                       
1375
1376                        labelMainGroup= gtk.Label(_("Main group:"))
1377                        mainGroupName = backend.getGroupByGid(user.getGid().getValue()).getName().getValue()
1378                        labelValue = gtk.Label(mainGroupName)               
1379
1380                        # Main group
1381                        mainTable.attach(labelMainGroup,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1382                        mainTable.attach(labelValue,1,2,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1383
1384
1385                        labelPassword = gtk.Label(_("Change Password:"))
1386                        buttonPassword = gtk.Button("Change Password")               
1387                        buttonPassword.connect("clicked", self.change_password, username)
1388
1389                        buttonEnable = gtk.Button("Enable User")               
1390                        buttonEnable.connect("clicked", self.enable_user, username,view)
1391
1392                        buttonDisable = gtk.Button("DisableUser")               
1393                        buttonDisable.connect("clicked", self.disable_user, username, view)
1394                        enabled = backend.isEnabled(username)
1395                       
1396                        if enabled != None:
1397                                if enabled:
1398                                        mainTable.attach(buttonDisable,0,1,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1399                                else:
1400                                        mainTable.attach(buttonEnable,0,1,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1401                        else:
1402                                log.debug("Is enabled returned None, problems")
1403                                mainTable.attach(buttonDisable,0,1,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1404
1405                                       
1406
1407                                       
1408                                             
1409
1410                        mainTable.attach(buttonPassword,1,2,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1411
1412
1413
1414                        # Privileges title
1415                        groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
1416                        groupLabel.set_alignment(0.1,0.3)
1417                        groupLabel.set_use_markup(True)
1418               
1419
1420                        mainTable.attach(groupLabel,0,1,6,7,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1421
1422
1423
1424                        #User unix groups
1425                       
1426                        userGroups = backend.groupsByUser(username)
1427                        #print "user groups: "+str(userGroups)
1428
1429                        # We need to load user group (privileges) and set active checkbox
1430
1431                        groups = backend.getUnixGroups()
1432                        groupsToShow = {}
1433                       
1434                        for group in self.__groupList:
1435                                if self.__groupList[group] in groups:
1436                                        # A dictionary of checkButtons
1437                                        groupsToShow[group] = gtk.CheckButton(group)
1438                                        if self.__groupList[group] in userGroups:
1439                                                groupsToShow[group].set_active(True)
1440                                                #groupsToShow[group].set_alignment(0,0)
1441                                       
1442                               
1443                        l = len(groupsToShow)
1444                       
1445                        if l%2 == 0: 
1446                                groupTable = gtk.Table(l/2,2,False)
1447                                groupTable.set_homogeneous(False)
1448                                list = groupsToShow.keys()
1449                                for i in range(l):
1450                                        if i%2 == 0: # SX
1451                                                groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,yoptions=gtk.FILL,xpadding=50,ypadding=5)
1452
1453         
1454                                        else: # DX
1455                                                groupTable.attach(groupsToShow[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
1456                                 
1457                        else:
1458                                groupTable = gtk.Table(l/2+1,2,False)
1459                                groupTable.set_homogeneous(False)
1460
1461                                list = groupsToShow.keys()
1462                                for i in range(l):
1463                                        if i%2 == 0: # SX
1464                                                groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=50,yoptions=gtk.FILL,ypadding=5)
1465
1466         
1467                                        else: # DX
1468                                                groupTable.attach(groupsToShow[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
1469
1470
1471               
1472
1473
1474
1475
1476                        vboxMain.pack_start(mainTable)
1477
1478                        vboxMain.pack_start(groupTable)
1479
1480                        mainFrame.add(vboxMain)
1481
1482                        # Save-cancel buttons
1483                        v = gtk.VBox()
1484                        v.pack_start(mainFrame)
1485                        save = gtk.Button(stock="gtk-save")
1486                        save.connect("clicked", self.__save_clicked, groupsToShow, username )
1487
1488                        cancel = gtk.Button(stock="gtk-cancel")
1489                        cancel.connect("clicked",self.__cancel_clicked,self )
1490                        hboxButton = gtk.HBox()
1491                        hboxButton.pack_end(save,False,False)
1492                        hboxButton.pack_end(cancel,False,False)
1493                        v.pack_start(hboxButton)
1494
1495                        del(userGroups)
1496
1497                        self.add(v)
1498                        self.show_all()
1499
1500
1501
1502                               
1503                else:
1504                        self.showDialog("Not a valid user!",gtk.MESSAGE_ERROR)
1505
1506        def __create_user_model_icon(self):
1507                """ Create model for list of users """
1508                #list = backend.getUsersList()
1509                list = backend.getLdapUsers()
1510                showUser = []
1511                for us in list:
1512                        #num = int(backend.getUser(us).getUidNumber().getValue())
1513                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
1514                        showUser.append(us)
1515
1516
1517                # create list store
1518                model = gtk.ListStore(
1519                gobject.TYPE_STRING,
1520                gtk.gdk.Pixbuf,
1521                )
1522                # add items
1523                for item in showUser:
1524                        iter = model.append()
1525                        type = ""
1526                        if backend.isUnixUser(item):
1527                                type=_("locale")
1528                        else:
1529                                if backend.isLdapUser(item):
1530                                        type = _("network")       
1531                        #User main group
1532                        if backend.isEnabled(item):
1533                                #pix = art.get_pixbuf_from_name("stock_person")
1534                                pix = art.get_pixbuf_from_name("user")
1535
1536                        else:
1537                                #pix = art.get_pixbuf_from_name("user_disabled")
1538                                pix = art.get_pixbuf_from_name("user-disabled")
1539
1540                        mainGroupLabel = _("main group")
1541                        mainGroupGid = backend.getUser(item).getGid().getValue()
1542                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
1543                        #tooltip = gtk.Tooltips()
1544                        #tooltip.set_tip(item,mainGroupName)     
1545
1546                        model.set(iter,0,item,1, pix)
1547
1548                return model
1549
1550
1551
1552        def enable_user(self, button, username, view):
1553                """ Set username's account enabled """
1554                backend.enableLdapUser(username)
1555                self.destroy()
1556                view.set_model(self.__create_user_model_icon()) 
1557                view.show()
1558
1559
1560
1561
1562
1563        def disable_user(self, button, username, view):
1564                """ Set username's account disabled """
1565                backend.disableLdapUser(username)
1566                self.destroy()
1567                view.set_model(self.__create_user_model_icon()) 
1568                view.show()
1569
1570
1571
1572        def __save_clicked(self, button, groupsToShow, username):
1573                """ Update user unix groups (privileges) """
1574                __groupList = getPermissions()
1575                groupsEmpty = []
1576                for g in groupsToShow:
1577                        value = False
1578                        value = groupsToShow[g].get_active()
1579                        if value:
1580                                groupsEmpty.append(__groupList[g]) 
1581                #print groups
1582                backend.updateUserLocalGroups(username,groupsEmpty)
1583                del(groupsEmpty)
1584                self.destroy()
1585
1586        def change_password(self, button, username):
1587                """ Show a dialog to change password """
1588                winPass = gtk.Window()
1589                table = gtk.Table(2,2)
1590               
1591                lab0 = gtk.Label(_("<b><big>Change Password</big></b>"))
1592                lab0.set_alignment(0.1,0.3)
1593                lab0.set_use_markup(True)
1594                lab1 = gtk.Label("Password")
1595                lab2 = gtk.Label("Confirm")
1596                en1 = gtk.Entry()
1597                en1.set_visibility(False)
1598                en2 = gtk.Entry()
1599                en2.set_visibility(False)
1600
1601                table.attach(lab0,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=15)
1602
1603                table.attach(lab1,0,1,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1604                table.attach(en1,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1605                table.attach(lab2,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1606                table.attach(en2,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
1607
1608                v = gtk.VBox()
1609                v.pack_start(table)
1610                save = gtk.Button(stock="gtk-save")
1611                save.connect("clicked", self.__change_ldap_password, en1,en2,username, winPass)
1612
1613                cancel = gtk.Button(stock="gtk-cancel")
1614                cancel.connect("clicked",self.__cancel_clicked, winPass)
1615                hboxButton = gtk.HBox()
1616                hboxButton.pack_end(save,False,False)
1617                hboxButton.pack_end(cancel,False,False)
1618                v.pack_start(hboxButton)
1619                winPass.add(v)
1620                winPass.show_all()
1621
1622
1623        def __cancel_clicked(self, button, window):
1624                window.destroy()
1625
1626        def __change_ldap_password(self, button, entry1,entry2, username, winPass):
1627                """ Change password of username user """
1628                try:
1629                        pass1 = entry1.get_text()
1630                        pass2 = entry2.get_text()
1631                        if pass1 and pass2:
1632                                if pass1 == pass2:
1633                                        #Change password
1634                                        backend.changeLdapPassword(username,pass1)
1635                                        winPass.destroy()
1636                                else:
1637                                        self.showDialog("Problem: passwords do not match!",gtk.MESSAGE_ERROR)
1638                               
1639                        else:
1640                                self.showDialog("Problem: password must be not null!", gtk.MESSAGE_ERROR)
1641                except Exception, e:
1642                        self.showDialog("Problem: "+str(e),gtk.MESSAGE_ERROR)
1643       
1644
1645
1646               
1647       
1648               
1649
1650
1651
1652               
1653
1654        def showDialog(self, message, type):
1655                dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
1656                dialog.run()
1657                dialog.destroy()     
1658
1659
1660             
1661               
1662               
1663
1664
1665
1666
1667class Newuser(gtk.Window):
1668        global backend
1669        __parent = None
1670        #backend = backend
1671        # Groups of user, the value is the name of group, the key is the description showed in
1672        # user creation gui. It searchs for group in system group list.
1673
1674        __groupList = getPermissions()
1675
1676        def __init__(self, iconView):
1677                gtk.Window.__init__(self)
1678                self.__parent = iconView
1679                mainFrame = gtk.Frame(_("New User"))
1680                vboxMain = gtk.VBox()
1681                mainTable = gtk.Table(8,2,False)
1682
1683                fieldLabel = gtk.Label(_("<b><big>Base preferences</big></b>"))
1684                fieldLabel.set_alignment(0.1,0.3)
1685                fieldLabel.set_use_markup(True)
1686
1687                mainTable.attach(fieldLabel,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=20,xpadding=10)
1688
1689
1690         
1691                labelRealname = gtk.Label(_("Real name"))
1692                al1 = gtk.Alignment(xalign=0.5)
1693                al1.add(labelRealname)
1694                entryRealname = gtk.Entry()               
1695                mainTable.attach(al1,0,1,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=20,xpadding=10)
1696                mainTable.attach(entryRealname,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1697               
1698
1699
1700
1701                labelUsername = gtk.Label(_("Username"))
1702                al2 = gtk.Alignment(xalign=0.5)
1703                al2.add(labelUsername)
1704
1705                labelUsername.set_justify(gtk.JUSTIFY_LEFT)
1706
1707                entryUsername = gtk.Entry()               
1708                mainTable.attach(al2,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1709                mainTable.attach(entryUsername,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1710
1711
1712
1713
1714                labelPassword = gtk.Label(_("Password"))
1715                al3 = gtk.Alignment(xalign=0.5)
1716                al3.add(labelPassword)
1717               
1718
1719                labelPassword.set_justify(gtk.JUSTIFY_LEFT)
1720
1721                entryPassword = gtk.Entry()               
1722                entryPassword.set_visibility(False)
1723                mainTable.attach(al3,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1724                mainTable.attach(entryPassword,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1725
1726
1727 
1728
1729
1730                labelPasswordConferma = gtk.Label(_("Confirm"))
1731                al4 = gtk.Alignment(xalign=0.5)
1732                al4.add(labelPasswordConferma)
1733
1734                entryPasswordConferma = gtk.Entry()
1735                entryPasswordConferma.set_visibility(False)
1736
1737                mainTable.attach(al4,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1738                mainTable.attach(entryPasswordConferma,1,2,4,5,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1739
1740
1741                #Home directory selection
1742                labelHome = gtk.Label(_("Home directory"))
1743                al5 = gtk.Alignment(xalign=0.5)
1744                al5.add(labelHome)
1745
1746                entryHome= gtk.Entry()
1747
1748                mainTable.attach(al5,0,1,5,6,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1749                mainTable.attach(entryHome,1,2,5,6,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1750
1751
1752                # Maing group selection comboBox
1753                groupComboLabel = gtk.Label(_("Main group"))
1754                al6 = gtk.Alignment(xalign=0.5)
1755                al6.add(groupComboLabel)
1756
1757                groupComboBox = gtk.ComboBox()
1758                groupComboBox.set_model(self.__create_group_model_icon())
1759                cell = gtk.CellRendererText()
1760                groupComboBox.pack_start(cell)
1761                groupComboBox.add_attribute(cell, 'text', 0)
1762                groupComboBox.prepend_text("Default Group")
1763                groupComboBox.set_active(0)
1764
1765                mainTable.attach(al6,0,1,6,7,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=30)
1766                mainTable.attach(groupComboBox,1,2,6,7,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1767
1768                groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
1769                groupLabel.set_alignment(0.1,0.3)
1770                groupLabel.set_use_markup(True)
1771
1772                mainTable.attach(groupLabel,0,1,7,8,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
1773
1774
1775
1776
1777
1778
1779                groups = backend.getUnixGroups()
1780                groupsToShow = {}
1781                 
1782                for group in self.__groupList:
1783                        if self.__groupList[group] in groups:
1784                                # A dictionary of checkButtons
1785                                groupsToShow[group] = gtk.CheckButton(group)
1786                                #groupsToShow[group].set_alignment(0,0)
1787                               
1788                       
1789                l = len(groupsToShow)
1790               
1791                if l%2 == 0: 
1792                        groupTable = gtk.Table(l/2,2,False)
1793                        groupTable.set_homogeneous(False)
1794                        list = groupsToShow.keys()
1795                        for i in range(l):
1796                                if i%2 == 0: # SX
1797                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=50,ypadding=5)
1798
1799 
1800                                else: # DX
1801                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i)
1802                         
1803                else:
1804                        groupTable = gtk.Table(l/2+1,2,False)
1805                        groupTable.set_homogeneous(False)
1806
1807                        list = groupsToShow.keys()
1808                        for i in range(l):
1809                                if i%2 == 0: # SX
1810                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=50,ypadding=5)
1811
1812 
1813                                else: # DX
1814                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i)
1815                         
1816
1817               
1818
1819                       
1820               
1821                """
1822                fieldLabel = gtk.Label(_("<b><big>Base preferences</big></b>"))
1823                fieldLabel.set_alignment(0.1,0.3)
1824                fieldLabel.set_use_markup(True)
1825
1826
1827               
1828                groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
1829                groupLabel.set_alignment(0.1,0.3)
1830                groupLabel.set_use_markup(True)
1831                """
1832                #vboxMain.pack_start(fieldLabel)
1833
1834                vboxMain.pack_start(mainTable)
1835                #vboxMain.pack_start(groupLabel,padding=10)
1836
1837                vboxMain.pack_start(groupTable)
1838
1839
1840                mainFrame.add(vboxMain)
1841                v = gtk.VBox()
1842                v.pack_start(mainFrame)
1843                save = gtk.Button(stock="gtk-add")
1844                save.connect("clicked", self.__save_clicked,entryRealname,entryUsername,entryPassword,entryPasswordConferma,entryHome,groupsToShow,self.__groupList, groupComboBox)
1845
1846                cancel = gtk.Button(stock="gtk-cancel")
1847                cancel.connect("clicked",self.__cancel_clicked)
1848                hboxButton = gtk.HBox()
1849                hboxButton.pack_end(save,False,False)
1850                hboxButton.pack_end(cancel,False,False)
1851                v.pack_start(hboxButton)
1852
1853
1854                self.add(v)
1855               
1856
1857        def __save_clicked(self,button, entryRealname,entryUsername,entryPassword,entryPasswordConferma,entryHome,groupsToShow, groupList,groupComboBox):
1858                """ Check fields and add the user to Ldap Server """ 
1859                global backend
1860               
1861                try:
1862                        if not entryRealname.get_text():
1863                                self.showDialog(_("Real name must be not empty"),gtk.MESSAGE_ERROR)
1864                                return
1865                               
1866                        if not entryUsername.get_text():
1867                                self.showDialog(_("Username must be not empty"),gtk.MESSAGE_ERROR)
1868                                return
1869                               
1870                        if not entryPassword.get_text():
1871                                self.showDialog(_("Password must be not empty"),gtk.MESSAGE_ERROR)
1872                                return
1873
1874                        if not entryPasswordConferma.get_text():
1875                                self.showDialog(_("Confirm password must be not empty"),gtk.MESSAGE_ERROR)
1876                                return
1877
1878                        if not entryPassword.get_text() == entryPasswordConferma.get_text():
1879                                self.showDialog(_("Password fields does not match."),gtk.MESSAGE_ERROR)
1880                                return
1881
1882                        active = []
1883                        for group in groupsToShow:
1884                                if groupsToShow[group].get_active():
1885                                        active.append(groupList[group])
1886
1887                        # Selection of main group
1888                        model = groupComboBox.get_model()
1889                        activeG = groupComboBox.get_active()
1890                        if activeG < 0:
1891                                mainGroup = None
1892                        else:
1893                                mainGroup = model[activeG][0]
1894                        n = LdapUser(['inetOrgPerson','shadowAccount','sambaSamAccount'])
1895                        n.setAttribute("cn", entryUsername.get_text())
1896                        n.setAttribute("sn", entryUsername.get_text())
1897                        uid = backend.getNewLdapID()
1898                        if not mainGroup:
1899                                gid = backend.getGroup("Domain Users").getGid().getValue()       
1900                        else:
1901                                if mainGroup == "Default Group":
1902                                        gid = backend.getGroup("Domain Users").getGid().getValue()
1903                                       
1904                                else:
1905                                        gid = backend.getGroup(mainGroup).getGid().getValue()
1906
1907                        attrObjectClass = ListAttribute("objectClass")
1908                        attrObjectClass.setValue(['posixAccount','top','inetOrgPerson','shadowAccount','sambaSamAccount'])
1909                        n.setAttribute("objectClass",attrObjectClass)
1910                        n.setAttribute("gidNumber", str(gid))
1911                        #n.setAttribute("homeDirectory","/home/"+entryUsername.get_text())
1912                        homePrefix = "/home/"
1913                        lenHome = len(homePrefix)
1914                        directoryHome = entryHome.get_text().replace(" ","")                               
1915
1916
1917           
1918                        #Check home directory
1919                        dir = entryHome.get_text().strip()
1920
1921                        if not dir:
1922                                n.setAttribute("homeDirectory","/home/"+entryUsername.get_text())
1923                        else:
1924                                if not self.__checkHomeDirectory(dir):
1925                                        self.showDialog(_("Invalid characters in home directory field!"),gtk.MESSAGE_ERROR)
1926                                        return
1927                                log.debug("Dirrecotry: "+dir)
1928                                n.setAttribute("homeDirectory",dir) 
1929                               
1930                           
1931                        n.setAttribute("loginShell","/bin/sh")
1932
1933                        n.setAttribute("uid", entryUsername.get_text())
1934                        n.setAttribute("uidNumber", str(uid))
1935                        n.setAttribute("gecos", entryRealname.get_text())
1936                        n.setAttribute("userPassword", entryPassword.get_text())
1937                        n.setAttribute("sambaSID", backend.getUserSID(uid))
1938                       
1939                        #New attributes
1940                        n.setAttribute("sambaLogonTime","0")
1941                        n.setAttribute("sambaLogoffTime", "0")
1942                        n.setAttribute("sambaKickoffTime","0")
1943                        n.setAttribute("sambaPwdCanChange","0")
1944                        n.setAttribute("sambaHomeDrive","H:")
1945                        n.setAttribute("sambaPwdCanChange","0")
1946                        n.setAttribute("sambaHomeDrive","H:")
1947                        n.setAttribute("sambaPrimaryGroupSID",backend.getGroupSID(str(gid)))
1948                        n.setAttribute("sambaAcctFlags", "[UX         ]")
1949
1950
1951
1952
1953
1954
1955
1956                        backend.addLdapUser2(n,active)
1957                     
1958                        self.__parent.set_model(self.__create_user_model_icon()) 
1959                        self.__parent.show()
1960                        backend = Backend()
1961
1962                        self.destroy() 
1963
1964
1965                                       
1966                               
1967                except Exception, inst:
1968                        username = entryUsername.get_text()
1969                        if backend.isUser(username):
1970                                if backend.getUser(username).getGid().getValue() == str(uid): 
1971                                        backend.deleteLdapUser2(username)
1972
1973                        self.showDialog(_("Problems adding new user")+"\n"+str(inst),gtk.MESSAGE_ERROR)
1974
1975        def __checkHomeDirectory(self,dir):
1976                """ Do checks on dir string """
1977                import re
1978                import os
1979           
1980                pat1 = re.compile(r"/home(/[a-zA-z0-9]+)+(/)?$")
1981                res1 = pat1.match(dir)       
1982                if res1 == None:
1983                        return False
1984
1985                sp = os.path.split(dir)[0]
1986                if dir.endswith("/"):
1987                        if not os.path.isdir(os.path.dirname(sp)):
1988                                return False
1989                else:
1990                        if not os.path.isdir(sp):
1991                                return False
1992           
1993                return True
1994
1995
1996
1997        def __create_user_model(self):
1998                """ Create model for list of users """
1999                list = backend.getUsersList()
2000
2001                # create list store
2002                model = gtk.ListStore(
2003                gobject.TYPE_STRING,
2004                gobject.TYPE_STRING,
2005                gobject.TYPE_STRING,
2006                )
2007                # add items
2008                for item in list:
2009                        iter = model.append()
2010                        type = ""
2011                        if backend.isUnixUser(item):
2012                                type=_("locale")
2013                        else:
2014                                if backend.isLdapUser(item):
2015                                        type = _("network")       
2016                        #User main group
2017                        mainGroupLabel = _("main group")
2018                        mainGroupGid = backend.getUser(item).getGid().getValue()
2019                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2020                        model.set(iter, 0, item, 1, type,  2, mainGroupName )
2021
2022                return model
2023
2024        def __create_user_model_icon(self):
2025                """ Create model for list of users """
2026                #list = backend.getUsersList()
2027                list = backend.getLdapUsers()
2028                showUser = []
2029                for us in list:
2030                        #num = int(backend.getUser(us).getUidNumber().getValue())
2031                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2032                        showUser.append(us)
2033
2034
2035                # create list store
2036                model = gtk.ListStore(
2037                gobject.TYPE_STRING,
2038                gtk.gdk.Pixbuf,
2039                )
2040                # add items
2041                for item in showUser:
2042                        iter = model.append()
2043                        type = ""
2044                        if backend.isUnixUser(item):
2045                                type=_("locale")
2046                        else:
2047                                if backend.isLdapUser(item):
2048                                        type = _("network")       
2049                        #User main group
2050                        if backend.isEnabled(item):
2051                                #pix = art.get_pixbuf_from_name("stock_person")
2052                                pix = art.get_pixbuf_from_name("user")
2053
2054                        else:
2055                                #pix = art.get_pixbuf_from_name("user_disabled")
2056                                pix = art.get_pixbuf_from_name("user-disabled")
2057
2058                        mainGroupLabel = _("main group")
2059                        mainGroupGid = backend.getUser(item).getGid().getValue()
2060                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2061                        #tooltip = gtk.Tooltips()
2062                        #tooltip.set_tip(item,mainGroupName)     
2063
2064                        model.set(iter,0,item,1, pix)
2065
2066                return model
2067
2068
2069
2070
2071
2072        def __create_group_model(self):
2073                """ Create model for list of groups """
2074                list = backend.getGroupsList()
2075                # create list store
2076                model = gtk.ListStore(
2077                gobject.TYPE_STRING,
2078                gobject.TYPE_STRING,
2079                )
2080                # add items
2081                for item in list:
2082                        iter = model.append()
2083                        type = ""
2084                        if backend.isUnixGroup(item):
2085                                type=_("locale")
2086                        else:
2087                                if backend.isLdapGroup(item):
2088                                        type = _("network")       
2089                                else:
2090                                        log.debug(_("Warning: problem identifying group type!"))
2091                        model.set (iter, 0, item, 1, type )
2092                return model
2093
2094        def __create_group_model_icon(self):
2095                """ Create model for list of groups """
2096                #list = backend.getGroupsList()
2097                list = backend.getLdapGroups()
2098
2099                showGroup = []
2100                for gr in list:
2101                        #num = int(backend.getGroup(gr).getGid().getValue())
2102                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2103                        showGroup.append(gr)
2104
2105
2106                # create list store
2107                model = gtk.ListStore(
2108                gobject.TYPE_STRING,
2109                gtk.gdk.Pixbuf,
2110                )
2111                # add items
2112                for item in showGroup:
2113                        iter = model.append()
2114                        type = ""
2115                        if backend.isUnixGroup(item):
2116                                type=_("locale")
2117                        else:
2118                                if backend.isLdapGroup(item):
2119                                        type = _("network")       
2120                                else:
2121                                        log.debug(_("Warning: problem identifying group type!"))
2122                        #pix = art.get_pixbuf_from_name("stock_people")
2123                        pix = art.get_pixbuf_from_name("group")
2124
2125
2126                        model.set (iter, 0, item, 1, pix )
2127                return model
2128
2129
2130
2131       
2132
2133        def __cancel_clicked(self, button):
2134                self.destroy()
2135
2136        def showDialog(self, message, type):
2137               dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
2138               dialog.run()
2139               dialog.destroy()     
2140
2141
2142class MassCreation(gtk.Window):
2143        global backend
2144        __parent = None 
2145        #backend = backend
2146        # Groups of user, the value is the name of group, the key is the description showed in
2147        # user creation gui. It searchs for group in system group list.
2148
2149        __groupList = getPermissions()
2150
2151        def __init__(self, iconView):
2152                gtk.Window.__init__(self)
2153                mainFrame = gtk.Frame(_("Massive User Creation"))
2154                self.__parent = iconView
2155 
2156                notebook = gtk.Notebook()
2157                # SIMPLE MASSIVE CREATION
2158                vbox = gtk.VBox()
2159                tableSimple = gtk.Table(6,2,False)
2160                labelSimple = gtk.Label(_("<b><big>Simple Massive User Creation</big></b>"))
2161                labelSimple.set_use_markup(True)
2162                tableSimple.attach(labelSimple, 0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2163
2164
2165               
2166                labelSimple.set_alignment(0.1,0.2)
2167                labelSimpleDescription = gtk.Label(_("<small>This panel lets you to create a certain number of user.\nUsername and password will be the same.</small>"))
2168                 
2169                #labelSimpleDescription.set_alignment(0.2,0.2)
2170                labelSimpleDescription.set_use_markup(True)
2171                tableSimple.attach(labelSimpleDescription, 1,2,1,2,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2172
2173
2174               
2175                numberLabel = gtk.Label(_("User number"))
2176                numberLabel.set_alignment(0.4,0.2)
2177                spin = gtk.SpinButton()
2178                spin.set_range(1,100)
2179                spin.set_increments(1,1)
2180                tableSimple.attach(numberLabel,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2181
2182                tableSimple.attach(spin,1,2,2,3,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2183
2184
2185                prefixLabel = gtk.Label(_("Username prefix"))
2186                prefixLabel.set_alignment(0.45,0.2)
2187
2188
2189                prefixEntry = gtk.Entry()
2190                tableSimple.attach(prefixLabel,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2191
2192                tableSimple.attach(prefixEntry,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2193
2194
2195                # Maing group selection comboBox
2196                groupComboLabel = gtk.Label(_("Main group"))
2197                groupComboLabel.set_alignment(0.395,0.2)
2198
2199
2200                groupComboBox = gtk.ComboBox()
2201                groupComboBox.set_model(self.__create_group_model_icon())
2202                cell = gtk.CellRendererText()
2203                groupComboBox.pack_start(cell)
2204                groupComboBox.add_attribute(cell, 'text', 0)
2205                groupComboBox.prepend_text("Default Group")
2206                groupComboBox.set_active(0)
2207
2208                tableSimple.attach(groupComboLabel,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=10)
2209                tableSimple.attach(groupComboBox,1,2,4,5,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2210
2211                groupLabel = gtk.Label(_("<b><big>User privileges</big></b>"))
2212                groupLabel.set_alignment(0.1,0.2)
2213                groupLabel.set_use_markup(True)
2214
2215                tableSimple.attach(groupLabel,0,1,5,6,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2216               
2217
2218
2219
2220                groups = backend.getUnixGroups()
2221                groupsToShow = {}
2222               
2223                for group in self.__groupList:
2224                        if self.__groupList[group] in groups:
2225                                # A dictionary of checkButtons
2226                                groupsToShow[group] = gtk.CheckButton(group)
2227                       
2228                l = len(groupsToShow)
2229               
2230                if l%2 == 0: 
2231                        groupTable = gtk.Table(l/2,2,False)
2232                        groupTable.set_homogeneous(False)
2233                        list = groupsToShow.keys()
2234                        for i in range(l):
2235                                if i%2 == 0: # SX
2236                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=94,ypadding=5)
2237
2238 
2239                                else: # DX
2240                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i)
2241                         
2242                else:
2243                        groupTable = gtk.Table(l/2+1,2,False)
2244                        groupTable.set_homogeneous(False)
2245
2246                        list = groupsToShow.keys()
2247                        for i in range(l):
2248                                if i%2 == 0: # SX
2249                                        groupTable.attach(groupsToShow[list[i]],0,1,i,i+1,xpadding=94,yoptions=gtk.FILL,ypadding=5)
2250
2251 
2252                                else: # DX
2253                                        groupTable.attach(groupsToShow[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
2254
2255
2256
2257                v = gtk.VBox()
2258                save = gtk.Button(stock="gtk-apply")
2259                save.connect("clicked", self.apply_simple_clicked,spin,prefixEntry,groupsToShow,self.__groupList, groupComboBox)
2260
2261                cancel = gtk.Button(stock="gtk-cancel")
2262                cancel.connect("clicked",self.cancel_clicked)
2263                h = gtk.HBox()
2264                h.pack_end(save,False,False)
2265                h.pack_end(cancel,False,False)
2266               
2267
2268
2269                #vbox.pack_start(labelSimple,True,True,padding=10)
2270                #vbox.pack_start(labelSimpleDescription)
2271                vbox.pack_start(tableSimple)
2272
2273                #vbox.pack_start(groupLabel, False,False, padding=10)
2274                #vbox.pack_start(labelGroupDescription)
2275                vbox.pack_start(groupTable)
2276                sep = gtk.HSeparator()
2277                vbox.pack_start(sep)
2278                vbox.pack_start(h)
2279                simple = gtk.Label(_("Simple"))
2280                notebook.insert_page(vbox,tab_label=simple)
2281
2282                # ADVANCED MASSIVE USER CREATION
2283               
2284
2285
2286                vboxAdvanced = gtk.VBox()
2287                tableAdvanced = gtk.Table(4,3,False)
2288                labelAdvanced = gtk.Label(_("<b><big>Advanced Massive User Creation</big></b>"))
2289                labelAdvanced.set_use_markup(True)
2290
2291                tableAdvanced.attach(labelAdvanced,0,1,0,1,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=25)
2292
2293
2294                groupLabelA = gtk.Label(_("<b><big>User privileges</big></b>"))
2295                groupLabelA.set_alignment(0.1,0.2)
2296                groupLabelA.set_use_markup(True)
2297
2298
2299               
2300                labelAdvanced.set_alignment(0.1,0.2)
2301                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>"))
2302                 
2303                #labelAdvancedDescription.set_alignment(0.2,0.2)
2304                labelAdvancedDescription.set_use_markup(True)
2305                tableAdvanced.attach(labelAdvancedDescription,1,2,1,2,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2306
2307
2308               
2309
2310
2311                fileLabel = gtk.Label(_("Users file"))
2312                fileLabel.set_alignment(0.43,0.2)
2313
2314
2315                fileEntry = gtk.Entry()
2316                tableAdvanced.attach(fileLabel,0,1,2,3,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2317                tableAdvanced.attach(fileEntry,1,2,2,3,xpadding=5)
2318                fileButton = gtk.Button(_("..."))
2319                fileButton.connect("clicked", self.fileChooser, fileEntry)
2320                tableAdvanced.attach(fileButton,2,3,2,3,xpadding=5, xoptions=gtk.SHRINK, yoptions=gtk.SHRINK)
2321
2322
2323                # Maing group selection comboBox
2324                groupComboLabelA = gtk.Label(_("Main group"))
2325                groupComboLabelA.set_alignment(0.45,0.2)
2326                groupComboBoxA = gtk.ComboBox()
2327                groupComboBoxA.set_model(self.__create_group_model_icon())
2328                cellA = gtk.CellRendererText()
2329                groupComboBoxA.pack_start(cellA)
2330                groupComboBoxA.add_attribute(cellA, 'text', 0)
2331                groupComboBoxA.prepend_text("Default Group")
2332                groupComboBoxA.set_active(0)
2333
2334                tableAdvanced.attach(groupComboLabelA,0,1,3,4,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2335                tableAdvanced.attach(groupComboBoxA,1,2,3,4,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2336
2337
2338                tableAdvanced.attach(groupLabelA,0,1,4,5,xoptions=gtk.FILL,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2339
2340
2341
2342
2343
2344
2345                groupsA = backend.getUnixGroups()
2346                groupsToShowA = {}
2347               
2348                for group in self.__groupList:
2349                        if self.__groupList[group] in groups:
2350                                # A dictionary of checkButtons
2351                                groupsToShowA[group] = gtk.CheckButton(group)
2352                       
2353                l = len(groupsToShowA)
2354               
2355                if l%2 == 0: 
2356                        groupTableA = gtk.Table(l/2,2,False)
2357                        groupTableA.set_homogeneous(False)
2358                        list = groupsToShowA.keys()
2359                        for i in range(l):
2360                                if i%2 == 0: # SX
2361                                        groupTableA.attach(groupsToShowA[list[i]],0,1,i,i+1,xpadding=120,yoptions=gtk.FILL,ypadding=5)
2362
2363 
2364                                else: # DX
2365                                        groupTableA.attach(groupsToShowA[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
2366                         
2367                else:
2368                        groupTableA = gtk.Table(l/2+1,2,False)
2369                        groupTableA.set_homogeneous(False)
2370
2371                        list = groupsToShowA.keys()
2372                        for i in range(l):
2373                                if i%2 == 0: # SX
2374                                        groupTableA.attach(groupsToShowA[list[i]],0,1,i,i+1,xpadding=120,yoptions=gtk.FILL,ypadding=5)
2375
2376                                else: # DX
2377                                        groupTableA.attach(groupsToShowA[list[i]],1,2,i-1,i,yoptions=gtk.FILL)
2378
2379               
2380               
2381
2382
2383
2384                vAdvanced = gtk.VBox()
2385                saveAdvanced = gtk.Button(stock="gtk-apply")
2386                saveAdvanced.connect("clicked", self.apply_advanced_clicked,fileEntry,groupsToShowA,self.__groupList,groupComboBoxA)
2387
2388                cancelAdvanced = gtk.Button(stock="gtk-cancel")
2389                cancelAdvanced.connect("clicked",self.cancel_clicked)
2390                hAdvanced = gtk.HBox()
2391                hAdvanced.pack_end(saveAdvanced,False,False)
2392                hAdvanced.pack_end(cancelAdvanced,False,False)
2393               
2394
2395
2396                #vboxAdvanced.pack_start(labelAdvanced,False,False,padding=10)
2397                #vboxAdvanced.pack_start(labelAdvancedDescription)
2398                vboxAdvanced.pack_start(tableAdvanced)
2399
2400                #vboxAdvanced.pack_start(groupLabelA, False,False, padding=10)
2401                #vbox.pack_start(labelGroupDescription)
2402                vboxAdvanced.pack_start(groupTableA)
2403                sepAdvanced = gtk.HSeparator()
2404
2405                vboxAdvanced.pack_start(sepAdvanced)
2406                vboxAdvanced.pack_start(hAdvanced)
2407                advanced = gtk.Label(_("Advanced"))
2408                notebook.insert_page(vboxAdvanced,tab_label=advanced)
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424                # Add tabs to main window
2425                self.add(notebook)
2426                self.show_all()
2427               
2428               
2429        def cancel_clicked(self, button):
2430                self.destroy()
2431
2432
2433        def apply_simple_clicked(self, button,spin,prefixEntry,groupsToShow,groupList,groupComboBox):
2434                """ Called when apply button is clicked on simple user mass creation """
2435                global backend
2436                if not prefixEntry.get_text():
2437                        self.showDialog(_("Username prefix must be not empty"),gtk.MESSAGE_ERROR)
2438                        return
2439               
2440                createdUsers = []
2441                failedUsers = []
2442                passwordUsers = {}
2443
2444                active = [] # User's groups
2445                for group in groupsToShow:
2446                        if groupsToShow[group].get_active():
2447                                active.append(groupList[group])
2448                #print active
2449                model = groupComboBox.get_model()
2450                activeG = groupComboBox.get_active()
2451                if activeG < 0:
2452                        mainGroup = None
2453                else:
2454                        mainGroup = model[activeG][0]
2455
2456                # Main group selection
2457
2458                if not mainGroup:
2459                        gid = backend.getGroup("Domain Users").getGid().getValue()       
2460                else:
2461                        if mainGroup == "Default Group":
2462                                gid = backend.getGroup("Domain Users").getGid().getValue()
2463                               
2464                        else:
2465                                gid = backend.getGroup(mainGroup).getGid().getValue()
2466
2467
2468
2469                for i in range(spin.get_value_as_int()):
2470                        value = i+1
2471                        username = prefixEntry.get_text()+str(value)
2472
2473
2474
2475
2476                        n = LdapUser(['inetOrgPerson','shadowAccount','sambaSamAccount'])
2477
2478                        attrObjectClass = ListAttribute("objectClass")
2479                        attrObjectClass.setValue(['posixAccount','top','inetOrgPerson','shadowAccount','sambaSamAccount'])
2480                        n.setAttribute("objectClass",attrObjectClass)
2481
2482                        n.setAttribute("cn", username)
2483                        n.setAttribute("sn", username)
2484                        uid = backend.getNewLdapID()
2485                        n.setAttribute("gidNumber", str(gid))
2486                        n.setAttribute("homeDirectory","/home/"+username)
2487                        n.setAttribute("loginShell","/bin/sh")
2488                        n.setAttribute("uid", username)
2489                        n.setAttribute("uidNumber", str(uid))
2490                        n.setAttribute("gecos", username)
2491                        password = backend.genPassword()
2492                        n.setAttribute("userPassword", password)
2493                        n.setAttribute("sambaSID", backend.getUserSID(uid))
2494
2495                        #New attributes
2496                        n.setAttribute("sambaLogonTime","0")
2497                        n.setAttribute("sambaLogoffTime", "0")
2498                        n.setAttribute("sambaKickoffTime","0")
2499                        n.setAttribute("sambaPwdCanChange","0")
2500                        n.setAttribute("sambaHomeDrive","H:")
2501                        n.setAttribute("sambaPwdCanChange","0")
2502                        n.setAttribute("sambaHomeDrive","H:")
2503                        n.setAttribute("sambaPrimaryGroupSID",backend.getGroupSID(str(gid)))
2504                        n.setAttribute("sambaAcctFlags", "[UX         ]")
2505
2506
2507                        try:
2508                                backend.addLdapUser2(n,active)
2509                                createdUsers.append(username)
2510                                passwordUsers[username] = password
2511                        except Exception, e:
2512                                #TODO: problem, if user already exists how to decide if user is old or new (if new => remove user)
2513                                if backend.isUser(username):
2514                                        if backend.getUser(username).getGid().getValue() == str(uid): 
2515                                                backend.deleteLdapUser2(username)
2516                                failedUsers.append(username)
2517                c = ""
2518                for us in createdUsers:
2519                      c = c + us + ", " 
2520                c=c.strip(" ")
2521                c=c.strip(",")
2522                f = ""
2523                for us in failedUsers:
2524                      f = f + us + ", " 
2525                f=f.strip(" ")
2526                f=f.strip(",")
2527                if c and f:
2528                        self.showDialog("Created: "+c+"\nFailed: "+f,gtk.MESSAGE_INFO)
2529                if c and not f:
2530                        self.showDialog("Created: "+c,gtk.MESSAGE_INFO)
2531                if not c and f:
2532                        self.showDialog("Failed : "+f,gtk.MESSAGE_INFO)
2533     
2534                # Update user list
2535                # Manca da ricaricare tutta la lista di utenti perchÚ altrimenti in memoria non ci sono e non possono essere cancellati
2536
2537                self.__parent.set_model(self.__create_user_model_icon()) 
2538                self.__parent.show()
2539                backend = Backend()
2540
2541                file = open("/tmp/filePassword","w")
2542                for user in passwordUsers:
2543                        file.write(user+","+passwordUsers[user]+"\n")
2544                file.close()
2545
2546                self.destroy()
2547                               
2548
2549                       
2550               
2551
2552                               
2553
2554
2555        def apply_advanced_clicked(self, button, fileEntry, groupsToShow, groupList, groupComboBox):
2556                """ Called when apply button is clicked on simple user mass creation """
2557                global backend
2558                if not fileEntry.get_text():
2559                        self.showDialog(_("File name must be not empty."),gtk.MESSAGE_ERROR)
2560                        return
2561               
2562                createdUsers = []
2563                failedUsers = []
2564                passwordUsers = {}
2565
2566                active = [] # User's groups
2567                for group in groupsToShow:
2568                        if groupsToShow[group].get_active():
2569                                active.append(groupList[group])
2570
2571                #print active
2572               
2573                model = groupComboBox.get_model()
2574                activeG = groupComboBox.get_active()
2575                if activeG < 0:
2576                        mainGroup = None
2577                else:
2578                        mainGroup = model[activeG][0]
2579                # Main group selection
2580
2581                if not mainGroup:
2582                        gid = backend.getGroup("Domain Users").getGid().getValue()       
2583                else:
2584                        if mainGroup == "Default Group":
2585                                gid = backend.getGroup("Domain Users").getGid().getValue()
2586                               
2587                        else:
2588                                gid = backend.getGroup(mainGroup).getGid().getValue()
2589
2590
2591
2592                try:
2593                        filename = fileEntry.get_text() 
2594                        reader = csv.reader(open(filename, "rb"))
2595                except Exception, e:
2596                        self.showDialog(e.strerror,gtk.MESSAGE_ERROR)
2597                        return
2598
2599                try:
2600                        for row in reader:
2601                                if row:
2602                                        # Create user
2603                                        nome = row[0]
2604                                        cognome = row[1]
2605                                        username = row[2]
2606                                        if row[0] and row[1] and row[2]:
2607
2608
2609
2610                                                n = LdapUser(['inetOrgPerson','shadowAccount','sambaSamAccount'])
2611
2612                                                attrObjectClass = ListAttribute("objectClass")
2613                                                attrObjectClass.setValue(['posixAccount','top','inetOrgPerson','shadowAccount','sambaSamAccount'])
2614                                                n.setAttribute("objectClass",attrObjectClass)
2615
2616
2617                                                n.setAttribute("cn", username)
2618                                                n.setAttribute("sn", username)
2619                                                uid = backend.getNewLdapID()
2620                                                n.setAttribute("gidNumber", str(gid))
2621                                                n.setAttribute("homeDirectory","/home/"+username)
2622                                                n.setAttribute("loginShell","/bin/sh")
2623                                                n.setAttribute("uid", username)
2624                                                n.setAttribute("uidNumber", str(uid))
2625                                                n.setAttribute("gecos", nome+cognome)
2626                                                password = backend.genPassword()
2627                                                n.setAttribute("userPassword", password)
2628                                                n.setAttribute("sambaSID", backend.getUserSID(uid))
2629                                                #New attributes
2630                                                n.setAttribute("sambaLogonTime","0")
2631                                                n.setAttribute("sambaLogoffTime", "0")
2632                                                n.setAttribute("sambaKickoffTime","0")
2633                                                n.setAttribute("sambaPwdCanChange","0")
2634                                                n.setAttribute("sambaHomeDrive","H:")
2635                                                n.setAttribute("sambaPwdCanChange","0")
2636                                                n.setAttribute("sambaHomeDrive","H:")
2637                                                n.setAttribute("sambaPrimaryGroupSID",backend.getGroupSID(str(gid)))
2638                                                n.setAttribute("sambaAcctFlags", "[UX         ]")
2639
2640
2641                                                try:
2642                                                        backend.addLdapUser2(n,active)
2643                                                        createdUsers.append(username)
2644                                                        passwordUsers[username] = password
2645                                                except:
2646                                                        if backend.isUser(username):
2647                                                                if backend.getUser(username).getGid().getValue() == str(uid): 
2648
2649                                                                        backend.deleteLdapUser2(username)
2650                                                        failedUsers.append(username)
2651                                        else:
2652                                                self.showDialog("Problem parsing user file!", gtk.MESSAGE_ERROR)
2653
2654                                       
2655                except csv.Error, e:
2656                        log.debug(str(e))
2657                        self.showDialog('file %s, line %d: %s' % (filename, reader.line_num, e),gtk.MESSAGE_ERROR)
2658                except IndexError,e:
2659                        self.showDialog("Error reading file!",gtk.MESSAGE_ERROR)
2660
2661                c = ""
2662                for us in createdUsers:
2663                      c = c + us + ", " 
2664                c=c.strip(" ")
2665                c=c.strip(",")
2666                f = ""
2667                for us in failedUsers:
2668                      f = f + us + ", " 
2669                f=f.strip(" ")
2670                f=f.strip(",")
2671                if c and f:
2672                        self.showDialog("Created: "+c+"\nFailed: "+f,gtk.MESSAGE_INFO)
2673                if c and not f:
2674                        self.showDialog("Created: "+c,gtk.MESSAGE_INFO)
2675                if not c and f:
2676                        self.showDialog("Failed : "+f,gtk.MESSAGE_INFO)
2677
2678
2679       
2680                # Update user list
2681                # Manca da ricaricare tutta la lista di utenti perchÚ altrimenti in memoria non ci sono e non possono essere cancellati
2682                self.__parent.set_model(self.__create_user_model_icon()) 
2683                self.__parent.show()
2684                backend = Backend()
2685                file = open("/tmp/filePassword","w")
2686                for user in passwordUsers:
2687                        file.write(user+","+passwordUsers[user]+"\n")
2688                file.close()
2689
2690                self.destroy()
2691
2692
2693 
2694
2695
2696        def fileChooser(self, button, entry):
2697                dialog = gtk.FileChooserDialog(_("Open.."),
2698                                       None,
2699                                       gtk.FILE_CHOOSER_ACTION_OPEN,
2700                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
2701                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
2702                dialog.set_default_response(gtk.RESPONSE_OK)
2703                filter = gtk.FileFilter()
2704                filter.set_name(_("All files"))
2705                filter.add_pattern("*")
2706                dialog.add_filter(filter)
2707                response = dialog.run()
2708                if response == gtk.RESPONSE_OK:
2709                        entry.set_text(dialog.get_filename())
2710                        dialog.destroy()
2711                elif response == gtk.RESPONSE_CANCEL:
2712                        dialog.destroy()
2713                       
2714        def showDialog(self, message, type):
2715               dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
2716               dialog.run()
2717               dialog.destroy()     
2718        def __create_user_model(self):
2719                """ Create model for list of users """
2720                list = backend.getUsersList()
2721
2722                # create list store
2723                model = gtk.ListStore(
2724                gobject.TYPE_STRING,
2725                gobject.TYPE_STRING,
2726                gobject.TYPE_STRING,
2727                )
2728                # add items
2729                for item in list:
2730                        iter = model.append()
2731                        type = ""
2732                        if backend.isUnixUser(item):
2733                                type=_("locale")
2734                        else:
2735                                if backend.isLdapUser(item):
2736                                        type = _("network")       
2737                        #User main group
2738                        mainGroupLabel = _("main group")
2739                        mainGroupGid = backend.getUser(item).getGid().getValue()
2740                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2741                        model.set(iter, 0, item, 1, type,  2, mainGroupName )
2742
2743                return model
2744
2745
2746        def __create_user_model_icon(self):
2747                """ Create model for list of users """
2748                #list = backend.getUsersList()
2749                list = backend.getLdapUsers()
2750                showUser = []
2751                for us in list:
2752                        #num = int(backend.getUser(us).getUidNumber().getValue())
2753                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2754                        showUser.append(us)
2755
2756
2757                # create list store
2758                model = gtk.ListStore(
2759                gobject.TYPE_STRING,
2760                gtk.gdk.Pixbuf,
2761                )
2762                # add items
2763                for item in showUser:
2764                        iter = model.append()
2765                        type = ""
2766                        if backend.isUnixUser(item):
2767                                type=_("locale")
2768                        else:
2769                                if backend.isLdapUser(item):
2770                                        type = _("network")       
2771                        #User main group
2772                        if backend.isEnabled(item):
2773                                #pix = art.get_pixbuf_from_name("stock_person")
2774                                pix = art.get_pixbuf_from_name("user")
2775
2776                        else:
2777                                #pix = art.get_pixbuf_from_name("user_disabled")
2778                                pix = art.get_pixbuf_from_name("user-disabled")
2779
2780
2781                        mainGroupLabel = _("main group")
2782                        mainGroupGid = backend.getUser(item).getGid().getValue()
2783                        mainGroupName = backend.getGroupByGid(mainGroupGid).getName().getValue()
2784                        #tooltip = gtk.Tooltips()
2785                        #tooltip.set_tip(item,mainGroupName)     
2786
2787                        model.set(iter,0,item,1, pix)
2788
2789                return model
2790
2791
2792
2793        def __create_group_model(self):
2794                """ Create model for list of groups """
2795                list = backend.getGroupsList()
2796                # create list store
2797                model = gtk.ListStore(
2798                gobject.TYPE_STRING,
2799                gobject.TYPE_STRING,
2800                )
2801                # add items
2802                for item in list:
2803                        iter = model.append()
2804                        type = ""
2805                        if backend.isUnixGroup(item):
2806                                type=_("locale")
2807                        else:
2808                                if backend.isLdapGroup(item):
2809                                        type = _("network")       
2810                                else:
2811                                        log.debug(_("Warning: problem identifying group type!"))
2812                        model.set (iter, 0, item, 1, type )
2813                return model
2814
2815        def __create_group_model_icon(self):
2816                """ Create model for list of groups """
2817                #list = backend.getGroupsList()
2818                list = backend.getLdapGroups()
2819                showGroup = []
2820                for gr in list:
2821                        #num = int(backend.getGroup(gr).getGid().getValue())
2822                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2823                        showGroup.append(gr)
2824
2825                # create list store
2826                model = gtk.ListStore(
2827                gobject.TYPE_STRING,
2828                gtk.gdk.Pixbuf,
2829                )
2830                # add items
2831                for item in showGroup:
2832                        iter = model.append()
2833                        type = ""
2834                        if backend.isUnixGroup(item):
2835                                type=_("locale")
2836                        else:
2837                                if backend.isLdapGroup(item):
2838                                        type = _("network")       
2839                                else:
2840                                        log.debug(_("Warning: problem identifying group type!"))
2841                        #pix = art.get_pixbuf_from_name("stock_people")
2842                        pix = art.get_pixbuf_from_name("group")
2843
2844
2845                        model.set (iter, 0, item, 1, pix )
2846                return model
2847
2848
2849
2850
2851
2852
2853               
2854
2855               
2856class Newgroup(gtk.Window):
2857        global backend
2858        __parent = None
2859        #backend = backend
2860        def __init__(self, iconView):
2861                gtk.Window.__init__(self)
2862                self.__parent = iconView
2863                mainFrame = gtk.Frame(_("New Group"))
2864                vboxMain = gtk.VBox()
2865                mainTable = gtk.Table(4,2,False)
2866
2867   
2868                labelRealname = gtk.Label(_("Group name"))
2869                entryRealname = gtk.Entry()               
2870                mainTable.attach(labelRealname,0,1,0,1,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2871                mainTable.attach(entryRealname,1,2,0,1,yoptions=gtk.SHRINK,ypadding=10,xpadding=5)
2872
2873
2874
2875
2876
2877                vboxMain.pack_start(mainTable)
2878               
2879
2880
2881
2882                mainFrame.add(vboxMain)
2883                v = gtk.VBox()
2884                v.pack_start(mainFrame)
2885                save = gtk.Button(stock="gtk-add")
2886                save.connect("clicked", self.__save_clicked,entryRealname)
2887
2888                cancel = gtk.Button(stock="gtk-cancel")
2889                cancel.connect("clicked",self.__cancel_clicked)
2890                hboxButton = gtk.HBox()
2891                hboxButton.pack_end(save,False,False)
2892                hboxButton.pack_end(cancel,False,False)
2893                v.pack_start(hboxButton)
2894
2895
2896                self.add(v)
2897               
2898
2899        def __save_clicked(self,button, entryGroupname):
2900                """ Check fields and add the user to Ldap Server """ 
2901                global backend
2902               
2903                try:
2904                        if not entryGroupname.get_text():
2905                                self.showDialog(_("Group name must be not empty"),gtk.MESSAGE_ERROR)
2906                                return
2907                               
2908                        # Set group attributes
2909                        g = LdapGroup(['sambaGroupMapping'])
2910                        attrObjectClass = ListAttribute("objectClass")
2911                        attrObjectClass.setValue(['posixGroup','top','sambaGroupMapping'])
2912                        g.setAttribute("objectClass",attrObjectClass)
2913
2914                        g.setAttribute("cn", entryGroupname.get_text())
2915                        uid = backend.getNewLdapID()
2916                        g.setAttribute("gidNumber", str(uid))
2917                        sid = backend.getGroupSID(uid)
2918                        g.setAttribute("sambaSID", str(sid))
2919                        g.setAttribute("sambaGroupType","2")
2920
2921
2922                        backend.addLdapGroup2(g)
2923                     
2924                        self.__parent.set_model(self.__create_group_model_icon()) 
2925                        self.__parent.show()
2926                        backend = Backend()
2927                        self.destroy() 
2928                                         
2929                except Exception, inst:
2930                        self.showDialog(_("Problems adding new group\n"+str(inst)),gtk.MESSAGE_ERROR)
2931
2932
2933
2934
2935
2936 
2937        def __create_group_model(self):
2938                """ Create model for list of groups """
2939                list = backend.getGroupsList()
2940                # create list store
2941                model = gtk.ListStore(
2942                gobject.TYPE_STRING,
2943                gobject.TYPE_STRING,
2944                )
2945                # add items
2946                for item in list:
2947                        iter = model.append()
2948                        type = ""
2949                        if backend.isUnixGroup(item):
2950                                type=_("locale")
2951                        else:
2952                                if backend.isLdapGroup(item):
2953                                        type = _("network")       
2954                                else:
2955                                        log.debug(_("Warning: problem identifying group type!"))
2956                        model.set (iter, 0, item, 1, type )
2957                return model
2958
2959        def __create_group_model_icon(self):
2960                """ Create model for list of groups """
2961                #list = backend.getGroupsList()
2962                list = backend.getLdapGroups()
2963                showGroup = []
2964                for gr in list:
2965                        #num = int(backend.getGroup(gr).getGid().getValue())
2966                        #if (num >= backend.ldapUid) and (num < backend.ldapMaxUid):
2967                        showGroup.append(gr)
2968
2969                # create list store
2970                model = gtk.ListStore(
2971                gobject.TYPE_STRING,
2972                gtk.gdk.Pixbuf,
2973                )
2974                # add items
2975                for item in showGroup:
2976                        iter = model.append()
2977                        type = ""
2978                        if backend.isUnixGroup(item):
2979                                type=_("locale")
2980                        else:
2981                                if backend.isLdapGroup(item):
2982                                        type = _("network")       
2983                                else:
2984                                        log.debug(_("Warning: problem identifying group type!"))
2985                        #pix = art.get_pixbuf_from_name("stock_people")
2986                        pix = art.get_pixbuf_from_name("group")
2987
2988
2989                        model.set (iter, 0, item, 1, pix )
2990                return model
2991
2992                               
2993                               
2994         
2995
2996        def __cancel_clicked(self, button):
2997                self.destroy()
2998
2999        def showDialog(self, message, type):
3000               dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,type, gtk.BUTTONS_OK, message)
3001               dialog.run()
3002               dialog.destroy()     
3003
3004               
3005               
3006
3007if __name__ == '__main__':
3008        m = Management()
3009        #m = MassCreation()
3010       
3011        w = gtk.Window()
3012        w.add(m)
3013        #m = Newuser()
3014        w.show_all()
3015        gtk.main()
3016
3017
3018
3019               
3020
3021
3022
3023
Note: See TracBrowser for help on using the browser.