Correction d'un bug lors du changement de mot de passe d'une personne non administrateur
[bilio:bilio.git] / src / liste_users.rb
1 # coding: utf-8
2
3 class ListeUsers_box < Gtk::VBox
4
5         attr_accessor :ajout
6         
7         def initialize window
8         
9                 super(false, 3)
10                 
11                 set_border_width 10
12                 
13                 @window = window
14                 @user = @window.login.user
15                 
16                 add agencement
17                 
18                 remplir_users
19         
20         end
21         
22         def agencement
23                 
24                 vboxaction = Gtk::VBox.new false, 3
25                 vboxaction.pack_start( boutons, false, false, 3 ) 
26                 vboxaction.pack_start( tableau_users, true, true, 3 ) 
27                 frame = Gtk::Frame.new
28                 frame.add vboxaction
29                 frame.label = "Liste des utilisateurs"
30                 
31                 frame
32                 
33         end
34         
35         def boutons
36         
37                 align = Gtk::Alignment.new 1, 1, 0, 0
38                 
39                 hbox = Gtk::HBox.new false, 2
40                 
41                 @changepassword = Gtk::Button.new
42                 hboxsuppr = Gtk::HBox.new false, 2
43                 hboxsuppr.add Gtk::Image.new( "./resources/icons/password.png" )
44                 hboxsuppr.add Gtk::Label.new "Changer le mot de passe"
45                 @changepassword.add hboxsuppr   
46                 @changepassword.sensitive = false
47                 @changepassword.signal_connect ("clicked") {
48                         change_password ( @view.model.get_value @view.selection.selected, 0 )
49                 }
50                 
51                 @modif = Gtk::Button.new
52                 hboxsuppr = Gtk::HBox.new false, 2
53                 hboxsuppr.add Gtk::Image.new( "./resources/icons/saisie.png" )
54                 hboxsuppr.add Gtk::Label.new "Mofifier utilisateur"
55                 @modif.add hboxsuppr    
56                 @modif.sensitive = false
57                 @modif.signal_connect ("clicked") {
58                         modif_user ( @view.model.get_value @view.selection.selected, 0 )
59                 }
60                 
61                 @ajout = Gtk::Button.new
62                 @ajout.sensitive = false
63                 hboxajout = Gtk::HBox.new false, 2
64                 hboxajout.add Gtk::Image.new( "./resources/icons/add.png" )
65                 hboxajout.add Gtk::Label.new "Nouvel utilisateur"
66                 @ajout.add hboxajout            
67                 @ajout.signal_connect ("clicked") {
68                         modif_user 0
69                 }
70                 
71                 hbox.pack_start(@changepassword, false, false, 2)
72                 hbox.pack_start(@modif, false, false, 2)
73                 hbox.pack_start(@ajout, false, false, 2)
74                 
75                 align.add hbox
76                 
77                 align
78         
79         end
80         
81         def tableau_users
82         
83                 @list_store = Gtk::TreeStore.new(Integer, String, String, String, Integer, Integer)
84                 @view = Gtk::TreeView.new(@list_store)
85                 @view.signal_connect ("row-activated") { |view, path, column|
86                         modif_user ( @view.model.get_value @view.selection.selected, 0 ) if ( @user.admin )
87                 }
88                 
89                 @view.signal_connect ("cursor-changed") {
90                         @changepassword.sensitive = ( @user.id.eql?( @view.model.get_value( @view.selection.selected, 0 ) ) or @user.admin )
91                         @modif.sensitive = @user.admin
92                 }
93                 
94                 # Create a renderer with the background property set
95                 renderer_left = Gtk::CellRendererText.new
96                 renderer_left.background = "white"
97                 renderer_left.xalign = 0        
98                 
99                 renderer_toggle_admin = Gtk::CellRendererToggle.new
100                 renderer_toggle_active = Gtk::CellRendererToggle.new
101                 
102                 col = Gtk::TreeViewColumn.new("Prénom", renderer_left, :text => 1)
103                 col.sort_column_id = 0
104                 col.resizable = true
105                 @view.append_column(col)
106                 
107                 col = Gtk::TreeViewColumn.new("Nom", renderer_left, :text => 2)
108                 col.sort_column_id = 1
109                 col.resizable = true
110                 col.expand = true
111                 @view.append_column(col)
112                 
113                 col = Gtk::TreeViewColumn.new("Login", renderer_left, :text => 3)
114                 col.sort_column_id = 2
115                 col.resizable = true
116                 @view.append_column(col)
117                 
118                 col = Gtk::TreeViewColumn.new("Admin ?", renderer_toggle_admin, :active => 4)
119                 col.sort_column_id = 3
120                 col.resizable = true
121                 @view.append_column(col)
122                 
123                 col = Gtk::TreeViewColumn.new("Actif ?", renderer_toggle_active, :active => 5)
124                 col.sort_column_id = 4
125                 col.resizable = true
126                 @view.append_column(col)
127                 
128                 scroll = Gtk::ScrolledWindow.new
129         scroll.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
130         scroll.add @view
131         
132         scroll
133         
134         end
135         
136         def remplir_users
137
138                 users = Utilisateur.order(:id)
139                 
140                 @list_store.clear
141                 
142                 users.each do |h|               
143                         iter = @list_store.append nil
144                         iter[0] = h.id
145                         iter[1] = h.firstname
146                         iter[2] = h.lastname
147                         iter[3] = h.username
148                         iter[4] = h.admin ? 1 : 0
149                         iter[5] = h.active ? 1 : 0
150                 end
151                         
152         end
153         
154         def modif_user id
155         
156                 @window.utilisateur.change id
157                 @window.affiche @window.utilisateur
158         
159         end
160         
161         def change_password id
162                 
163                 dialog = Gtk::Dialog.new("Mot de pase",
164                              @window,
165                              Gtk::Dialog::DESTROY_WITH_PARENT,
166                              [ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK ],
167                              [ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ]
168                              )
169
170                 vbox = Gtk::VBox.new false, 2
171                 hbox1 = Gtk::HBox.new false, 2
172                 hbox2 = Gtk::HBox.new false, 2
173                 vbox.add hbox1
174                 vbox.add hbox2
175                 hbox1.pack_start( Gtk::Label.new( "Nouveau mot de passe :" ), false, false, 3 )
176                 motpasse1 = Gtk::Entry.new
177                 motpasse1.visibility = false
178                 hbox1.pack_start( motpasse1, false, false, 3 )
179                 hbox2.pack_start( Gtk::Label.new( "Confirmer mot de passe :" ), false, false, 3 )
180                 motpasse2 = Gtk::Entry.new
181                 motpasse2.visibility = false
182                 hbox2.pack_start( motpasse2, false, false, 3 )
183                 
184                 dialog.vbox.add(vbox)
185                 vbox.show_all
186                 dialog.run { |response| 
187                         if response==Gtk::Dialog::RESPONSE_OK then
188                                 if (motpasse1.text==motpasse2.text and !motpasse1.text.empty?) then
189                                         password = BCrypt::Password.create(motpasse1.text)
190                                         user = Utilisateur.find(id)
191                                         user.password = password
192                                         user.save
193                                         
194                                         dialog2 = Gtk::MessageDialog.new(dialog, 
195                                                 Gtk::Dialog::DESTROY_WITH_PARENT,
196                                                 Gtk::MessageDialog::INFO,
197                                                 Gtk::MessageDialog::BUTTONS_OK,
198                                                 "Le mot de passe a été changé avec succés")
199                                         dialog2.run
200                                         dialog2.destroy
201                                 else
202                                         erreur = ""
203                                         erreur += "Les mots de passe ne correspondent pas !\n" unless motpasse1.text==motpasse2.text
204                                         erreur += "Le mot de passe ne peut pas être vide !\n" if motpasse1.text.empty?
205                                         erreur += "Le mot de passe n'a pas été changé."
206                                         dialog2 = Gtk::MessageDialog.new(dialog, 
207                                                 Gtk::Dialog::DESTROY_WITH_PARENT,
208                                                 Gtk::MessageDialog::ERROR,
209                                                 Gtk::MessageDialog::BUTTONS_OK,
210                                                 erreur)
211                                         dialog2.run
212                                         dialog2.destroy
213                                 end
214                         end
215                         dialog.destroy
216                 }       
217         
218         end
219         
220 end