Project

General

Profile

« Previous | Next » 

Revision 8481

Added by Jing Tao almost 11 years ago

Add a main method which will be used in the command line users management tool.

View differences:

src/edu/ucsb/nceas/metacat/authentication/AuthFile.java
138 138
        try {
139 139
            init();
140 140
        } catch (Exception e) {
141
            e.printStackTrace();
141 142
            throw new AuthenticationException(e.getMessage());
142 143
        }
143 144
        
......
160 161
        
161 162
        //if the password file doesn't exist, create a new one and set the initial content
162 163
        if(!passwordFile.exists()) {
164
            File parent = passwordFile.getParentFile();
165
            if(!parent.exists()) {
166
                parent.mkdirs();
167
            }
163 168
            passwordFile.createNewFile();
164 169
            OutputStreamWriter writer = null;
165 170
            FileOutputStream output = null;
......
580 585
      return hashClass.hash(plain);
581 586
    }
582 587
    
588
    
589
    /**
590
     * A method is used to help administrator to manage users and groups
591
     * @param argus
592
     * @throws Exception
593
     */
594
    public static void main(String[] argus) throws Exception {
595
            String USERADD = "useradd";
596
            String USERMOD = "usermod";
597
            String GROUPADD = "groupadd";
598
            String USAGE = "usage";
599
            if(argus == null || argus.length ==0) {
600
              System.out.println("Please make sure that there are two arguments - \"$BASE_WEB_INF\" and\" $@\" after the class name edu.ucsb.nceas.metacat.authentication.AuthFile in the script file.");
601
              System.exit(1);
602
            } else if(argus.length ==1) {
603
                printUsage();
604
                System.exit(1);
605
            }
606
            PropertyService.getInstance(argus[0]);
607
            AuthFile authFile = AuthFile.getInstance();
608
            if(argus[1] != null && argus[1].equals(GROUPADD)) {
609
                handleGroupAdd(authFile,argus);
610
            } else if (argus[1] != null && argus[1].equals(USERADD)) {
611
                handleUserAdd(authFile,argus);
612
            } else if (argus[1] != null && argus[1].equals(USERMOD)) {
613
                
614
            } else if (argus[1] != null && argus[1].equals(USAGE)) {
615
                printUsage();
616
            } else {
617
                System.out.print("The unknown action "+argus[1]);
618
            }
619
    }
620
    
621
    /*
622
     * Handle the groupAdd action in the main method
623
     */
624
    private static void handleGroupAdd(AuthFile authFile, String[]argus) throws AuthenticationException {
625
        HashMap<String, String> map = new <String, String>HashMap();
626
        String DASHG = "-g";
627
        String DASHD = "-d";
628
        for(int i=2; i<argus.length; i++) {
629
            String arg = argus[i];
630
            
631
            if(map.containsKey(arg)) {
632
                System.out.println("The command line for groupadd can't have the duplicated options "+arg+".");
633
                System.exit(1);
634
            }
635
            
636
            if(arg.equals(DASHG) && i<argus.length-1) {
637
                map.put(arg, argus[i+1]);
638
            } else if (arg.equals(DASHD) && i<argus.length-1) {
639
                map.put(arg, argus[i+1]);
640
            } else if(!arg.equals(DASHG) && !arg.equals(DASHD)) {
641
                //check if the previous argument is -g or -d
642
                if(!argus[i-1].equals(DASHG) || !argus[i-1].equals(DASHD)) {
643
                    System.out.println("An illegal argument "+arg+" in the groupadd command ");
644
                }
645
            }
646
        } 
647
        String groupName = null;
648
        String description = null;
649
        if(map.keySet().size() ==1 || map.keySet().size() ==2) {
650
            groupName = map.get(DASHG);
651
            if(groupName == null) {
652
                System.out.println("The "+DASHG+" group-name is required in the groupadd command line.");
653
            }
654
            description = map.get(DASHD);
655
            authFile.addGroup(groupName, description);
656
            System.out.println("Successfully add a group "+groupName+" to the file authentication system");
657
        } else {
658
            printError(argus);
659
            System.exit(1);
660
        }
661
    }
662
    
663
    /*
664
     * Handle the userAdd action in the main method
665
     */
666
    private static void  handleUserAdd(AuthFile authFile,String[]argus) {
667
        String I = "-i";
668
        String H = "-h";
669
        String DN = "-dn";
670
        String G = "-g";
671
        String E = "-e";
672
        String S = "-s";
673
        String F = "-f";
674
        String O= "-o";
675
        HashMap<String, String> map = new <String, String>HashMap();
676
        
677
    }
678
    
679
    /*
680
     * Print out the usage statement
681
     */
682
    private static void printUsage() {
683
        System.out.println("Usage:\n"+
684
                        "./authFileManager.sh useradd -i -dn user-distinguish-name -g groupname -e email-address -s surname -f given-name -o organizationName\n" +
685
                        "./authFileManager.sh useradd -h hashed-password -dn user-distinguish-name -g groupname -e email-address -s surname -f given-name -o organizationName\n"+
686
                        "./authFileManager.sh groupadd -g group-name -d description\n" +
687
                        "./authFileManager.sh usermod -password -dn user-distinguish-name -i\n"+
688
                        "./authFileManager.sh usermod -password -dn user-distinguish-name -h new-hashed-password\n"+
689
                        "./authFileManager.sh usermod -group -a -dn user-disinguish-name -g added-group-name\n" +
690
                        "./authFileManager.sh usermod -group -r -dn user-distinguish-name -g removed-group-name\n"+
691
                        "Note:\n1. if a value of an option has spaces, the value should be enclosed by the double quotes.\n"+
692
                        "  For example: ./authFileManager.sh groupadd -g nceas-dev -d \"Developers at NCEAS\"\n"+
693
                        "2. \"-d description\" in groupadd is optional; \"-g groupname -e email-address -s surname -f given-name -o organizationName\" in useradd are optional as well.");
694
                       
695
                        
696
    }
697
    
698
    /*
699
     * Print out the statement to say it is a illegal command
700
     */
701
    private static void printError(String[] argus) {
702
        if(argus != null) {
703
            System.out.println("It is an illegal command: ");
704
            for(int i=0; i<argus.length; i++) {
705
                if(i!= 0) {
706
                    System.out.print(argus[i]+" ");
707
                }
708
            }
709
            System.out.println("");
710
        }
711
       
712
    }
583 713

  
584 714
    
585 715
    /**

Also available in: Unified diff