Changeset 35333 for main


Ignore:
Timestamp:
2021-09-06T16:48:05+12:00 (3 years ago)
Author:
kjdon
Message:

a few mods to this. Allow username of ALL - will do the required mods to all users. new option -addGroups - this will add a group/groups to the specified user's group list. Use with username ALL to put everyine into a default group. Now also allows empty strings for all fields except password.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/ModifyUsersDB.java

    r35298 r35333  
    2020
    2121import java.sql.SQLException;
     22import java.util.Iterator;
    2223import org.greenstone.gsdl3.service.Authentication;
    2324//import org.greenstone.admin.guiext.PropertiesStep;
     
    2728    then run:
    2829
    29     java -Dgsdl3.writablehome=/Scratch/ak19/gs3-svn-2Sep2015/web -cp web/WEB-INF/lib/gsdl3.jar:web/WEB-INF/lib/gutil.jar:web/WEB-INF/lib/derby.jar:web/WEB-INF/lib/derbyclient.jar:web/WEB-INF/lib/log4j-1.2.8.jar:web/WEB-INF/lib/commons-codec-1.7.jar:web/WEB-INF/classes org.greenstone.gsdl3.util.ModifyUsersDB web/etc/usersDB/ <username> [options specifying user fields to change, e.g.: password=me!]
     30    java -Dgsdl3.writablehome=/Scratch/ak19/gs3-svn-2Sep2015/web -cp web/WEB-INF/lib/gsdl3.jar:web/WEB-INF/lib/gutil.jar:web/WEB-INF/lib/derby.jar:web/WEB-INF/lib/derbyclient.jar:web/WEB-INF/lib/log4j-1.2.8.jar:web/WEB-INF/lib/commons-codec-1.7.jar:web/WEB-INF/classes org.greenstone.gsdl3.util.ModifyUsersDB web/etc/usersDB/ <username or 'ALL'> [options specifying user fields to change, e.g.: password=me!]
    3031   
    3132    Don't forget to stop the networked derby server again at the end, if you had started it: ant stop-derby
    32 
     33   [[ OLD METHOD ]]
    3334    or if using embedded derby, ensure that tomcat is stopped, then run:
    3435    java -cp /full/path/to/GS3/web/WEB-INF/lib/gsdl3.jar:/full/path/to/GS3/web/WEB-INF/lib/derby.jar org.greenstone.gsdl3.util.ModifyUsersDB web/etc/usersDB/
     
    4445        if (args.length < 3)
    4546        { // at minimum one field belonging to a username has to be changed
    46             System.out.println("Usage: java org.greenstone.gsdl3.ModifyUsersDB <full_path_of_the_usersDB> <username> [-noAdd] [password=pwd] [groups=grp] [accounstatus=status] [comment=cmt] [email=address]");
     47            System.out.println("Usage: java org.greenstone.gsdl3.ModifyUsersDB <full_path_of_the_usersDB> <username|ALL> [-noAdd] [password=pwd] [groups=grp] [addgroups=grp] [accounstatus=status] [comment=cmt] [email=address]");
    4748            System.exit(0);
    4849        }
     
    5152        String username = args[1];
    5253
    53         String password = "";
    54         String groups = "";
    55         String accountstatus = "true";
    56         String comment = "";
    57         String email = "";
     54        String password = null;
     55        String groups = null;
     56                String addgroups = null;
     57        String accountstatus = null;
     58        String comment = null;
     59        String email = null;
    5860
    5961        boolean noAdd = false;
     
    6971                if (password.length() < PWD_MIN_LENGTH || password.length() > PWD_MAX_LENGTH)
    7072                {
    71                     if (!password.equals(""))
    72                     {
    73                         System.out.println("Password not updated. It should be between " + PWD_MIN_LENGTH + " and " + PWD_MAX_LENGTH + " characters (inclusive).");
    74                     }
     73                                  System.out.println("Password not updated. It should be between " + PWD_MIN_LENGTH + " and " + PWD_MAX_LENGTH + " characters (inclusive).");
     74                                 
     75                                  password = null;
    7576                }
    7677                else
     
    8687            {
    8788                groups = args[i].substring("groups=".length());
    88             }
     89                                groups = UserTermInfo.expandGroups(groups);
     90            }
     91                        else if (args[i].startsWith("addgroups="))
     92                        {
     93                          addgroups = args[i].substring("addgroups=".length());
     94                          addgroups = UserTermInfo.expandGroups(addgroups);
     95                        }
    8996            else if (args[i].startsWith("accountstatus="))
    9097            {
     
    109116        }
    110117
     118                if (groups != null && addgroups != null) {
     119                  System.err.println("You can't use groups and addgroup at the same time");
     120                  System.exit(0);
     121                }
    111122        // find the user to modify
    112123        DerbyWrapper dw = new DerbyWrapper(usersDB);
    113         UserQueryResult findUserResult = dw.findUser(username);
    114 
    115         if (findUserResult == null)
    116         {
    117             if (noAdd)
    118             {
    119                 System.out.println("Failed to update user. Cannot find user " + username + " in " + usersDB + " database.");
    120             }
    121             else
    122             { // add new user
    123                 groups = UserTermInfo.expandGroups(groups);
    124                 //System.err.println("**** Trying to add user: ");
    125                 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
    126                 dw.addUser(username, password, groups, accountstatus, comment, email);
    127             }
     124                if (username.equals("ALL")) {
     125                  // modify all users
     126                  UserQueryResult findUserResult = dw.findUser(null, null);// this returns all users
     127                  Iterator<UserTermInfo> it = findUserResult.getUserTerms().iterator();
     128                  while (it.hasNext()) {
     129                    UserTermInfo uti = it.next();
     130                    String un = uti.getUsername();
     131                    modifyUser(dw, uti, un, password, groups, addgroups, accountstatus, comment, email);
     132                  }
     133                } else {                 
     134                  UserQueryResult findUserResult = dw.findUser(username, null);
     135
     136                  if (findUserResult == null)
     137                  {
     138                    if (noAdd)
     139                    {
     140                      System.out.println("Failed to update user. Cannot find user " + username + " in " + usersDB + " database.");
     141                    }
     142                    else
     143                    { // add new user
     144
     145                      //System.err.println("**** Trying to add user: ");
     146                      //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
     147
     148                      if (password == null) {
     149                        System.err.println("cannot add a user without a password");
     150                       
     151                      } else {
     152                        if (groups == null) {
     153                          if (addgroups != null) {
     154                            groups = addgroups;
     155                          } else {
     156                            groups = "";
     157                          }
     158                        }
     159                        if (accountstatus == null) {
     160                          accountstatus = "true";
     161                        }
     162                        if (comment == null) {
     163                          comment = "";
     164                        }
     165                        if (email == null) {
     166                          email = "";
     167                        }
     168                        System.out.println("Adding new user: un=" + username + ", pw=" + password + ", groups=" + groups + ", status=" + accountstatus + ", comment=" + comment + ", email=" + email);
     169                        dw.addUser(username, password, groups, accountstatus, comment, email);
     170                      }
     171                    }
     172                  }
     173                  else
     174                  { // modify existing user data
     175                    UserTermInfo user = findUserResult.getUserTerms().get(0);
     176                    modifyUser(dw, user, username, password, groups, addgroups, accountstatus, comment, email);
     177                  }
     178       
    128179        }
    129         else
    130         { // modify existing user data
    131 
    132             // in case any of the other fields are not specified, get fallbacks from the database
    133             UserTermInfo user = findUserResult.getUserTerms().get(0);
    134 
    135             if (password.equals(""))
    136             {
    137                 password = user.getPassword(); // already stored hashed-and-hexed in DB
    138             }
    139             if (groups.equals(""))
    140             {
    141                 // groups should be expandedGroups because we no longer store the groups in userDB
    142                 // as user-entered or compacted, but as programmatically expanded.
    143                 // This allows HttpServletRequest.isUserInRole() to now automatically retrieve the
    144                 // expandedGroups list of a user to check collectionConfig.xml security elements against.
    145                
    146                 groups = user.getExpandedGroups();
    147             } else {
    148                 groups = UserTermInfo.expandGroups(groups); // ensure groups are stored expanded in userDB
    149             }
    150             if (accountstatus.equals(""))
    151             {
    152                 accountstatus = user.getAccountStatus().equals("") ? "true" : user.getAccountStatus();
    153             }
    154             if (comment.equals(""))
    155             {
    156                 comment = user.getComment();
    157             }
    158             if (email.equals(""))
    159             {
    160                 email = user.getEmail();
    161             }
    162 
    163             //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
    164             dw.modifyUserInfo(username, password, groups, accountstatus, comment, email); // other than username and pwd, remaining fields are allowed to be ""
    165         }
    166180
    167181        dw.closeDatabase();
    168182
    169183    }
     184
     185  public static void modifyUser(DerbyWrapper dw, UserTermInfo user, String username, String password, String groups, String addgroups, String accountstatus, String comment, String email) { 
     186                   
     187    if (groups == null && addgroups != null) {
     188      groups = user.getExpandedGroups(); // get the groups from db, as we want to add on to what is already there
     189    }
     190    if (addgroups != null) {
     191      if (!groups.equals("")) {
     192        groups += ",";
     193      }
     194      groups += addgroups;
     195    }
     196    System.out.println("Modifying existing user: un=" + username + ", pw=" + password + ", groups=" + groups + ", status=" + accountstatus + ", comment=" + comment + ", email=" + email);
     197    dw.modifyUserInfo(username, password, groups, accountstatus, comment, email);
     198  }
    170199}
     200
     201
Note: See TracChangeset for help on using the changeset viewer.