OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.users;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.element.BaseSQLComponent;
18
import org.openconcerto.sql.element.ConfSQLElement;
19
import org.openconcerto.sql.element.SQLComponent;
20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowAccessor;
22
import org.openconcerto.sql.request.ComboSQLRequest;
23
import org.openconcerto.sql.sqlobject.itemview.SimpleRowItemView;
24
import org.openconcerto.sql.ui.Login;
25
import org.openconcerto.ui.DefaultGridBagConstraints;
26
import org.openconcerto.ui.ISpinner;
27
import org.openconcerto.ui.ISpinnerIntegerModel;
28
import org.openconcerto.ui.warning.JLabelWarning;
29
import org.openconcerto.utils.CollectionMap;
30
import org.openconcerto.utils.text.SimpleDocumentListener;
31
 
32
import java.awt.GridBagConstraints;
33
import java.awt.GridBagLayout;
34
import java.awt.Insets;
35
import java.util.ArrayList;
36
import java.util.List;
37
 
38
import javax.swing.BorderFactory;
39
import javax.swing.JCheckBox;
40
import javax.swing.JLabel;
41
import javax.swing.JPanel;
42
import javax.swing.JPasswordField;
43
import javax.swing.JTextField;
44
import javax.swing.SwingConstants;
45
import javax.swing.event.DocumentEvent;
46
 
