📄 createadminaccountpanel.java
字号:
/* * ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */package vocal.pw;import vocal.comm.VPPTransactionWrapper;import vocal.comm.VPPException;import vocal.comm.VPPNoSuchFileException;import vocal.pw.PasswordManager;import vocal.pw.AdminTableModel;import vocal.userEditor.Buttons;import vocal.userEditor.EditorModes;import javax.swing.*;import javax.swing.border.EmptyBorder;import java.io.FileNotFoundException;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.security.NoSuchAlgorithmException;/** * Used to both create and edit administrative accounts.<p> * Allows the user to provide a user id, password, confirmed password and select * whether either (or both) administrator or technician access is enabled for * the account. Then goes to check whether an administrative account with that * name already exists and writes it to the provisioning server if it does not. */public class CreateAdminAccountPanel extends JPanel{ /** * A "template" for the contents of the administrative account file. */ private static final String xml = "<admin> " + "<name>vovida</name>" + "<password>6f7d5634f65192a3b9c8479cee3b655</password>" + // it's "vovida" "<accessLevel type=\"administrator\" enabled=\"true\"></accessLevel>\n" + "<accessLevel type=\"technician\" enabled=\"true\"></accessLevel>" + "</admin>"; private JTextField loginId; private JPasswordField password; private JPasswordField reEnterPassword; private JCheckBox adminAccess; private JCheckBox techAccess; private JButton ok; private JButton cancel; private VPPTransactionWrapper connection; private String adminXml; private int lastButtonClicked = Buttons.BUTTON_NONE; private int mode = EditorModes.ADD_NEW_MODE; public CreateAdminAccountPanel(VPPTransactionWrapper con) { connection = con; loginId = new JTextField(""); password = new JPasswordField(); reEnterPassword = new JPasswordField(); ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // make sure they entered a login name if ((loginId.getText() == null) || (loginId.getText().equals(""))) { JOptionPane.showMessageDialog(CreateAdminAccountPanel.this.getTopLevelAncestor(), "Please enter a login id."); return; } // make sure they entered a password if ((password.getPassword() == null) || (password.getPassword().equals(""))) { JOptionPane.showMessageDialog(CreateAdminAccountPanel.this.getTopLevelAncestor(), "Please enter a password."); return; } String pw1 = new String(password.getPassword()); String pw2 = new String(reEnterPassword.getPassword()); // make sure the two passwords they entered matched if (!pw1.equals(pw2)) { JOptionPane.showMessageDialog(CreateAdminAccountPanel.this.getTopLevelAncestor(), "The passwords did not match. Please try again."); password.setText(""); reEnterPassword.setText(""); return; } try { // build up the xml for the new administrator/tech adminXml = "<admin>\n <name>" + loginId.getText() + "</name>\n" + " <password>" + PasswordManager.computeHash(new String(password.getPassword())) + "</password>\n"; if (adminAccess.isSelected()) { adminXml += " <accessLevel type=\"administrator\" enabled=\"true\"></accessLevel>\n"; } else { adminXml += " <accessLevel type=\"administrator\" enabled=\"false\"></accessLevel>\n"; } if (techAccess.isSelected()) { adminXml += " <accessLevel type=\"technician\" enabled=\"true\"></accessLevel>"; } else { adminXml += " <accessLevel type=\"technician\" enabled=\"false\"></accessLevel>"; } adminXml += "</admin>"; } catch (NoSuchAlgorithmException exx) { exx.printStackTrace(); JOptionPane.showMessageDialog(CreateAdminAccountPanel.this.getTopLevelAncestor(), "Could not find a provider for the password hash algorithm. \n\n"); return; } if (mode == EditorModes.ADD_NEW_MODE) { // go to the admin accounts directory and find out if an admin of this // name already exists. try { CreateAdminAccountPanel.this.connection.doGet("Admin_Accounts", loginId.getText()); JOptionPane.showMessageDialog(CreateAdminAccountPanel.this.getTopLevelAncestor(), "An account with that id already exists. Please use a different id."); loginId.setText(""); return; } catch (VPPNoSuchFileException ex) { // no such admin exists so it is ok to create a new one // ok - go ahead and save the xml file } catch (VPPException ex) { CreateAdminAccountPanel.this.connection.showVPPException(ex, ""); } } // save the xml to the pserver try { connection.doPut("Admin_Accounts", loginId.getText(), adminXml); } catch (VPPException ex) { connection.showVPPException(ex, ""); } lastButtonClicked = Buttons.BUTTON_OK; CreateAdminAccountPanel.this.getTopLevelAncestor().setVisible(false); clear(); } }); cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { lastButtonClicked = Buttons.BUTTON_CANCEL; CreateAdminAccountPanel.this.getTopLevelAncestor().setVisible(false); clear(); } }); adminAccess = new JCheckBox("Administrator access "); adminAccess.setSelected(true); techAccess = new JCheckBox("Technician access "); techAccess.setSelected(true); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setBorder(new EmptyBorder(5, 5, 5, 5)); JPanel temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(new JLabel("Login id: ")); temp.add(Box.createHorizontalGlue()); add(temp); add(loginId); add(Box.createVerticalStrut(5)); temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(new JLabel("Password: ")); temp.add(Box.createHorizontalGlue()); add(temp); add(password); add(Box.createVerticalStrut(5)); temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(new JLabel("Re-enter password: ")); temp.add(Box.createHorizontalGlue()); add(temp); add(reEnterPassword); add(Box.createVerticalStrut(5)); temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(new JLabel("Access level: ")); temp.add(Box.createHorizontalGlue()); add(temp); temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(Box.createHorizontalStrut(10)); temp.add(adminAccess); temp.add(Box.createHorizontalGlue()); add(temp); temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(Box.createHorizontalStrut(10)); temp.add(techAccess); temp.add(Box.createHorizontalGlue()); add(temp); add(Box.createVerticalStrut(10)); temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(Box.createHorizontalGlue()); temp.add(ok); temp.add(Box.createHorizontalStrut(5)); temp.add(cancel); add(temp); loginId.setPreferredSize(new Dimension(360, (int) loginId.getPreferredSize().getHeight())); } private void clear() { loginId.setText(""); password.setText(""); reEnterPassword.setText(""); adminAccess.setSelected(true); techAccess.setSelected(true); } public int getLastButtonClicked() { return lastButtonClicked; } public String getAdminXml() { return adminXml; } /** * Sets the value of a data field in the ui. <p> * Each piece of data (eg: password, login id, etc) is passed to this method * as a string along with an intiger identifying which data it is. This method * then finds the appropriate ui widget which corresponds to this data and * sets its value based on the given string. * @param data string representation of the data to be set * @param id the identifier of the data to be set. Each piece of data is * assigned a unique integer identifier in <code>AdminTableModel</code> */ public void setData(String data, int id) { switch (id) { case AdminTableModel.LOGIN_ID: { loginId.setText(data); } break; case AdminTableModel.ADMIN_ACCESS: { if (data.equals("true")) { adminAccess.setSelected(true); } else { adminAccess.setSelected(false); } } break; case AdminTableModel.TECH_ACCESS: { if (data.equals("true")) { techAccess.setSelected(true); } else { techAccess.setSelected(false); } } break; case AdminTableModel.PASSWORD: { // should this be set? } } } /** * Sets a flag indicating whether this class is being used to add a new * account or edit an existing account. <p> * The distinction exists because it should not be possible to edit the * account name after it has been created so this is used to disable editing * on the account id ui widget. <p> * @param newMode the identifier of the new mode as defined in <code> * EditorModes</code>. */ public void setMode(int newMode) { if (mode != newMode) { if (newMode == EditorModes.ADD_NEW_MODE) { loginId.setEnabled(true); } else if (newMode == EditorModes.EDIT_EXISTING_MODE) { loginId.setEnabled(false); } } mode = newMode; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -