001package jmri.jmrit.permission.swing;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005
006import javax.swing.*;
007
008import jmri.*;
009import jmri.util.swing.JmriJOptionPane;
010
011/**
012 * Dialog to add user
013 *
014 * @author Daniel Bergqvist (C) 2024
015 */
016public class AddUserDialog extends JDialog {
017
018    public interface UserAdded {
019        void userAdded(User user);
020    }
021
022    private final PermissionManager _mngr;
023    private final UserAdded _userAdded;
024    private final JTextField _usernameTextField;
025    private final JPasswordField _passwordTextField;
026    private final JPasswordField _secondPasswordTextField;
027    private final JTextField _nameTextField;
028    private final JTextField _commentTextField;
029
030
031    public AddUserDialog(PermissionManager mngr, Frame owner, UserAdded userAdded) {
032        super(owner, Bundle.getMessage("AddUserDialog_AddUserTitle"), true);
033
034        this._mngr = mngr;
035        this._userAdded = userAdded;
036
037        JPanel contentPanel = new JPanel();
038        rootPane.getContentPane().add(contentPanel);
039
040        JPanel p = contentPanel;
041
042        p.setLayout(new java.awt.GridBagLayout());
043        java.awt.GridBagConstraints c = new java.awt.GridBagConstraints();
044        c.gridwidth = 1;
045        c.gridheight = 1;
046        c.gridx = 0;
047        c.gridy = 0;
048        c.anchor = java.awt.GridBagConstraints.EAST;
049        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_UserName")), c);
050        c.gridy = 1;
051        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Password")), c);
052        c.gridy = 2;
053        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_PasswordAgain")), c);
054        c.gridy = 3;
055        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Name")), c);
056        c.gridy = 4;
057        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Comment")), c);
058
059        c.gridx = 1;
060        c.gridy = 0;
061        contentPanel.add(Box.createHorizontalStrut(5), c);
062
063        c.gridx = 2;
064        c.gridy = 0;
065        c.anchor = java.awt.GridBagConstraints.WEST;
066        _usernameTextField = new JTextField(20);
067        contentPanel.add(_usernameTextField, c);
068        c.gridy = 1;
069        _passwordTextField = new JPasswordField(20);
070        contentPanel.add(_passwordTextField, c);
071        c.gridy = 2;
072        _secondPasswordTextField = new JPasswordField(20);
073        contentPanel.add(_secondPasswordTextField, c);
074        c.gridy = 3;
075        _nameTextField = new JTextField(40);
076        contentPanel.add(_nameTextField, c);
077        c.gridy = 4;
078        _commentTextField = new JTextField(40);
079        contentPanel.add(_commentTextField, c);
080
081        JPanel buttonPanel = new JPanel();
082        JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel"));    // NOI18N
083        buttonPanel.add(buttonCancel);
084        buttonCancel.addActionListener((ActionEvent e) -> {
085            dispose();
086        });
087
088        // OK
089        JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK"));    // NOI18N
090        buttonPanel.add(buttonOK);
091        buttonOK.addActionListener((ActionEvent e) -> {
092            if (okPressed()) {
093                dispose();
094            }
095        });
096        buttonOK.setToolTipText(Bundle.getMessage("AddUserDialogOkHint"));      // NOI18N
097
098        c.gridx = 0;
099        c.gridy = 5;
100        c.gridwidth = 2;
101        c.anchor = java.awt.GridBagConstraints.CENTER;
102        contentPanel.add(buttonPanel, c);
103
104        setLocationRelativeTo(owner);
105        pack();
106    }
107
108    private boolean okPressed() {
109        String username = _usernameTextField.getText();
110        String passwd1 = new String(_passwordTextField.getPassword());
111        String passwd2 = new String(_secondPasswordTextField.getPassword());
112        String name = _nameTextField.getText();
113        String comment = _commentTextField.getText();
114
115        if (username.isBlank()) {
116            JmriJOptionPane.showMessageDialog(null,
117                    Bundle.getMessage("AddUserDialog_UsernameEmpty"),
118                    jmri.Application.getApplicationName(),
119                    JmriJOptionPane.ERROR_MESSAGE);
120            return false;
121        }
122
123        if (!username.equals(username.trim())) {
124            JmriJOptionPane.showMessageDialog(null,
125                    Bundle.getMessage("AddUserDialog_SpaceNotAllowedInUsername"),
126                    jmri.Application.getApplicationName(),
127                    JmriJOptionPane.ERROR_MESSAGE);
128            return false;
129        }
130
131        if (passwd1.isBlank() && !_mngr.isAllowEmptyPasswords()) {
132            JmriJOptionPane.showMessageDialog(null,
133                    Bundle.getMessage("AddUserDialog_PasswordEmpty"),
134                    jmri.Application.getApplicationName(),
135                    JmriJOptionPane.ERROR_MESSAGE);
136            return false;
137        }
138
139        if (!passwd1.equals(passwd1.trim())) {
140            JmriJOptionPane.showMessageDialog(null,
141                    Bundle.getMessage("AddUserDialog_SpaceNotAllowedInPassword"),
142                    jmri.Application.getApplicationName(),
143                    JmriJOptionPane.ERROR_MESSAGE);
144            return false;
145        }
146
147        if (!passwd1.equals(passwd2)) {
148            JmriJOptionPane.showMessageDialog(null,
149                    Bundle.getMessage("AddUserDialog_PasswordsAreNotEqual"),
150                    jmri.Application.getApplicationName(),
151                    JmriJOptionPane.ERROR_MESSAGE);
152            return false;
153        }
154
155        try {
156            User user = _mngr.addUser(_usernameTextField.getText(), passwd1);
157            user.setName(name);
158            user.setComment(comment);
159            _userAdded.userAdded(user);
160            return true;
161        } catch (PermissionManager.UserAlreadyExistsException e) {
162            JmriJOptionPane.showMessageDialog(null,
163                    Bundle.getMessage("AddUserDialog_UsernameExists", username.toLowerCase()),
164                    jmri.Application.getApplicationName(),
165                    JmriJOptionPane.ERROR_MESSAGE);
166            return false;
167        }
168    }
169
170}