package com.novell.admin.ndssnapins.loginMethods.CPassword;
import java.awt.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.*;
import com.novell.application.console.widgets.*;
import com.novell.admin.common.ui.*;
import com.novell.admin.ns.*;
import com.novell.admin.ns.nds.*;
import com.novell.admin.common.snapins.*;
import com.novell.admin.common.exceptions.*;
import com.novell.application.console.snapin.*;
import com.novell.utility.nmsgbox.*;
import com.novell.security.japi.nmas.*;
public final class CPasswordPanel extends AdminPage
implements ItemListener, ActionListener, KeyListener
{
private String treeName, userDN;
private NSObject userObj;
private String cPasswordLabel = CPassword.getString(CPassword.CPassword_LABEL);
private String entryLabel = CPassword.getString(CPassword.CPassword_ENTRY_LABEL);
private String confirmLabel = CPassword.getString(CPassword.CPassword_CONFIRM_LABEL);
private String allowLabel = CPassword.getString(CPassword.CPassword_ALLOW_LABEL);
private String cPasswordInstructions = CPassword.getString(CPassword.CPassword_INSTRUCTIONS);
private String noRightsLabel = CPassword.getString(CPassword.CPassword_NO_RIGHTS_LABEL);
private NDSObjectAttribute hostServerAttrib = null;
private JCheckBox cPasswordAllowCheck = new JCheckBox(allowLabel, false);
private JPasswordField entryField = new JPasswordField(20);
private JPasswordField confirmField = new JPasswordField(20);
private JLabel entryJLabel = new JLabel(entryLabel);
private JLabel confirmJLabel = new JLabel(confirmLabel);
private JPanel entryJPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
private JPanel confirmJPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
private boolean debug = false;
private boolean hasAdminRights = false;
private boolean hasWriteRights = false;
private boolean cPasswordAllow = false;
private boolean allowModified = false;
private boolean anythingToSave = false;
private boolean readDataComplete = false;
private Color colorOn;
private Color colorOff;
public CPasswordPanel( InitSnapinInfo snapinInfo,
AdminPageSnapin snapin,
NSObject obj,
String tree,
String user)
{
super( snapinInfo, snapin, obj );
printDebuggingLine("tree: " + tree + " user: " + user);
treeName = tree;
userDN = user;
userObj = obj;
ObjectEntryCollection oec = shell.getCurrentSelections();
ObjectEntry oe;
if (oec.hasNoElements())
{
oe = shell.getTreeSelection();
}
else
{
oe = oec.getFirstElement();
}
if (null != oe)
{
NDSNamespace ns = ((NDSNamespace)oe.getObjectType().getNamespace());
try
{
ObjectEntry authUserOE = ((AuthenticationNamespace)ns).getAuthenticatedIdentity(oe);
NDSObjectRights objRights = (NDSObjectRights)ns.getObjectEffectiveRights(oe, authUserOE);
hasAdminRights = objRights.hasSupervisorRights();
if (hasAdminRights == true)
{
hasWriteRights = true;
}
else
{
hasWriteRights = objRights.hasWriteRights();
}
}
catch (SPIException exception)
{
printDebuggingLine("Got exception checking for admin rights.");
}
}
setLayout( new BorderLayout(0,5) );
}
protected void enableCPasswordEntry()
{
entryField.setEnabled(true);
entryField.setBackground(colorOn);
entryJLabel.setEnabled(true);
confirmField.setEnabled(true);
confirmField.setBackground(colorOn);
confirmJLabel.setEnabled(true);
}
protected void disableCPasswordEntry()
{
entryField.setBackground(colorOff);
entryField.setEnabled(false);
entryJLabel.setEnabled(false);
confirmField.setBackground(colorOff);
confirmField.setEnabled(false);
confirmJLabel.setEnabled(false);
}
public void itemStateChanged(ItemEvent event)
{
printDebuggingLine("itemStateChanged.event: " + event);
Object src = event.getSource();
int i;
if (readDataComplete)
{
if (src == cPasswordAllowCheck)
{
anythingToSave = true;
propertyBook.setModified(true, pageSnapin);
allowModified = (!allowModified);
cPasswordAllow = cPasswordAllowCheck.isSelected();
printDebuggingLine("cPasswordAllow: " + cPasswordAllow);
if (cPasswordAllow)
{
enableCPasswordEntry();
}
else
{
disableCPasswordEntry();
}
}
}
}
public void actionPerformed( ActionEvent evt )
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
anythingToSave = true;
propertyBook.setModified(true, pageSnapin);
}
public void keyPressed(KeyEvent e)
{
}
protected void checkRights() throws SPIException
{
}
protected void printDebuggingLine(String theLine)
{
if(debug)
{
System.out.println(theLine);
}
}
protected void layoutControls()
{
JPanel cPasswordJPanel = null;
NWrapLabel header = null;
if (hasWriteRights == true)
{
colorOff = entryJLabel.getBackground();
colorOn = entryField.getBackground();
entryJPanel.add(entryJLabel);
entryJPanel.add(entryField);
confirmJPanel.add(confirmJLabel);
confirmJPanel.add(confirmField);
cPasswordAllowCheck.setSelected(false);
entryField.setEchoChar('*');
confirmField.setEchoChar('*');
disableCPasswordEntry();
NVerticalFlowLayout myFLayout = new NVerticalFlowLayout();
myFLayout.setExternalPadLeft(10);
cPasswordJPanel = new JPanel(myFLayout);
cPasswordJPanel.add(cPasswordAllowCheck);
cPasswordJPanel.add(entryJPanel);
cPasswordJPanel.add(confirmJPanel);
cPasswordJPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),cPasswordLabel));
}
else
{
}
NVerticalFlowLayout vflControl = new NVerticalFlowLayout(false, 0);
vflControl.setExternalPadLeft(0);
JPanel controlPanel = new JPanel(vflControl);
if (cPasswordJPanel != null)
{
controlPanel.add(cPasswordJPanel, NVerticalFlowLayout.LEFT);
}
if (hasAdminRights || hasWriteRights)
{
header = new NWrapLabel(cPasswordInstructions);
}
else
{
header = new NWrapLabel(noRightsLabel);
}
header.setMaxWidth(615);
add(header, BorderLayout.NORTH);
add(controlPanel, BorderLayout.WEST);
}
public boolean saveData()
{
return(true);
}
public boolean savePassword()
{
int ccode,i;
if (!hasWriteRights && !hasAdminRights)
{
printDebuggingLine("Doesn't have write rights.");
propertyBook.setModified(false, pageSnapin);
return(true);
}
if (!anythingToSave)
{
printDebuggingLine("Nothing to save.");
propertyBook.setModified(false, pageSnapin);
return(true);
}
if (cPasswordAllow)
{
if (allowModified)
{
if ((ccode = NMASWrap.NMAS_PutLoginConfig(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.AllowTag, CPassword.AllowDataLen, CPassword.AllowData)) != 0)
{
String message = CPassword.getString(CPassword.CPassword_PLC_ERR_MSG) + ccode;
NMsgBox msg = new NMsgBox(shell.getShellFrame(), CPassword.getString(CPassword.CPassword_ENTRY_ERR_TITLE), message, NMsgBox.ERROR, null, null);
msg.show();
return(false);
}
allowModified = false;
}
String cPasswordString = new String(entryField.getPassword());
String cPasswordConfirmString = new String(confirmField.getPassword());
printDebuggingLine("clear password: " + cPasswordString + "\n clear confirm : " + cPasswordConfirmString);
if (cPasswordString.equals(cPasswordConfirmString) == false)
{
String message = CPassword.getString(CPassword.CPassword_UNEQUAL_ERR_MSG);
NMsgBox msg = new NMsgBox(shell.getShellFrame(), CPassword.getString(CPassword.CPassword_ENTRY_ERR_TITLE), message, NMsgBox.ERROR, null, null);
msg.show();
propertyBook.setModified(true, pageSnapin);
return(false);
}
byte CPasswordData[] = cPasswordString.getBytes();
if ((ccode = NMASWrap.NMAS_PutLoginSecret(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.SecretTag, CPasswordData.length, CPasswordData)) != 0)
{
String message = CPassword.getString(CPassword.CPassword_PLC_ERR_MSG) + ccode;
NMsgBox msg = new NMsgBox(shell.getShellFrame(), CPassword.getString(CPassword.CPassword_ENTRY_ERR_TITLE), message, NMsgBox.ERROR, null, null);
msg.show();
return(false);
}
else
{
printDebuggingLine("NMAS_PutLoginSecret returned " + ccode);
printDebuggingLine("The Clear Password was '" + cPasswordString + "'");
}
}
else
{
if (allowModified)
{
if ((ccode = NMASWrap.NMAS_PutLoginConfig(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.AllowTag, CPassword.AllowDataLen, CPassword.DoNotAllowData)) != 0)
{
String message = CPassword.getString(CPassword.CPassword_PLC_ERR_MSG) + ccode;
NMsgBox msg = new NMsgBox(shell.getShellFrame(), CPassword.getString(CPassword.CPassword_ENTRY_ERR_TITLE), message, NMsgBox.ERROR, null, null);
msg.show();
return(false);
}
if ((ccode = NMASWrap.NMAS_DeleteLoginSecret(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.SecretTag)) != 0)
{
String message = CPassword.getString(CPassword.CPassword_DLC_ERR_MSG) + ccode;
NMsgBox msg = new NMsgBox(shell.getShellFrame(), CPassword.getString(CPassword.CPassword_ENTRY_ERR_TITLE), message, NMsgBox.ERROR, null, null);
msg.show();
return(false);
}
allowModified = false;
}
}
anythingToSave = false;
propertyBook.setModified(false, pageSnapin);
return true;
}
public void setupMOD()
{
}
protected void readData()
{
int ccode = 0;
int bytesRead[] = new int[1];
byte areCPasswordsAllowed[] = new byte[CPassword.AllowDataLen];
if ((ccode = NMASWrap.NMAS_GetLoginConfig(treeName, userDN, CPassword.MethodIDLen, CPassword.MethodID, CPassword.AllowTag, CPassword.AllowDataLen, bytesRead, areCPasswordsAllowed)) != 0)
{
printDebuggingLine("Error" + ccode + "from NMAS_GetLoginConfig");
}
else if (bytesRead[0] == 0)
{
printDebuggingLine("Zero bytes read from NMAS_GetLoginSecret");
}
else if (areCPasswordsAllowed[0] == 1)
{
cPasswordAllow = true;
cPasswordAllowCheck.setSelected(true);
enableCPasswordEntry();
}
else
{
cPasswordAllow = false;
cPasswordAllowCheck.setSelected(false);
disableCPasswordEntry();
}
cPasswordAllowCheck.addItemListener(this);
entryField.addKeyListener(this);
confirmField.addKeyListener(this);
readDataComplete = true;
}
public void setActive(boolean isFirstTimeShown)
{
printDebuggingLine("setActive.isFirstTimeShown: " + isFirstTimeShown);
super.setActive(isFirstTimeShown);
}
public boolean killActive()
{
printDebuggingLine("killActive");
return super.killActive();
}
public void shutdownSnapin()
{
printDebuggingLine("shutdownSnapin");
super.shutdownSnapin();
}
public String[] getHandledAttributeNames()
{
String[] attributes = new String[1];
attributes[0] = "";
return attributes;
}
}