Link to home
Start Free TrialLog in
Avatar of jaw0807
jaw0807Flag for United States of America

asked on

converting asp vbscript to asp javascript

I'm trying to create an ASP (classic) page that checks allows access if the user belongs to a certain AD group. I have found ASP VBScript that works just fine, but I need ASP Javascript. However, I keep running into "Object doesn't support this property or method" exceptions when trying to enumerate the user groups.

Below is the working VBS code (I stripped out some logic and made the variables fixed strings vs. form input to save time/space), as well as what I have for the Javascript tests. I'm getting hung up in the for each loop in the GetUserGroups function. All the rest of the code has converted just fine. But the Javascript version of getUserGroups keeps throwing exceptions.  I've tried standard for loops, for-in, and enumerators and can't get any to work. I know I'm probably getting hung up on one tiny detail I'm missing, most likely due to not being familiar with objUser.Groups

If anyone can lend a hand it would be greatly appreciated.
VB:(slightly simplified logic to save space)
 
Dim strUserName
Dim strADsPath
Dim strDomain
strDomain = "mylocalserver.local"
strUserName = "myusername"
strADsPath = "WinNT://" & strDomain
Dim oADsObject  
Dim tempstr
tempstr = strDomain & "\" & strUserName
 
Set oADsObject = GetObject(strADsPath)
Dim strADsNamespace
Dim oADsNamespace
strADsNamespace = left(strADsPath, instr(strADsPath, ":"))
set oADsNamespace = GetObject(strADsNamespace)
Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, tempstr, strPassword, 0)
 
         
Session("USER_LOGIN") = strUserName
Session("isLoggedIn") = True
Session("ValidUser") = True
Response.Write("logged in!<br />")
Response.Write GetUserFullName() & "<br />"
Response.Write GetUserGroups() & "<br />"
 
Function GetUserFullName()
 On Error Resume Next
 
 Set oUser = GetObject(strADsPath& "/" & strUserName & ",user")
 GetUserFullName = oUser.fullName
 Set oUser = Nothing
 
 If Err <> 0 Then
     GetUserFullName = "User not found"
 End If
End Function
 
Function getUserGroups()
 On Error Resume Next
 
 Set oUser = GetObject(strADsPath& "/" & & strUserName & ",user")
      For Each oGroup in oUser.Groups
            sUserGroups=sUserGroups & oGroup.Name & ", "			
      Next
getUserGroups=sUserGroups
set oUser=nothing      
End Function
 
Javascript: (just the getUserGroups() function, as the rest works okay)
 
function getUserGroups(){
try{
var objUser=GetObject("WinNT://mylocalserver.local/"+strUserName+",user");		
var a=new Array();
var s,x;		
for(o in objUser.Groups){
    x=objUser.Groups[o];
    a.push(x.Name);
    /*
    s+=x.Name+ " ";
    */
}
return a;
//return s;
objUser=null;
} catch(x) {		
    return x.description;
}
	
}

Open in new window

Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

jaw0807,

You aren't going to try to mix server side Javascript and vbscript are you?  You can not do that in a page.

I don't usually use Javascript on the server side so let me know if there is a problem.  The conversion should be correct (see snippet)

Let me know if you have any questions or need more information.

b0lsc0tt
VB:(slightly simplified logic to save space)
 
var strUserName
var strADsPath
var strDomain
strDomain = "mylocalserver.local";
strUserName = "myusername";
strADsPath = "WinNT://" + strDomain;
var oADsObject;
var tempstr;
tempstr = strDomain + "\" + strUserName;
 
Set oADsObject = GetObject(strADsPath);
var strADsNamespace;
var oADsNamespace;
strADsNamespace = strAdsPath.substr(0, indexOf(":"));
set oADsNamespace = GetObject(strADsNamespace);
Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, tempstr, strPassword, 0);
 
         
Session("USER_LOGIN") = strUserName;
Session("isLoggedIn") = True;
Session("ValidUser") = True;
Response.Write("logged in!");
Response.Write GetUserFullName() + "";
Response.Write GetUserGroups() + "";
 
