Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to enumerate the Users #3

Open
gliden opened this issue Jun 20, 2023 · 6 comments
Open

Add the possibility to enumerate the Users #3

gliden opened this issue Jun 20, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@gliden
Copy link

gliden commented Jun 20, 2023

it would be really helpful if you can enumerate over the users of a specific group

@EdZava
Copy link
Owner

EdZava commented Jun 21, 2023

I think it could be done.
Right now, I don't have the means to test anything related to DA as I no longer have access to a DA server.

However, I'll share an example code that I believe could work. It would need to be tested and adjusted accordingly.

procedure GetGroupMembers(const groupName: string);
var
  group: IADsGroup;
  members: IADsMembers;
  member: IUnknown;
  enumVar: IEnumVariant;
  count: LongWord;
  userName, fullName: WideString;
begin
  group := ADsGetObject('LDAP://CN=' + groupName + ',OU=UnidadOrganizativa,DC=dominio,DC=com') as IADsGroup;

  if Assigned(group) then
  begin
    members := group.Members as IADsMembers;
    members._NewEnum(EnumVar);
    EnumVar.Reset;

    while EnumVar.Next(1, member, Count) = S_OK do
    begin
      if Supports(member, IADsUser) then
      begin
        userName := IADsUser(member).Name;
        fullName := IADsUser(member).FullName;
        // Add List (userName, fullName)
      end;

      member := nil;
    end;
  end;
end;

If you would like to contribute to the project, any help would be welcome.

@EdZava
Copy link
Owner

EdZava commented Jul 6, 2023

@gliden
Could you try the example that I gave you?

@gliden
Copy link
Author

gliden commented Jul 7, 2023

@EdZava I tried it but there is an OLE-Exception
GetGroupMembers = Error.Class: EOleException | Error.Message: Schnittstelle nicht unterstützt

The exception occures in this code
ADsGetObject('LDAP://OU=XXX,DC=XXX,DC=local', IADsGroup, group);

@EdZava
Copy link
Owner

EdZava commented Jul 11, 2023

@gliden
Try to try this other example method:

function TActiveDirectoryClientWinapi.GetGroupUsers(inDomainName, inGroupName: string; out outUsers: TStringList): Boolean;
var
  Path: string;
  Resultado: HRESULT;
  Group: IADsGroup;
  Enum: IEnumVariant;
  varUser: OleVariant;
  Temp: LongWord;
begin
  Result := False;
  outUsers := TStringList.Create;

  if (Trim(inDomainName) = '') then
    Exit;

  if (Trim(inGroupName) = '') then
    Exit;

  Path := TActiveDirectoryWinapiUtils.GetPathLDAP(inDomainName, 'CN=' + inGroupName);
  Resultado := ADsGetObject(Path, IADsGroup, Group);

  if (Failed(Resultado)) or (Group = nil) then
    Exit;

  Enum := Group.Members._NewEnum as IEnumVariant;

  if (Enum <> nil) then
  begin
    while (Enum.Next(1, varUser, Temp) = S_OK) do
    begin
      outUsers.Add(varUser.Name);
      VariantClear(varUser);
    end;
  end;

  Result := True;
end;

Then tell me if it worked for you.

@gliden
Copy link
Author

gliden commented Jul 12, 2023

Its not working with this error
GetGroupUsers = Error.Class: EOleException | Error.Message: Es ist ein Fehler bei der Ausführung aufgetreten

In our Domain the users are organized in an organizationalUnit but also the group "CN=Builtin" is not working.

In Ldap-Admin it looks like this:
image

@EdZava
Copy link
Owner

EdZava commented Jul 13, 2023

@gliden
I'm sorry for not being able to help further, but without debugging the code, it's difficult for me to indicate what the solution could be.

When I have some free time at work, I will try to recover the VM I had with the DA configured to attempt implementing this method.

If I find anything else, I will let you know here.
Thank you!

@EdZava EdZava added the enhancement New feature or request label Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants