Skip to content

Commit 91ff273

Browse files
committed
is_admin: functionalize, fallback, test
1 parent b3a84d7 commit 91ff273

File tree

8 files changed

+65
-26
lines changed

8 files changed

+65
-26
lines changed

+stdlib/+dotnet/is_admin.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function y = is_admin()
2+
3+
y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
4+
5+
end

+stdlib/+sys/get_username.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
if s == 0
1111
n = string(strtrim(n)); % Remove any trailing newline or spaces
1212
else
13-
warning("Failed to get username from system command: %s", n);
13+
warning("Failed to get username from system %s: %s", cmd, n);
1414
n = string.empty;
1515
end
1616

+stdlib/+sys/is_admin.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function y = is_admin()
2+
3+
if ispc()
4+
cmd = 'pwsh -c "([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)"';
5+
else
6+
cmd = 'id -u';
7+
end
8+
9+
[s, r] = system(cmd);
10+
if s ~= 0
11+
warning("stdlib:is_admin:RuntimeError", "Failed to execute command '%s': %s", cmd, r);
12+
y = false;
13+
return;
14+
end
15+
16+
r = string(strip(r));
17+
18+
if ispc()
19+
y = r == "True";
20+
else
21+
y = r == "0";
22+
end
23+
24+
end

+stdlib/is_admin.m

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,14 @@
55

66
if (isunix() || ~isMATLABReleaseOlderThan('R2024a')) && stdlib.has_python()
77
y = stdlib.python.is_admin();
8+
elseif ispc() && stdlib.has_dotnet()
9+
y = stdlib.dotnet.is_admin();
10+
elseif isunix() && stdlib.has_java()
11+
y = stdlib.java.is_admin();
812
elseif stdlib.isoctave()
913
y = getuid() == 0;
10-
elseif ispc() && stdlib.has_dotnet()
11-
% com.sun.security.auth.module.NTSystem().getGroupIDs();
12-
% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
13-
% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
14-
15-
% Use .NET System.Security.Principal to check for admin privileges
16-
identity = System.Security.Principal.WindowsIdentity.GetCurrent();
17-
principal = System.Security.Principal.WindowsPrincipal(identity);
18-
y = principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
19-
elseif isunix()
20-
if stdlib.has_java()
21-
y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
22-
else
23-
[s, r] = system('id -u');
24-
y = s == 0 && strip(r) == "0";
25-
end
2614
else
27-
error("stdlib:is_admin:UnsupportedPlatform", "is_admin() is not supported on this platform")
15+
y = stdlib.sys.is_admin();
2816
end
2917

3018
end

test/TestFileImpure.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ function test_get_pid(tc)
5454
end
5555

5656

57-
function test_is_admin(tc)
58-
tc.verifyClass(stdlib.is_admin(), "logical")
59-
end
60-
61-
6257
function test_handle2filename(tc, ph)
6358
tc.verifyEqual(stdlib.handle2filename(ph{1}), ph{2})
6459
end

test/TestSys.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
cpu_arch_fun = {@stdlib.cpu_arch, @stdlib.dotnet.cpu_arch, @stdlib.java.cpu_arch}
1111
host_fun = {@stdlib.hostname, @stdlib.dotnet.get_hostname, @stdlib.java.get_hostname, @stdlib.python.get_hostname}
1212
user_fun = {@stdlib.get_username, @stdlib.dotnet.get_username, @stdlib.java.get_username, @stdlib.python.get_username}
13+
is_admin_fun = {@stdlib.is_admin, @stdlib.sys.is_admin, @stdlib.dotnet.is_admin, @stdlib.java.is_admin, @stdlib.python.is_admin}
1314
ram_free_fun = {@stdlib.ram_free, @stdlib.sys.ram_free, @stdlib.java.ram_free, @stdlib.python.ram_free}
1415
ram_total_fun = {@stdlib.ram_total, @stdlib.sys.ram_total, @stdlib.dotnet.ram_total @stdlib.java.ram_total}
1516
end
@@ -25,6 +26,12 @@ function test_is_cygwin(tc)
2526
tc.verifyFalse(stdlib.is_cygwin())
2627
end
2728

29+
function test_is_admin(tc, is_admin_fun)
30+
is_capable(tc, is_admin_fun)
31+
tc.verifyClass(is_admin_fun(), "logical")
32+
tc.verifyNotEmpty(is_admin_fun())
33+
end
34+
2835
function test_get_shell(tc)
2936
tc.assumeFalse(tc.CI, "get_shell is not tested in CI due to platform differences")
3037
tc.verifyNotEmpty(stdlib.get_shell())

test/is_capable.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function is_capable(tc, f)
1212

1313
tc.assumeGreaterThan(dapi, 0)
1414

15-
if endsWith(n, ["owner"])
16-
tc.assumeFalse(isunix(), "Windows only function")
15+
if endsWith(n, ["is_admin", "owner"])
16+
tc.assumeTrue(ispc(), "Windows only function")
1717
end
1818

1919
if endsWith(n, ["create_symlink", "ram_total", "read_symlink"])
@@ -30,6 +30,10 @@ function is_capable(tc, f)
3030
tc.assumeGreaterThanOrEqual(japi, 11)
3131
end
3232

33+
if endsWith(n, "is_admin")
34+
tc.assumeTrue(isunix())
35+
end
36+
3337
elseif contains(n, "python")
3438

3539
tc.assumeTrue(stdlib.has_python())
@@ -40,10 +44,14 @@ function is_capable(tc, f)
4044
tc.assumeTrue(has_psutil, "need Python psutil package")
4145
end
4246

43-
if endsWith(n, ["owner"])
47+
if endsWith(n, "owner")
4448
tc.assumeFalse(ispc(), "unix only function")
4549
end
4650

51+
if endsWith(n, "is_admin")
52+
tc.assumeTrue(isunix() || ~isMATLABReleaseOlderThan('R2024a'))
53+
end
54+
4755
end
4856

4957
end

0 commit comments

Comments
 (0)