Function GetUserFullName() {
 On Error Resume Next;
 
 Set oUser = GetObject(strADsPath& "/" & strUserName & ",user");
 GetUserFullName = oUser.fullName;
 Set oUser = Nothing;
 
 if (Err <> 0) {
     GetUserFullName = "User not found";
 }
}
 
Function getUserGroups() {
 On Error Resume Next;
 
 Set oUser = GetObject(strADsPath& "/" + strUserName + ",user");
      for (var i; i<oUser.Groups.length; i++) {
            sUserGroups=sUserGroups + oUser.Groups[i].Name + ", ";
      }
getUserGroups=sUserGroups;
set oUser=nothing;
}
 
Javascript: (just the getUserGroups() function, as the rest works okay)
 
function getUserGroups(){
try{
var objUser=GetObject("WinNT://mylocalserver.local/"+strUserName+",user");            
var a=new Array();
var s,x;            
for(o in objUser.Groups){
    x=objUser.Groups[o];
    a.push(x.Name);
    /*
    s+=x.Name+ " ";
    */
}
return a;
//return s;
objUser=null;
} catch(x) {            
    return x.description;
}
      
}

Open in new window

Avatar of jaw0807

ASKER

It's all server-side Javascript/Jscript. I thought the conversion was correct, too, but ASP disagrees. So frustrating.
What conversion?  Did you try mine?  What do you mean disagrees?

Remove the "On Error Resume Next" line so we can get useful errors.  Make sure you have no vbscript in that page.

bol
Avatar of alsaan
alsaan

The for...in construction (unlike VB's For...Each) only works with arrays, not with COM collections. In JScript you need to use an Enumerator
for (var e = new Enumerator(objUser.Groups); !e.atEnd(); e.moveNext())
{
    x=e.item();
    a.push(x.Name);
    /*
    s+=x.Name+ " ";
    */
}

Open in new window

Avatar of jaw0807

ASKER

Sorry for the delayed responses. I have been out of town.

b0lsc0tt: There's no VBscript in the Jscript. I just included both code pages in the same code block on EE. The only part of the code that was giving errors was the getUserGroups() method on the Jscript page (other code was just included so people would have an idea of what I was trying to do, and also on the off chance that someone knew a better way of doing it). In my cutting/pasting, I accidentally paste some JScript code into the VBScript section (the for loop). Sorry for any confusion there. Below is the correct VBScript for the GetUserGroups function that I'm trying to convert. Again sorry about the mixup (that will teach me to stay up all night!)

alsaan: Unfortunately, the enumerator did not work, either. The page still throws an exception that reads: Object doesn't support this property or method. I don't understand why VBScript can access Group.Name in objuser.Groups yet I cannot seem to access it with Jscript.

Points increased due to my c&p mistake and complexity of question.

Working VBScript function:
 
Function getUserGroups()
 On Error Resume Next
 
 Set oUser = GetObject("WinNT://mylocalserver.local/" & strUserName & ",user")
      For Each oGroup in oUser.Groups
            sUserGroups=sUserGroups & oGroup.Name & ", "			
      Next
getUserGroups=sUserGroups
set oUser=nothing      
End Function
 
Broken Jscript:
 
function getUserGroups(){
  try{
   var objUser=GetObject("WinNT://mylocalserver.local/"+strUserName+",user");		
   var a=new Array();
   var s,x;		
   /*
      //incorrect for-in, arrays only
      for(o in objUser.Groups){
      x=objUser.Groups[o];
      a.push(x.Name);
   }*/
   
   //enumerator not finding group names
   for (var e = new Enumerator(objUser.Groups); !e.atEnd(); e.moveNext()){
      x=e.item();
      a.push(x.Name);
      /*
      s+=x.Name+ " ";
      */
      }
      return a;
      objUser=null;
   } catch(x) {		
      return x.description;
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of alsaan
alsaan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaw0807

ASKER

Aha! That makes perfect sense, and more importantly it works! Thank you!