ebiard
(Fresh Scripter)
2004-10-27 06:34 PM
MemberOf() - A better InGroup function

; Function : MemberOf()
;
; Author : Emmanuel Biard
; Contributor : Howard A. Bullock for some part of his TranslateName() function
;
; Action : MembreDe function tests if a user is member of a group (directly or not)
;
; Syntax : MemberOf(Group[, User])
;
; Version : 1.0
; Date : 27 december 2004
; Date revised : -
;
; Parameters :
; - Group, name of the group
; - User, name of the user (@USERID if not specified)
;
; Remarks : This function was created because the built-in InGroup kix function is not reliable at all, neither for direct membership tests nor for indirect ones.
;
; Returns :
; 0 if the user is not member of the group
; 1 if the user is member of the group
;
; Dependencies : LDAP directory
; KiXtart version : 4.20
;
; Example : if MemberOf("ThisGroup", "ThisUser")
;
Function NomComplet($NomCourt)
$NomComplet = ""
$oNomComplet = CreateObject("NameTranslate")
If Not(@ERROR)
$oNomComplet.Init(3, "")
If Not(@ERROR)
$oNomComplet.Set (3,$NomCourt)
If Not(@ERROR)
$NomComplet = $oNomComplet.Get(1)
EndIf
EndIf
EndIf
EndFunction

Function RecursifMembreDe($Groupe, $User)
$LDAPGroupe = NomComplet("@DOMAIN\"+$Groupe)
$oGroupe = GetObject("LDAP://" + $LDAPGroupe)
If Not(@ERROR)
For Each $Objet In $oGroupe.Members
Select
Case $Objet.Class = "group"
$Result = RecursifMembreDe($Objet.cn, $User)
If ($Result = 1)
$RecursifMembreDe = 1
Return
EndIf
Case $Objet.Class = "user"
If ($Objet.samAccountName == @USERID)
$RecursifMembreDe = 1
Return
EndIf
EndSelect
Next
EndIf
$RecursifMembreDe = 0
EndFunction

Function MemberOf($Groupe, OPTIONAL $User)
If ($User = "")
$User = @USERID
EndIf
$MemberOf = RecursifMembreDe($Groupe, $User)
EndFunction


LonkeroAdministrator
(KiX Master Guru)
2004-10-27 06:48 PM
Re: MembreDe() - Un meilleur / A better InGroup

sorry boss....
but, your udf is not really an udf but library which uses UDFs.

could you strip it down to UDF.
and once done, at the proper header like said in the UDF library rules.

thanks.


ebiard
(Fresh Scripter)
2004-10-27 07:00 PM
Re:

I'm sorry not to understand what you mean buy "this is not an UDF", that i have to "strip it down to UDF" (I'm french !). Please explain me with more details.
And for the header, the link to use the correct header does not work. I've rebuilt the header to be more close of what is recommended.


LonkeroAdministrator
(KiX Master Guru)
2004-10-27 07:31 PM
Re:

what link?
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82788&page=0&view=collapsed&sb=5&o=&fpart=1

is the first post seen on UDF library.
nothing not working in that.

what comes to trim to UDF means, you have multiple udfs in your udf sourcecode.
that's not udf anymore, that makes it library.
one udf is one udf.
loose the others from there.
if they are needed and not found in udf library, post them separately and mark them as dependencies.


Cybex
(Getting the hang of it)
2004-10-27 09:01 PM
Re:

ebiard, I think what he was trying to say about the header is that you need to "Make sure to post the
entire UDf within code-tags or post with PostPrep."

and also follow the standard format for the examples and the code. Which you seem to have gone back and done.


LonkeroAdministrator
(KiX Master Guru)
2004-10-27 09:25 PM
Re:

not really.
basically, the standard defines the header.

but it does not remove the fact that you have 3 UDFs in this post.
only 1 is allowed. thus the name UDF! not multiple but one.


Sealeopard
(KiX Master)
2004-10-28 04:03 AM
Re:

Also, I'm sure you didn' write it on this date
Quote:


; Date : 27 december 2004






Les
(KiX Master)
2004-10-28 04:14 AM
Re:

I propose this thread be moved to scripts. It can then be further discussed and if the original author has a desire to meat the UDF posting guidlines, can be reposted here as separate UDFs.

NTDOCAdministrator
(KiX Master)
2004-10-28 08:16 AM
Re: MemberOf() - A better InGroup function

Hello ebiard

I have moved this post to the SCRIPTS forum where it is more appropriate.

In essence, a UDF is a single User Defined Function that performs some task. Your post has 3 UDFs in it, which would make it more like a LIBRARY of functions.

What you could do if you wanted to would be to post all 3 of them as individual UDFs and list each of them as a dependancy for each other. However that may be too much work and thus is better to just have it here in the SCRIPTS forum where this type of post normally would belong.

I take it that you're wanting to do more then just TRUE/FALSE on membership in a group by saying that InGroup does not work. I have not found that to be the case. I use InGroup quite a bit and including Universal groups, and so far it has been working for me. How far the nesting goes for InGroup I'm not sure, but deep nesting is also frowned upon by Microsoft and most Admins even if AD does support it.

I see that you say dependancy is KiXtart v4.20 I don't know for sure, but perhaps your issues or complaint about InGroup is from the version level. Have you tried v4.22 to determine if that is able to correct the issue?


ebiard
(Fresh Scripter)
2004-10-28 12:24 PM
Re: MemberOf() - A better InGroup function

Ok i've understood the problem that a UDF must be one function.
For the link that does not work, it is the link for postprep zip that brings to a 404 censored page, but maybe i don't need it.
NTDOC, I can assure u the InGroup function is not reliable, and 4.20 is not a dependancy but the kix32 version used. 4.22 does not change anything on this point.
I modified the code to obtain a unique function.
Do what you want of it.
Bye.
Code:

Function MemberOf($Groupe, OPTIONAL $User)
;
; IF THE USER IS NOT SPECIFIED, CURRENT USER IS USED
If ($User = "")
$User = @USERID
EndIf
;
; RETRIEVES THE FULL QUALIFIED NAME OF THE GROUP
$LDAPGroupe = ""
$oLDAPGroupe = CreateObject("NameTranslate")
If Not(@ERROR)
$oLDAPGroupe.Init(3, "")
If Not(@ERROR)
$oLDAPGroupe.Set(3,"@DOMAIN\"+$Groupe)
If Not(@ERROR)
$LDAPGroupe = $oLDAPGroupe.Get(1)
EndIf
EndIf
EndIf
;
; CONNECTION TO THE GROUP
$oGroupe = GetObject("LDAP://" + $LDAPGroupe)
;
; BROWSE GROUP MEMBERS
If Not(@ERROR)
For Each $Objet In $oGroupe.Members
Select
;
; IF THE MEMBER IS A GROUP, RECURSIVELY GOES ON
Case $Objet.Class = "group"
$Result = MemberOf($Objet.cn, $User)
If ($Result = 1)
$MemberOf= 1
Return
EndIf
; IF THE MEMBER IS A USER, IS IT THE ONE ?
Case $Objet.Class = "user"
If ($Objet.samAccountName == @USERID)
$MemberOf= 1
Return
EndIf
EndSelect
Next
EndIf
$MemberOf = 0
EndFunction



LonkeroAdministrator
(KiX Master Guru)
2004-10-28 03:00 PM
Re: MemberOf() - A better InGroup function

alrighty, now as that not working part is back in the discussion, I would also like to know where kixtart fails with ingroup.
what type of environment how it's ran and what type of failure in specific.
met by everyone or just somebody?
problem happens for new or for old groups too?

I would wait yet still some time before saying that kixtart ingroup would not always work...

although, have seen some problems with it, specifically, iirc, we had a name resolution problem back then.


bekickst
(Fresh Scripter)
2009-04-27 04:17 PM
Re: MemberOf() - A better InGroup function

I had the same problem Lonkero. When I tested the skript on my own computer, the group membership was corrected when I logged on again and ran the skript (including /f). It didn`t work on some other Computers though. In the end I used the function memberof, that I found here. Now its working perfectly.

It`s not a bug. Itīs a feature.