Added:
LDAP lookup for user info (for help generation) LDAP authentication class (if useful one day)
This commit is contained in:
@@ -115,8 +115,6 @@ public class NewTest extends javax.swing.JPanel {
|
||||
jTableParams = new javax.swing.JTable();
|
||||
jPanel1 = new javax.swing.JPanel();
|
||||
jButtonGenerate = new javax.swing.JButton();
|
||||
jButton2 = new javax.swing.JButton();
|
||||
jTextField1 = new javax.swing.JTextField();
|
||||
|
||||
jMenuItemAddRow.setText("Add Row");
|
||||
jMenuItemAddRow.addActionListener(new java.awt.event.ActionListener() {
|
||||
@@ -208,25 +206,12 @@ public class NewTest extends javax.swing.JPanel {
|
||||
}
|
||||
});
|
||||
|
||||
jButton2.setText("jButton2");
|
||||
jButton2.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
jButton2ActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
jTextField1.setText("Boccioli Marco");
|
||||
|
||||
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
|
||||
jPanel1.setLayout(jPanel1Layout);
|
||||
jPanel1Layout.setHorizontalGroup(
|
||||
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(jPanel1Layout.createSequentialGroup()
|
||||
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(jButton2)
|
||||
.addGap(48, 48, 48)
|
||||
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addContainerGap())
|
||||
);
|
||||
@@ -234,10 +219,7 @@ public class NewTest extends javax.swing.JPanel {
|
||||
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(jPanel1Layout.createSequentialGroup()
|
||||
.addGap(0, 0, 0)
|
||||
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(jButton2)
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addContainerGap())
|
||||
);
|
||||
|
||||
@@ -339,15 +321,6 @@ public class NewTest extends javax.swing.JPanel {
|
||||
}
|
||||
}//GEN-LAST:event_jMenuItemDeleteRowActionPerformed
|
||||
|
||||
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
|
||||
// TODO add your handling code here:
|
||||
// LdapClient ldap = new LdapClient();
|
||||
// System.out.println( "boccioli_m exists:: "+ldap.userExists("boccioli_m"));
|
||||
|
||||
|
||||
getUserInfo(this.jTextField1.getText());
|
||||
}//GEN-LAST:event_jButton2ActionPerformed
|
||||
|
||||
/**
|
||||
* fill table with example parameters
|
||||
* @param newType of type TypeOfNewFile, can be device or test.
|
||||
@@ -529,14 +502,15 @@ public class NewTest extends javax.swing.JPanel {
|
||||
SwingUtils.showMessage(this, "checkDataFields()",
|
||||
"Please provide a valid user name");
|
||||
return false;
|
||||
} /*else{ //check if the user name is in LDAP *FOR THE MOMENT IT DOES NOT WORK*
|
||||
LdapClient ldap = new LdapClient();
|
||||
if ( !ldap.userExists(txtContactName.getText())){
|
||||
SwingUtils.showMessage(this, "checkDataFields()",
|
||||
"Could not find user name "+txtContactName.getText()+"\nPlease provide a valid user name");
|
||||
return false;
|
||||
} else{ //check if the user name is in LDAP *FOR THE MOMENT IT DOES NOT WORK*
|
||||
if ( !userExists(txtContactName.getText())){
|
||||
OptionResult ret = SwingUtils.showOption(this, "checkDataFields()",
|
||||
"Could not find user name "+txtContactName.getText() + "\nDo you want to continue generating the test?", OptionType.YesNoCancel);
|
||||
if(ret != OptionResult.Yes){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
//check parameters table
|
||||
for(int row=0 ; row<jTableParams.getRowCount() ; row++){
|
||||
String sParam = jTableParams.getValueAt(row, 0).toString();
|
||||
@@ -880,17 +854,15 @@ public class NewTest extends javax.swing.JPanel {
|
||||
}
|
||||
}
|
||||
Attributes user = null;
|
||||
/* //does not work for the moment
|
||||
LdapClient ldap = new LdapClient();
|
||||
user = ldap.getUserInfo(sContactName);
|
||||
*/
|
||||
user = getUserInfo(sContactName);
|
||||
String contactTelephone = "";
|
||||
String contactFirstName = sContactName;
|
||||
String contactSurname = "";
|
||||
if (user != null){
|
||||
contactTelephone = user.get("telephoneNumber").toString().replace("telephoneNumber: ", "");
|
||||
contactTelephone = user.get("telephoneNumber").toString().replace("telephoneNumber: ", "").replace(" ", "");
|
||||
contactTelephone = "Tel. " + contactTelephone.substring(contactTelephone.length()-4);
|
||||
contactFirstName = user.get("givenname").toString().replace("givenName: ", "");
|
||||
contactSurname = user.get("sn").toString().replace("sn: ", "");
|
||||
contactSurname = user.get("sn").toString().replace("sn: ", "");
|
||||
}
|
||||
success = replaceParameters(path,
|
||||
new String[]{"$testName","$testDescription","$testParameters","$contactUserName","$contactFirstName","$contactSurname","$contactTelephone"},
|
||||
@@ -951,86 +923,56 @@ public class NewTest extends javax.swing.JPanel {
|
||||
//does not work
|
||||
private Attributes getUserInfo(String userName){
|
||||
Attributes attrs = null;
|
||||
Hashtable env = new Hashtable();
|
||||
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
||||
"com.sun.jndi.ldap.LdapCtxFactory");
|
||||
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
|
||||
DirContext ctx = null;
|
||||
try {
|
||||
Hashtable env = new Hashtable();
|
||||
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
||||
"com.sun.jndi.ldap.LdapCtxFactory");
|
||||
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
|
||||
|
||||
DirContext ctx = null;
|
||||
try {
|
||||
ctx = new InitialDirContext(env);
|
||||
} catch (NamingException ex) {
|
||||
System.out.println("initialdircontext error");
|
||||
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
try {
|
||||
attrs = ctx.getAttributes("sn="+userName+",ou=People");
|
||||
} catch (NamingException ex) {
|
||||
System.out.println("get attributes error");
|
||||
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
if(attrs == null){
|
||||
System.out.println("nothing found for " + userName);
|
||||
}else{
|
||||
System.out.println( attrs.get("sn").get().toString());
|
||||
}
|
||||
ctx = new InitialDirContext(env);
|
||||
} catch (NamingException ex) {
|
||||
System.out.println("initialdircontext error");
|
||||
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
try {
|
||||
attrs = ctx.getAttributes("cn="+userName+",ou=users,ou=psi,dc=d,dc=psi,dc=ch");
|
||||
} catch (NamingException ex) {
|
||||
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
if(attrs == null){
|
||||
System.out.println("LDAP: user" + userName +" not found.");
|
||||
}
|
||||
return attrs;
|
||||
}
|
||||
|
||||
private boolean userExists(String userName){
|
||||
Attributes attrs = getUserInfo(userName);
|
||||
return (attrs!=null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* LDAP tool for checking if a user name exists in LDAP.
|
||||
* ######### IT DOES NOT WORK YET ################
|
||||
* ######### Problem: I don't manage to access LDAP as anonymous user
|
||||
* #########(I need to give a username+password in order to get info from a user)
|
||||
* LDAP tool for authenticating a user
|
||||
*/
|
||||
|
||||
public class LdapClient {
|
||||
|
||||
public void LdapClient() {
|
||||
|
||||
}
|
||||
|
||||
public void test() {
|
||||
System.out.println("run: " + new Date());
|
||||
LdapContext ldapContext = getLdapContext();
|
||||
SearchControls searchControls = getSearchControls();
|
||||
getUserInfo("boccioli_m", ldapContext, searchControls);
|
||||
getUserInfo("rees_s", ldapContext, searchControls);
|
||||
getUserInfo("Andrey_Zinovyev", ldapContext, searchControls);
|
||||
getUserInfo("Roman_Kurbangaliyev", ldapContext, searchControls);
|
||||
getUserInfo("fake_user", ldapContext, searchControls);
|
||||
System.out.println("done: " + new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Ldap context. It is used later for getting user info
|
||||
* @return LdapContext
|
||||
*/
|
||||
private LdapContext getLdapContext() {
|
||||
private LdapContext getLdapContext(String userName, String password) {
|
||||
LdapContext ctx = null;
|
||||
try {
|
||||
Hashtable<String, String> env = new Hashtable<String, String>();
|
||||
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
|
||||
//env.put(Context.SECURITY_AUTHENTICATION, "none");//with "none" it does not seem to work. It works with "simple" but it needs a user name and password
|
||||
//env.put(Context.SECURITY_PRINCIPAL, "PSICH\\boccioli_m");//input user & password for access to ldap
|
||||
//env.put(Context.SECURITY_CREDENTIALS, "");//here I should write my password in clear!?!?!?
|
||||
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
|
||||
//env.put(Context.REFERRAL, "follow");
|
||||
|
||||
//this works:
|
||||
/*
|
||||
//this can be used to authenticate:
|
||||
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
|
||||
env.put(Context.SECURITY_AUTHENTICATION, "simple");//with "none" it does not seem to work. It works with "simple" but it needs a user name and password
|
||||
env.put(Context.SECURITY_PRINCIPAL, "PSICH\\boccioli_m");//input user & password for access to ldap
|
||||
env.put(Context.SECURITY_CREDENTIALS, "");//here I should write my password in clear!?!?!?
|
||||
env.put(Context.SECURITY_PRINCIPAL, "PSICH\\"+userName);//input user & password for access to ldap
|
||||
env.put(Context.SECURITY_CREDENTIALS, password);//here I should write my password in clear!?!?!?
|
||||
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
|
||||
env.put(Context.REFERRAL, "follow");
|
||||
*/
|
||||
|
||||
ctx = new InitialLdapContext(env, null);
|
||||
|
||||
@@ -1041,7 +983,7 @@ public class NewTest extends javax.swing.JPanel {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
public Attributes getUserInfo(String userName, LdapContext ctx, SearchControls searchControls) {
|
||||
private Attributes authenticateUser(String userName, LdapContext ctx, SearchControls searchControls) {
|
||||
System.out.println("*** " + userName + " ***");
|
||||
Attributes attrs = null;
|
||||
try {
|
||||
@@ -1061,25 +1003,17 @@ public class NewTest extends javax.swing.JPanel {
|
||||
return attrs;
|
||||
}
|
||||
|
||||
public Attributes getUserInfo(String userName) {
|
||||
public Attributes authenticateUser(String userName, String password) {
|
||||
Attributes attrs = null;
|
||||
LdapContext ldapContext = getLdapContext();
|
||||
LdapContext ldapContext = getLdapContext(userName,password);
|
||||
SearchControls searchControls = getSearchControls();
|
||||
attrs = getUserInfo(userName, ldapContext, searchControls);
|
||||
attrs = authenticateUser(userName, ldapContext, searchControls);
|
||||
String tel = attrs.get("telephonenumber").toString().replace(" ", "");
|
||||
tel = "Tel.: " + tel.substring(tel.length()-4);
|
||||
attrs.put("telephoneNumber",tel);
|
||||
return attrs;
|
||||
}
|
||||
|
||||
public boolean userExists(String userName) {
|
||||
//nex.printStackTrace();
|
||||
|
||||
LdapContext ldapContext = getLdapContext();
|
||||
SearchControls searchControls = getSearchControls();
|
||||
Attributes attrs = getUserInfo(userName, ldapContext, searchControls);
|
||||
return (attrs != null);
|
||||
}
|
||||
|
||||
private SearchControls getSearchControls() {
|
||||
SearchControls cons = new SearchControls();
|
||||
@@ -1094,7 +1028,6 @@ public class NewTest extends javax.swing.JPanel {
|
||||
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
private javax.swing.JButton jButton2;
|
||||
private javax.swing.JButton jButtonGenerate;
|
||||
private javax.swing.JComboBox jComboBoxTestSuites;
|
||||
private javax.swing.JMenuItem jMenuItemAddRow;
|
||||
@@ -1104,7 +1037,6 @@ public class NewTest extends javax.swing.JPanel {
|
||||
private javax.swing.JScrollPane jScrollPane1;
|
||||
private javax.swing.JScrollPane jScrollPane3;
|
||||
private javax.swing.JTable jTableParams;
|
||||
private javax.swing.JTextField jTextField1;
|
||||
private javax.swing.JLabel lblContact;
|
||||
private javax.swing.JLabel lblDescr;
|
||||
private javax.swing.JLabel lblName;
|
||||
|
||||
Reference in New Issue
Block a user