Project

General

Profile

« Previous | Next » 

Revision 1000

Added by berkley over 22 years ago

fixed groups error, I hope.

View differences:

src/edu/ucsb/nceas/metacat/AuthLdap.java
480 480
  public String[] getGroups(String user, String password, String foruser) 
481 481
         throws ConnectException
482 482
  {
483
    String[] groups = null;
483
    Vector uvec = new Vector();
484 484
    //Pass the username and password to run() method
485 485
    userName=user;
486 486
    userPassword=password;
......
508 508
        } else {
509 509
            filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
510 510
        }
511
        MetaCatUtil.debug("searching for groups: " + filter);
511 512
        NamingEnumeration enum = ctx.search(ldapBase, filter, ctls);
512 513

  
513 514
        // Print the groups
514
        Vector uvec = new Vector();
515
        MetaCatUtil.debug("getting group results.");
515 516
        while (enum.hasMore()) {
516
          SearchResult sr = (SearchResult)enum.next();
517
          SearchResult sr = (SearchResult)enum.next();          
517 518
          uvec.add(sr.getName()+","+ldapBase);
519
          MetaCatUtil.debug("group " + sr.getName() + " added to Group vector");
518 520
        }
519
      
520

  
521
        // initialize groups[] and fill it
522
        groups = new String[uvec.size()];
523
        for (int i=0; i < uvec.size(); i++) {
524
          groups[i] = (String)uvec.elementAt(i); 
525
        }
526

  
527 521
        // Close the context when we're done
528 522
        ctx.close();
529 523

  
530
    } catch (ReferralException re) {
524
    } 
525
    catch (ReferralException re) 
526
    {
527
      refExc = re;
528
      Thread t = new Thread(new GetGroup());
529
      util.debugMessage("Starting thread...");
530
      t.start();
531
      util.debugMessage("sleeping for 5 seconds.");
531 532
      try
532 533
      {
533
        refExc = re;
534
        Thread t = new Thread(new GetGroup());
535
        util.debugMessage("Starting thread...");
536
        t.start();
537
        util.debugMessage("sleeping for 5 seconds.");
538
        Thread.sleep(5000); 
539
        //this is a manual override of ldap's hideously long time
540
        //out period.
541
        util.debugMessage("Awake after 5 seconds.");
542
        if (referralContext == null)
534
        Thread.sleep(5000);
535
      }
536
      catch(InterruptedException ie)
537
      {
538
        MetaCatUtil.debug("main thread interrupted: " + ie.getMessage());
539
      }
540
      //this is a manual override of jndi's hideously long time
541
      //out period.
542
      util.debugMessage("Awake after 5 seconds.");
543
      if (referralContext == null)
544
      {
545
        util.debug("thread timed out...returning groups: " + uvec.toString());
546
        String groups[] = new String[uvec.size()];
547
        for(int i=0; i<uvec.size(); i++)
543 548
        {
544
          t.interrupt();
545
          return null;
549
          groups[i] = (String)uvec.elementAt(i);
546 550
        }
547
        DirContext dc = (DirContext)referralContext;
548
        String[] attrIDs = {"cn"};
549
        // Specify the attributes to match.
550
        // Groups are objects with attribute objectclass=groupofuniquenames.
551
        // and have attribute uniquemember: uid=foruser,ldapbase.
552
        SearchControls ctls = new SearchControls();
553
        ctls.setReturningAttributes(attrIDs);
554
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
555
        
556
        String filter = null;
557
        String gfilter = "(objectClass=groupOfUniqueNames)";
558
        if (null == foruser) {
559
            filter = gfilter;
560
        } else {
561
            filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
562
        }
551
        t.interrupt();
552
        return groups;
553
      }
554
      DirContext dc = (DirContext)referralContext;
555
      String[] attrIDs = {"cn"};
556
      // Specify the attributes to match.
557
      // Groups are objects with attribute objectclass=groupofuniquenames.
558
      // and have attribute uniquemember: uid=foruser,ldapbase.
559
      SearchControls ctls = new SearchControls();
560
      ctls.setReturningAttributes(attrIDs);
561
      ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
562
      
563
      String filter = null;
564
      String gfilter = "(objectClass=groupOfUniqueNames)";
565
      if (null == foruser) {
566
          filter = gfilter;
567
      } else {
568
          filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
569
      }
570
      
571
      try
572
      {
563 573
        NamingEnumeration enum = dc.search(ldapBase, filter, ctls);
564

  
565 574
        // Print the groups
566
        Vector uvec = new Vector();
567 575
        while (enum.hasMore()) {
568 576
          SearchResult sr = (SearchResult)enum.next();
569 577
          uvec.add(sr.getName()+","+ldapBase);
570 578
        }
571

  
572
        // initialize groups[] and fill it
573
        groups = new String[uvec.size()];
574
        for (int i=0; i < uvec.size(); i++) {
575
          groups[i] = (String)uvec.elementAt(i); 
576
        }
579
        
577 580
        referralContext.close();
578 581
        dc.close();
579 582
      }
580
      catch (Exception e)
583
      catch(NamingException ne)
581 584
      {
582
        return groups;
585
        MetaCatUtil.debug("Naming Exception in AuthLdap.getGroups");
583 586
      }
584 587
    } catch (NamingException e) {
585 588
      e.printStackTrace(System.err);
586 589
      throw new ConnectException(
587 590
      "Problem getting groups for a user in AuthLdap.getGroups:" + e);
588 591
    } 
