Changeset 35332


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

there were 2 almost identical methods findUser(un, pw) and findUser(un) - made the latter call the former to remove duplicate code. Some tidying up to modifyUserInfo - allow null groups, in which case don't modify them. separate out comment and accountstatus - may want to change one and not the other.

File:
1 edited

Legend:

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

    r35298 r35332  
    571571    }
    572572
     573  // single arg method for convenience
     574  public UserQueryResult findUser(String username) {
     575    return findUser(username, null);
     576  }
    573577    public UserQueryResult findUser(String username, String password)
    574578    {
     
    619623            {
    620624                ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
    621 
     625                                conn.commit();
     626                               
    622627                String group = "";
    623628                while (gs.next())
     
    650655    }
    651656
    652     // findUser(null) will return all users, which is why a UserQueryResult
    653     // (a vector of UserTermInfo) is returned
    654     public UserQueryResult findUser(String username)
    655     {
    656         UserQueryResult userQueryResult = new UserQueryResult();
    657 
    658         String sql_find_user = "SELECT  username, password, accountstatus, comment, email FROM " + USERS;
    659         String append_sql = "";
    660 
    661         if (username != null)
    662         {
    663             append_sql = " WHERE username = '" + username + "'";
    664         }
    665         if (!append_sql.equals(""))
    666         {
    667             sql_find_user += append_sql;
    668         }
    669 
    670         ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
    671 
    672         try
    673         {
    674             Statement state = conn.createStatement();
    675             ResultSet rs = state.executeQuery(sql_find_user);
    676             conn.commit();
    677             while (rs.next())
    678             {
    679                 HashMap<String, String> user = new HashMap<String, String>();
    680                 user.put("username", rs.getString("username"));
    681                 user.put("password", rs.getString("password"));
    682                 user.put("as", rs.getString("accountstatus"));
    683                 user.put("comment", rs.getString("comment"));
    684                 user.put("email", rs.getString("email"));
    685 
    686                 users.add(user);
    687             }
    688             state.close();
    689 
    690             state = conn.createStatement();
    691             for (HashMap<String, String> user : users)
    692             {
    693                 ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
    694                 conn.commit();
    695 
    696                 String group = "";
    697                 while (gs.next())
    698                 {
    699                     if (!group.equals(""))
    700                     {
    701                         group += ",";
    702                     }
    703                     group += gs.getString("role");
    704                 }
    705 
    706                 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
    707             }
    708             state.close();
    709         }
    710         catch (Exception ex)
    711         {
    712             ex.printStackTrace();
    713         }
    714 
    715         if (userQueryResult.getSize() > 0)
    716         {
    717             return userQueryResult;
    718         }
    719         else
    720         {
    721             System.out.println("couldn't find the user");
    722             return null;
    723         }
    724     }
    725 
    726657    public String modifyUserInfo(String username, String new_password, String expandedGroups, String accountstatus, String comment, String email)
    727658    {
     
    737668            }
    738669
    739             if (accountstatus != null && comment != null)
    740             {
    741                 sql_modify_user_info += (needComma ? "," : "") + " accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'";
     670            if (accountstatus != null) {
     671                          sql_modify_user_info += (needComma ? "," : "") + " accountstatus='" + accountstatus + "'";
     672                          needComma = true;
     673                        }
     674                        if (comment != null)
     675            {
     676                          sql_modify_user_info += (needComma ? "," : "") + "  comment='" + comment + "'";
    742677                needComma = true;
    743678            }
     
    746681            {
    747682                sql_modify_user_info += (needComma ? "," : "") + " email='" + email + "'";
    748             }
    749 
    750             sql_modify_user_info += " where username='" + username + "'";
    751             Statement state = conn.createStatement();
    752             state.execute(sql_modify_user_info);
    753 
    754             String sql_delete_groups = "delete from " + ROLES + " where username='" + username + "'";
    755             state.execute(sql_delete_groups);
    756 
    757             String[] groupsArray = expandedGroups.split(",");
    758             for (String g : groupsArray)
    759             {
    760                 String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
    761                 state.execute(sql_insert_group);
    762             }
    763 
    764             conn.commit();
    765             state.close();
     683                                needComma = true;
     684            }
     685                        Statement state = conn.createStatement();
     686                        if (needComma) {
     687                          // it is possible that we are only modifying groups, so use needComma to
     688                          // see if we actually need to run this step
     689                          sql_modify_user_info += " where username='" + username + "'";
     690                          //System.err.println("about to execute sql: "+sql_modify_user_info);
     691                         
     692                          state.execute(sql_modify_user_info);
     693                        }
     694                        if (expandedGroups != null) {
     695                          // delete the groups we have currently
     696                         
     697                          String sql_delete_groups = "delete from " + ROLES + " where username='" + username + "'";
     698                          state.execute(sql_delete_groups);
     699
     700                          // add the new groups
     701                          String[] groupsArray = expandedGroups.split(",");
     702                          for (String g : groupsArray)
     703                          {
     704                            String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
     705                            state.execute(sql_insert_group);
     706                          }
     707                         
     708                          conn.commit();
     709                          state.close();
     710                        }
    766711        }
    767712        catch (Throwable e)
Note: See TracChangeset for help on using the changeset viewer.