Rahul Mishra
Rahul Mishra

Reputation: 15

How to provide customer a custom password rule setting mechanism?

In our product, the customer wants a custom password rule setter. Meaning that he want to define rules for password length, inclusion of upper case, lower case, special characters, numbers etc for the password field. How can we provide customer a proper interface to define all this without making it complicated or hard to understand? Also, is it a bit too much and we should just provide a custom length and mandate upper case, lower case nad special characters?

Upvotes: 0

Views: 56

Answers (1)

A. Baena
A. Baena

Reputation: 131

Quick code to ilustrate how I would go about it:

public class Test {

    StringBuilder passwordRules = new StringBuilder();

    private void addLowerCaseRule(int length, boolean numbersAllowed){
        if(numbersAllowed){
            passwordRules.append("[a-z0-9]{").append(length).append("}");
        }else{
            passwordRules.append("[a-z]{").append(length).append("}");
        }
    }

    private void addUpperCaseRule(int length, boolean numbersAllowed){
        if(numbersAllowed){
            passwordRules.append("[A-Z0-9]{").append(length).append("}");
        }else{
            passwordRules.append("[A-Z]{").append(length).append("}");
        }
    }

    private void addLowerAndUpperCaseRule(int length, boolean numbersAllowed){
        if(numbersAllowed){
            passwordRules.append("[aA-zZ0-9]{").append(length).append("}");
        }else{
            passwordRules.append("[aA-zZ]{").append(length).append("}");
        }
    }

    private boolean checkPassword(String password){
        if (password.matches(passwordRules.toString())) return true;
        return false;
    }

    public static void main(String[] args){
        Test test = new Test();
        test.addLowerAndUpperCaseRule(4, false);
        test.addLowerAndUpperCaseRule(5, true);

        String password = "tPrz05637";
        String passwordTwo = "t8ea17r88";

        System.out.println("First password returns " + test.checkPassword(password));
        System.out.println("Second password returns " + test.checkPassword(passwordTwo));
    }
}

All you need to do is add as many different rules as you need, then build a layout (say, a Jframe with buttons, textFields and a list to show the already added rules, in order) to allow the customer to add these rules in a visual fashion. Every time a new rule is added, call the corresponding method.

You should also add code to remove rules, of course, but I think this little code gives an idea on how to proceed.

Update: I've developed an extremely unpleasant layout using Swing to better illustrate my approach.

public class Test extends JFrame{

    String passwordRules;

    JButton addLowerAndUpperCaseRuleButton;
    JButton deleteSelectedButton;
    JTextField addLowerAndUpperCaseRuleField;
    JCheckBox addLowerAndUpperCaseRuleBox;

    JList<String> rulesList;
    DefaultListModel<String> listModel;

    JTextField testPasswordField;
    JButton testPasswordButton;

    public Test(){
        this.setPreferredSize(new Dimension(900, 100));

        addLowerAndUpperCaseRuleField = new JTextField();
        addLowerAndUpperCaseRuleField.setText(String.valueOf(1));
        addLowerAndUpperCaseRuleField.setPreferredSize(new Dimension(30, 25));

        addLowerAndUpperCaseRuleBox = new JCheckBox();

        addLowerAndUpperCaseRuleButton = new JButton();
        addLowerAndUpperCaseRuleButton.setText("Add lower and upper rule");
        addLowerAndUpperCaseRuleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                addLowerAndUpperCaseRule(Integer.valueOf(addLowerAndUpperCaseRuleField.getText()), addLowerAndUpperCaseRuleBox.isSelected());
            }
        });

        deleteSelectedButton = new JButton();
        deleteSelectedButton.setText("Delete selected");
        deleteSelectedButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                listModel.remove(rulesList.getSelectedIndex());
            }
        });


        rulesList = new JList<>();
        rulesList.setSize(new Dimension(250, 50));
        listModel = new DefaultListModel<>();
        rulesList.setModel(listModel);

        testPasswordField = new JTextField();
        testPasswordField.setPreferredSize(new Dimension(90, 25));
        testPasswordButton = new JButton("Check");
        testPasswordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                passwordRules = testPasswordField.getText();
                boolean result = checkPassword(passwordRules);
                JOptionPane.showMessageDialog(null, "The password check returns " + result);
            }
        });

        this.add(addLowerAndUpperCaseRuleButton);
        this.add(addLowerAndUpperCaseRuleField);
        this.add(addLowerAndUpperCaseRuleBox);
        this.add(rulesList);
        this.add(deleteSelectedButton);
        this.add(new JLabel("Password to test: "));
        this.add(testPasswordField);
        this.add(testPasswordButton);

        this.setLayout(new FlowLayout());
        this.pack();
        this.setVisible(true);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void addLowerCaseRule(int length, boolean numbersAllowed){
        String rule;
        if(numbersAllowed){
            rule = "[a-z0-9]{" + length + "}";
        }else{
            rule = "[a-z]{" + length + "}";
        }
        listModel.addElement(rule);
    }

    private void addUpperCaseRule(int length, boolean numbersAllowed){
        String rule;
        if(numbersAllowed){
            rule = "[A-Z0-9]{" + length + "}";
        }else{
            rule = "[A-Z]{" + length + "}";
        }
        listModel.addElement(rule);
    }

    private void addLowerAndUpperCaseRule(int length, boolean numbersAllowed){
        String rule;
        if(numbersAllowed){
            rule = "[aA-zZ0-9]{" + length + "}";
        }else{
            rule = "[aA-zZ]{" + length + "}";
        }
        listModel.addElement(rule);
    }

    private boolean checkPassword(String password){
        StringBuilder rules = new StringBuilder();
        for(int i = 0; i < listModel.size(); i++){
            rules.append(listModel.get(i));
        }
        if (password.matches(rules.toString())) return true;
        return false;
    }

    public static void main(String[] args){
        Test test = new Test();
    }
}

Upvotes: 1

Related Questions