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 change a user password. 013 * 014 * @author Daniel Bergqvist (C) 2024 015 */ 016public class ChangeUserPasswordDialog extends JDialog { 017 018 private final PermissionManager _mngr; 019 private final JPasswordField _passwordTextField; 020 private final JPasswordField _secondPasswordTextField; 021 022 023 public ChangeUserPasswordDialog( 024 PermissionManager mngr, Frame owner, User user, Runnable passwordChangedRunnable) { 025 026 super(owner, Bundle.getMessage("ChangeUserPasswordDialog_ChangePasswordTitle"), true); 027 028 this._mngr = mngr; 029 030 JPanel contentPanel = new JPanel(); 031 rootPane.getContentPane().add(contentPanel); 032 033 JPanel p = contentPanel; 034 035 p.setLayout(new java.awt.GridBagLayout()); 036 java.awt.GridBagConstraints c = new java.awt.GridBagConstraints(); 037 c.gridwidth = 1; 038 c.gridheight = 1; 039 c.gridx = 0; 040 c.gridy = 0; 041 c.anchor = java.awt.GridBagConstraints.EAST; 042 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_UserName")), c); 043 c.gridy = 1; 044 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Password")), c); 045 c.gridy = 2; 046 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_PasswordAgain")), c); 047 048 c.gridx = 1; 049 c.gridy = 0; 050 contentPanel.add(Box.createHorizontalStrut(5), c); 051 052 c.gridx = 2; 053 c.gridy = 0; 054 c.anchor = java.awt.GridBagConstraints.WEST; 055 contentPanel.add(new JLabel(user.getUserName()), c); 056 c.gridy = 1; 057 _passwordTextField = new JPasswordField(20); 058 contentPanel.add(_passwordTextField, c); 059 c.gridy = 2; 060 _secondPasswordTextField = new JPasswordField(20); 061 contentPanel.add(_secondPasswordTextField, c); 062 063 JPanel buttonPanel = new JPanel(); 064 JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel")); // NOI18N 065 buttonPanel.add(buttonCancel); 066 buttonCancel.addActionListener((ActionEvent e) -> { 067 dispose(); 068 }); 069 070 // OK 071 JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK")); // NOI18N 072 buttonPanel.add(buttonOK); 073 buttonOK.addActionListener((ActionEvent e) -> { 074 if (okPressed(user)) { 075 passwordChangedRunnable.run(); 076 dispose(); 077 } 078 }); 079 buttonOK.setToolTipText(Bundle.getMessage("ChangePasswordDialogOkButtonHint")); // NOI18N 080 081 c.gridx = 0; 082 c.gridy = 3; 083 c.gridwidth = 2; 084 c.anchor = java.awt.GridBagConstraints.CENTER; 085 contentPanel.add(buttonPanel, c); 086 087 setLocationRelativeTo(owner); 088 pack(); 089 } 090 091 private boolean okPressed(User user) { 092 String passwd1 = new String(_passwordTextField.getPassword()); 093 String passwd2 = new String(_secondPasswordTextField.getPassword()); 094 095 if (passwd1.isBlank() && !_mngr.isAllowEmptyPasswords()) { 096 JmriJOptionPane.showMessageDialog(null, 097 Bundle.getMessage("AddUserDialog_PasswordEmpty"), 098 jmri.Application.getApplicationName(), 099 JmriJOptionPane.ERROR_MESSAGE); 100 return false; 101 } 102 103 if (!passwd1.equals(passwd1.trim())) { 104 JmriJOptionPane.showMessageDialog(null, 105 Bundle.getMessage("AddUserDialog_SpaceNotAllowedInPassword"), 106 jmri.Application.getApplicationName(), 107 JmriJOptionPane.ERROR_MESSAGE); 108 return false; 109 } 110 111 if (!passwd1.equals(passwd2)) { 112 JmriJOptionPane.showMessageDialog(null, 113 Bundle.getMessage("AddUserDialog_PasswordsAreNotEqual"), 114 jmri.Application.getApplicationName(), 115 JmriJOptionPane.ERROR_MESSAGE); 116 return false; 117 } 118 119 user.setPassword(passwd1); 120 return true; 121 } 122 123}