Browse Source

Added basic user grants checkouts

toomanysugar 4 years ago
parent
commit
45614f6cd2

BIN
out/production/JavaFxApplication/com/moviesdb/Controller.class


+ 43 - 10
src/com/moviesdb/Controller.java

@@ -67,6 +67,11 @@ public class Controller {
     @FXML
     MenuBar menuPane;
 
+    @FXML
+    Label deleteMovieButton;
+    @FXML
+    Label editMovieButton;
+
     @FXML
             TextField userTextBox;
     @FXML
@@ -136,20 +141,26 @@ public class Controller {
 
         workParams = new ConnectionParams("connection.properties");
         if (!workParams.initilized())
-            //TODO this is temporaly handler
+            //TODO this is temporal handler
             System.out.println(workParams.getInitilizeErrCause().toString());
         else {
             my_connector = new DataBaseConnector();
             my_connector.initConnection(workParams);
         }
 
+        System.out.println(my_connector.granted("USAGE"));
+        System.out.println(my_connector.granted("INSERT"));
+        System.out.println(my_connector.granted("UPDATE"));
+        System.out.println(my_connector.granted("DELETE"));
+
+
     }
 
     public void loadPosters()
     {
         //detailsPane.setMinWidth(300);
         //get movies array
-        if (my_connector.initialized()) {
+        if (my_connector.initialized() & my_connector.granted("USAGE")) {
             movies = my_connector.getMovies();
             labelsMovies = new Label[movies.length];
 
@@ -235,6 +246,32 @@ public class Controller {
             //moviesGrid.setPrefWidth(browserPane.getWidth());
             //moviesGrid.setPrefHeight(browserPane.getHeight());
             //loadPosters();
+            if (my_connector.granted("DELETE")) {
+                deleteMovieButton.setDisable(false);
+                deleteMovieButton.setOpacity(1);
+                deleteMovieButton.setOnMouseClicked(ed -> {
+                    System.out.println("delete");
+                });
+
+            } else {
+                deleteMovieButton.setDisable(true);
+                deleteMovieButton.setOpacity(0);
+                deleteMovieButton.setOnMouseClicked(null);
+            }
+
+            if (my_connector.granted("UPDATE")) {
+                editMovieButton.setDisable(false);
+                editMovieButton.setOpacity(1);
+                editMovieButton.setOnMouseClicked(ed -> {
+                    System.out.println("edit");
+                });
+
+            } else {
+                editMovieButton.setDisable(true);
+                editMovieButton.setOpacity(0);
+                editMovieButton.setOnMouseClicked(null);
+            }
+
 
             gridResize();
             //button.setText(Double.toString(browserPane.getWidth()));
@@ -368,12 +405,15 @@ public class Controller {
         controller = loader.getController();
          //controller.initialize(parent);
         if(controller.newArgLogin(userTextBox.getText(), passTextBox.getText())) {
+            controller.loadPosters();
+            controller.closeDetails();
             stage.close();
         }
         else
         {
             Text errorMessage = new Text("Incorrect login or password!");
             errorMessage.setStyle("-fx-font-size: 14; -fx-fill: #FF0000;");
+            errorTextBox.getChildren().clear();
             errorTextBox.getChildren().add(errorMessage);
         }
         //System.out.println("LoginForm closed");
@@ -399,17 +439,10 @@ public class Controller {
             setUserName(user);
             return my_connector.initialized();
         }
-        else
-        {
-            ConnectionParams defaultParams  = new ConnectionParams("connection.properties");
-            setUserName("Guest");
-            my_connector.initConnection(defaultParams);
-        }
         return false;
     }
 
-    public void setUserName(String name)
-    {
+    public void setUserName(String name){
         userLabel.setText("Logged in as " + name);
     }
 }

+ 22 - 7
src/com/moviesdb/DataBaseConnector.java

@@ -1,14 +1,14 @@
 package com.moviesdb;
 
-import java.net.ConnectException;
 import java.sql.*;
-import com.moviesdb.Movie;
-import com.moviesdb.ConnectionParams;
+import java.util.ArrayList;
+import java.util.List;
 
 public class DataBaseConnector {
 
     private Connection conn = null;
-    private Boolean initilizeErr;
+    private List<String> connRights;
+    private Boolean initializeErr;
 
     public void initConnection(ConnectionParams connectionParams) {
 
@@ -21,12 +21,22 @@ public class DataBaseConnector {
             DriverManager.setLoginTimeout(2);
             conn = DriverManager.getConnection(url, connectionParams.user(), connectionParams.password());
             if (conn != null) {
-                initilizeErr = false;
+                initializeErr = false;
             }
             System.out.println("Successful");
+
+            Statement statement = conn.createStatement();
+            String req = "SELECT * FROM information_schema.user_privileges WHERE grantee LIKE \"'" + connectionParams.user() + "'%\";";
+            ResultSet rs = statement.executeQuery(req);
+            connRights= new ArrayList<String>();
+            while (rs.next()) {
+                connRights.add(rs.getString("PRIVILEGE_TYPE"));
+            }
+            System.out.println("Connection grants:\n\t" + connRights);
+
         } catch (SQLException ex) {
             //exception
-            initilizeErr = true;
+            initializeErr = true;
             if (ex.getMessage().contains("Communications link failure")) {
                 System.out.println(ex.getMessage());
             }
@@ -64,7 +74,7 @@ public class DataBaseConnector {
     }
 
     public boolean initialized(){
-        return !initilizeErr;
+        return !initializeErr;
     }
 
 
@@ -207,4 +217,9 @@ public class DataBaseConnector {
             ex.printStackTrace();
         }
     }
+
+    public boolean granted(String grantName){
+        return connRights.contains(grantName);
+    }
+
 }

+ 12 - 2
src/com/moviesdb/test2.fxml

@@ -133,9 +133,9 @@
                   <Label layoutX="822.0" layoutY="19.0" text="Button" textFill="#9f9f9f" AnchorPane.rightAnchor="22.0" AnchorPane.topAnchor="19.0" />
                </children>
             </AnchorPane>
-        <AnchorPane fx:id="detailsPane" maxWidth="0.0" minWidth="0.0" prefWidth="0.0" style="-fx-background-color: #212121;" SplitPane.resizableWithParent="false">
+        <AnchorPane fx:id="detailsPane" maxWidth="0.0" minWidth="0.0" prefHeight="550.0" prefWidth="318.0" style="-fx-background-color: #212121;" SplitPane.resizableWithParent="false">
           <children>
-            <Label alignment="CENTER" font="$x1" layoutX="14.0" layoutY="14.0" style="&#10;" text="Details" textAlignment="CENTER" textFill="$x2" wrapText="false" />
+            <Label alignment="CENTER" font="$x1" layoutX="14.0" layoutY="14.0" style="&#10;" text="Details" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false" />
                   <AnchorPane fx:id="detailsPicPane" layoutX="14.0" layoutY="41.0" prefHeight="222.0" AnchorPane.bottomAnchor="287.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="41.0" />
                   <Label fx:id="detailsNameLabel" layoutX="14.0" layoutY="265.0" text="Info" textFill="#9f9f9f">
                      <font>
@@ -143,6 +143,16 @@
                      </font>
                   </Label>
                   <TextFlow fx:id="detailsContent" layoutX="14.0" layoutY="287.0" prefHeight="255.0" prefWidth="277.0" />
+                  <Label fx:id="deleteMovieButton" layoutX="-14.0" layoutY="528.0" text="Delete" textFill="#9f9f9f" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0">
+                     <font>
+                        <Font size="16.0" />
+                     </font>
+                  </Label>
+                  <Label fx:id="editMovieButton" layoutX="212.0" layoutY="534.0" text="Edit" textFill="#9f9f9f" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="71.0">
+                     <font>
+                        <Font size="16.0" />
+                     </font>
+                  </Label>
 
 
           </children>