check admin privileges

This commit is contained in:
sirjonasxx 2018-11-05 20:10:47 +01:00
parent aebcb5c6b2
commit fc227923cb
2 changed files with 46 additions and 0 deletions

View File

@ -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;

View File

@ -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;
}
}