Skip to content

Commit d615490

Browse files
committed
is_admin: doesn't require MEX
Can also use Java on Unix-like or .Net on Windows
1 parent 8e8a617 commit d615490

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

+stdlib/is_admin.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
%% IS_ADMIN is the process run as root / admin
2-
% requires: mex
2+
% optional: mex
3+
4+
function y = is_admin()
5+
6+
if ispc()
7+
% com.sun.security.auth.module.NTSystem().getGroupIDs();
8+
% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
9+
% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
10+
11+
% Use .NET System.Security.Principal to check for admin privileges
12+
identity = System.Security.Principal.WindowsIdentity.GetCurrent();
13+
principal = System.Security.Principal.WindowsPrincipal(identity);
14+
y = principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
15+
elseif stdlib.isoctave()
16+
y = getuid() == 0;
17+
else
18+
y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
19+
end
320

4-
function is_admin()
5-
error("buildtool mex")
621
end
722

823
%!testif 0

test/TestFileImpure.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ function test_get_pid(tc)
6060
end
6161

6262

63+
function test_is_admin(tc)
64+
tc.assumeTrue(ispc() || stdlib.has_java() || stdlib.is_mex_fun("stdlib.is_admin"))
65+
66+
tc.verifyClass(stdlib.is_admin(), "logical")
67+
end
68+
69+
6370
function test_handle2filename(tc, ph)
6471
tc.verifyEqual(stdlib.handle2filename(ph{1}), ph{2})
6572
end

test/TestMex.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ function test_is_char_device(tc)
1414
end
1515

1616

17-
function test_is_admin(tc)
18-
tc.verifyClass(stdlib.is_admin(), "logical")
19-
end
20-
21-
2217
function test_remove_file(tc)
2318
tc.assertTrue(stdlib.is_mex_fun("stdlib.remove"))
2419

0 commit comments

Comments
 (0)