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 UserAdded _userAdded;
023    private final JTextField _usernameTextField;
024    private final JPasswordField _passwordTextField;
025    private final JPasswordField _secondPasswordTextField;
026    private final JTextField _nameTextField;
027    private final JTextField _commentTextField;
028
029
030    public AddUserDialog(Frame owner, UserAdded userAdded) {
031        super(owner, Bundle.getMessage("AddUserDialog_AddUserTitle"), true);
032
033        this._userAdded = userAdded;
034
035        JPanel contentPanel = new JPanel();
036        rootPane.getContentPane().add(contentPanel);
037
038        JPanel p = contentPanel;
039
040        p.setLayout(new java.awt.GridBagLayout());
041        java.awt.GridBagConstraints c = new java.awt.GridBagConstraints();
042        c.gridwidth = 1;
043        c.gridheight = 1;
044        c.gridx = 0;
045        c.gridy = 0;
046        c.anchor = java.awt.GridBagConstraints.EAST;
047        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_UserName")), c);
048        c.gridy = 1;
049        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Password")), c);
050        c.gridy = 2;
051        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_PasswordAgain")), c);
052        c.gridy = 3;
053        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Name")), c);
054        c.gridy = 4;
055        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Comment")), c);
056
057        c.gridx = 1;
058        c.gridy = 0;
059        contentPanel.add(Box.createHorizontalStrut(5), c);
060
061        c.gridx = 2;
062        c.gridy = 0;
063        c.anchor = java.awt.GridBagConstraints.WEST;
064        _usernameTextField = new JTextField(20);
065        contentPanel.add(_usernameTextField, c);
066        c.gridy = 1;
067        _passwordTextField = new JPasswordField(20);
068        contentPanel.add(_passwordTextField, c);
069        c.gridy = 2;
070        _secondPasswordTextField = new JPasswordField(20);
071        contentPanel.add(_secondPasswordTextField, c);
072        c.gridy = 3;
073        _nameTextField = new JTextField(40);
074        contentPanel.add(_nameTextField, c);
075        c.gridy = 4;
076        _commentTextField = new JTextField(40);
077        contentPanel.add(_commentTextField, c);
078
079        JPanel buttonPanel = new JPanel();
080        JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel"));    // NOI18N
081        buttonPanel.add(buttonCancel);
082        buttonCancel.addActionListener((ActionEvent e) -> {
083            dispose();
084        });
085//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
086        buttonCancel.setToolTipText("CancelLogixButtonHint");      // NOI18N
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//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
097        buttonOK.setToolTipText("CancelLogixButtonHint");      // NOI18N
098
099        c.gridx = 0;
100        c.gridy = 5;
101        c.gridwidth = 2;
102        c.anchor = java.awt.GridBagConstraints.CENTER;
103        contentPanel.add(buttonPanel, c);
104
105        setLocationRelativeTo(owner);
106        pack();
107    }
108
109    private boolean okPressed() {
110        PermissionManager mngr = InstanceManager.getDefault(PermissionManager.class);
111
112        String username = _usernameTextField.getText();
113        String passwd1 = new String(_passwordTextField.getPassword());
114        String passwd2 = new String(_secondPasswordTextField.getPassword());
115        String name = _nameTextField.getText();
116        String comment = _commentTextField.getText();
117
118        if (username.isBlank()) {
119            JmriJOptionPane.showMessageDialog(null,
120                    Bundle.getMessage("AddUserDialog_UsernameEmpty"),
121                    jmri.Application.getApplicationName(),
122                    JmriJOptionPane.ERROR_MESSAGE);
123            return false;
124        }
125
126        if (!username.equals(username.trim())) {
127            JmriJOptionPane.showMessageDialog(null,
128                    Bundle.getMessage("AddUserDialog_SpaceNotAllowedInUsername"),
129                    jmri.Application.getApplicationName(),
130                    JmriJOptionPane.ERROR_MESSAGE);
131            return false;
132        }
133
134        if (passwd1.isBlank() && !mngr.isAllowEmptyPasswords()) {
135            JmriJOptionPane.showMessageDialog(null,
136                    Bundle.getMessage("AddUserDialog_PasswordEmpty"),
137                    jmri.Application.getApplicationName(),
138                    JmriJOptionPane.ERROR_MESSAGE);
139            return false;
140        }
141
142        if (!passwd1.equals(passwd1.trim())) {
143            JmriJOptionPane.showMessageDialog(null,
144                    Bundle.getMessage("AddUserDialog_SpaceNotAllowedInPassword"),
145                    jmri.Application.getApplicationName(),
146                    JmriJOptionPane.ERROR_MESSAGE);
147            return false;
148        }
149
150        if (!passwd1.equals(passwd2)) {
151            JmriJOptionPane.showMessageDialog(null,
152                    Bundle.getMessage("AddUserDialog_PasswordsAreNotEqual"),
153                    jmri.Application.getApplicationName(),
154                    JmriJOptionPane.ERROR_MESSAGE);
155            return false;
156        }
157
158        try {
159            User user = mngr.addUser(_usernameTextField.getText(), passwd1);
160            user.setName(name);
161            user.setComment(comment);
162            _userAdded.userAdded(user);
163            return true;
164        } catch (PermissionManager.UserAlreadyExistsException e) {
165            JmriJOptionPane.showMessageDialog(null,
166                    Bundle.getMessage("AddUserDialog_UsernameExists", username.toLowerCase()),
167                    jmri.Application.getApplicationName(),
168                    JmriJOptionPane.ERROR_MESSAGE);
169            return false;
170        }
171    }
172
173}