简体   繁体   中英

Refresh MySQL connection object when privileges are changed for a user

I have a connection object with username:'user' to a DB in MySql , now I have changed some user privilege for that 'user' explicitly but it is not taking effect to the existing connection objects. This privilege change is only applicable to a new connection object.

For example: I have a connection object(conn) with all privileges assigned and now I have changed say( write/read ) privilege for the same connection object(conn) but that object(conn) is still able to do select/insert operations.

Is there a way to refresh the connection object automatically when any privileges are changed for a user.

I found this class for you,it might help https://www.codota.com/code/java/methods/android.database.DataSetObservable/notifyChanged But on your place I'd don't save any info in object except token maybe. You can try to create tables where will be information about users and check this info in that moment when you need to run some method only for privileged users. In my old project I create this tables:

Table users

Table access rights

And create methods which check access_right to a functions such as panel of administrator

Example of function:

public class Blog extends Initialization {
   private boolean stop;
   private Statement st;
   private ResultSet rs;
   private String login;
   private BufferedReader reader;
   private BufferedWriter writer;

   Blog(Statement st, String login, BufferedReader reader, BufferedWriter writer) {
    this.st = st;
    this.login = login;
    this.reader = reader;
    this.writer = writer;
}
private void checkRightsToCallAdministrationPanel() {
    try {
        String access_rightsString = "";

        rs = st.executeQuery("SELECT access_id FROM users WHERE login='" + login + "';");
        while (rs.next()) {
            access_rightsString = rs.getString(1);
        }

        if (Integer.parseInt(access_rightsString) < 3) {
            callAdministrationPanel();
        } else {
            writer.write("No such variant\n");
            writer.flush();
        }
    } catch (IOException | SQLException e) {
        e.printStackTrace();
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM