File tree Expand file tree Collapse file tree 3 files changed +25
-8
lines changed Expand file tree Collapse file tree 3 files changed +25
-8
lines changed Original file line number Diff line number Diff line change 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" )
621end
722
823% !testif 0
Original file line number Diff line number Diff line change @@ -60,6 +60,13 @@ function test_get_pid(tc)
6060end
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+
6370function test_handle2filename(tc , ph )
6471tc .verifyEqual(stdlib .handle2filename(ph{1 }), ph{2 })
6572end
Original file line number Diff line number Diff line change @@ -14,11 +14,6 @@ function test_is_char_device(tc)
1414end
1515
1616
17- function test_is_admin(tc )
18- tc .verifyClass(stdlib .is_admin(), " logical" )
19- end
20-
21-
2217function test_remove_file(tc )
2318tc .assertTrue(stdlib .is_mex_fun(" stdlib.remove" ))
2419
You can’t perform that action at this time.
0 commit comments