Einzelnen Beitrag anzeigen

Benutzerbild von MaBuSE
MaBuSE

Registriert seit: 23. Sep 2002
Ort: Frankfurt am Main (in der Nähe)
1.840 Beiträge
 
Delphi 10 Seattle Enterprise
 
#3

AW: Benutzergruppen des angemeldeten Benutzers ermitteln

  Alt 16. Nov 2010, 13:12
Nun ja, zumindest die lokalen Gruppen eines Benutzers bekommst du mit EnumUserGroups aus MpuNTUser.pas. Die solltest du eigentlich auch entsprechend für Server umschreiben können.
Ja, konnte ich

Danke.

Die Funktion GetDomainUserGroups gibt einfach alle Gruppen als String zurück.

Beisp:
Delphi-Quellcode:
...
uses
  u_GetDomainUserGroups;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Text := GetDomainUserGroups('MyDomain', 'MyUserName');
end;
...
Viel Spaß
MaBuSE

PS: Hier der Quelltext der Unit:
Delphi-Quellcode:
////////////////////////////////////////////////////////////////////////////////
// u_GetDomainUserGroups
////////////////////////////////////////////////////////////////////////////////
// (c) 2010 MaBuSE member of delphipraxis.net
// based on MpuNTUser.pas (c) 2005, 2006 Michael Puff (MPu)
////////////////////////////////////////////////////////////////////////////////
// there is no warranty of any kind, use at your own risk
////////////////////////////////////////////////////////////////////////////////
unit u_GetDomainUserGroups;

interface

uses
  Windows, SysUtils, Classes;

// http://msdn.microsoft.com/en-us/library/aa370655%28VS.85%29.aspx
function NetUserGetGroups(servername: LPCWSTR; username: LPCWSTR; level: DWORD; var bufptr: Pointer;
  prefmaxlen: DWORD; var entriesread: DWORD; var totalentries: DWORD): DWORD; stdcall;

// http://msdn.microsoft.com/en-us/library/aa370304(VS.85).aspx
function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall;

// Gibt Gruppen des Benutzers User in der Domäne Domain zurück
function GetDomainUserGroups(const Domain, User: WideString): string;

implementation

function NetUserGetGroups; external 'netapi32.dllname 'NetUserGetGroups';
function NetApiBufferFree; external 'netapi32.dllname 'NetApiBufferFree';

const
  MAX_PREFERRED_LENGTH = DWORD(-1);

type
  TGroupInfo0 = record
    grpi0_name: LPWSTR;
  end;
  PGroupInfo0 = ^TGroupInfo0;

function GetDomainUserGroups(const Domain, User: WideString): string;
var
  gui0: Pointer;
  i: Integer;
  Error, EntriesRead, EntriesTotal: DWORD;
begin
  gui0 := nil;
  try
    Error := NetUserGetGroups(PWideChar(Domain), PWideChar(User),
      0, gui0, MAX_PREFERRED_LENGTH, EntriesRead, EntriesTotal);
    if Error <> 0 then RaiseLastOSError(Error)
    else
      with TStringList.Create do
      try
        for i := 0 to EntriesRead - 1 do
          Add(PGroupInfo0(Integer(gui0)+(i*SizeOf(Pointer)))^.grpi0_name);
        Sort;
        Result := Text;
      finally
        Free;
      end;
  finally
    NetApiBufferFree(gui0);
  end;
end;

end.
(°¿°) MaBuSE - proud to be a DP member
(°¿°) MaBuSE - proud to be a "Rüsselmops" ;-)

Geändert von MaBuSE (16. Nov 2010 um 13:41 Uhr)
  Mit Zitat antworten Zitat