Skip to content

Commit 1c9c8dd

Browse files
authored
Merge pull request #1043 from OpenBCI/change-windows-java-rights
Change Windows Priveleges and Check Privileges
2 parents 877e3df + 796b9d1 commit 1c9c8dd

6 files changed

Lines changed: 112 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Stop data stream when no data received after 5 seconds #1011
55
* Revisit Ganglion Impedance widget so it behaves like new Cyton Impedance Widget #1021
66
* Fix dropdown backgrounds in Networking Widget
7+
* Update priveleges for Windows users and check if GUI has been run as Administrator
78

89
### Improvements
910
* Add new FilterUI to allow custom filters per channel #988

OpenBCI_GUI/Extras.pde

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import java.io.OutputStream;
2+
import java.io.PrintStream;
3+
import java.util.prefs.Preferences;
4+
import static java.lang.System.setErr;
5+
import static java.util.prefs.Preferences.systemRoot;
6+
17
//------------------------------------------------------------------------
28
// Global Functions
39
//------------------------------------------------------------------------
@@ -41,7 +47,103 @@ private void checkIs64BitJava() {
4147
PopupMessage msg = new PopupMessage("32-bit Java Detected", "OpenBCI GUI v5 and BrainFlow are made for 64-bit Java (Windows, Linux, and Mac). Please update your OS, computer, Processing IDE, or revert to GUI v4 or earlier.");
4248
}
4349
}
44-
50+
/**
51+
* Determines if elevated rights are required to install/uninstall the application.
52+
*
53+
* @return <code>true</code> if elevation is needed to have administrator permissions, <code>false</code> otherwise.
54+
*/
55+
public boolean isElevationNeeded() {
56+
return isElevationNeeded(null);
57+
}
58+
/**
59+
* Determines if elevated rights are required to install/uninstall the application.
60+
*
61+
* @param path the installation path, or <tt>null</tt> if the installation path is unknown
62+
* @return <tt>true</tt> if elevation is needed to have administrator permissions, <tt>false</tt> otherwise.
63+
*/
64+
public boolean isElevationNeeded(String path) {
65+
boolean result;
66+
if (isWindows()) {
67+
if (path != null) {
68+
// use the parent path, as that needs to be written to in order to delete the tree
69+
path = new File(path).getParent();
70+
}
71+
if (path == null || path.trim().length() == 0) {
72+
path = getWindowsProgramFiles();
73+
}
74+
result = !canWrite(path);
75+
} else {
76+
if (path != null) {
77+
result = !canWrite(path);
78+
} else {
79+
result = !System.getProperty("user.name").equals("root");
80+
}
81+
}
82+
return result;
83+
}
84+
/**
85+
* Determine if user has administrative privileges.
86+
*
87+
* @return
88+
*/
89+
public boolean isAdminUser() {
90+
if (isWindows()) {
91+
try {
92+
String NTAuthority = "HKU\\S-1-5-19";
93+
String command = "reg query \""+ NTAuthority + "\"";
94+
Process p = Runtime.getRuntime().exec(command);
95+
p.waitFor();
96+
return (p.exitValue() == 0);
97+
} catch (Exception e) {
98+
return canWrite(getWindowsProgramFiles());
99+
}
100+
}
101+
try {
102+
String command = "id -u";
103+
Process p = Runtime.getRuntime().exec(command);
104+
p.waitFor();
105+
InputStream stdIn = p.getInputStream();
106+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stdIn));
107+
String value = bufferedReader.readLine();
108+
return value.equals("0");
109+
} catch (Exception e) {
110+
return System.getProperty("user.name").equals("root");
111+
}
112+
}
113+
/**
114+
* Tries to determine the Windows Program Files directory.
115+
*
116+
* @return the Windows Program Files directory
117+
*/
118+
private String getWindowsProgramFiles() {
119+
String path = System.getenv("ProgramFiles");
120+
if (path == null) {
121+
path = "C:\\Program Files";
122+
}
123+
return path;
124+
}
125+
/**
126+
* Determines if the specified path can be written to.
127+
*
128+
* @param path the path to check
129+
* @return <tt>true</tt> if the path can be written to, otherwise <tt>false</tt>
130+
*/
131+
private boolean canWrite(String path) {
132+
File file = new File(path);
133+
boolean canWrite = file.canWrite();
134+
if (canWrite) {
135+
// make sure that the path can actually be written to, for IZPACK-727
136+
try {
137+
File test = File.createTempFile(".izpackwritecheck", null, file);
138+
if (!test.delete()) {
139+
test.deleteOnExit();
140+
}
141+
} catch (IOException exception) {
142+
canWrite = false;
143+
}
144+
}
145+
return canWrite;
146+
}
45147

46148
//compute the standard deviation
47149
float std(float[] data) {

OpenBCI_GUI/OpenBCI_GUI.pde

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import http.requests.*;
6060
// Global Variables & Instances
6161
//------------------------------------------------------------------------
6262
//Used to check GUI version in TopNav.pde and displayed on the splash screen on startup
63-
String localGUIVersionString = "v5.1.0-alpha.8";
63+
String localGUIVersionString = "v5.1.0-alpha.9";
6464
String localGUIVersionDate = "April 2022";
6565
String guiLatestVersionGithubAPI = "https://api.github.com/repos/OpenBCI/OpenBCI_GUI/releases/latest";
6666
String guiLatestReleaseLocation = "https://github.com/OpenBCI/OpenBCI_GUI/releases/latest";
@@ -479,6 +479,10 @@ void delayedSetup() {
479479

480480
//Apply GUI-wide settings to front end at the end of setup
481481
guiSettings.applySettings();
482+
483+
if (!isAdminUser() || isElevationNeeded()) {
484+
outputError("OpenBCI_GUI: This application is not being run with Administrator access. This could limit the ability to connect to devices or read/write files.");
485+
}
482486
}
483487

484488
//====================== END-OF-SETUP ==========================//

release_script/windows_only/gui.manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
1111
<security>
1212
<requestedPrivileges>
13-
<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
13+
<requestedExecutionLevel level="HighestAvailable" uiAccess="false"></requestedExecutionLevel>
1414
</requestedPrivileges>
1515
</security>
1616
</trustInfo>

release_script/windows_only/java.manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
1111
<security>
1212
<requestedPrivileges>
13-
<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
13+
<requestedExecutionLevel level="HighestAvailable" uiAccess="false"></requestedExecutionLevel>
1414
</requestedPrivileges>
1515
</security>
1616
</trustInfo>

release_script/windows_only/javaw.manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
1111
<security>
1212
<requestedPrivileges>
13-
<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
13+
<requestedExecutionLevel level="HighestAvailable" uiAccess="false"></requestedExecutionLevel>
1414
</requestedPrivileges>
1515
</security>
1616
</trustInfo>

0 commit comments

Comments
 (0)