首页 > 解决方案 > 为我的应用创建管理员

问题描述

因此,如果用户在 db 中的列 admin 1 而不是 0 处加载不同的 fxml 文件,管理员可以在其中执行比普通用户更多的操作。

这是负责用户登录的控制器,测试密码和用户名是否在数据库中。

PS 更多关于我如何尝试在下面制作方法的评论。

public class LogareController implements Initializable {

LoginVerifier loginVerifier = new LoginVerifier();


   @FXML
   private TextField Numeutilzator;

   @FXML
   private PasswordField Parola;

   @FXML
   private Label Stare;






@Override
 public void initialize(URL location, ResourceBundle resources) {


  if (loginVerifier.Conexiune()) {
  Stare.setText("");
  } else {

  Stare.setText("Conexiune nereusita!");

  }

 }

 public void Autentificare (ActionEvent event) {
     try {
         if(loginVerifier.testaredate(Numeutilzator.getText(),         Parola.getText())) {
             Stare.setText("Autentificare reusita !");
             ((Node)event.getSource()).getScene().getWindow().hide();
                Stage st= new Stage();
            FXMLLoader loader= new FXMLLoader();
            Pane Pane =     loader.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());

            Scene scene = new Scene(Pane);
            scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            st.setScene(scene);
            st.show();

          }
     else { 
         Stare.setText("Nume de utilizator sau parola incorect");

     }




} catch (SQLException e) {





     e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
    }
         }
     @FXML
    public void Inregistrare(ActionEvent event) {
        try {
             ((Node)event.getSource()).getScene().getWindow().hide();
                    Stage PS= new Stage();
                FXMLLoader loader= new FXMLLoader();
                Pane Pane1 =     loader.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").openStream());
                Scene scena = new Scene(Pane1);
                scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            PS.setScene(scena);
            PS.show();
    } catch (Exception e) {

    }



    }
// This is what I came up with, I know its bad, but I can't think of anything else, in this method I am trying to save the value of "Admin" from the table in a variable, which I can you to determen if the user has 0 or 1 at the admin, If he has 1 then a new fxml will be loaded for him, if he has 0 he is a 
regular user

public boolean admin(int admin) throws SQLException {
      ConectaredB ConectaredB=new ConectaredB();
       Connection conectare=ConectaredB.logareDB();
      PreparedStatement PSMG= null;
     ResultSet RSMG = null;
      String Interogare = "SELECT Admin FROM accounts where Admin='1'";
      try {
          PSMG = conectare.prepareStatement(Interogare);
              PSMG.setLong(1, admin);


          LogareController Adminstatus = new LogareController();

    String Adminstatus = admin.getBytes() //IT only lets me to use getBytes(), i wanted to get the value from admin, after the query executed, this causes a confict with the primitive type "int".

        } catch (Exception exceptie2) {
        return true;

        }
    return false;
         }


}
    }

基本上,我如何创建一种方法,在该方法中我可以从 sql 中保存 admin 的值,然后在测试登录凭据以创建一个“if”条件时,该条件将确定应该加载什么 fxml。

登录验证器

public class LoginVerifier {



  public LoginVerifier () {


      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();



   if (conectare == null) {

   System.out.println("Conectare nereusita!");
    System.exit(1);}
  }

  public boolean Conexiune() {
      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();
   try {
  return !conectare.isClosed();
 } catch (SQLException e) {

  e.printStackTrace();
  return false;
 }

}
  public boolean testaredate(String numeutil, String parola) throws SQLException {
      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();
      PreparedStatement PSMG= null;
     ResultSet RSMG = null;
      String Interogare = "SELECT * FROM accounts where Username=? and Password=?";
      try {
          PSMG = conectare.prepareStatement(Interogare);
          PSMG.setString(1, numeutil);
          PSMG.setString(2, parola);

          RSMG = PSMG.executeQuery();
            if(RSMG.next()){
                return true;
            }
            else {
                return false;
            }

        } catch (Exception exceptie2) {
            return false;

        }
    }
  }

标签: javamysqlsqljavafx

解决方案


这是一个如何实现它的粗略示例。

User类是描述用户的模型。我刚刚将它们与其他 fx 组件兼容。

public class User {
    private IntegerProperty id = new SimpleIntegerProperty();
    private StringProperty name = new SimpleStringProperty();
    private StringProperty password = new SimpleStringProperty();
    private BooleanProperty admin = new SimpleBooleanProperty();

    public int getId() {
        return id.get();
    }

    public IntegerProperty idProperty() {
        return id;
    }

    public void setId(int id) {
        this.id.set(id);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getPassword() {
        return password.get();
    }

    public StringProperty passwordProperty() {
        return password;
    }

    public void setPassword(String password) {
        this.password.set(password);
    }

    public boolean isAdmin() {
        return admin.get();
    }

    public BooleanProperty adminProperty() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin.set(admin);
    }
}

User-class 对象的创建和初始化在LoginVerifier#login函数中完成

public class LoginVerifier {

    public Optional<User> login(String username, String password) throws SQLException {
        ConectaredB ConectaredB = new ConectaredB();
        Connection conectare = ConectaredB.logareDB();

        PreparedStatement PSMG = null;
        ResultSet RSMG = null;
        String Interogare = "SELECT * FROM accounts where Username = ? and Password = ?";

        PSMG = conectare.prepareStatement(Interogare);
        PSMG.setString(1, username);
        PSMG.setString(2, password);

        RSMG = PSMG.executeQuery();
        if(RSMG.next()) {
            User user = new User();
            user.setName(username);
            user.setPassword(password);
            user.setId(RSMG.getInt("id"));
            user.setAdmin(RSMG.getInt("Admin") == 1);

            return Optional.of(user);
        }

        return Optional.empty();
    }
}

通过检查是否Optional包含对象来完成身份验证,并通过检查User#isAdmin属性来进行授权

public void Autentificare(ActionEvent event) {
    try {
        Optional<User> optional = loginVerifier.login(Numeutilzator.getText(), Parola.getText());

        if(optional.isPresent()) {
            String fxmlFile = optional.get().isAdmin() ? "/LicentaApp/Meniu_Admin.fxml" : "/LicentaApp/Meniu.fxml";

            Stare.setText("Autentificare reusita !");
            ((Node)event.getSource()).getScene().getWindow().hide();

            Stage st = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane Pane = loader.load(getClass().getResource(fxmlFile).openStream());

            Scene scene = new Scene(Pane);
            scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            st.setScene(scene);
            st.show();
        }
        else {
            Stare.setText("Nume de utilizator sau parola incorect");
        }
    }
    catch (SQLException | IOException e) {
        e.printStackTrace();
    }
}

推荐阅读