Get Windows groups (JNA/Windows) Tag(s): JNA
For the current user
import com.sun.jna.platform.win32.Advapi32Util; import com.sun.jna.platform.win32.Netapi32Util; import com.sun.jna.platform.win32.Netapi32Util.Group; // https://github.com/twall/jna#readme // you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar Group[] groups = Netapi32Util.getUserGroups(Advapi32Util.getUserName(), Netapi32Util.getDCName()); for (Group group:groups) { System.out.println(group.name); }
import com.sun.jna.platform.win32.Netapi32Util; import com.sun.jna.platform.win32.Netapi32Util.Group; ... // https://github.com/twall/jna#readme // you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar Group[] groups = Netapi32Util.getUserGroups("username", Netapi32Util.getDCName()); for (Group group:groups) { System.out.println(group.name); }
Implement "userInRole()" method is easy.
public static boolean isUserInRole(String user, String role) { try { Group[] groups = Netapi32Util.getUserGroups(user, Netapi32Util.getDCName()); boolean isAuthorized = false; for (Group group:groups) { if (group.name.equals(role)) { isAuthorized = true; break; } } return isAuthorized; } catch (Exception e) { return false; } }