mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2025-01-31 12:52:36 +01:00
check admin privileges
This commit is contained in:
parent
aebcb5c6b2
commit
fc227923cb
@ -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;
|
||||
|
37
G-Earth/src/main/java/gearth/misc/AdminValidator.java
Normal file
37
G-Earth/src/main/java/gearth/misc/AdminValidator.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user