Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if user is admin [duplicate]

Tags:

java

So, I want to know if a user has run the application with administrator privileges -- irrespective of the OS the person is on.

I found the solution for Windows (from a website):

public static boolean isAdmin() {
    String groups[] = (new com.sun.security.auth.module.NTSystem()).getGroupIDs();
    for (String group : groups) {
        if (group.equals("S-1-5-32-544"))
            return true;
    }
    return false;
}  

What about Mac and Ubuntu ?

like image 472
An SO User Avatar asked Jun 14 '13 17:06

An SO User


1 Answers

I don't think is possible to be totally OS independent, but a few months ago I had to check IzPack source code and it does exactly what you need.

In the PrivilegedRunner class it has to check if it has admin privileges, check the method isElevationNeeded

Here is the source code

like image 90
Federico Nafria Avatar answered Oct 20 '22 15:10

Federico Nafria