We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b3a84d7 commit 91ff273Copy full SHA for 91ff273
+stdlib/+dotnet/is_admin.m
@@ -0,0 +1,12 @@
1
+function y = is_admin()
2
+
3
+% com.sun.security.auth.module.NTSystem().getGroupIDs();
4
+% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
5
+% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
6
7
+% Use .NET System.Security.Principal to check for admin privileges
8
+identity = System.Security.Principal.WindowsIdentity.GetCurrent();
9
+principal = System.Security.Principal.WindowsPrincipal(identity);
10
+y = principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
11
12
+end
+stdlib/+java/is_admin.m
@@ -0,0 +1,5 @@
+y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
+stdlib/+sys/get_username.m
@@ -10,7 +10,7 @@
if s == 0
n = string(strtrim(n)); % Remove any trailing newline or spaces
else
13
- warning("Failed to get username from system command: %s", n);
+ warning("Failed to get username from system %s: %s", cmd, n);
14
n = string.empty;
15
end
16
+stdlib/+sys/is_admin.m
@@ -0,0 +1,24 @@
+if ispc()
+ cmd = 'pwsh -c "([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)"';
+else
+ cmd = 'id -u';
+[s, r] = system(cmd);
+if s ~= 0
+ warning("stdlib:is_admin:RuntimeError", "Failed to execute command '%s': %s", cmd, r);
+ y = false;
+ return;
+r = string(strip(r));
17
18
19
+ y = r == "True";
20
21
+ y = r == "0";
22
23
24
+stdlib/is_admin.m
@@ -5,26 +5,14 @@
if (isunix() || ~isMATLABReleaseOlderThan('R2024a')) && stdlib.has_python()
y = stdlib.python.is_admin();
+elseif ispc() && stdlib.has_dotnet()
+ y = stdlib.dotnet.is_admin();
+elseif isunix() && stdlib.has_java()
+ y = stdlib.java.is_admin();
elseif stdlib.isoctave()
y = getuid() == 0;
-elseif ispc() && stdlib.has_dotnet()
- % com.sun.security.auth.module.NTSystem().getGroupIDs();
- % Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
- % only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
-
- % Use .NET System.Security.Principal to check for admin privileges
- identity = System.Security.Principal.WindowsIdentity.GetCurrent();
- principal = System.Security.Principal.WindowsPrincipal(identity);
- y = principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
-elseif isunix()
- if stdlib.has_java()
- y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
- else
- [s, r] = system('id -u');
- y = s == 0 && strip(r) == "0";
25
- end
26
27
- error("stdlib:is_admin:UnsupportedPlatform", "is_admin() is not supported on this platform")
+ y = stdlib.sys.is_admin();
28
29
30
test/TestFileImpure.m
@@ -54,11 +54,6 @@ function test_get_pid(tc)
54
55
56
57
-function test_is_admin(tc)
58
-tc.verifyClass(stdlib.is_admin(), "logical")
59
-end
60
61
62
function test_handle2filename(tc, ph)
63
tc.verifyEqual(stdlib.handle2filename(ph{1}), ph{2})
64
test/TestSys.m
@@ -10,6 +10,7 @@
cpu_arch_fun = {@stdlib.cpu_arch, @stdlib.dotnet.cpu_arch, @stdlib.java.cpu_arch}
host_fun = {@stdlib.hostname, @stdlib.dotnet.get_hostname, @stdlib.java.get_hostname, @stdlib.python.get_hostname}
user_fun = {@stdlib.get_username, @stdlib.dotnet.get_username, @stdlib.java.get_username, @stdlib.python.get_username}
+is_admin_fun = {@stdlib.is_admin, @stdlib.sys.is_admin, @stdlib.dotnet.is_admin, @stdlib.java.is_admin, @stdlib.python.is_admin}
ram_free_fun = {@stdlib.ram_free, @stdlib.sys.ram_free, @stdlib.java.ram_free, @stdlib.python.ram_free}
ram_total_fun = {@stdlib.ram_total, @stdlib.sys.ram_total, @stdlib.dotnet.ram_total @stdlib.java.ram_total}
@@ -25,6 +26,12 @@ function test_is_cygwin(tc)
tc.verifyFalse(stdlib.is_cygwin())
+function test_is_admin(tc, is_admin_fun)
+is_capable(tc, is_admin_fun)
31
+tc.verifyClass(is_admin_fun(), "logical")
32
+tc.verifyNotEmpty(is_admin_fun())
33
34
35
function test_get_shell(tc)
36
tc.assumeFalse(tc.CI, "get_shell is not tested in CI due to platform differences")
37
tc.verifyNotEmpty(stdlib.get_shell())
test/is_capable.m
@@ -12,8 +12,8 @@ function is_capable(tc, f)
tc.assumeGreaterThan(dapi, 0)
- if endsWith(n, ["owner"])
- tc.assumeFalse(isunix(), "Windows only function")
+ if endsWith(n, ["is_admin", "owner"])
+ tc.assumeTrue(ispc(), "Windows only function")
if endsWith(n, ["create_symlink", "ram_total", "read_symlink"])
@@ -30,6 +30,10 @@ function is_capable(tc, f)
tc.assumeGreaterThanOrEqual(japi, 11)
+ if endsWith(n, "is_admin")
+ tc.assumeTrue(isunix())
+ end
elseif contains(n, "python")
38
39
tc.assumeTrue(stdlib.has_python())
@@ -40,10 +44,14 @@ function is_capable(tc, f)
40
44
tc.assumeTrue(has_psutil, "need Python psutil package")
41
45
42
46
43
47
+ if endsWith(n, "owner")
48
tc.assumeFalse(ispc(), "unix only function")
49
50
51
52
+ tc.assumeTrue(isunix() || ~isMATLABReleaseOlderThan('R2024a'))
53
0 commit comments