html中的Java检测是否为admin

时间:2014-06-06 12:57:57

标签: java

我有这个代码在控制台模式下运行,以检测Windows会话是否处于Admin或没有管理员权限,我想将它直接嵌入到网页中作为applet或类似的东西。

你能帮我解决一下网络使用吗?

import java.io.*;

public class WindowsUtils {

  private WindowsUtils() {  }

  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;
  }

  public static void main(String[] args) throws IOException {
    System.out.println("Current user is admin ? " + WindowsUtils.isAdmin());
  }
}

1 个答案:

答案 0 :(得分:-1)

您正在使用PHP进行网络处理。所以更好的选择是使用Javascript而不是applet。

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();

    var currentUser = currentContext.get_web().get_currentUser();
    currentContext.load(currentUser);

    var allGroups = currentWeb.get_siteGroups();
    currentContext.load(allGroups);

    var group = allGroups.getByName(groupName);
    currentContext.load(group);

    var groupUsers = group.get_users();
    currentContext.load(groupUsers);

    currentContext.executeQueryAsync(OnSuccess,OnFailure);

    function OnSuccess(sender, args) {
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }  
        OnComplete(userInGroup);
    }

    function OnFailure(sender, args) {
        OnComplete(false);
    }    
}

用法如下:

//Usage
function IsCurrentUserWithContributePerms() 
{
  IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {
        // The current user is in the [Members] group
    }
  });

}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserWithContributePerms, 'SP.js');

Link for SP.js