From 7fa225214ade28ba8dec26c0f2f51da1a9a6aae8 Mon Sep 17 00:00:00 2001 From: boccioli_m Date: Wed, 16 Sep 2015 15:05:12 +0200 Subject: [PATCH] Added: LDAP lookup for user info (for help generation) LDAP authentication class (if useful one day) --- plugins/NewTest.form | 23 +----- plugins/NewTest.java | 152 +++++++++++---------------------------- plugins/TestingList.java | 2 +- 3 files changed, 44 insertions(+), 133 deletions(-) diff --git a/plugins/NewTest.form b/plugins/NewTest.form index 6e8107d..45fe096 100644 --- a/plugins/NewTest.form +++ b/plugins/NewTest.form @@ -287,10 +287,6 @@ - - - - @@ -300,11 +296,7 @@ - - - - - + @@ -319,19 +311,6 @@ - - - - - - - - - - - - - diff --git a/plugins/NewTest.java b/plugins/NewTest.java index 89e40cb..290024d 100644 --- a/plugins/NewTest.java +++ b/plugins/NewTest.java @@ -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 env = new Hashtable(); - 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; diff --git a/plugins/TestingList.java b/plugins/TestingList.java index 62d562e..108b222 100644 --- a/plugins/TestingList.java +++ b/plugins/TestingList.java @@ -2043,7 +2043,7 @@ public class TestingList extends Panel { public void loadSettings() { final JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(TESTS_CONFIG_DEFAULT_DIR.toFile()); - int returnVal = fc.showOpenDialog(this); + int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); loadSettings(file);