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