diff --git a/G-Earth/src/main/java/gearth/Main.java b/G-Earth/src/main/java/gearth/Main.java index 5f72843..a07c8f2 100644 --- a/G-Earth/src/main/java/gearth/Main.java +++ b/G-Earth/src/main/java/gearth/Main.java @@ -1,10 +1,14 @@ package gearth; +import gearth.misc.AdminValidator; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; +import javafx.scene.layout.Region; import javafx.stage.Stage; import gearth.ui.GEarthController; @@ -35,6 +39,11 @@ public class Main extends Application { System.exit(0); }); + if (!AdminValidator.isAdmin()) { + Alert alert = new Alert(Alert.AlertType.ERROR, "G-Earth needs admin privileges in order to work properly, please restart G-Earth unless you know what you're doing", ButtonType.OK); + alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE); + alert.show(); + } } public static String[] args; diff --git a/G-Earth/src/main/java/gearth/misc/AdminValidator.java b/G-Earth/src/main/java/gearth/misc/AdminValidator.java new file mode 100644 index 0000000..d2d37a9 --- /dev/null +++ b/G-Earth/src/main/java/gearth/misc/AdminValidator.java @@ -0,0 +1,37 @@ +package gearth.misc; + +import java.io.PrintStream; +import java.util.prefs.Preferences; + +/** + * Created by Jeunez on 5/11/2018. + */ +public class AdminValidator { + + //https://stackoverflow.com/questions/4350356/detect-if-java-application-was-run-as-a-windows-admin + + private static Boolean isAdmin = null; + + public static boolean isAdmin() { + if (isAdmin == null) { + Preferences prefs = Preferences.systemRoot(); + PrintStream systemErr = System.err; + synchronized(systemErr){ // better synchroize to avoid problems with other threads that access System.err + System.setErr(null); + try{ + prefs.put("foo", "bar"); // SecurityException on Windows + prefs.remove("foo"); + prefs.flush(); // BackingStoreException on Linux + isAdmin = true; + }catch(Exception e){ + isAdmin = false; + }finally{ + System.setErr(systemErr); + } + } + } + + return isAdmin; + } + +}