Project

General

Profile

« Previous | Next » 

Revision 8487

Added by Jing Tao about 11 years ago

Add methods to handle modify user's attributes.

View differences:

src/edu/ucsb/nceas/metacat/authentication/AuthFile.java
33 33

  
34 34

  
35 35

  
36

  
37

  
38

  
36 39
import org.apache.commons.configuration.ConfigurationException;
37 40
import org.apache.commons.configuration.XMLConfiguration;
38 41
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
......
602 605
            } else if (argus[1] != null && argus[1].equals(USERADD)) {
603 606
                handleUserAdd(authFile,argus);
604 607
            } else if (argus[1] != null && argus[1].equals(USERMOD)) {
605
                
608
                handleUserMod(authFile, argus);
606 609
            } else if (argus[1] != null && argus[1].equals(USAGE)) {
607 610
                printUsage();
608 611
            } else {
......
639 642
        } 
640 643
        String groupName = null;
641 644
        String description = null;
642
        if(map.keySet().size() ==1 || map.keySet().size() ==2) {
645
        if(map.keySet().size() == 0) {
646
            System.out.println("Error: the "+DASHG+" group-name is required in the groupadd command line.");
647
            System.exit(1);
648
        }
649
        else if(map.keySet().size() ==1 || map.keySet().size() ==2) {
643 650
            groupName = map.get(DASHG);
644 651
            if(groupName == null) {
645 652
                System.out.println("Error: the "+DASHG+" group-name is required in the groupadd command line.");
653
                System.exit(1);
646 654
            }
647 655
            description = map.get(DASHD);
648 656
            authFile.addGroup(groupName, description);
......
759 767
        System.out.println("Successfully added a user "+dn+" to the file authentication system ");
760 768
    }
761 769
    
770
    /*
771
     * Handle modify a user's password or group information.
772
     */
773
    private static void handleUserMod(AuthFile authFile, String[] argus) throws AuthenticationException, UnsupportedEncodingException {
774
        String PASSWORD = "-password";
775
        String GROUP = "-group";
776
        if(argus.length < 3) {
777
            System.out.println("Error: the sub action \"-password\" or \"-group\" should follow the action \"usermod\"");
778
            System.exit(1);
779
        } else {
780
            if(argus[2] != null && argus[2].equals(PASSWORD)) {
781
                handleModifyPass(authFile, argus);
782
            } else if (argus[2] != null && argus[2].equals(GROUP)) {
783
                handleModifyGroup(authFile, argus);
784
            } else {
785
                System.out.println("Error: the sub action \""+argus[2]+"\" is unkown in the action \"usermod\"");
786
                System.exit(1);
787
            }
788
        }
789
    }
762 790
    
763 791
    /*
792
     * Handle the action to modify the password of a user
793
     */
794
    private static void handleModifyPass(AuthFile authFile, String[] argus) throws UnsupportedEncodingException, AuthenticationException {
795
        String DN = "-dn";
796
        String I= "-i";
797
        String H = "-h";
798
        Vector<String> possibleOptions = new <String>Vector();
799
        possibleOptions.add(I);
800
        possibleOptions.add(H);
801
        possibleOptions.add(DN);
802
        boolean inputPassword = false;
803
        boolean passingHashedPassword = false;
804
        boolean hasDN = false;
805
        HashMap<String, String> map = new <String, String>HashMap();
806
        for(int i=3; i<argus.length; i++) {
807
            String arg = argus[i];
808
            if(map.containsKey(arg)) {
809
                System.out.println("Error: the command line for usermod -password can't have the duplicated options "+arg+".");
810
                System.exit(1);
811
            }
812
            
813
            //this is the scenario that "-i" is at the end of the arguments.
814
            if(arg.equals(I) && i==argus.length-1) {
815
                map.put(I, I);//we need to input password.
816
                inputPassword = true;
817
            } 
818
            
819
            if(possibleOptions.contains(arg) && i<argus.length-1) {
820
                //System.out.println("find the option "+arg);
821
                if(arg.equals(I)) {
822
                    //this is the scenario that "-i" is NOT at the end of the arguments.
823
                    if(!possibleOptions.contains(argus[i+1])) {
824
                        System.out.println("Error: The option \"-i\" means the user will input a password in the usermod -password command. So it can't be followed by a value. It only can be followed by another option.");
825
                        System.exit(1);
826
                    }
827
                    map.put(I, I);//we need to input password.
828
                    inputPassword = true;
829
                } else {
830
                    if(arg.equals(H)) {
831
                        passingHashedPassword = true;
832
                    } else if (arg.equals(DN)) {
833
                        hasDN = true;
834
                    }
835
                    map.put(arg, argus[i+1]);
836
                }
837
                
838
            } else if(!possibleOptions.contains(arg)) {
839
                //check if the previous argument is an option
840
                if(!possibleOptions.contains(argus[i-1])) {
841
                    System.out.println("Error: an illegal argument "+arg+" in the usermod -password command ");
842
                    System.exit(1);
843
                }
844
            }
845
        } 
846
        
847
        String dn = null;
848
        String plainPassword = null;
849
        String hashedPassword = null;
850
        if(!hasDN) {
851
            System.out.println("The \"-dn user-distinguish-name\" is requried in the usermod -password command ."); 
852
            System.exit(1);
853
        } else {
854
            dn = map.get(DN);
855
        }
856

  
857
        if(inputPassword && passingHashedPassword) {
858
            System.out.println("Error: you can choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the usermod -password command.");
859
            System.exit(1);
860
        } else if (!inputPassword && !passingHashedPassword) {
861
            System.out.println("Error: you must choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the usermod -password command.");
862
            System.exit(1);
863
        } else if(inputPassword) {
864
            plainPassword = inputPassword();
865
            authFile.modifyPassWithPlain(dn, plainPassword);
866
            System.out.println("Successfully modified the password for the user "+dn);
867
            //System.out.println("============the plain password is "+plainPassword);
868
        } else if(passingHashedPassword) {
869
            hashedPassword = map.get(H);
870
            authFile.modifyPassWithHash(dn, hashedPassword);
871
            System.out.println("Successfully modified the password for the user "+dn);
872
        }
873
    }
874
    
875
    /*
876
     * Handle the action adding/removing a user to/from a group
877
     */
878
    private static void handleModifyGroup(AuthFile authFile, String[] argus) throws AuthenticationException {
879
        String DN = "-dn";
880
        String A= "-a";
881
        String R = "-r";
882
        String G = "-g";
883
        Vector<String> possibleOptions = new <String>Vector();
884
        possibleOptions.add(DN);
885
        possibleOptions.add(A);
886
        possibleOptions.add(R);
887
        possibleOptions.add(G);
888
        HashMap<String, String> map = new <String, String>HashMap();
889
        for(int i=3; i<argus.length; i++) {
890
            String arg = argus[i];
891
            if(map.containsKey(arg)) {
892
                System.out.println("Error: the command line for the usermod -group can't have the duplicated options "+arg+".");
893
                System.exit(1);
894
            }
895
            
896
            //this is the scenario that "-a" or "-r" is at the end of the arguments.
897
            if((arg.equals(A) || arg.equals(R)) && i==argus.length-1) {
898
                map.put(arg, arg);//we need to input password.
899
            } 
900
            
901
            if(possibleOptions.contains(arg) && i<argus.length-1) {
902
                //System.out.println("find the option "+arg);
903
                if(arg.equals(A) || arg.equals(R)) {
904
                    //this is the scenario that "-a" or "-r" is NOT at the end of the arguments.
905
                    if(!possibleOptions.contains(argus[i+1])) {
906
                        System.out.println("Error: The option \"-i\" means the user will input a password in the usermod -group command. So it can't be followed by a value. It only can be followed by another option.");
907
                        System.exit(1);
908
                    }
909
                    map.put(arg, arg);
910
                    
911
                } else {
912
                    map.put(arg, argus[i+1]);
913
                }
914
                
915
            } else if(!possibleOptions.contains(arg)) {
916
                //check if the previous argument is an option
917
                if(!possibleOptions.contains(argus[i-1])) {
918
                    System.out.println("Error: an illegal argument "+arg+" in the usermod -group command ");
919
                    System.exit(1);
920
                }
921
            }
922
        }
923
        
924
        String add = map.get(A);
925
        String remove = map.get(R);
926
        String group = map.get(G);
927
        String dn = map.get(DN);
928
        if(dn == null || dn.trim().equals("")) {
929
            System.out.println("Erorr: the \"-dn user-distinguish-name\" is required in the usermod -group command");
930
            System.exit(1);
931
        }
932
        
933
        if(group == null || group.trim().equals("")) {
934
            System.out.println("Erorr: the \"-g group-name\" is required in the usermod -group command");
935
            System.exit(1);
936
        }
937
        
938
        if(add != null && remove!= null) {
939
            System.out.println("Erorr: You can only choose either \"-a\"(add the user to the group or \"-d\"(remove the user from the group in the usermod -group command");
940
            System.exit(1);
941
        } else if (add == null && remove == null) {
942
            System.out.println("Erorr: You must only choose either \"-a\"(add the user to the group or \"-d\"(remove the user from the group in the usermod -group command");
943
            System.exit(1);
944
        } else if (remove != null) {
945
            authFile.removeUserFromGroup(dn, group);
946
            System.out.println("Successfully removed the user "+dn+" from the group "+group);
947
        } else {
948
            authFile.addUserToGroup(dn, group);
949
            System.out.println("Successfully added the user "+dn+" to the group "+group);
950
        }
951
    }
952
    
953
    /*
764 954
     * Input the password
765 955
     */
766 956
    private static String inputPassword() throws UnsupportedEncodingException {
......
827 1017
     */
828 1018
    private static void printError(String[] argus) {
829 1019
        if(argus != null) {
830
            System.out.println("Error: it is an illegal command: ");
1020
            System.out.println("Error: it is an illegal command (probably with some illegal options): ");
831 1021
            for(int i=0; i<argus.length; i++) {
832 1022
                if(i!= 0) {
833 1023
                    System.out.print(argus[i]+" ");

Also available in: Unified diff