592
    
593
    MetaCatUtil.debug("The user is in the following groups: " + uvec.toString());
594
    String groups[] = new String[uvec.size()];
595
    for(int i=0; i<uvec.size(); i++)
596
    {
597
      groups[i] = (String)uvec.elementAt(i);
598
    }
589 599
    return groups;
590 600
  }
591 601

  
......
978 988
    public void run()
979 989
    {
980 990
      referralContext = null;
991
      MetaCatUtil.debug("getting groups context");
981 992
      DirContext refDirContext=null;
982 993
      boolean moreReferrals=true;
983 994
      //set a while loop is because we don't know if a referral excption 
......
989 1000
          //revise environment variable
990 1001
          String refInfo = null;
991 1002
          refInfo = (String)refExc.getReferralInfo();
992
          //refInfo = (String)refExc.getReferralContext().getEnvironment()
993
          //.get(Context.PROVIDER_URL);
994
          //System.out.println("refInfo: " + refInfo);
995 1003
          if(refInfo != null)
996 1004
          {
997
            //System.out.println("Referral in thread to: " + 
998
            //                  refInfo.toString());
1005
            MetaCatUtil.debug("Referral in thread to: " + 
1006
                              refInfo.toString());
999 1007
          }
1000 1008
          else
1001 1009
          {
1010
            MetaCatUtil.debug("getting refInfo Manually");
1002 1011
            refInfo = (String)refExc.getReferralContext().getEnvironment().
1003 1012
                                                  get(Context.PROVIDER_URL);
1004 1013
          }
1014
          MetaCatUtil.debug("refInfo: " + refInfo);
1005 1015
        
1006
          /*env.put(Context.PROVIDER_URL, refExc.getReferralInfo());
1007 1016
          env.put(Context.INITIAL_CONTEXT_FACTORY, 
1008
                "com.sun.jndi.ldap.LdapCtxFactory");
1009
          env.put(Context.SECURITY_PRINCIPAL, userName);
1010
          env.put(Context.SECURITY_CREDENTIALS, userPassword);
1011
          env.put(Context.REFERRAL, "throw");*/
1012
        
1013
          //get a context object for referral in the new envriment
1014
          //rContext = refExc.getReferralContext();
1015
        
1016
          env.put(Context.INITIAL_CONTEXT_FACTORY, 
1017 1017
              "com.sun.jndi.ldap.LdapCtxFactory");
1018 1018
          env.put(Context.REFERRAL, "throw");
1019 1019
          env.put(Context.PROVIDER_URL, refInfo);
1020 1020
        
1021
          MetaCatUtil.debug("creating referralContext");
1021 1022
          referralContext = new InitialDirContext(env);
1022
          //casting the context to dircontext and it will create a
1023
          //autherntication or naming exception if DN and password is incorrect
1024
          //referralContext=rContext;
1025
          //refDirContext=(DirContext)rContext;
1026
          //refDirContext.close();
1023
          MetaCatUtil.debug("referralContext created");
1027 1024
          //get context and jump out the while loop
1028 1025
          moreReferrals=false;
1029 1026
        }//try
src/edu/ucsb/nceas/metacat/MetaCatUtil.java
48 48
  public static AbstractDatabase dbAdapter;
49 49
  private static PropertyResourceBundle options = null;
50 50
  private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
51
  private static boolean debug = false;
51
  private static boolean debug = true;
52 52

  
53 53
  private Hashtable connectionPool = new Hashtable();
54 54

  
......
383 383
    }
384 384
  }
385 385
  
386
  public static void debug(String msg)
387
  {
388
    if(debug)
389
    {
390
      System.err.println(msg);
391
    }
392
  }
393
  
386 394
  public static Vector getOptionList(String optiontext)
387 395
  {
388 396
    Vector options = new Vector();

Also available in: Unified diff