001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.ui;
019
020import static org.opends.messages.AdminToolMessages.*;
021
022import java.awt.Component;
023import java.awt.GridBagConstraints;
024import java.util.ArrayList;
025
026import javax.swing.JLabel;
027import javax.swing.JPasswordField;
028
029import org.opends.guitools.controlpanel.browser.BrowserController;
030import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
031import org.opends.guitools.controlpanel.task.ResetUserPasswordTask;
032import org.opends.guitools.controlpanel.task.Task;
033import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
034import org.opends.guitools.controlpanel.util.Utilities;
035import org.forgerock.i18n.LocalizableMessage;
036
037/** Panel that appears when the user wants to change the password of a user. */
038public class ResetUserPasswordPanel extends StatusGenericPanel
039{
040  private static final long serialVersionUID = 8733172823605832626L;
041  private JLabel dn = Utilities.createDefaultLabel();
042  private JLabel name = Utilities.createDefaultLabel();
043  private JLabel lPassword = Utilities.createPrimaryLabel();
044  private JLabel lConfirmPassword = Utilities.createPrimaryLabel();
045  private JPasswordField password = Utilities.createPasswordField(25);
046  private JPasswordField confirmPassword = Utilities.createPasswordField(25);
047
048  private BasicNode node;
049  private BrowserController controller;
050
051  /** Constructor of the panel. */
052  public ResetUserPasswordPanel()
053  {
054    super();
055    createLayout();
056  }
057
058  /**
059   * Sets the node representing the entry that the user wants to modify the
060   * password from.
061   * @param node the node.
062   * @param controller the browser controller.
063   */
064  public void setValue(BasicNode node, BrowserController controller)
065  {
066    this.node = node;
067    this.controller = controller;
068    setPrimaryValid(lPassword);
069    setPrimaryValid(lConfirmPassword);
070
071    dn.setText(node.getDN());
072    name.setText(node.getDisplayName());
073
074    password.setText("");
075    confirmPassword.setText("");
076
077    packParentDialog();
078  }
079
080  @Override
081  public Component getPreferredFocusComponent()
082  {
083    return password;
084  }
085
086  @Override
087  public void okClicked()
088  {
089    final ArrayList<LocalizableMessage> errors = new ArrayList<>();
090
091    setPrimaryValid(lPassword);
092    setPrimaryValid(lConfirmPassword);
093
094    String pwd1 = new String(password.getPassword());
095    String pwd2 = new String(confirmPassword.getPassword());
096
097    if (pwd1.length() == 0)
098    {
099      errors.add(ERR_CTRL_PANEL_NEW_PASSWORD_REQUIRED.get());
100      setPrimaryInvalid(lPassword);
101    }
102    else if (!pwd1.equals(pwd2))
103    {
104      errors.add(ERR_CTRL_PANEL_PASSWORD_DO_NOT_MATCH.get());
105      setPrimaryInvalid(lPassword);
106      setPrimaryInvalid(lConfirmPassword);
107    }
108    if (errors.isEmpty())
109    {
110      ProgressDialog dlg = new ProgressDialog(
111          Utilities.createFrame(),
112          Utilities.getParentDialog(this),
113          INFO_CTRL_PANEL_RESET_USER_PASSWORD_TITLE.get(), getInfo());
114      ResetUserPasswordTask newTask =
115        new ResetUserPasswordTask(getInfo(), dlg, node, controller,
116            password.getPassword());
117      for (Task task : getInfo().getTasks())
118      {
119        task.canLaunch(newTask, errors);
120      }
121      if (errors.isEmpty())
122      {
123        launchOperation(newTask,
124            INFO_CTRL_PANEL_RESETTING_USER_PASSWORD_SUMMARY.get(),
125            INFO_CTRL_PANEL_RESETTING_USER_PASSWORD_SUCCESSFUL_SUMMARY.get(),
126            INFO_CTRL_PANEL_RESETTING_USER_PASSWORD_SUCCESSFUL_DETAILS.get(),
127            ERR_CTRL_PANEL_RESETTING_USER_PASSWORD_ERROR_SUMMARY.get(),
128            ERR_CTRL_PANEL_RESETTING_USER_PASSWORD_ERROR_DETAILS.get(),
129            null,
130            dlg);
131        Utilities.getParentDialog(this).setVisible(false);
132        dlg.setVisible(true);
133      }
134    }
135    if (!errors.isEmpty())
136    {
137      displayErrorDialog(errors);
138    }
139  }
140
141  @Override
142  public LocalizableMessage getTitle()
143  {
144    return INFO_CTRL_PANEL_RESET_USER_PASSWORD_TITLE.get();
145  }
146
147  @Override
148  public void configurationChanged(ConfigurationChangeEvent ev)
149  {
150  }
151
152  /** Creates the layout of the panel (but the contents are not populated here). */
153  private void createLayout()
154  {
155    GridBagConstraints gbc = new GridBagConstraints();
156    gbc.gridx = 0;
157    gbc.gridy = 0;
158    gbc.weightx = 0.0;
159    gbc.weighty = 0.0;
160    gbc.fill = GridBagConstraints.HORIZONTAL;
161
162    LocalizableMessage[] strings =
163    {
164        INFO_CTRL_PANEL_RESET_USER_PASSWORD_DN_LABEL.get(),
165        INFO_CTRL_PANEL_RESET_USER_PASSWORD_NAME_LABEL.get(),
166        INFO_CTRL_PANEL_RESET_USER_PASSWORD_PWD_LABEL.get(),
167        INFO_CTRL_PANEL_RESET_USER_PASSWORD_CONFIRM_LABEL.get()
168    };
169    JLabel[] labels = {null, null, lPassword, lConfirmPassword};
170    Component[] comps = {dn, name, password, confirmPassword};
171
172    for (int i=0; i<strings.length; i++)
173    {
174      if (labels[i] == null)
175      {
176        labels[i] = Utilities.createPrimaryLabel(strings[i]);
177      }
178      else
179      {
180        labels[i].setText(strings[i].toString());
181      }
182
183      gbc.gridx = 0;
184      gbc.insets.left = 0;
185      gbc.weightx = 0.0;
186      add(labels[i], gbc);
187      gbc.insets.left = 10;
188      gbc.gridx ++;
189      gbc.weightx = 1.0;
190      add(comps[i], gbc);
191
192      gbc.insets.top = 10;
193      gbc.gridy ++;
194    }
195
196    addBottomGlue(gbc);
197  }
198}