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// cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint")); // NOI18N 088 buttonCancel.setToolTipText("CancelLogixButtonHint"); // NOI18N 089 090 // OK 091 JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK")); // NOI18N 092 buttonPanel.add(buttonOK); 093 buttonOK.addActionListener((ActionEvent e) -> { 094 if (okPressed()) { 095 dispose(); 096 } 097 }); 098// cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint")); // NOI18N 099 buttonOK.setToolTipText("CancelLogixButtonHint"); // NOI18N 100 101 c.gridx = 0; 102 c.gridy = 5; 103 c.gridwidth = 2; 104 c.anchor = java.awt.GridBagConstraints.CENTER; 105 contentPanel.add(buttonPanel, c); 106 107 setLocationRelativeTo(owner); 108 pack(); 109 } 110 111 private boolean okPressed() { 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}