Пример #1
0
  /**
   * Overwrite an <strong>existing</strong> user data with some new data
   *
   * @param UID The ID of the user that is being updating
   * @param name The new name for this user, or <em>null</em> to leave it unchanged
   * @param passwd the new password for this user, or <em>null</em> to leave it unchanged
   * @param level The new access level for this user, or <em>null</em> to leave it unchanged
   * @return <em>True</em> if everything was fine, or <em>False</em> if that UID doesn't point to
   *     any user,
   * @throws NameExistsException If this name already belongs to another user,
   * @throws SQLException If there are some thing wrong with the database
   */
  public static boolean overrideUser(int UID, String name, String passwd, AccessLevel level)
      throws NameExistsException, SQLException {
    synchronized (userPool) {
      if (userPool.containsKey(UID)) {
        User target = userPool.get(UID);
        target.username = name;
        target.accessLevel = level;
      }
    }

    synchronized (userByName) { // override operation changes the name
      userByName.setString(1, name);
      if (userByName.executeQuery().first())
        throw new NameExistsException(); // A user with same name already exists

      String sql = "UPDATE USER SET ";
      if (name != null) sql += String.format("Username = '******', ", name);
      if (passwd != null) sql += String.format("Passwd = '%s', ", passwd);
      if (level != null) sql += String.format("AccessLevel = %d, ", level.value);
      sql = sql.substring(0, sql.length() - 2) + String.format(" WHERE UID = %d", UID);

      if (getGP().getConn().createStatement().executeUpdate(sql) != 1) return false;
      // TODO: invalidate all sessions that is login as this user
      return true;
    }
  }
Пример #2
0
 public static void clear() throws SQLException {
   for (User u : userPool.values()) {
     u.save();
     userPool.remove(u);
   }
   userPool = null;
   userByID.close();
   userByName.close();
   newUser.close();
 }