001package jmri.jmrit.permission.swing;
002
003import java.awt.event.ActionEvent;
004
005import javax.swing.Icon;
006
007import jmri.*;
008import jmri.util.swing.*;
009
010
011/**
012 * Let a user change his/her own password.
013 *
014 * @author Daniel Bergqvist (C) 2024
015 */
016public class ChangePasswordAction extends JmriAbstractAction {
017
018    public ChangePasswordAction(String s, WindowInterface wi) {
019        super(s, wi);
020        checkPermission();
021    }
022
023    public ChangePasswordAction(String s, Icon i, WindowInterface wi) {
024        super(s, i, wi);
025        checkPermission();
026    }
027
028    public ChangePasswordAction() {
029        super(Bundle.getMessage("ChangePasswordAction_Title"));
030        checkPermission();
031    }
032
033    private void checkPermission() {
034        var permissionManager = InstanceManager.getDefault(PermissionManager.class);
035
036        if (permissionManager.isEnabled()) {
037            setEnabled(permissionManager.isLoggedIn());
038            permissionManager.addLoginListener((isLogin) -> {
039                setEnabled(isLogin && permissionManager.isCurrentUserPermittedToChangePassword());
040            });
041        } else {
042            setEnabled(false);
043        }
044    }
045
046    @Override
047    public void actionPerformed(ActionEvent e) {
048        var permissionManager = InstanceManager.getDefault(PermissionManager.class);
049        if (!permissionManager.hasAtLeastPermission(PermissionsSystemAdmin.PERMISSION_EDIT_PERMISSIONS,
050                BooleanPermission.BooleanValue.TRUE)) {
051            // Note that the line above _asks_ about the permission and the line below
052            // _checks_ for the permission. If the line below doesn't have the permission,
053            // a JOptionPane will show an error message.
054            if (!permissionManager.ensureAtLeastPermission(PermissionsSystemAdmin.PERMISSION_EDIT_OWN_PASSWORD,
055                    BooleanPermission.BooleanValue.TRUE)) {
056                return;
057            }
058        }
059        new ChangePasswordDialog().setVisible(true);
060    }
061
062    @Override
063    public JmriPanel makePanel() {
064        throw new IllegalArgumentException("Should not be invoked");
065    }
066
067}