Solved

How to know in code if a windows network user is member of a network group?

Posted on 2006-11-08
2
337 Views
Last Modified: 2010-04-04
Dear experts,
I need to use Windows identification to assign user rights in my software.
How can i know if the current user is a member of a certain group,
in order to allow him some rights allowed only to this group?
I need some sample code in Delphi 3 if possible.
Thanks in advance
0
Comment
Question by:icad01
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17900023

Example user group listing done and tested in D3. Requires admin or account operator privs to execute.

Regards,
Russell

----

example usage:

var listGroup: TStringList;
begin
 
  listGroup:=TStringList.Create;
  try
     GetUserGroups('administrator', listGroup);
     ShowMessage(listGroup.Text);
  finally
     listGroup.Free;
  end;

end;

--- the code ---

////////////////////////////////////////////////////////////////////////////////
//   LanMan constants
////////////////////////////////////////////////////////////////////////////////
const
  MAX_PREFERRED_LENGTH       =  DWORD(-1);
  LG_INCLUDE_INDIRECT        = $0001;

////////////////////////////////////////////////////////////////////////////////
//   LanMan types
////////////////////////////////////////////////////////////////////////////////
type
  NET_API_STATUS             =  DWORD;

  LOCALGROUP_USERS_INFO_0    =  packed record
     lgrui0_name:            LPWSTR;
  end;
  LPLOCALGROUP_USERS_INFO_0  =  ^LOCALGROUP_USERS_INFO_0;
  TLocalGroupUserInfo0       = LOCALGROUP_USERS_INFO_0;
  PLocalGroupUserInfo0       =  ^TLocalGroupUserInfo0;

////////////////////////////////////////////////////////////////////////////////
//   LanMan functions
////////////////////////////////////////////////////////////////////////////////
function   NetUserGetLocalGroups(ServerName: LPCWSTR; UserName: LPCWSTR; Level: DWORD; Flags: DWORD; var BufPtr: Pointer; PrefMaxLen: DWORD; EntriesRead: PDWORD; TotalEntries: PDWORD): NET_API_STATUS; stdcall; external 'netapi32.dll';
function   NetApiBufferFree(pBuf: Pointer): NET_API_STATUS; stdcall; external 'netapi32.dll';

////////////////////////////////////////////////////////////////////////////////
//   DomainUserName should be passed in the format of DOMAIN\USERNAME unless the user
//   account is local to the system executing the function. Local accounts
//   should be passed in as USERNAME only
////////////////////////////////////////////////////////////////////////////////
function GetUserGroups(DomainUserName: String; GroupList: TStrings): Integer;
var  lpBuffer:      PLocalGroupUserInfo0;
     lpGroup:       PLocalGroupUserInfo0;
     dwRead:        DWORD;
     dwTotal:       DWORD;
     dwIndex:       Integer;
begin

  // Check list
  if Assigned(GroupList) then
  begin
     // Lock the list
     GroupList.BeginUpdate;
     // Resource protection
     try
        // Clear the list
        GroupList.Clear;
        // Get the local groups that this user / group might belong to
        if (NetUserGetLocalGroups(nil, PWideChar(WideString(DomainUserName)), 0, LG_INCLUDE_INDIRECT, Pointer(lpBuffer), MAX_PREFERRED_LENGTH, @dwRead, @dwTotal) = 0) then
        begin
           // Resource protection
           try
              // Save start of pointer
              lpGroup:=lpBuffer;
              // Enumerate the groups
              for dwIndex:=0 to Pred(dwRead) do
              begin
                 // Copy the account name
                 GroupList.Add(WideCharToString(lpGroup^.lgrui0_name));
                 // Push next group name
                 Inc(lpGroup);
              end;
           finally
              // Free the allocated buffer
              NetApiBufferFree(lpBuffer);
           end;
        end;
     finally
        // Unlock list
        GroupList.EndUpdate;
     end;
     // Return list count
     result:=GroupList.Count;
  end
  else
     // No list passed
     result:=(-1);

end;
0
 

Author Comment

by:icad01
ID: 17928881
Dear rllibby,
Sorry for the delay.
Thanks a lot for sharing this great and helpfull  code!


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now