47
// FIXME Login user unique ?
48
public class UserCommonSQLElement extends ConfSQLElement {
49
 
50
    /**
51
     * Set this system property to "true" if this should generate old style passwords.
52
     */
53
    public static final String LEGACY_PASSWORDS = "org.openconcerto.sql.legacyPasswords";
54
 
55
    public UserCommonSQLElement() {
56
        super("USER_COMMON", "un utilisateur", "utilisateurs");
57
    }
58
 
59
    @Override
60
    protected List<String> getListFields() {
61
        final List<String> l = new ArrayList<String>();
62
        l.add("NOM");
63
        l.add("PRENOM");
64
        l.add("LOGIN");
65
        return l;
66
    }
67
 
68
    @Override
69
    protected List<String> getComboFields() {
70
        final List<String> l = new ArrayList<String>();
71
        l.add("NOM");
72
        l.add("PRENOM");
73
        return l;
74
    }
75
 
76
    @Override
77
    public ComboSQLRequest getComboRequest() {
78
        final ComboSQLRequest res = super.getComboRequest();
79
        res.setFieldSeparator(" ");
80
        return res;
81
    }
82
 
83
    @Override
84
    public CollectionMap<String, String> getShowAs() {
85
        return CollectionMap.singleton(null, "PRENOM", "NOM");
86
    }
87
 
88
    /*
89
     * (non-Javadoc)
90
     *
91
     * @see org.openconcerto.devis.SQLElement#getComponent()
92
     */
93
    @Override
94
    public SQLComponent createComponent() {
95
        return new BaseSQLComponent(this) {
96
 
97
            private JPasswordField passField, passFieldConfirm;
98
            private JPanel panelWarning;
99
            private AccesSocieteTable table;
100
            private final JTextField encryptedPass = new JTextField();
101
 
102
            @Override
103
            public void addViews() {
104
                final GridBagConstraints c = new GridBagConstraints();
105
                c.insets = new Insets(0, 0, 0, 0);
106
                c.gridx = 0;
107
                c.gridy = 0;
108
                c.weightx = 0;
109
                c.weighty = 0;
110
                c.fill = GridBagConstraints.HORIZONTAL;
111
                c.anchor = GridBagConstraints.WEST;
112
 
113
                this.panelWarning = new JPanel(new GridBagLayout());
114
                this.panelWarning.setBorder(null);
115
                final JLabelWarning labelWarning = new JLabelWarning();
116
                // labelWarning.setBorder(null);
117
                this.panelWarning.add(labelWarning, c);
118
                final JLabel labelTextWarning = new JLabel("Confirmation incorrecte");
119
                // labelTextWarning.setBorder(null);
120
                c.gridx++;
121
                this.panelWarning.add(labelTextWarning, c);
122
 
123
                final GridBagLayout layout = new GridBagLayout();
124
                this.setLayout(layout);
125
 
126
                // Login
127
                c.gridx = 0;
128
                c.insets = new Insets(2, 2, 1, 2);
129
                final JLabel labelLogin = new JLabel(getLabelFor("LOGIN"));
130
                labelLogin.setHorizontalAlignment(SwingConstants.RIGHT);
131
                this.add(labelLogin, c);
132
                final JTextField textLogin = new JTextField();
133
                c.gridx++;
134
                DefaultGridBagConstraints.lockMinimumSize(textLogin);
135
                c.weightx = 1;
136
                this.add(textLogin, c);
137
 
138
                // Warning
139
                c.gridwidth = GridBagConstraints.REMAINDER;
140
                c.gridx++;
141
                c.insets = new Insets(0, 0, 0, 0);
142
                this.add(this.panelWarning, c);
143
                this.panelWarning.setVisible(false);
144
 
145
                // Pass
146
                c.gridy++;
147
                c.gridwidth = 1;
148
                c.gridx = 0;
149
                c.weightx = 0;
150
                c.insets = new Insets(2, 2, 1, 2);
151
                final JLabel labelPass = new JLabel(getLabelFor("PASSWORD"));
152
                labelPass.setHorizontalAlignment(SwingConstants.RIGHT);
153
                this.add(labelPass, c);
154
                // JTextField textPass = new JTextField();
155
                this.passField = new JPasswordField(15);
156
                c.gridx++;
157
                c.weightx = 1;
158
                DefaultGridBagConstraints.lockMinimumSize(this.passField);
159
                this.add(this.passField, c);
160
 
161
                // Confirmation password
162
                c.gridx++;
163
                c.weightx = 0;
164
                final JLabel labelConfirmationPass = new JLabel("Confirmation");
165
                labelConfirmationPass.setHorizontalAlignment(SwingConstants.RIGHT);
166
                this.add(labelConfirmationPass, c);
167
                this.passFieldConfirm = new JPasswordField(15);
168
                c.gridx++;
169
                c.weightx = 1;
170
                DefaultGridBagConstraints.lockMinimumSize(this.passFieldConfirm);
171
                this.add(this.passFieldConfirm, c);
172
 
173
                // Nom
174
                c.gridx = 0;
175
                c.gridy++;
176
                c.weightx = 0;
177
                final JLabel labelNom = new JLabel(getLabelFor("NOM"));
178
                labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
179
                this.add(labelNom, c);
180
                final JTextField textNom = new JTextField();
181
                c.gridx++;
182
                c.weightx = 1;
183
                this.add(textNom, c);
184
 
185
                // Prenom
186
                c.gridx++;
187
                c.weightx = 0;
188
                final JLabel labelPrenom = new JLabel(getLabelFor("PRENOM"));
189
                labelPrenom.setHorizontalAlignment(SwingConstants.RIGHT);
190
                this.add(labelPrenom, c);
191
                final JTextField textPrenom = new JTextField();
192
                c.gridx++;
193
                c.weightx = 1;
194
                this.add(textPrenom, c);
195
 
196
                // Surnom
197
                c.gridx = 0;
198
                c.gridy++;
199
                c.weightx = 0;
200
                final JLabel labelSurnom = new JLabel(getLabelFor("SURNOM"));
201
                labelSurnom.setHorizontalAlignment(SwingConstants.RIGHT);
202
                this.add(labelSurnom, c);
203
                final JTextField textSurnom = new JTextField();
204
                c.gridx++;
205
                c.weightx = 1;
206
                this.add(textSurnom, c);
207
 
208
                if (this.getTable().contains("ADMIN")) {
209
                    c.gridx++;
210
                    c.gridwidth = GridBagConstraints.REMAINDER;
211
                    c.weightx = 0;
212
                    final JCheckBox checkAdmin = new JCheckBox(getLabelFor("ADMIN"));
213
                    this.add(checkAdmin, c);
214
                    this.addView(checkAdmin, "ADMIN");
215
                }
216
 
217
                c.gridy++;
218
                c.gridwidth = 1;
219
                c.gridx = 0;
220
                c.weightx = 0;
221
 
222
                if (getTable().contains("MAIL")) {
223
                    final JLabel labelMail = new JLabel(getLabelFor("MAIL"));
224
                    labelMail.setHorizontalAlignment(SwingConstants.RIGHT);
225
                    c.anchor = GridBagConstraints.NORTHWEST;
226
                    this.add(labelMail, c);
227
                    c.gridx++;
228
                    final JTextField textMail = new JTextField();
229
                    c.gridwidth = GridBagConstraints.REMAINDER;
230
                    c.weightx = 1;
231
                    c.weighty = 1;
232
 
233
                    this.add(textMail, c);
234
                    this.addView(textMail, "MAIL");
235
                }
236
 
237
                boolean gestionHoraire = false;
238
                if (Configuration.getInstance().getAppName().startsWith("OpenConcerto") && gestionHoraire) {
239
 
240
                    c.gridwidth = 1;
241
                    JPanel panelHoraires = new JPanel(new GridBagLayout());
242
                    GridBagConstraints cH = new DefaultGridBagConstraints();
243
 
244
                    createHalfDay(panelHoraires, cH, "Matin :", "MATIN", 8, 12);
245
                    createHalfDay(panelHoraires, cH, "Après midi :", "MIDI", 13, 17);
246
 
247
                    c.gridy++;
248
                    c.gridx = 0;
249
                    c.gridwidth = GridBagConstraints.REMAINDER;
250
                    c.weightx = 1;
251
                    c.weighty = 0;
252
                    panelHoraires.setBorder(BorderFactory.createTitledBorder("Horaires"));
253
                    this.add(panelHoraires, c);
254
 
255
                    c.gridy++;
256
                    c.weighty = 1;
257
                    this.table = new AccesSocieteTable();
258
                    this.add(this.table, c);
259
                }
260
 
261
                this.addRequiredSQLObject(textLogin, "LOGIN");
262
                this.addView(this.encryptedPass, "PASSWORD", REQ);
263
                this.addSQLObject(textNom, "NOM");
264
                this.addSQLObject(textPrenom, "PRENOM");
265
                this.addSQLObject(textSurnom, "SURNOM");
266
 
267
                this.passField.getDocument().addDocumentListener(new SimpleDocumentListener() {
268
                    @Override
269
                    public void update(DocumentEvent e) {
270
                        updateEncrypted();
271
                        fireValidChange();
272
                    }
273
                });
274
                this.passFieldConfirm.getDocument().addDocumentListener(new SimpleDocumentListener() {
275
                    @Override
276
                    public void update(DocumentEvent e) {
277
                        fireValidChange();
278
                    }
279
                });
280
            }
281
 
282
            // après midi arrivée 13:30
283
            // __________ départ 17:53
284
            private void createHalfDay(JPanel panelHoraires, GridBagConstraints cH, String label, String field, int startHour, int endHour) {
285
                panelHoraires.add(new JLabel(label), cH);
286
                cH.gridx++;
287
                createTime(panelHoraires, cH, "arrivée", field + "_A", startHour);
288
                cH.gridy++;
289
                cH.gridx = 1;
290
                createTime(panelHoraires, cH, "départ", field + "_D", endHour);
291
                cH.gridy++;
292
                cH.gridx = 0;
293
            }
294
 
295
            // départ 17:53
296
            private void createTime(JPanel panelHoraires, GridBagConstraints cH, String label, String field, int hour) {
297
                panelHoraires.add(new JLabel(label), cH);
298
                cH.gridx++;
299
 
300
                final ISpinner spinHourMA = createSpinner(panelHoraires, cH, true, hour);
301
                final ISpinner spinMinMA = createSpinner(panelHoraires, cH, false, 0);
302
 
303
                this.addView(new SimpleRowItemView<Integer>(spinHourMA), "HEURE_" + field, null);
304
                this.addView(new SimpleRowItemView<Integer>(spinMinMA), "MINUTE_" + field, null);
305
            }
306
 
307
            // 17 h or 53 min
308
            private ISpinner createSpinner(JPanel panelHoraires, GridBagConstraints cH, final boolean hour, int value) {
309
                ISpinnerIntegerModel modelHourMA = new ISpinnerIntegerModel(0, hour ? 23 : 59, value);
310
                ISpinner spinHourMA = new ISpinner(modelHourMA);
311
                panelHoraires.add(spinHourMA.getComp(), cH);
312
                cH.gridx++;
313
                panelHoraires.add(new JLabel(hour ? "h" : "min"), cH);
314
                cH.gridx++;
315
                return spinHourMA;
316
            }
317
 
318
            private void updateEncrypted() {
319
                final String pass = String.valueOf(this.passField.getPassword());
320
                final String dbPass = Boolean.getBoolean(LEGACY_PASSWORDS) ? '"' + pass + '"' : pass;
321
                this.encryptedPass.setText(Login.encodePassword(dbPass));
322
            }
323
 
324
            private boolean checkValidityPassword() {
325
                final boolean b = String.valueOf(this.passField.getPassword()).equalsIgnoreCase(String.valueOf(this.passFieldConfirm.getPassword()));
326
                this.panelWarning.setVisible(!b);
327
                return b;
328
            }
329
 
330
            @Override
331
            public synchronized boolean isValidated() {
332
                return super.isValidated() && checkValidityPassword();
333
            }
334
 
335
            @Override
336
            public void select(final SQLRowAccessor row) {
337
                // show something in the user-visible text fields, but do it before the real select
338
                // so that this.encryptedPass (which will be updated by updateEncrypted() and thus
339
                // have a bogus value) will be changed to its database value and thus the user can
340
                // update any field without changing the password.
341
                if (row != null) {
342
                    final String bogusPass = "bogusPass!";
343
                    this.passField.setText(bogusPass);
344
                    this.passFieldConfirm.setText(bogusPass);
345
                    if (this.table != null) {
346
                        this.table.insertFrom("ID_USER_COMMON", row.getID());
347
                    }
348
                }
349
                super.select(row);
350
            }
351
 
352
            @Override
353
            public int insert(SQLRow order) {
354
                int id = super.insert(order);
355
                if (this.table != null) {
356
                    this.table.updateField("ID_USER_COMMON", id);
357
                }
358
                return id;
359
            }
360
 
361
            @Override
362
            public void update() {
363
                super.update();
364
                if (this.table != null) {
365
                    this.table.updateField("ID_USER_COMMON", getSelectedID());
366
                }
367
            }
368
        };
369
    }
370
}